This commit is contained in:
ZXMushroom63 2024-09-30 13:26:39 +08:00
commit 402a73642d

View File

@ -0,0 +1,39 @@
(() => {
PluginAPI.dedicatedServer.appendCode(function () {
PluginAPI.addEventListener("processcommand", (event) => {
// Check if the command is "/spawnnpc"
if (event.command.toLowerCase().startsWith("/spawnnpc")) {
const world = event.sender.getServerForPlayer();
const senderPos = event.sender.getPosition();
// Create a sheep entity
const EntitySheepClass = ModAPI.reflect.getClassById("net.minecraft.entity.passive.EntitySheep");
const sheep = EntitySheepClass.constructors[1](world.getRef());
// Set sheep's position to player's position
sheep.setPosition(senderPos.getX(), senderPos.getY(), senderPos.getZ());
// Disable AI (no AI behavior)
sheep.getDataWatcher().updateObject(15, 1); // AI flag, 15 is the byte for AI, 1 means no AI
// Disable gravity
sheep.noGravity = 1;
// Make sheep invincible
sheep.setEntityInvulnerable(1);
// Add the sheep to the world
world.spawnEntityInWorld(sheep);
// Notify the player that the sheep has been spawned
const ChatComponentTextClass = ModAPI.reflect.getClassById("net.minecraft.util.ChatComponentText");
event.sender.addChatMessage(ChatComponentTextClass.constructors[0](ModAPI.util.str("A special sheep has been spawned!")));
// Prevent the command from executing further
event.preventDefault = true;
}
});
});
})();