From 48d64cf8a4b3eef2e33c420e134e8d2f8e01166a Mon Sep 17 00:00:00 2001 From: radmanplays <95340057+radmanplays@users.noreply.github.com> Date: Sat, 14 Sep 2024 15:57:14 +0330 Subject: [PATCH] Update npcspawner.js --- examplemods/npcspawner.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/examplemods/npcspawner.js b/examplemods/npcspawner.js index 5f2b0a9..f244add 100644 --- a/examplemods/npcspawner.js +++ b/examplemods/npcspawner.js @@ -1,33 +1,35 @@ (() => { PluginAPI.dedicatedServer.appendCode(function () { PluginAPI.addEventListener("processcommand", (event) => { - if (!ModAPI.reflect.getClassById("net.minecraft.entity.player.EntityPlayerMP").instanceOf(event.sender.getRef())) { return } + if (!ModAPI.reflect.getClassById("net.minecraft.entity.player.EntityPlayerMP").instanceOf(event.sender.getRef())) { return; } - if (event.command.toLowerCase().startsWith("/spawnnpc")) { - const world = event.sender.getEntityWorld(); - const playerPos = event.sender.getPosition(); + if (event.command.toLowerCase().startsWith("//spawnsteve")) { + const world = event.sender.getServerForPlayer(); + const senderPos = event.sender.getPosition(); - - // Create a fake player entity with a string-based UUID + // Create a fake player GameProfile const GameProfileClass = ModAPI.reflect.getClassById("com.mojang.authlib.GameProfile"); const fakeProfile = GameProfileClass.constructors[2]( - event.sender.getGameProfile().id2.toString(), ModAPI.util.str("Steve") + ModAPI.reflect.getClassById("java.util.UUID").staticMethods.randomUUID(), ModAPI.util.str("Steve") ); + // Get the EntityPlayerMP class to spawn the fake player const EntityPlayerMPClass = ModAPI.reflect.getClassById("net.minecraft.entity.player.EntityPlayerMP"); const fakePlayer = EntityPlayerMPClass.constructors[1]( world.getMinecraftServer(), world.getRef(), fakeProfile, world.getPlayerInteractionManager() ); - // Set the player position - fakePlayer.setPosition(playerPos.$getX(), playerPos.$getY(), playerPos.$getZ()); + // Set the fake player position to be near the command sender + fakePlayer.setPosition(senderPos.$getX(), senderPos.$getY(), senderPos.$getZ()); // Add the fake player to the world world.addEntityToWorld(fakePlayer.getEntityId(), fakePlayer); - // Send a confirmation message to the player - event.sender.addChatMessage(ModAPI.reflect.getClassById("net.minecraft.util.ChatComponentText").constructors[0](ModAPI.util.str("Fake Steve has been spawned!"))); + // Notify the player that the fake player has been spawned + const ChatComponentTextClass = ModAPI.reflect.getClassById("net.minecraft.util.ChatComponentText"); + event.sender.addChatMessage(ChatComponentTextClass.constructors[0](ModAPI.util.str("Fake Steve has been spawned!"))); + // Prevent the command from executing further event.preventDefault = true; } });