Skip to content

Commit

Permalink
feat: added Serenity.commands
Browse files Browse the repository at this point in the history
  • Loading branch information
PMK744 committed Feb 7, 2025
1 parent 7286318 commit e60674c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 71 deletions.
71 changes: 1 addition & 70 deletions devapp/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import {
Serenity,
LevelDBProvider,
CustomItemType,
WorldEvent,
EntityIdentifier,
ItemStack,
EntityEquipmentTrait,
ItemEnchantableTrait,
ItemIdentifier
} from "@serenityjs/core";
import { Serenity, LevelDBProvider } from "@serenityjs/core";
import { Pipeline } from "@serenityjs/plugins";
import { Enchantment, EquipmentSlot, WearableSlot } from "@serenityjs/protocol";

// Create a new Serenity instance
const serenity = new Serenity({
Expand All @@ -30,61 +19,3 @@ serenity.registerProvider(LevelDBProvider, { path: "./worlds" });

// Start the server
serenity.start();

const ht = new CustomItemType("bridge:armor0_helmet", { maxAmount: 1 });
ht.components.maxStackSize = 1;
ht.components.wearable.slot = WearableSlot.Head;
ht.components.wearable.protection = 100;
ht.components.icon = "bridge_armor0_helmet";

const ct = new CustomItemType("bridge:armor0_chestplate", { maxAmount: 1 });
ct.components.maxStackSize = 1;
ct.components.wearable.slot = WearableSlot.Chest;
ct.components.wearable.protection = 100;
ct.components.icon = "bridge_armor0_chestplate";

const lt = new CustomItemType("bridge:armor0_leggings", { maxAmount: 1 });
lt.components.maxStackSize = 1;
lt.components.wearable.slot = WearableSlot.Legs;
lt.components.wearable.protection = 100;
lt.components.icon = "bridge_armor0_leggings";

const bt = new CustomItemType("bridge:armor0_boots", { maxAmount: 1 });
bt.components.maxStackSize = 1;
bt.components.wearable.slot = WearableSlot.Feet;
bt.components.wearable.protection = 100;
bt.components.icon = "bridge_armor0_boots";

for (const [, world] of serenity.worlds) {
world.itemPalette.registerType(ht);
world.itemPalette.registerType(ct);
world.itemPalette.registerType(lt);
world.itemPalette.registerType(bt);
}

serenity.on(WorldEvent.EntitySpawned, ({ entity }) => {
if (entity.identifier !== EntityIdentifier.Zombie) return;

const equipment = entity.addTrait(EntityEquipmentTrait);

const helmet = new ItemStack(ItemIdentifier.NetheriteHelmet);
const he = helmet.addTrait(ItemEnchantableTrait);
he.addEnchantment(Enchantment.Protection, 4);

const chestplate = new ItemStack(ItemIdentifier.NetheriteChestplate);
const hc = chestplate.addTrait(ItemEnchantableTrait);
hc.addEnchantment(Enchantment.Protection, 4);

const leggings = new ItemStack(ItemIdentifier.NetheriteLeggings);
const hl = leggings.addTrait(ItemEnchantableTrait);
hl.addEnchantment(Enchantment.Protection, 4);

const boots = new ItemStack(ItemIdentifier.NetheriteBoots);
const hb = boots.addTrait(ItemEnchantableTrait);
hb.addEnchantment(Enchantment.Protection, 4);

equipment.setEqupment(EquipmentSlot.Head, helmet);
equipment.setEqupment(EquipmentSlot.Chest, chestplate);
equipment.setEqupment(EquipmentSlot.Legs, leggings);
equipment.setEqupment(EquipmentSlot.Feet, boots);
});
8 changes: 7 additions & 1 deletion packages/core/src/serenity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
type WorldProvider
} from "./world";
import { Player } from "./entity";
import { ConsoleInterface, WorldEnum } from "./commands";
import { ConsoleInterface, WorldEnum, Commands } from "./commands";
import { Permissions } from "./permissions";
import { ServerEvent } from "./enums";
import { ResourcePackManager } from "./resource-packs";
Expand Down Expand Up @@ -89,6 +89,12 @@ class Serenity extends Emitter<WorldEventSignals & ServerEvents> {

public readonly resourcePacks: ResourcePackManager;

/**
* The global commands registry for the server.
* The commands will register on every world that is created.
*/
public readonly commands = new Commands();

/**
* Whether the server is currently running or not
*/
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/world/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ class World extends Emitter<WorldEventSignals> {
// Create a new logger for the world
this.logger = new Logger(this.identifier, LoggerColors.GreenBright);

// Register all the global commands
for (const command of serenity.commands.getAll())
this.commands.commands.set(command.name, command);

// Register the admin commands
for (const command of [...AdminCommands, ...CommonCommands]) command(this);

Expand Down

0 comments on commit e60674c

Please sign in to comment.