|
| 1 | +export function registerNpcSpawnCommand(): void { |
| 2 | + commands.registerWorld("npcspawn", "Spawn an NPC by role name", (ctx) => { |
| 3 | + const input = ctx.getInput(); |
| 4 | + const parts = input.split(" "); |
| 5 | + |
| 6 | + if (parts.length < 2) { |
| 7 | + ctx.sendMessage("Usage: /npcspawn <role_name> [scale]"); |
| 8 | + ctx.sendMessage("Example: /npcspawn trork_warrior"); |
| 9 | + return; |
| 10 | + } |
| 11 | + |
| 12 | + const roleName = parts[1]; |
| 13 | + const scale = parts.length >= 3 ? parseFloat(parts[2]) : 1.0; |
| 14 | + |
| 15 | + if (isNaN(scale) || scale <= 0) { |
| 16 | + ctx.sendMessage("Invalid scale value"); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + const senderName = ctx.getSenderName(); |
| 21 | + const universe = Universe.get(); |
| 22 | + const players = universe.getPlayers(); |
| 23 | + |
| 24 | + let foundPlayerRef = null; |
| 25 | + for (let i = 0; i < players.length; i++) { |
| 26 | + if (players[i].getUsername() === senderName) { |
| 27 | + foundPlayerRef = players[i]; |
| 28 | + break; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + if (!foundPlayerRef) { |
| 33 | + ctx.sendMessage("Could not find player"); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + const transform = foundPlayerRef.getTransform(); |
| 38 | + const playerPos = transform.getPosition(); |
| 39 | + const playerRot = transform.getRotation(); |
| 40 | + |
| 41 | + const spawnPos = new Vector3d(playerPos.getX() + 2, playerPos.getY(), playerPos.getZ()); |
| 42 | + const spawnRot = new Vector3f(0, playerRot.getYaw(), 0); |
| 43 | + |
| 44 | + const npcPlugin = NPCPlugin.get(); |
| 45 | + |
| 46 | + if (!npcPlugin.hasRoleName(roleName)) { |
| 47 | + ctx.sendMessage("Unknown NPC role: " + roleName); |
| 48 | + ctx.sendMessage("Use /npcroles to see available roles"); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const worldUuid = foundPlayerRef.getWorldUuid(); |
| 53 | + if (!worldUuid) { |
| 54 | + ctx.sendMessage("Player is not in a world"); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + const world = universe.getWorld(worldUuid); |
| 59 | + if (!world) { |
| 60 | + ctx.sendMessage("Could not find player's world"); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + const store = world.getEntityStore().getStore(); |
| 65 | + |
| 66 | + const result = npcPlugin.spawnNPC(store, roleName, null, spawnPos, spawnRot); |
| 67 | + |
| 68 | + if (result) { |
| 69 | + ctx.sendMessage("Spawned NPC: " + roleName); |
| 70 | + } else { |
| 71 | + ctx.sendMessage("Failed to spawn NPC: " + roleName); |
| 72 | + } |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +export function registerNpcRolesCommand(): void { |
| 77 | + commands.register("npcroles", "List available NPC roles", (ctx) => { |
| 78 | + const npcPlugin = NPCPlugin.get(); |
| 79 | + const roles = npcPlugin.getRoleTemplateNames(true); |
| 80 | + |
| 81 | + ctx.sendMessage("Available NPC roles (" + roles.length + "):"); |
| 82 | + |
| 83 | + const maxDisplay = 20; |
| 84 | + for (let i = 0; i < Math.min(roles.length, maxDisplay); i++) { |
| 85 | + ctx.sendMessage(" - " + roles[i]); |
| 86 | + } |
| 87 | + |
| 88 | + if (roles.length > maxDisplay) { |
| 89 | + ctx.sendMessage(" ... and " + (roles.length - maxDisplay) + " more"); |
| 90 | + } |
| 91 | + }); |
| 92 | +} |
| 93 | + |
| 94 | +export function registerNpcDespawnCommand(): void { |
| 95 | + commands.registerWorld("npcdespawn", "Despawn nearby NPCs", (ctx) => { |
| 96 | + const input = ctx.getInput(); |
| 97 | + const parts = input.split(" "); |
| 98 | + |
| 99 | + const radius = parts.length >= 2 ? parseFloat(parts[1]) : 10.0; |
| 100 | + |
| 101 | + if (isNaN(radius) || radius <= 0) { |
| 102 | + ctx.sendMessage("Invalid radius"); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + const senderName = ctx.getSenderName(); |
| 107 | + const universe = Universe.get(); |
| 108 | + const players = universe.getPlayers(); |
| 109 | + |
| 110 | + let foundPlayerRef = null; |
| 111 | + for (let i = 0; i < players.length; i++) { |
| 112 | + if (players[i].getUsername() === senderName) { |
| 113 | + foundPlayerRef = players[i]; |
| 114 | + break; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (!foundPlayerRef) { |
| 119 | + ctx.sendMessage("Could not find player"); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + const transform = foundPlayerRef.getTransform(); |
| 124 | + const playerPos = transform.getPosition(); |
| 125 | + |
| 126 | + const worldUuid = foundPlayerRef.getWorldUuid(); |
| 127 | + if (!worldUuid) { |
| 128 | + ctx.sendMessage("Player is not in a world"); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + const world = universe.getWorld(worldUuid); |
| 133 | + if (!world) { |
| 134 | + ctx.sendMessage("Could not find player's world"); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + const store = world.getEntityStore().getStore(); |
| 139 | + |
| 140 | + const npcComponentType = NPCEntity.getComponentType(); |
| 141 | + if (!npcComponentType) { |
| 142 | + ctx.sendMessage("NPC system not available"); |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + let despawnCount = 0; |
| 147 | + const radiusSq = radius * radius; |
| 148 | + |
| 149 | + store.forEachChunk(npcComponentType, (archetypeChunk, commandBuffer) => { |
| 150 | + for (let index = 0; index < archetypeChunk.size(); index++) { |
| 151 | + const npcEntity = archetypeChunk.getComponent(index, npcComponentType); |
| 152 | + if (npcEntity) { |
| 153 | + const npcRef = archetypeChunk.getReferenceTo(index); |
| 154 | + const transformComponent = commandBuffer.getComponent(npcRef, TransformComponent.getComponentType()); |
| 155 | + if (transformComponent) { |
| 156 | + const npcPos = transformComponent.getPosition(); |
| 157 | + const dx = npcPos.getX() - playerPos.getX(); |
| 158 | + const dy = npcPos.getY() - playerPos.getY(); |
| 159 | + const dz = npcPos.getZ() - playerPos.getZ(); |
| 160 | + const distSq = dx * dx + dy * dy + dz * dz; |
| 161 | + |
| 162 | + if (distSq <= radiusSq) { |
| 163 | + npcEntity.setToDespawn(); |
| 164 | + npcEntity.setDespawnTime(0); |
| 165 | + despawnCount++; |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + }); |
| 171 | + |
| 172 | + ctx.sendMessage("Marked " + despawnCount + " NPCs for despawn within " + radius + " blocks"); |
| 173 | + }); |
| 174 | +} |
0 commit comments