Skip to content

Commit f705932

Browse files
authored
Upload generated files for scripts (JaylyDev#224)
1 parent 4184b4e commit f705932

9 files changed

Lines changed: 145 additions & 105 deletions

File tree

scripts/editor-fullbright/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class FullbrightToggle {
1313
const enableAction = uiSession.actionManager.createAction({
1414
actionType: ActionTypes.NoArgsAction,
1515
onExecute: () => {
16-
player.addEffect(MinecraftEffectTypes.nightVision, 20000000, 1, false);
16+
player.addEffect(MinecraftEffectTypes.nightVision, 20000000, { amplifier: 1, showParticles: false });
1717
},
1818
});
1919
const disableAction = uiSession.actionManager.createAction({

scripts/jaylydb/index.js

Lines changed: 103 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Script example for ScriptAPI
22
// Author: Jayly <https://github.com/JaylyDev>
33
// Project: https://github.com/JaylyDev/ScriptAPI
4-
var _a, _b, _c;
4+
var _a;
55
// Script example for ScriptAPI
66
// Author: Jayly <https://github.com/JaylyDev>
77
// Project: https://github.com/JaylyDev/ScriptAPI
@@ -14,138 +14,156 @@ const str = () => ('00000000000000000' + (Math.random() * 0xffffffffffffffff).to
1414
* @beta
1515
*/
1616
const uuid = () => {
17-
const a = str();
18-
const b = str();
19-
return a.slice(0, 8) + '-' + a.slice(8, 12) + '-4' + a.slice(13) + '-a' + b.slice(1, 4) + '-' + b.slice(4);
17+
const [a, b] = [str(), str()];
18+
return `${a.slice(0, 8)}-${a.slice(8, 12)}-4${a.slice(13)}-a${b.slice(1, 4)}-${b.slice(4)}`;
2019
};
21-
/**
22-
* an insecure but simple text cipher/decipher utility.
23-
*/
24-
const cipher = {
25-
cipher(salt) {
26-
const textToChars = (text) => text.split('').map(c => c.charCodeAt(0));
27-
const byteHex = (n) => ("0" + Number(n).toString(16)).substr(-2);
28-
const applySaltToChar = (code) => textToChars(salt).reduce((a, b) => a ^ b, code);
29-
return (text) => text.split('')
30-
.map(textToChars)
31-
.map(applySaltToChar)
32-
.map(byteHex)
33-
.join('');
34-
},
35-
decipher(salt) {
36-
const textToChars = (text) => text.split('').map((c) => c.charCodeAt(0));
37-
const applySaltToChar = (code) => textToChars(salt).reduce((a, b) => a ^ b, code);
38-
return (encoded) => encoded.match(/.{1,2}/g)
39-
.map(hex => parseInt(hex, 16))
40-
.map(applySaltToChar)
41-
.map(charCode => String.fromCharCode(charCode))
42-
.join('');
20+
const allowedTypes = ["string", "number", "boolean"];
21+
function encrypt(data, salt) {
22+
const encryptedChars = [];
23+
for (let i = 0; i < data.length; i++) {
24+
const charCode = data.charCodeAt(i) + salt.charCodeAt(i % salt.length);
25+
encryptedChars.push(charCode);
4326
}
44-
};
27+
return String.fromCharCode(...encryptedChars);
28+
}
29+
function decrypt(encrypted, salt) {
30+
const decryptedChars = [];
31+
for (let i = 0; i < encrypted.length; i++) {
32+
const charCode = encrypted.charCodeAt(i) - salt.charCodeAt(i % salt.length);
33+
decryptedChars.push(charCode);
34+
}
35+
return String.fromCharCode(...decryptedChars);
36+
}
4537
/**
4638
* Parse and stringify scoreboard display name
4739
* @beta
4840
*/
49-
const DisplayName = new (_b = class DisplayName {
50-
constructor() {
51-
this[_a] = DisplayName.name;
41+
const DisplayName = {
42+
parse(text, salt) {
43+
try {
44+
const a = JSON.parse(`"${salt ? decrypt(text, salt) : text}"`);
45+
return JSON.parse(`{${a}}`);
5246
}
53-
parse(text, salt) {
54-
try {
55-
const d = cipher.decipher(salt);
56-
const c = d(text);
57-
const a = JSON.parse(`"${c}"`); // "key":"value"
58-
const b = JSON.parse(`{${a}}`); // {"key":"value"}
59-
return b;
60-
}
61-
catch (error) {
62-
console.error(error);
63-
return {};
64-
}
65-
}
66-
stringify(value, salt) {
67-
const d = cipher.cipher(salt);
68-
const rawtext = JSON.stringify(JSON.stringify(value).slice(1, -1)).slice(1, -1);
69-
return d(rawtext);
47+
catch (error) {
48+
throw new Error(`Failed to parse JSON data: ${error.message}`);
7049
}
7150
},
72-
_a = Symbol.toStringTag,
73-
_b)();
51+
stringify(value, salt) {
52+
try {
53+
const a = JSON.stringify(JSON.stringify(value).slice(1, -1)).slice(1, -1);
54+
return salt ? encrypt(a, salt) : a;
55+
}
56+
catch (error) {
57+
throw new Error(`Failed to stringify JSON data: ${error.message}`);
58+
}
59+
}
60+
};
61+
const overworld = world.getDimension("overworld");
62+
;
7463
/**
7564
* Database using scoreboard
7665
* @beta
7766
*/
7867
class JaylyDB {
79-
constructor(id) {
80-
this[_c] = JaylyDB.name;
81-
this.objective = world.scoreboard.getObjective(`jaylydb:` + id) ?? world.scoreboard.addObjective(`jaylydb:` + id, uuid());
68+
get salt() {
69+
return this.encrypted ? this.objective.displayName : undefined;
8270
}
8371
;
84-
getParticipant(key) {
85-
return this.objective.getParticipants().find(participant => participant.type === ScoreboardIdentityType.fakePlayer
86-
&& key in DisplayName.parse(participant.displayName, this.objective.displayName));
72+
findParticipant(key, getOptions) {
73+
let data;
74+
let participant;
75+
this.displayNames.find(displayName => {
76+
const displayData = DisplayName.parse(displayName, this.salt);
77+
if (!(key in displayData))
78+
return false;
79+
if (getOptions.data)
80+
data = displayData;
81+
if (getOptions.participant)
82+
participant = this.objective.getParticipants().find((participant) => participant.displayName === displayName);
83+
return true;
84+
});
85+
if (!data)
86+
return;
87+
return { data, participant };
8788
}
8889
;
90+
updateParticipants() {
91+
this.participants = this.objective.getParticipants().filter((participant) => participant.type === ScoreboardIdentityType.fakePlayer);
92+
this.displayNames = this.participants.map((participant) => participant.displayName);
93+
this.displayKeys = this.displayNames.map((displayName) => Object.keys(DisplayName.parse(displayName, this.salt))[0]);
94+
}
95+
constructor(id, encrypted = false) {
96+
this[_a] = JaylyDB.name;
97+
this.objective = world.scoreboard.getObjective(`jaylydb:` + id) ?? world.scoreboard.addObjective(`jaylydb:` + id, uuid());
98+
this.encrypted = encrypted;
99+
this.updateParticipants();
100+
}
101+
get size() {
102+
return this.participants.length;
103+
}
89104
clear() {
90-
this.objective.getParticipants().forEach(this.objective.removeParticipant);
105+
this.participants.forEach(this.objective.removeParticipant);
106+
this.updateParticipants();
91107
}
92108
delete(key) {
93-
const participant = this.getParticipant(key);
94-
if (!participant)
95-
return;
96-
return this.objective.removeParticipant(participant);
109+
const scoreboard = this.findParticipant(key, {
110+
participant: true,
111+
data: false
112+
});
113+
if (!scoreboard)
114+
return false;
115+
const success = this.objective.removeParticipant(scoreboard.participant);
116+
this.updateParticipants();
117+
return success;
97118
}
98119
forEach(callbackfn) {
99-
for (const [key, value] of this.entries()) {
120+
for (const [key, value] of this.entries())
100121
callbackfn(value, key, this);
101-
}
102122
}
103123
get(key) {
104-
const participant = this.getParticipant(key);
105-
return DisplayName.parse(participant.displayName, this.objective.displayName)[key];
124+
const scoreboard = this.findParticipant(key, {
125+
participant: false,
126+
data: true
127+
});
128+
if (!scoreboard)
129+
return;
130+
return scoreboard.data[key];
106131
}
107132
has(key) {
108-
const participant = this.getParticipant(key);
109-
return !!participant;
133+
return this.displayKeys.includes(key);
110134
}
111135
set(key, value) {
112-
const allowedTypes = ["string", "number", "boolean"];
113136
if (!allowedTypes.includes(typeof (value)))
114137
throw TypeError("JaylyDB::set only accepts value of string, number, or boolean.");
138+
if (this.get(key) === value)
139+
return this; // No need to update if value hasn't changed
115140
// throws error if string value is over 32767
116-
if (typeof (value) === "string" && value.length > 32767)
117-
throw RangeError("JaylyDB::set only accepts string value of length less than 32767.");
141+
const str = DisplayName.stringify({ [key]: value }, this.salt);
142+
if (str.length > 32767)
143+
throw RangeError("JaylyDB::set only accepts string value less than 32767 characters.");
118144
this.delete(key);
119-
world.getDimension("overworld").runCommand(`scoreboard players set "${DisplayName.stringify({ [key]: value }, this.objective.displayName)}" ${this.objective.id} 0`);
145+
overworld.runCommand(`scoreboard players set "${str}" ${this.objective.id} 0`);
146+
this.updateParticipants();
120147
return this;
121148
}
122-
get size() {
123-
return this.objective.getParticipants().length;
124-
}
125-
;
126149
*entries() {
127-
const values = this.objective.getParticipants();
128-
for (const value of values) {
129-
const valueObject = DisplayName.parse(value.displayName, this.objective.displayName);
130-
const valueLength = DisplayName.stringify(valueObject, this.objective.displayName).length;
131-
if (valueLength > 0)
132-
yield [Object.keys(valueObject)[0], Object.values(valueObject)[0]];
150+
for (const displayName of this.displayNames) {
151+
const valueObject = DisplayName.parse(displayName, this.salt);
152+
yield [Object.keys(valueObject)[0], Object.values(valueObject)[0]];
133153
}
134154
}
135155
*keys() {
136-
for (const [key] of this.entries()) {
156+
for (const [key] of this.entries())
137157
yield key;
138-
}
139158
}
140159
*values() {
141-
for (const [, value] of this.entries()) {
160+
for (const [, value] of this.entries())
142161
yield value;
143-
}
144162
}
145163
[Symbol.iterator]() {
146164
return this.entries();
147165
}
148166
}
149-
_c = Symbol.toStringTag;
167+
_a = Symbol.toStringTag;
150168
;
151169
export { JaylyDB };

scripts/player-velocity-fix/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export function setVelocity(velocity, player) {
3131
let health = entity.getComponent('health');
3232
let movement = entity.getComponent('movement');
3333
let rideable = entity.getComponent('rideable');
34-
entity.addEffect(MinecraftEffectTypes.invisibility, 0x7fff, 255, false); // makes the entity invisible
35-
entity.addEffect(MinecraftEffectTypes.resistance, 0x7fff, 255, false); // makes the entity invisible
34+
entity.addEffect(MinecraftEffectTypes.invisibility, 0x7fff, { amplifier: 255, showParticles: false }); // makes the entity invisible
35+
entity.addEffect(MinecraftEffectTypes.resistance, 0x7fff, { amplifier: 255, showParticles: false }); // makes the entity invisible
3636
entity.applyImpulse(velocity);
3737
let onInterval = setInterval((isEntityMoving) => {
3838
try {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# scoreboard-identity
2+
3+
## Description
4+
> This README is auto generated, Edit the README so that users know what this package does.
5+
6+
## Credits
7+
These scripts were written by [bot174](https://github.com/bot174)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Script example for ScriptAPI
2+
// Author: bot174 <https://github.com/bot174>
3+
// Project: https://github.com/bot174/ScriptAPI
4+
import { world, MinecraftDimensionTypes } from "@minecraft/server";
5+
function createScoreboardIdentity(objective, displayName) {
6+
world.getDimension(MinecraftDimensionTypes.overworld).runCommand(`scoreboard players add "${displayName}" "${objective.id}" 0`);
7+
const participant = objective.getParticipants().find(participant => participant.displayName === displayName);
8+
return participant;
9+
}
10+
;
11+
export default createScoreboardIdentity;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { world } from "@minecraft/server";
2+
import createScoreboardIdentity from "./index";
3+
const objective = world.scoreboard.addObjective("HelloWorld", "HelloWorld");
4+
const participant = createScoreboardIdentity(objective, "My Helo");
5+
console.warn(participant.displayName);
6+
participant.removeFromObjective(objective);

scripts/simulated-player/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,12 @@ export class SimulatedPlayer {
131131
* @param showParticles
132132
* @throws This function can throw errors.
133133
*/
134-
addEffect(effectType, duration, amplifier, showParticles) { return this.__player.addEffect(effectType, duration, amplifier, showParticles); }
134+
addEffect(effectType, duration, amplifier, showParticles) {
135+
return this.__player.addEffect(effectType, duration, {
136+
amplifier,
137+
showParticles
138+
});
139+
}
135140
;
136141
/**
137142
* @remarks

scripts/spawn-simulated-player/index.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99
import * as MinecraftServer from "@minecraft/server";
1010
import * as GameTest from "@minecraft/server-gametest";
11-
import { SimulatedPlayer } from "../simulated-player/index";
11+
;
1212
/**
1313
* Spawns a simulated player
1414
* @param target The player the simulated player is going to spawn at
@@ -20,15 +20,12 @@ export function SpawnSimulatedPlayer(target, callback) {
2020
if (!(target instanceof MinecraftServer.Player))
2121
throw new TypeError("Native type conversion failed.");
2222
GameTest.registerAsync(testClassName, testName, async function (test) {
23-
let simulatedplayer = test.spawnSimulatedPlayer({
24-
x: 0,
25-
y: 1,
26-
z: 0
27-
});
28-
callback(new SimulatedPlayer(simulatedplayer, test));
29-
}).structureName("DebugTests:always_succeed")
23+
let simulatedplayer = test.spawnSimulatedPlayer({ x: 0, y: 1, z: 0, });
24+
simulatedplayer.despawn = () => test.removeSimulatedPlayer(simulatedplayer);
25+
callback(simulatedplayer);
26+
})
27+
.structureName("DebugTests:always_succeed")
3028
.tag(GameTest.Tags.suiteDefault)
3129
.maxTicks(0x7fffffff);
3230
target.runCommandAsync(`gametest run ${testClassName}:${testName}`);
3331
}
34-
;
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { world, MinecraftEffectTypes, MinecraftItemTypes, ItemStack } from "@minecraft/server";
1+
import { world, MinecraftEffectTypes, MinecraftItemTypes, ItemStack, } from "@minecraft/server";
22
import { SpawnSimulatedPlayer } from "./index.js";
33
let host = [...world.getPlayers()][0];
44
SpawnSimulatedPlayer(host, function (simulatedPlayer) {
@@ -7,10 +7,6 @@ SpawnSimulatedPlayer(host, function (simulatedPlayer) {
77
simulatedPlayer.dimension.createExplosion(simulatedPlayer.location, 5);
88
simulatedPlayer.giveItem(new ItemStack(MinecraftItemTypes.acaciaBoat));
99
simulatedPlayer.runCommandAsync("scoreboard players add @s money 1");
10-
simulatedPlayer.teleport({
11-
x: 0,
12-
y: 0,
13-
z: 0
14-
}, simulatedPlayer.dimension, 0, 0);
15-
simulatedPlayer.kick("All tasks completed.");
10+
simulatedPlayer.teleport({ x: 0, y: 0, z: 0 }, { dimension: simulatedPlayer.dimension, rotation: { x: 0, y: 0 } });
11+
simulatedPlayer.despawn();
1612
});

0 commit comments

Comments
 (0)