Skip to content

Commit 5d25bd3

Browse files
Make evaluated API in classes and objects with limited life
1 parent 4a3b151 commit 5d25bd3

File tree

4 files changed

+51
-26
lines changed

4 files changed

+51
-26
lines changed

wwwsrc/api/agent.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
export const agent = {
1+
export class AgentApi {
22
teleport(position) {
33
return minecraft.runCommand(`agent tp ${ position.x || '~' } ${ position.y || '~' } ${ position.z || '~' }`);
4-
},
4+
}
5+
56
async getPosition() {
67
return await minecraft.runCommand('agent getposition');
7-
},
8+
}
9+
810
move(direction, blocks) {
911
let commands = [];
1012

@@ -13,33 +15,41 @@ export const agent = {
1315
}
1416

1517
return Promise.all(commands);
16-
},
18+
}
19+
1720
turn(direction) {
1821
return minecraft.runCommand(`agent turn ${ direction }`);
19-
},
22+
}
23+
2024
attack(direction) {
2125
return minecraft.runCommand(`agent attack ${ direction }`);
22-
},
26+
}
27+
2328
destroy(direction) {
2429
return minecraft.runCommand(`agent destroy ${ direction }`);
25-
},
30+
}
31+
2632
drop(slot, amount, direction) {
2733
if (slot === null) {
2834
return minecraft.runCommand(`agent dropall ${ direction }`);
2935
} else {
3036
return minecraft.runCommand(`agent drop ${ slot } ${ amount } ${ direction }`);
3137
}
32-
},
38+
}
39+
3340
build(slot, direction) {
3441
return minecraft.runCommand(`agent place ${ slot } ${ direction }`);
35-
},
42+
}
43+
3644
till(direction) {
3745
return minecraft.runCommand(`agent till ${ direction }`);
38-
},
46+
}
47+
3948
collect(id) {
4049
return minecraft.runCommand(`agent collect ${ id }`);
41-
},
50+
}
51+
4252
transfer(srcSlotNum, quantity, dstSlotNum) {
4353
return minecraft.runCommand(`agent transfer ${ srcSlotNum } ${ quantity } ${ dstSlotNum }`);
4454
}
45-
};
55+
}

wwwsrc/api/lifecycle.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
export const lifecycle = {
2-
entrypoints: [],
1+
export class LifecycleApi {
2+
entrypoints = [];
33

44
on(eventName, callback) {
55
switch (eventName) {
66
case 'run':
77
this.entrypoints.push(callback);
8+
break;
89
}
910
}
10-
};
11+
12+
fire(eventName) {
13+
console.log(eventName);
14+
switch (eventName) {
15+
case 'run':
16+
this.entrypoints.forEach(callback => callback());
17+
break;
18+
}
19+
}
20+
}

wwwsrc/api/player.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
export const player = {
2-
itemAmountUsed: 0,
3-
itemId: '',
4-
message: '',
1+
export class PlayerApi {
2+
itemAmountUsed = 0;
3+
itemId = '';
4+
message = '';
55

66
teleport(position) {
77
return minecraft.runCommand(`tp ${ position.x || '~' } ${ position.y || '~' } ${ position.z || '~' }`);
8-
},
8+
}
9+
910
async getPosition() {
1011
const body = await minecraft.runCommand('querytarget @s');
1112
const playerDetails = JSON.parse(body.details)[0];
1213

1314
return playerDetails.position;
14-
},
15+
}
16+
1517
on(eventName, callback) {
1618
switch (eventName) {
1719
case 'die':

wwwsrc/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { javascriptGenerator } from 'blockly/javascript';
99
import { javascriptBlocks } from './generators/javascript.js';
1010
import { toolbox } from './toolbox.js';
1111
import Sval from 'sval';
12-
import { agent } from './api/agent.js';
13-
import { player } from './api/player.js';
14-
import { lifecycle } from './api/lifecycle.js';
12+
import { AgentApi } from './api/agent.js';
13+
import { PlayerApi } from './api/player.js';
14+
import { LifecycleApi } from './api/lifecycle.js';
1515
import './index.css';
1616
import 'vex-js/dist/css/vex.css';
1717
import 'vex-js/dist/css/vex-theme-default.css';
@@ -93,15 +93,18 @@ ${ genCode }\
9393
// Top-level await works in the module type.
9494
const interpreter = new Sval({ sourceType: 'module' });
9595

96+
const agent = new AgentApi();
97+
const player = new PlayerApi();
98+
const lifecycle = new LifecycleApi();
99+
96100
interpreter.import('agent', { agent });
97101
interpreter.import('player', { player });
98102
interpreter.import('lifecycle', { lifecycle });
99103

100104
minecraft.resetEventListeners();
101105

102106
interpreter.run(code);
103-
104-
lifecycle.entrypoints.forEach((callback) => callback());
107+
lifecycle.fire('run');
105108
});
106109
});
107110

0 commit comments

Comments
 (0)