Skip to content

Commit 18e0be3

Browse files
authored
Rename Entity.id to Entity.typeId (JaylyDev#127)
1 parent 5c10940 commit 18e0be3

9 files changed

Lines changed: 130 additions & 127 deletions

File tree

scripts/anti-nuker/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ world.events.blockBreak.subscribe(({ block, brokenBlockPermutation, dimension, p
1313
if (blockLog.has(player.name) && log.get(player.name).amount === 0) {
1414
dimension.getBlock(blockLog.get(player.name).location).setPermutation(blockLog.get(player.name).permutation)
1515
setTickTimeout(() => {
16-
dimension.getEntitiesAtBlockLocation(blockLog.get(player.name)?.location ?? block.location)?.filter((entity) => entity.id === "minecraft:item")?.forEach((item) => item.kill())
16+
dimension.getEntitiesAtBlockLocation(blockLog.get(player.name)?.location ?? block.location)?.filter((entity) => entity.typeId === "minecraft:item")?.forEach((item) => item.kill())
1717
blockLog.delete(player.name)
1818
}, 0)
1919
}
2020
dimension.getBlock(block.location).setPermutation(brokenBlockPermutation)
2121
setTickTimeout(() => {
22-
dimension.getEntitiesAtBlockLocation(block.location)?.filter((entity) => entity.id === "minecraft:item").forEach((item) => item.kill())
22+
dimension.getEntitiesAtBlockLocation(block.location)?.filter((entity) => entity.typeId === "minecraft:item").forEach((item) => item.kill())
2323
}, 0)
2424
log.set(player.name, { time: Date.now(), amount: ++old.amount })
2525
})

scripts/cps-counter/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { MinecraftEntityTypes, world, Player } from "@minecraft/server";
22

33
world.events.entityHit.subscribe(function ({ entity }) {
4-
entity.id === MinecraftEntityTypes.player.id &&
4+
entity.typeId === MinecraftEntityTypes.player.id &&
55
(entity["clicks"] || (entity["clicks"] = []),
66
entity["clicks"].push({ timestamp: new Date().getTime() }));
77
});

scripts/entity-hit/entityHit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { EntityHealthComponent, world } from "@minecraft/server";
33

44
world.events.entityHit.subscribe((evd) => {
5-
if (evd.entity.id !== 'minecraft:player') return;
5+
if (evd.entity.typeId !== 'minecraft:player') return;
66

77
const ent = evd.hitEntity;
88
if (!ent) return;
Lines changed: 79 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
2-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
3-
if (ar || !(i in from)) {
4-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
5-
ar[i] = from[i];
6-
}
7-
}
8-
return to.concat(ar || Array.prototype.slice.call(from));
9-
};
101
/**
112
* @license MIT
123
* @author JaylyMC
134
* @link https://github.com/JaylyDev/GameTestDB
145
*/
15-
import { world, Vector } from "@minecraft/server";
6+
import { world, Vector, system } from "@minecraft/server";
167
import "@minecraft/server-gametest"; // import "@minecraft/server-gametest" native module to support Simulated Players
178
/**
189
* @license MIT
@@ -55,53 +46,16 @@ function comparePlayer(playerA, playerB) {
5546
* Keeping this class private to avoid confusion between
5647
* Player class from "@minecraft/server" module.
5748
*/
58-
var Player = /** @class */ (function () {
59-
/**
60-
* @remarks
61-
* Manually assign some player property to the new player class
62-
* before failed to get property due to GameTest can't fetch
63-
* despawned entities.
64-
* @param {MinecraftPlayer} player
65-
*/
66-
function Player(player) {
67-
// PRIVATE PROPERTIES
68-
// This properties should not be used by public
69-
try {
70-
this.__PlayerBlockFromViewDirection = player.getBlockFromViewDirection();
71-
}
72-
catch (_a) { }
73-
;
74-
try {
75-
this.__PlayerEntitiesFromViewDirection = player.getEntitiesFromViewDirection();
76-
}
77-
catch (_b) { }
78-
;
79-
this.__PlayerComponents = cloneJSON(player.getComponents());
80-
this.__PlayerTags = player.getTags();
81-
// PUBLIC PROPERTIES
82-
this.dimension = player.dimension;
83-
this.headLocation = player.headLocation;
84-
this.id = player.id;
85-
this.isSneaking = player.isSneaking;
86-
this.location = player.location;
87-
this.name = player.name;
88-
this.nameTag = player.nameTag;
89-
this.rotation = player.rotation;
90-
this.scoreboard = player.scoreboard;
91-
this.selectedSlot = player.selectedSlot;
92-
this.target = player.target;
93-
this.getVelocity() = player.getVelocity();
94-
this.viewDirection = new Vector(player.getViewDirection().x, player.getViewDirection().y, player.getViewDirection().z);
95-
}
49+
class Player {
9650
/**
9751
* @remarks
9852
* Gets the first block that intersects with the vector of the
9953
* view of this entity.
10054
* @throws This function can throw errors.
10155
*/
102-
Player.prototype.getBlockFromViewDirection = function () {
56+
getBlockFromViewDirection() {
10357
return this.__PlayerBlockFromViewDirection;
104-
};
58+
}
10559
;
10660
/**
10761
* @remarks
@@ -113,23 +67,23 @@ var Player = /** @class */ (function () {
11367
* 'minecraft:' is assumed. If the component is not present on
11468
* the entity, undefined is returned.
11569
*/
116-
Player.prototype.getComponent = function (componentId) {
70+
getComponent(componentId) {
11771
return this.__PlayerComponents.find(function (component) {
11872
if (!componentId.startsWith("minecraft:"))
11973
componentId = "minecraft:" + componentId;
120-
if (component.id === componentId)
74+
if (component.typeId === componentId)
12175
return true;
12276
});
123-
};
77+
}
12478
;
12579
/**
12680
* @remarks
12781
* Returns all components that are both present on this entity
12882
* and supported by the API.
12983
*/
130-
Player.prototype.getComponents = function () {
84+
getComponents() {
13185
return this.__PlayerComponents;
132-
};
86+
}
13387
;
13488
/**
13589
* @remarks
@@ -138,18 +92,18 @@ var Player = /** @class */ (function () {
13892
* Additional options for processing this raycast query.
13993
* @throws This function can throw errors.
14094
*/
141-
Player.prototype.getEntitiesFromViewDirection = function () {
95+
getEntitiesFromViewDirection() {
14296
return this.__PlayerEntitiesFromViewDirection;
143-
};
97+
}
14498
;
14599
/**
146100
* @remarks
147101
* Returns all tags associated with an entity.
148102
* @throws This function can throw errors.
149103
*/
150-
Player.prototype.getTags = function () {
104+
getTags() {
151105
return this.__PlayerTags;
152-
};
106+
}
153107
;
154108
/**
155109
* @remarks
@@ -160,13 +114,13 @@ var Player = /** @class */ (function () {
160114
* to retrieve. If no namespace prefix is specified,
161115
* 'minecraft:' is assumed.
162116
*/
163-
Player.prototype.hasComponent = function (componentId) {
164-
var componentIndex = this.__PlayerComponents.findIndex(function (entityComponent) { return entityComponent.id === componentId; });
117+
hasComponent(componentId) {
118+
let componentIndex = this.__PlayerComponents.findIndex((entityComponent) => entityComponent.id === componentId);
165119
if (componentIndex < 0)
166120
return false;
167121
else
168122
return true;
169-
};
123+
}
170124
;
171125
/**
172126
* @remarks
@@ -175,92 +129,120 @@ var Player = /** @class */ (function () {
175129
* Identifier of the tag to test for.
176130
* @throws This function can throw errors.
177131
*/
178-
Player.prototype.hasTag = function (tag) {
179-
var tagIndex = this.__PlayerTags.findIndex(function (playerTag) { return playerTag === tag; });
132+
hasTag(tag) {
133+
let tagIndex = this.__PlayerTags.findIndex(playerTag => playerTag === tag);
180134
if (tagIndex < 0)
181135
return false;
182136
else
183137
return true;
184-
};
138+
}
185139
;
140+
/**
141+
* @remarks
142+
* Manually assign some player property to the new player class
143+
* before failed to get property due to GameTest can't fetch
144+
* despawned entities.
145+
* @param {MinecraftPlayer} player
146+
*/
147+
constructor(player) {
148+
// PRIVATE PROPERTIES
149+
// This properties should not be used by public
150+
try {
151+
this.__PlayerBlockFromViewDirection = player.getBlockFromViewDirection();
152+
}
153+
catch (_a) { }
154+
;
155+
try {
156+
this.__PlayerEntitiesFromViewDirection = player.getEntitiesFromViewDirection();
157+
}
158+
catch (_b) { }
159+
;
160+
this.__PlayerComponents = cloneJSON(player.getComponents());
161+
this.__PlayerTags = player.getTags();
162+
// PUBLIC PROPERTIES
163+
const velocity = player.getVelocity();
164+
this.dimension = player.dimension;
165+
this.headLocation = player.getHeadLocation();
166+
this.id = player.id;
167+
this.typeId = player.typeId;
168+
this.isSneaking = player.isSneaking;
169+
this.location = player.location;
170+
this.name = player.name;
171+
this.nameTag = player.nameTag;
172+
this.rotation = player.getRotation();
173+
this.scoreboard = player.scoreboard;
174+
this.selectedSlot = player.selectedSlot;
175+
this.target = player.target;
176+
this.velocity = new Vector(velocity.x, velocity.y, velocity.z);
177+
this.viewDirection = new Vector(player.getViewDirection().x, player.getViewDirection().y, player.getViewDirection().z);
178+
}
186179
;
187-
return Player;
188-
}());
180+
}
189181
;
190182
/**
191183
* Contains information regarding a player that has left the
192184
* world.
193185
*/
194-
var PlayerLeaveEvent = /** @class */ (function () {
195-
function PlayerLeaveEvent(player) {
186+
export class PlayerLeaveEvent {
187+
constructor(player) {
196188
this.player = player;
197189
}
198190
;
199-
return PlayerLeaveEvent;
200-
}());
201-
export { PlayerLeaveEvent };
191+
}
202192
;
203193
/**
204194
* Manages callbacks that are connected to a player leaving the
205195
* world.
206196
*/
207-
var PlayerLeaveEventSignal = /** @class */ (function () {
208-
function PlayerLeaveEventSignal() {
209-
}
197+
export class PlayerLeaveEventSignal {
210198
/**
211199
* @remarks
212200
* Adds a callback that will be called when a player leaves the
213201
* world.
214202
* @param {(arg: PlayerLeaveEvent) => void} callback
215203
* @return {(arg: PlayerLeaveEvent) => void}
216204
*/
217-
PlayerLeaveEventSignal.prototype.subscribe = function (callback) {
205+
subscribe(callback) {
218206
callback["playerLeave"] = true;
219-
var players = __spreadArray([], world.getPlayers(), true).map(function (pl) { return new Player(pl); });
220-
var executedPlayers = [];
221-
var TickEventCallback = system.runInterval(function () {
207+
let players = [...world.getPlayers()].map(pl => new Player(pl));
208+
let executedPlayers = [];
209+
let TickEventCallback = system.runInterval(() => {
222210
if (callback["playerLeave"] !== true)
223211
system.clearRun(TickEventCallback);
224212
// Change from player class to custom player class
225-
var currentPlayers = __spreadArray([], world.getPlayers(), true);
226-
var _loop_1 = function (player) {
227-
var executedPlayerIndex = executedPlayers.findIndex(function (executedPlayer) { return comparePlayer(executedPlayer, player); });
228-
if (!currentPlayers.find(function (pl) { return comparePlayer(pl, player); }) && executedPlayerIndex < 0) {
213+
let currentPlayers = [...world.getPlayers()];
214+
for (let player of players) {
215+
let executedPlayerIndex = executedPlayers.findIndex(executedPlayer => comparePlayer(executedPlayer, player));
216+
if (!currentPlayers.find(pl => comparePlayer(pl, player)) && executedPlayerIndex < 0) {
229217
executedPlayers.push(player);
230-
var onPlayerSpawn_1 = world.events.playerSpawn.subscribe(function (playerJoinEvent) {
231-
var playerIndex = executedPlayers.findIndex(function (pl) { return comparePlayer(pl, playerJoinEvent.player); });
218+
let onPlayerSpawn = world.events.playerSpawn.subscribe((playerJoinEvent) => {
219+
let playerIndex = executedPlayers.findIndex(pl => comparePlayer(pl, playerJoinEvent.player));
232220
if (playerIndex >= 0) {
233221
executedPlayers.splice(playerIndex);
234-
world.events.playerSpawn.unsubscribe(onPlayerSpawn_1);
222+
world.events.playerSpawn.unsubscribe(onPlayerSpawn);
235223
}
236224
;
237225
});
238226
callback(new PlayerLeaveEvent(player));
239227
}
240228
;
241-
};
242-
for (var _i = 0, players_1 = players; _i < players_1.length; _i++) {
243-
var player = players_1[_i];
244-
_loop_1(player);
245229
}
246230
;
247231
players = [];
248-
players = __spreadArray([], world.getPlayers(), true).map(function (pl) { return new Player(pl); });
232+
players = [...world.getPlayers()].map(pl => new Player(pl));
249233
});
250234
return callback;
251-
};
235+
}
252236
;
253237
/**
254238
* @remarks
255239
* Removes a callback from being called when a player leaves
256240
* the world.
257241
* @param {(arg: PlayerLeaveEvent) => void} callback
258242
*/
259-
PlayerLeaveEventSignal.prototype.unsubscribe = function (callback) {
243+
unsubscribe(callback) {
260244
callback["playerLeave"] = false;
261-
};
245+
}
262246
;
263-
return PlayerLeaveEventSignal;
264-
}());
265-
export { PlayerLeaveEventSignal };
247+
}
266248
;

scripts/player-leave-event/PlayerLeaveEvent.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class Player {
116116
* @throws This property can throw when used.
117117
*/
118118
public readonly 'viewDirection': Vector;
119+
readonly typeId: string;
119120
/**
120121
* @remarks
121122
* Gets the first block that intersects with the vector of the
@@ -136,9 +137,9 @@ class Player {
136137
* the entity, undefined is returned.
137138
*/
138139
getComponent(componentId: string): IEntityComponent {
139-
return this.__PlayerComponents.find(function (component: any) {
140+
return this.__PlayerComponents.find(function (component: IEntityComponent) {
140141
if (!componentId.startsWith("minecraft:")) componentId = "minecraft:" + componentId;
141-
if (component.id === componentId) return true;
142+
if (component.typeId === componentId) return true;
142143
});
143144
};
144145
/**
@@ -215,6 +216,7 @@ class Player {
215216
this.dimension = player.dimension;
216217
this.headLocation = player.getHeadLocation();
217218
this.id = player.id;
219+
this.typeId = player.typeId;
218220
this.isSneaking = player.isSneaking;
219221
this.location = player.location;
220222
this.name = player.name;

scripts/sell-items/sell-items.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://discord.com/channels/523663022053392405/854033525546942464/988294124392284181
2-
import { Items, ItemStack, Player, PlayerInventoryComponentContainer } from "@minecraft/server";
2+
import { Items, ItemStack, MinecraftItemTypes, Player, PlayerInventoryComponentContainer } from "@minecraft/server";
33

44
let sellItems = [{
55
id: 'minecraft:sand',
@@ -23,15 +23,14 @@ const sell = (player) => {
2323
*/
2424
// @ts-ignore
2525
const inv = player.getComponent('inventory').container, { size } = inv
26-
const voidSlot = new ItemStack(Items.get('minecraft:air'), 1)
2726
let amount = 0;
2827
for (let i = 0; i < size; i++) {
2928
const item = inv.getItem(i)
3029
if (!item) continue;
3130
const soldItem = sellItems.find(element => element.id === item.typeId)
3231
if (!soldItem) continue;
3332
amount = amount + soldItem.value * item.amount
34-
inv.setItem(i, voidSlot)
33+
inv.setItem(i);
3534
}
3635
player.runCommandAsync(`scoreboard players add @s Money ${amount}`)
3736
return amount

0 commit comments

Comments
 (0)