Skip to content

Commit

Permalink
Create npcspawner.js
Browse files Browse the repository at this point in the history
  • Loading branch information
radmanplays authored Sep 14, 2024
1 parent 5a6628a commit 1fd14a6
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions examplemods/npcspawner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(() => {
PluginAPI.dedicatedServer.appendCode(function () {
PluginAPI.addEventListener("processcommand", (event) => {
if (!ModAPI.reflect.getClassById("net.minecraft.entity.player.EntityPlayerMP").instanceOf(event.sender.getRef())) { return }

if (event.command.toLowerCase().startsWith("/spawnnpc")) {
const world = event.sender.getServerForPlayer();
const playerPos = event.sender.getPosition();

// Create a fake player entity
const FakePlayerClass = ModAPI.reflect.getClassById("com.mojang.authlib.GameProfile");
const fakeProfile = FakePlayerClass.constructors[2](
java.util.UUID.randomUUID(), ModAPI.util.str("Steve")
);

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());

// 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!")));

event.preventDefault = true;
}
});
});
})();

0 comments on commit 1fd14a6

Please sign in to comment.