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

231 lines
7.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(`${region} unlocked`);
return;
}
// reason codes are not display text - this is developer-facing
// for now. Player copy belongs in the lang file later.
let why;
switch (result.reason) {
case "already_unlocked":
why = `${region} is already unlocked`;
break;
case "missing_prior_region":
why = result.requires
? `You must unlock ${result.requires} first`
: `${region} has nothing before it in the chain`;
break;
case "insufficient_funds":
why = `You need ${result.cost} coins, you have ${result.balance}`;
break;
case "unknown_region":
why = `Unknown region: ${region}`;
break;
default:
why = `Cannot unlock ${region}`;
}
origin.sourceEntity.sendMessage(why);
});
return {
status: CustomCommandStatus.Success,
message: `Unlock requested for ${region}`,
};
}),
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}`,
};
}
)})