Files
ip_content/ti_bp/scripts/core/commands.js
T

249 lines
8.0 KiB
JavaScript

import {
CommandPermissionLevel,
CustomCommandStatus,
CustomCommandParamType,
system,
} from "@minecraft/server";
import { REGIONS } from "../config/regions.js";
import { resetGates, resetGate, unlockGate, unlockGates,
updateBalance, returnBalance, printGateState} from "../save/state.js";
import { unlockRegion } from "../systems/progression.js";
//the system calls this function we do not
//our import into main runs the first line of this etc
system.beforeEvents.startup.subscribe(({ customCommandRegistry }) => {
customCommandRegistry.registerEnum("rzb_tamagotchi:region", Object.keys(REGIONS));
customCommandRegistry.registerCommand(
{
name: "rzb_tamagotchi:print_gates_states",
description: "This command prints the gate states",
permissionLevel: CommandPermissionLevel.Host,
cheatsRequired: true,
},
() => {
system.run(() => {
printGateState();
});
return {
status: CustomCommandStatus.Success
};
}
);
customCommandRegistry.registerCommand(
{
name: "rzb_tamagotchi:reset_gates_all",
description: "This command resets all gates to default",
permissionLevel: CommandPermissionLevel.Host,
cheatsRequired: true,
},
() => {
system.run(() => {
resetGates();
});
return {
status: CustomCommandStatus.Success,
message: "Gates reset to default",
};
}
);
customCommandRegistry.registerCommand(
{
name: "rzb_tamagotchi:reset_gate",
description: "This command resets a single gate",
permissionLevel: CommandPermissionLevel.Host,
cheatsRequired: true,
mandatoryParameters: [
{
// Use the enum by setting the name to the enum name
name: "rzb_tamagotchi:region",
type: CustomCommandParamType.Enum,
},
],
},
(origin, region) => {
system.run(() => {
resetGate(region);
});
return {
status: CustomCommandStatus.Success,
message: "Gate ${REGION[id]} reset to default",
};
});
customCommandRegistry.registerCommand(
{
name: "rzb_tamagotchi:unlocks_gates_all",
description: "This command unlocks all gates",
permissionLevel: CommandPermissionLevel.Host,
cheatsRequired: true,
},
() => {
system.run(() => {
unlockGates();
});
return {
status: CustomCommandStatus.Success,
message: "All gates Unlocked",
};
}
);
customCommandRegistry.registerCommand(
{
name: "rzb_tamagotchi:unlock_gate",
description: "This command unlocks a single gate",
permissionLevel: CommandPermissionLevel.Host,
cheatsRequired: true,
mandatoryParameters: [
{
// Use the enum by setting the name to the enum name
name: "rzb_tamagotchi:region",
type: CustomCommandParamType.Enum,
},
],
},
(origin, region) => {
system.run(() => {
unlockGate(region, true);
});
return {
status: CustomCommandStatus.Success,
message: `Gate ${REGIONS[id]} unlocked`,
};
}),
customCommandRegistry.registerCommand(
{
name: "rzb_tamagotchi:request_unlock",
description: "This command calls unlockRegion(), for entities",
permissionLevel: CommandPermissionLevel.Any,
cheatsRequired: false,
mandatoryParameters: [
{
// Use the enum by setting the name to the enum name
name: "rzb_tamagotchi:region",
type: CustomCommandParamType.Enum,
},
],
},
(origin, region) => {
system.run(() => {
const result = unlockRegion(region);
// no entity to talk to when run from a command block or console
if (!origin.sourceEntity) { return; }
if (result.ok) {
origin.sourceEntity.sendMessage({
translate: "tama.unlock.success",
with: { rawtext: [{ translate: REGIONS[region].nameKey }] }
});
return;
}
// reason codes are turned into player-facing text here.
// Every branch produces a RawMessage - a plain string would be
// sent literally instead of being looked up in the lang file.
let why;
switch (result.reason) {
case "already_unlocked":
why = {
translate: "tama.unlock.already_unlocked",
with: { rawtext: [{ translate: REGIONS[region].nameKey }] }
};
break;
case "missing_prior_region":
why = result.requires
? {
translate: "tama.unlock.missing_prior",
with: { rawtext: [{ translate: REGIONS[result.requires].nameKey }] }
}
: { translate: "tama.unlock.first_region" };
break;
case "insufficient_funds":
why = {
translate: "tama.unlock.insufficient_funds",
with: [String(result.cost), String(result.balance)]
};
break;
case "unknown_region":
why = { translate: "tama.unlock.unknown_region" };
break;
default:
why = {
translate: "tama.unlock.default",
with: { rawtext: [{ translate: REGIONS[region].nameKey }] }
};
}
origin.sourceEntity.sendMessage(why);
});
// note: CustomCommandResult.message is typed `string`, so it cannot
// carry a RawMessage and can never be translated. All player-facing
// text for this command goes through sendMessage above instead.
return {
status: CustomCommandStatus.Success,
};
}),
customCommandRegistry.registerCommand(
{
name: "rzb_tamagotchi:balance_update",
description: "This command updates the player balance",
permissionLevel: CommandPermissionLevel.Host,
cheatsRequired: true,
mandatoryParameters: [
{
name: "rzb_tamagotchi:cost",
type: CustomCommandParamType.Integer,
},
],
},
(origin, cost) => {
system.run(() => { //deferred tick so we get return message first
updateBalance(cost);
// the new balance only exists after the deferred update runs,
// so report it here rather than in the return value below
if (origin.sourceEntity) {
origin.sourceEntity.sendMessage(`Your balance is ${returnBalance()}`);
}
});
return {
status: CustomCommandStatus.Success,
message: `Balance changed by ${cost}`,
};
}
)})