sapdon 3.0.3 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,7 @@ export class GRegistry {
8
8
  * @param {string} data 实例 必须包含 toJson 方法
9
9
  */
10
10
  static register(name, root, path, data) {
11
+ console.log("client:",data)
11
12
  GRegistry.dataList.push({ name, root, path, data });
12
13
  }
13
14
  static getDataList() {
@@ -0,0 +1,44 @@
1
+ export class Actor {
2
+ target;
3
+ manager;
4
+ constructor(target, manager) {
5
+ this.target = target;
6
+ this.manager = manager;
7
+ }
8
+ static from(target, manager) {
9
+ return new Actor(target, manager);
10
+ }
11
+ /**
12
+ * 优先使用 `Actor.getComponent`
13
+ * @param ctors
14
+ * @returns
15
+ */
16
+ getMinecraftComponent(...ids) {
17
+ return ids.map(id => this.target.getComponent(id));
18
+ }
19
+ /**
20
+ * 优先使用 `Actor.getComponent`
21
+ * @param ctors
22
+ * @returns
23
+ */
24
+ getCustomComponent(...ctors) {
25
+ return this.manager.getComponents(...ctors);
26
+ }
27
+ getComponent(...descs) {
28
+ return descs.map(desc => {
29
+ if (typeof desc === 'string') {
30
+ return this.target.getComponent(desc);
31
+ }
32
+ return this.manager.getComponentUnsafe(desc);
33
+ });
34
+ }
35
+ addComponent(...components) {
36
+ return this.manager.attachComponent(...components);
37
+ }
38
+ removeComponent(ctor) {
39
+ return this.manager.detachComponent(ctor);
40
+ }
41
+ updateComponent(ctor, fn) {
42
+ return this.manager.update(ctor, fn);
43
+ }
44
+ }
@@ -0,0 +1,201 @@
1
+ import { system, world } from '@minecraft/server';
2
+ import { Optional } from './optional.js';
3
+ const REFLECT_MANAGER = Symbol('reflect-manager');
4
+ const REFLECT_ENTITY = Symbol('reflect-entity');
5
+ export class CustomComponent {
6
+ [REFLECT_MANAGER];
7
+ [REFLECT_ENTITY] = Optional.none();
8
+ onTick(manager, en) { }
9
+ detach(manager) {
10
+ const ctor = Object.getPrototypeOf(this).constructor;
11
+ return manager.detachComponent(ctor);
12
+ }
13
+ getManager() {
14
+ return this[REFLECT_MANAGER];
15
+ }
16
+ getEntity() {
17
+ return this[REFLECT_ENTITY];
18
+ }
19
+ }
20
+ export class BaseComponent extends CustomComponent {
21
+ onAttach(manager) { }
22
+ onDetach(manager) { }
23
+ }
24
+ export class ComponentManager {
25
+ static profilerEnable = false;
26
+ static global = new ComponentManager();
27
+ #components = new Map();
28
+ #prependTicks = [];
29
+ #nextTicks = [];
30
+ getComponentUnsafe(ctor) {
31
+ return this.#components.get(ctor);
32
+ }
33
+ getComponent(ctor) {
34
+ return Optional.some(this.#components.get(ctor));
35
+ }
36
+ getComponents(...ctor) {
37
+ return ctor.map(c => this.#components.get(c));
38
+ }
39
+ async #attachComponent(ctor, component, shouldRebuild = true) {
40
+ let init = !this.#components.get(ctor);
41
+ if (!init && shouldRebuild) {
42
+ await this.detachComponent(ctor);
43
+ init = true;
44
+ }
45
+ if (REQUIRED_COMPONENTS in component) {
46
+ //@ts-ignore
47
+ for (const [ctor, comp] of component[REQUIRED_COMPONENTS]) {
48
+ this.#attachComponent(ctor, comp, false);
49
+ }
50
+ }
51
+ if (init && 'onAttach' in component) {
52
+ await component.onAttach(this);
53
+ }
54
+ this.#components.set(ctor, component);
55
+ return Optional.some(component);
56
+ }
57
+ async attachComponent(...component) {
58
+ const components = [];
59
+ for (const obj of component) {
60
+ components.push(await this.#attachComponent(Object.getPrototypeOf(obj).constructor, obj));
61
+ }
62
+ return components;
63
+ }
64
+ async getOrCreate(ctor, ...args) {
65
+ let component = this.#components.get(ctor);
66
+ if (component) {
67
+ return Optional.some(component);
68
+ }
69
+ return this.#attachComponent(ctor, new ctor(...args));
70
+ }
71
+ async detachComponent(ctor) {
72
+ const component = this.#components.get(ctor);
73
+ if (component && 'onDetach' in component) {
74
+ await component.onDetach(this);
75
+ }
76
+ return this.#components.delete(ctor);
77
+ }
78
+ clear() {
79
+ this.#components.clear();
80
+ }
81
+ getComponentKeys() {
82
+ return this.#components.keys();
83
+ }
84
+ has(ctor) {
85
+ return this.#components.has(ctor);
86
+ }
87
+ afterTick(fn) {
88
+ this.#nextTicks.push(fn);
89
+ }
90
+ beforeTick(fn) {
91
+ this.#prependTicks.unshift(fn);
92
+ }
93
+ handleTicks(en) {
94
+ for (const prependTick of this.#prependTicks) {
95
+ this.profiler(() => prependTick.call(null, Optional.some(en)));
96
+ // prependTick.call(null, Optional.some(en))
97
+ }
98
+ this.#prependTicks.length = 0;
99
+ for (const component of this.#components.values()) {
100
+ //@ts-ignore
101
+ if (!component[REFLECT_ENTITY]) {
102
+ //@ts-ignore
103
+ component[REFLECT_ENTITY] = Optional.some(en);
104
+ }
105
+ //@ts-ignore
106
+ if (!component[REFLECT_MANAGER]) {
107
+ //@ts-ignore
108
+ component[REFLECT_MANAGER] = this;
109
+ }
110
+ const { onTick } = component;
111
+ if (onTick) {
112
+ this.profiler(() => onTick.call(component, this, Optional.some(en)), component);
113
+ // onTick.call(component, this, Optional.some(en))
114
+ }
115
+ }
116
+ for (const afterTick of this.#nextTicks) {
117
+ this.profiler(() => afterTick.call(null, Optional.some(en)));
118
+ // afterTick.call(null, Optional.some(en))
119
+ }
120
+ this.#nextTicks.length = 0;
121
+ }
122
+ update(ctor, fn) {
123
+ const component = this.#components.get(ctor);
124
+ if (component) {
125
+ fn(component);
126
+ return true;
127
+ }
128
+ return false;
129
+ }
130
+ profiler(fn, component, name) {
131
+ if (!ComponentManager.profilerEnable) {
132
+ return fn();
133
+ }
134
+ const conponentName = component ? Object.getPrototypeOf(component).constructor.name : '';
135
+ const profileName = name ? name
136
+ : conponentName ? (`${conponentName}.${fn.name}`)
137
+ : fn.name;
138
+ const now = performance.now();
139
+ const val = fn();
140
+ console.log(`[Profiler] ${profileName} took ${performance.now() - now}ms`);
141
+ return val;
142
+ }
143
+ }
144
+ const REQUIRED_COMPONENTS = Symbol('REQUIRED_COMPONENTS');
145
+ export function RequireComponents(...params) {
146
+ return class CRequiredComponent extends BaseComponent {
147
+ [REQUIRED_COMPONENTS] = new Map();
148
+ constructor() {
149
+ super();
150
+ for (const param of params) {
151
+ if (Array.isArray(param)) {
152
+ const [Ctor, ...args] = param;
153
+ this[REQUIRED_COMPONENTS].set(Ctor, Reflect.construct(Ctor, args));
154
+ continue;
155
+ }
156
+ this[REQUIRED_COMPONENTS].set(param, Reflect.construct(param, []));
157
+ }
158
+ }
159
+ getComponent(ctor) {
160
+ return this[REQUIRED_COMPONENTS].get(ctor);
161
+ }
162
+ };
163
+ }
164
+ export var oc;
165
+ (function (oc) {
166
+ const table = new Map();
167
+ function addEntity(entityId) {
168
+ const manager = new ComponentManager();
169
+ table.set(entityId, manager);
170
+ return manager;
171
+ }
172
+ oc.addEntity = addEntity;
173
+ function removeEntity(entityId) {
174
+ const uid = entityId;
175
+ const manager = table.get(uid);
176
+ manager?.clear();
177
+ table.delete(uid);
178
+ }
179
+ oc.removeEntity = removeEntity;
180
+ function getManager(entityId) {
181
+ return table.get(entityId) ?? addEntity(entityId);
182
+ }
183
+ oc.getManager = getManager;
184
+ function tick() {
185
+ for (const [id, manager] of table.entries()) {
186
+ const entity = world.getEntity(id);
187
+ if (entity) {
188
+ manager.handleTicks(entity);
189
+ }
190
+ }
191
+ }
192
+ oc.tick = tick;
193
+ function start() {
194
+ system.runInterval(tick);
195
+ }
196
+ oc.start = start;
197
+ function toPlayer(entity) {
198
+ return Optional.some(world.getAllPlayers().find(p => p.id === entity.id));
199
+ }
200
+ oc.toPlayer = toPlayer;
201
+ })(oc || (oc = {}));
@@ -4,7 +4,7 @@ import { Entity, Player, InputButton } from '@minecraft/server';
4
4
  declare class Optional<T = any> {
5
5
  private value;
6
6
  static none<T>(): Optional<T>;
7
- static some<T>(value: T): Optional<T>;
7
+ static some<T>(value?: T): Optional<T>;
8
8
  constructor(value: T);
9
9
  unwrap(): T;
10
10
  isEmpty(): boolean;
@@ -90,8 +90,8 @@ type ComponentDescriptor = string | ComponentCtor;
90
90
  declare class Actor {
91
91
  readonly target: Entity;
92
92
  readonly manager: ComponentManager;
93
- constructor(target: Entity, manager: ComponentManager);
94
- static from(target: Entity, manager: ComponentManager): Actor;
93
+ constructor(target: Entity);
94
+ static from(target: Entity): Actor;
95
95
  /**
96
96
  * 优先使用 `Actor.getComponent`
97
97
  * @param ctors
package/dist/oc/index.js CHANGED
@@ -1 +1 @@
1
- import{world as t,system as e,InputButton as n,ButtonState as s}from"@minecraft/server";class o{target;manager;constructor(t,e){this.target=t,this.manager=e}static from(t,e){return new o(t,e)}getMinecraftComponent(...t){return t.map((t=>this.target.getComponent(t)))}getCustomComponent(...t){return this.manager.getComponents(...t)}getComponent(...t){return t.map((t=>"string"==typeof t?this.target.getComponent(t):this.manager.getComponentUnsafe(t)))}addComponent(...t){return this.manager.attachComponent(...t)}removeComponent(t){return this.manager.detachComponent(t)}updateComponent(t,e){return this.manager.update(t,e)}}class r{value;static none(){return new r(null)}static some(t){return new r(t)}constructor(t){this.value=t}unwrap(){if(!this.isEmpty())return this.value;throw new Error("Optional is empty")}isEmpty(){return void 0===this.value||null===this.value}orElse(t){return this.value??t}use(t,e){return!this.isEmpty()&&(t.call(e,this.value),!0)}}const a=Symbol("reflect-manager"),i=Symbol("reflect-entity");class c{[a];[i]=r.none();onTick(t,e){}detach(t){const e=Object.getPrototypeOf(this).constructor;return t.detachComponent(e)}getManager(){return this[a]}getEntity(){return this[i]}}class p extends c{onAttach(t){}onDetach(t){}}class u{static profilerEnable=!1;static global=new u;#t=new Map;#e=[];#n=[];getComponentUnsafe(t){return this.#t.get(t)}getComponent(t){return r.some(this.#t.get(t))}getComponents(...t){return t.map((t=>this.#t.get(t)))}async#s(t,e,n=!0){let s=!this.#t.get(t);if(!s&&n&&(await this.detachComponent(t),s=!0),l in e)for(const[t,n]of e[l])this.#s(t,n,!1);return s&&"onAttach"in e&&await e.onAttach(this),this.#t.set(t,e),r.some(e)}async attachComponent(...t){const e=[];for(const n of t)e.push(await this.#s(Object.getPrototypeOf(n).constructor,n));return e}async getOrCreate(t,...e){let n=this.#t.get(t);return n?r.some(n):this.#s(t,new t(...e))}async detachComponent(t){const e=this.#t.get(t);return e&&"onDetach"in e&&await e.onDetach(this),this.#t.delete(t)}clear(){this.#t.clear()}getComponentKeys(){return this.#t.keys()}has(t){return this.#t.has(t)}afterTick(t){this.#n.push(t)}beforeTick(t){this.#e.unshift(t)}handleTicks(t){for(const e of this.#e)this.profiler((()=>e.call(null,r.some(t))));this.#e.length=0;for(const e of this.#t.values()){e[i]||(e[i]=r.some(t)),e[a]||(e[a]=this);const{onTick:n}=e;n&&this.profiler((()=>n.call(e,this,r.some(t))),e)}for(const e of this.#n)this.profiler((()=>e.call(null,r.some(t))));this.#n.length=0}update(t,e){const n=this.#t.get(t);return!!n&&(e(n),!0)}profiler(t,e,n){if(!u.profilerEnable)return t();const s=e?Object.getPrototypeOf(e).constructor.name:"",o=n||(s?`${s}.${t.name}`:t.name),r=performance.now(),a=t();return console.log(`[Profiler] ${o} took ${performance.now()-r}ms`),a}}const l=Symbol("REQUIRED_COMPONENTS");function h(...t){return class extends p{[l]=new Map;constructor(){super();for(const e of t)if(Array.isArray(e)){const[t,...n]=e;this[l].set(t,Reflect.construct(t,n))}else this[l].set(e,Reflect.construct(e,[]))}getComponent(t){return this[l].get(t)}}}var m,f;!function(n){const s=new Map;function o(t){const e=new u;return s.set(t,e),e}function a(){for(const[e,n]of s.entries()){const s=t.getEntity(e);s&&n.handleTicks(s)}}n.addEntity=o,n.removeEntity=function(t){const e=t,n=s.get(e);n?.clear(),s.delete(e)},n.getManager=function(t){return s.get(t)??o(t)},n.tick=a,n.start=function(){e.runInterval(a)},n.toPlayer=function(e){return r.some(t.getAllPlayers().find((t=>t.id===e.id)))}}(m||(m={})),function(t){t[t.Press=0]="Press",t[t.Release=1]="Release",t[t.None=2]="None"}(f||(f={}));class g extends p{[n.Jump]=f.None;[n.Sneak]=f.None;static setup(){t.afterEvents.playerButtonInput.subscribe((t=>{const e=m.getManager(t.player.id);e.getComponent(g).use((n=>{n[t.button]=t.newButtonState===s.Pressed?f.Press:f.Release,e.afterTick((()=>{n[t.button]=f.None}))}))}))}}class d extends p{[n.Jump]=f.None;[n.Sneak]=f.None;_lastJump=s.Released;_lastSneak=s.Released;onTick(t,e){const o=m.toPlayer(e.unwrap()).unwrap(),r=o.inputInfo.getButtonState(n.Jump),a=o.inputInfo.getButtonState(n.Sneak);r===s.Pressed&&this._lastJump===s.Released?this[n.Jump]=f.Press:r===s.Released&&this._lastJump===s.Pressed?this[n.Jump]=f.Release:this[n.Jump]=f.None,a===s.Pressed&&this._lastSneak===s.Released?this[n.Sneak]=f.Press:a===s.Released&&this._lastSneak===s.Pressed?this[n.Sneak]=f.Release:this[n.Sneak]=f.None,this._lastJump=r,this._lastSneak=a}}export{o as Actor,p as BaseComponent,u as ComponentManager,c as CustomComponent,f as InputChangeState,r as Optional,d as PlayerInputCompatibilityComponent,g as PlayerInputComponent,h as RequireComponents,m as oc};
1
+ import{world as t,system as e,InputButton as n,ButtonState as s}from"@minecraft/server";class o{value;static none(){return new o(null)}static some(t){return new o(t)}constructor(t){this.value=t}unwrap(){if(!this.isEmpty())return this.value;throw new Error("Optional is empty")}isEmpty(){return void 0===this.value||null===this.value}orElse(t){return this.value??t}use(t,e){return!this.isEmpty()&&(t.call(e,this.value),!0)}}const r=Symbol("reflect-manager"),a=Symbol("reflect-entity");class i{[r];[a]=o.none();onTick(t,e){}detach(t){const e=Object.getPrototypeOf(this).constructor;return t.detachComponent(e)}getManager(){return this[r]}getEntity(){return this[a]}}class c extends i{onAttach(t){}onDetach(t){}}class p{static profilerEnable=!1;static global=new p;#t=new Map;#e=[];#n=[];getComponentUnsafe(t){return this.#t.get(t)}getComponent(t){return o.some(this.#t.get(t))}getComponents(...t){return t.map((t=>this.#t.get(t)))}async#s(t,e,n=!0){let s=!this.#t.get(t);if(!s&&n&&(await this.detachComponent(t),s=!0),u in e)for(const[t,n]of e[u])this.#s(t,n,!1);return s&&"onAttach"in e&&await e.onAttach(this),this.#t.set(t,e),o.some(e)}async attachComponent(...t){const e=[];for(const n of t)e.push(await this.#s(Object.getPrototypeOf(n).constructor,n));return e}async getOrCreate(t,...e){let n=this.#t.get(t);return n?o.some(n):this.#s(t,new t(...e))}async detachComponent(t){const e=this.#t.get(t);return e&&"onDetach"in e&&await e.onDetach(this),this.#t.delete(t)}clear(){this.#t.clear()}getComponentKeys(){return this.#t.keys()}has(t){return this.#t.has(t)}afterTick(t){this.#n.push(t)}beforeTick(t){this.#e.unshift(t)}handleTicks(t){for(const e of this.#e)this.profiler((()=>e.call(null,o.some(t))));this.#e.length=0;for(const e of this.#t.values()){e[a]||(e[a]=o.some(t)),e[r]||(e[r]=this);const{onTick:n}=e;n&&this.profiler((()=>n.call(e,this,o.some(t))),e)}for(const e of this.#n)this.profiler((()=>e.call(null,o.some(t))));this.#n.length=0}update(t,e){const n=this.#t.get(t);return!!n&&(e(n),!0)}profiler(t,e,n){if(!p.profilerEnable)return t();const s=e?Object.getPrototypeOf(e).constructor.name:"",o=n||(s?`${s}.${t.name}`:t.name),r=performance.now(),a=t();return console.log(`[Profiler] ${o} took ${performance.now()-r}ms`),a}}const u=Symbol("REQUIRED_COMPONENTS");function l(...t){return class extends c{[u]=new Map;constructor(){super();for(const e of t)if(Array.isArray(e)){const[t,...n]=e;this[u].set(t,Reflect.construct(t,n))}else this[u].set(e,Reflect.construct(e,[]))}getComponent(t){return this[u].get(t)}}}var h,m;!function(n){const s=new Map;function r(t){const e=new p;return s.set(t,e),e}function a(){for(const[e,n]of s.entries()){const s=t.getEntity(e);s&&n.handleTicks(s)}}n.addEntity=r,n.removeEntity=function(t){const e=t,n=s.get(e);n?.clear(),s.delete(e)},n.getManager=function(t){return s.get(t)??r(t)},n.tick=a,n.start=function(){e.runInterval(a)},n.toPlayer=function(e){return o.some(t.getAllPlayers().find((t=>t.id===e.id)))}}(h||(h={}));class f{target;manager;constructor(t){this.target=t,this.manager=h.getManager(t.id)}static from(t){return new f(t)}getMinecraftComponent(...t){return t.map((t=>this.target.getComponent(t)))}getCustomComponent(...t){return this.manager.getComponents(...t)}getComponent(...t){return t.map((t=>"string"==typeof t?this.target.getComponent(t):this.manager.getComponentUnsafe(t)))}addComponent(...t){return this.manager.attachComponent(...t)}removeComponent(t){return this.manager.detachComponent(t)}updateComponent(t,e){return this.manager.update(t,e)}}!function(t){t[t.Press=0]="Press",t[t.Release=1]="Release",t[t.None=2]="None"}(m||(m={}));class g extends c{[n.Jump]=m.None;[n.Sneak]=m.None;static setup(){t.afterEvents.playerButtonInput.subscribe((t=>{const e=h.getManager(t.player.id);e.getComponent(g).use((n=>{n[t.button]=t.newButtonState===s.Pressed?m.Press:m.Release,e.afterTick((()=>{n[t.button]=m.None}))}))}))}}class d extends c{[n.Jump]=m.None;[n.Sneak]=m.None;_lastJump=s.Released;_lastSneak=s.Released;onTick(t,e){const o=h.toPlayer(e.unwrap()).unwrap(),r=o.inputInfo.getButtonState(n.Jump),a=o.inputInfo.getButtonState(n.Sneak);r===s.Pressed&&this._lastJump===s.Released?this[n.Jump]=m.Press:r===s.Released&&this._lastJump===s.Pressed?this[n.Jump]=m.Release:this[n.Jump]=m.None,a===s.Pressed&&this._lastSneak===s.Released?this[n.Sneak]=m.Press:a===s.Released&&this._lastSneak===s.Pressed?this[n.Sneak]=m.Release:this[n.Sneak]=m.None,this._lastJump=r,this._lastSneak=a}}export{f as Actor,c as BaseComponent,p as ComponentManager,i as CustomComponent,m as InputChangeState,o as Optional,d as PlayerInputCompatibilityComponent,g as PlayerInputComponent,l as RequireComponents,h as oc};
@@ -0,0 +1,67 @@
1
+ import { ButtonState, InputButton, world } from "@minecraft/server";
2
+ import { BaseComponent } from "./core.js";
3
+ import { oc } from './core.js';
4
+ export var InputChangeState;
5
+ (function (InputChangeState) {
6
+ InputChangeState[InputChangeState["Press"] = 0] = "Press";
7
+ InputChangeState[InputChangeState["Release"] = 1] = "Release";
8
+ InputChangeState[InputChangeState["None"] = 2] = "None";
9
+ })(InputChangeState || (InputChangeState = {}));
10
+ /**
11
+ * `@minecraft/server` version 2.0.0-beta
12
+ * 低于此版本请不要使用此组件
13
+ */
14
+ export class PlayerInputComponent extends BaseComponent {
15
+ ;
16
+ [InputButton.Jump] = InputChangeState.None;
17
+ [InputButton.Sneak] = InputChangeState.None;
18
+ static setup() {
19
+ world.afterEvents.playerButtonInput.subscribe(e => {
20
+ const manager = oc.getManager(e.player.id);
21
+ const playerInput = manager.getComponent(PlayerInputComponent);
22
+ playerInput.use(playerInput => {
23
+ playerInput[e.button] = e.newButtonState === ButtonState.Pressed ? InputChangeState.Press : InputChangeState.Release;
24
+ manager.afterTick(() => {
25
+ playerInput[e.button] = InputChangeState.None;
26
+ });
27
+ });
28
+ });
29
+ }
30
+ }
31
+ /**
32
+ * `@minecraft/server` version 1.17.0-beta
33
+ * 有 1tick 延迟
34
+ * 条件允许请使用 `PlayerInputComponent`
35
+ */
36
+ export class PlayerInputCompatibilityComponent extends BaseComponent {
37
+ ;
38
+ [InputButton.Jump] = InputChangeState.None;
39
+ [InputButton.Sneak] = InputChangeState.None;
40
+ _lastJump = ButtonState.Released;
41
+ _lastSneak = ButtonState.Released;
42
+ onTick(manager, en) {
43
+ const player = oc.toPlayer(en.unwrap()).unwrap();
44
+ const jump = player.inputInfo.getButtonState(InputButton.Jump);
45
+ const sneak = player.inputInfo.getButtonState(InputButton.Sneak);
46
+ if (jump === ButtonState.Pressed && this._lastJump === ButtonState.Released) {
47
+ this[InputButton.Jump] = InputChangeState.Press;
48
+ }
49
+ else if (jump === ButtonState.Released && this._lastJump === ButtonState.Pressed) {
50
+ this[InputButton.Jump] = InputChangeState.Release;
51
+ }
52
+ else {
53
+ this[InputButton.Jump] = InputChangeState.None;
54
+ }
55
+ if (sneak === ButtonState.Pressed && this._lastSneak === ButtonState.Released) {
56
+ this[InputButton.Sneak] = InputChangeState.Press;
57
+ }
58
+ else if (sneak === ButtonState.Released && this._lastSneak === ButtonState.Pressed) {
59
+ this[InputButton.Sneak] = InputChangeState.Release;
60
+ }
61
+ else {
62
+ this[InputButton.Sneak] = InputChangeState.None;
63
+ }
64
+ this._lastJump = jump;
65
+ this._lastSneak = sneak;
66
+ }
67
+ }
@@ -0,0 +1,31 @@
1
+ export class Optional {
2
+ value;
3
+ static none() {
4
+ return new Optional(null);
5
+ }
6
+ static some(value) {
7
+ return new Optional(value);
8
+ }
9
+ constructor(value) {
10
+ this.value = value;
11
+ }
12
+ unwrap() {
13
+ if (!this.isEmpty()) {
14
+ return this.value;
15
+ }
16
+ throw new Error('Optional is empty');
17
+ }
18
+ isEmpty() {
19
+ return this.value === undefined || this.value === null;
20
+ }
21
+ orElse(other) {
22
+ return this.value ?? other;
23
+ }
24
+ use(fn, self) {
25
+ if (!this.isEmpty()) {
26
+ fn.call(self, this.value);
27
+ return true;
28
+ }
29
+ return false;
30
+ }
31
+ }
File without changes
package/doc/sapdon-ts.md CHANGED
@@ -18,11 +18,10 @@ import { ItemAPI } from '@sapdon/core'
18
18
  ## 从js迁移到ts
19
19
  js 的 `main.mjs` 包含了一些预定义的代码:
20
20
  ```js
21
- import { startDevServer } from '@sapdon/cli'
22
- import { GRegistry, UISystemRegistry } from '@sapdon/core'
21
+ import { registry } from '@sapdon/core'
23
22
 
24
- // 开启开发服务器, 不开启开发服务器无法使用文件同步和热更新
25
- startDevServer(GRegistry, UISystemRegistry)
23
+ // 进行注册后需进行一次提交, 通知开发服务器更新文件
24
+ registry.setup()
26
25
  ```
27
26
  如果需要从js迁移到ts,删除 `startDevServer` 那行即可(不删也没事)
28
27
 
@@ -59,4 +58,7 @@ manager.attachComponent(new MyComponent())
59
58
 
60
59
  你也可以动态增删改查组件! 这很方便!
61
60
 
62
- 如果你想看它到底是怎么回事,可以从 `examples/oc-core` 开始了解
61
+ 如果你想看它到底是怎么回事,可以从 `examples/oc-core` 开始了解
62
+
63
+ ## [Object - Component](./oc/index.md)
64
+ 关于 `@sapdon/oc` 的一切
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sapdon",
3
- "version": "3.0.3",
3
+ "version": "3.1.0",
4
4
  "scripts": {
5
5
  "build": "rollup -c",
6
6
  "pub": "rollup -c && npm publish"
package/doc/cli.md DELETED
@@ -1,24 +0,0 @@
1
- # `@sapdon/cli` 包
2
-
3
- ## 自定义处理器
4
- ```ts
5
- import { devServer } from '@sapdon/cli'
6
- devServer.handle('ping', () => {
7
- console.log('pong')
8
- })
9
- ```
10
-
11
- ## 拦截cli处理器
12
- ```ts
13
- import { devServer, ServerHandles } from '@sapdon/cli'
14
-
15
- devServer.interceptHandler(ServerHandles.WRITE_ADDON, handler => {
16
- return (...args: any[]) => {
17
- const now = peformance.now()
18
- handler.apply(args)
19
- const dt = peformance.now() - now
20
- console.log(`执行耗时 ${dt}ms.`)
21
- }
22
- })
23
- ```
24
-