From 707d3a1f14d6fb89504faab39ad73478e13bdd18 Mon Sep 17 00:00:00 2001 From: radmanplays <95340057+radmanplays@users.noreply.github.com> Date: Sun, 29 Sep 2024 16:22:54 +0330 Subject: [PATCH 1/8] Create mobnpcspawner.js --- examplemods/mobnpcspawner.js | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 examplemods/mobnpcspawner.js diff --git a/examplemods/mobnpcspawner.js b/examplemods/mobnpcspawner.js new file mode 100644 index 0000000..127295a --- /dev/null +++ b/examplemods/mobnpcspawner.js @@ -0,0 +1,56 @@ +(() => { + PluginAPI.dedicatedServer.appendCode(function () { + PluginAPI.addEventListener("processcommand", (event) => { + // Check if the sender is a player + if (!ModAPI.reflect.getClassById("net.minecraft.entity.player.EntityPlayerMP").instanceOf(event.sender.getRef())) { return; } + + // 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 = true; + + // Make sheep invincible + sheep.setEntityInvulnerable(true); + + // Prevent the sheep from taking any damage + const DamageSourceClass = ModAPI.reflect.getClassById("net.minecraft.util.DamageSource"); + ModAPI.addEventListener("livinghurt", (event) => { + if (sheep.equals(event.entityLiving)) { + event.setCanceled(true); + } + }); + + // Add the sheep to the world + world.addEntityToWorld(sheep.getEntityId(), 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!"))); + + // Add an event listener for when the sheep is right-clicked + ModAPI.addEventListener("rightclickentity", (clickEvent) => { + if (sheep.equals(clickEvent.target)) { + // Send message to the player who right-clicked + clickEvent.entityPlayer.addChatMessage(ChatComponentTextClass.constructors[0](ModAPI.util.str("bahhh"))); + } + }); + + // Prevent the command from executing further + event.preventDefault = true; + } + }); + }); +})(); From 0224803460c24b51ef34ba8186b67a9e2c757cba Mon Sep 17 00:00:00 2001 From: radmanplays <95340057+radmanplays@users.noreply.github.com> Date: Sun, 29 Sep 2024 16:23:54 +0330 Subject: [PATCH 2/8] Update mobnpcspawner.js --- examplemods/mobnpcspawner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examplemods/mobnpcspawner.js b/examplemods/mobnpcspawner.js index 127295a..6893152 100644 --- a/examplemods/mobnpcspawner.js +++ b/examplemods/mobnpcspawner.js @@ -34,7 +34,7 @@ }); // Add the sheep to the world - world.addEntityToWorld(sheep.getEntityId(), sheep); + world.spawnEntityInWorld(sheep.getEntityId(), sheep); // Notify the player that the sheep has been spawned const ChatComponentTextClass = ModAPI.reflect.getClassById("net.minecraft.util.ChatComponentText"); From 6ee7f22b972ccbab8c6effbf12d82909216d1aa0 Mon Sep 17 00:00:00 2001 From: radmanplays <95340057+radmanplays@users.noreply.github.com> Date: Sun, 29 Sep 2024 16:25:04 +0330 Subject: [PATCH 3/8] Update mobnpcspawner.js --- examplemods/mobnpcspawner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examplemods/mobnpcspawner.js b/examplemods/mobnpcspawner.js index 6893152..7cea939 100644 --- a/examplemods/mobnpcspawner.js +++ b/examplemods/mobnpcspawner.js @@ -14,7 +14,7 @@ const sheep = EntitySheepClass.constructors[1](world.getRef()); // Set sheep's position to player's position - sheep.setPosition(senderPos.$getX(), senderPos.$getY(), senderPos.$getZ()); + 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 From bbba5feead167685bc4fb8b76f9a464e7baa742b Mon Sep 17 00:00:00 2001 From: radmanplays <95340057+radmanplays@users.noreply.github.com> Date: Sun, 29 Sep 2024 16:25:30 +0330 Subject: [PATCH 4/8] Update mobnpcspawner.js --- examplemods/mobnpcspawner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examplemods/mobnpcspawner.js b/examplemods/mobnpcspawner.js index 7cea939..6d9983c 100644 --- a/examplemods/mobnpcspawner.js +++ b/examplemods/mobnpcspawner.js @@ -20,7 +20,7 @@ sheep.getDataWatcher().updateObject(15, 1); // AI flag, 15 is the byte for AI, 1 means no AI // Disable gravity - sheep.noGravity = true; + sheep.noGravity = 1; // Make sheep invincible sheep.setEntityInvulnerable(true); From 0ca46f67afc54fd712afe00c196101dc79d742d2 Mon Sep 17 00:00:00 2001 From: radmanplays <95340057+radmanplays@users.noreply.github.com> Date: Sun, 29 Sep 2024 16:25:45 +0330 Subject: [PATCH 5/8] Update mobnpcspawner.js --- examplemods/mobnpcspawner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examplemods/mobnpcspawner.js b/examplemods/mobnpcspawner.js index 6d9983c..2247a4a 100644 --- a/examplemods/mobnpcspawner.js +++ b/examplemods/mobnpcspawner.js @@ -23,7 +23,7 @@ sheep.noGravity = 1; // Make sheep invincible - sheep.setEntityInvulnerable(true); + sheep.setEntityInvulnerable(1); // Prevent the sheep from taking any damage const DamageSourceClass = ModAPI.reflect.getClassById("net.minecraft.util.DamageSource"); From a8412c54b4ed6de4f3f09a07ad246a109d97093c Mon Sep 17 00:00:00 2001 From: radmanplays <95340057+radmanplays@users.noreply.github.com> Date: Sun, 29 Sep 2024 16:27:27 +0330 Subject: [PATCH 6/8] Update mobnpcspawner.js --- examplemods/mobnpcspawner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examplemods/mobnpcspawner.js b/examplemods/mobnpcspawner.js index 2247a4a..7856fb9 100644 --- a/examplemods/mobnpcspawner.js +++ b/examplemods/mobnpcspawner.js @@ -34,7 +34,7 @@ }); // Add the sheep to the world - world.spawnEntityInWorld(sheep.getEntityId(), sheep); + world.spawnEntityInWorld(sheep); // Notify the player that the sheep has been spawned const ChatComponentTextClass = ModAPI.reflect.getClassById("net.minecraft.util.ChatComponentText"); From 17a1e6ebcf6deb65b22255a5422ec3bc141389df Mon Sep 17 00:00:00 2001 From: radmanplays <95340057+radmanplays@users.noreply.github.com> Date: Sun, 29 Sep 2024 16:29:10 +0330 Subject: [PATCH 7/8] Update mobnpcspawner.js --- examplemods/mobnpcspawner.js | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/examplemods/mobnpcspawner.js b/examplemods/mobnpcspawner.js index 7856fb9..8e24aae 100644 --- a/examplemods/mobnpcspawner.js +++ b/examplemods/mobnpcspawner.js @@ -25,14 +25,6 @@ // Make sheep invincible sheep.setEntityInvulnerable(1); - // Prevent the sheep from taking any damage - const DamageSourceClass = ModAPI.reflect.getClassById("net.minecraft.util.DamageSource"); - ModAPI.addEventListener("livinghurt", (event) => { - if (sheep.equals(event.entityLiving)) { - event.setCanceled(true); - } - }); - // Add the sheep to the world world.spawnEntityInWorld(sheep); @@ -40,13 +32,6 @@ const ChatComponentTextClass = ModAPI.reflect.getClassById("net.minecraft.util.ChatComponentText"); event.sender.addChatMessage(ChatComponentTextClass.constructors[0](ModAPI.util.str("A special sheep has been spawned!"))); - // Add an event listener for when the sheep is right-clicked - ModAPI.addEventListener("rightclickentity", (clickEvent) => { - if (sheep.equals(clickEvent.target)) { - // Send message to the player who right-clicked - clickEvent.entityPlayer.addChatMessage(ChatComponentTextClass.constructors[0](ModAPI.util.str("bahhh"))); - } - }); // Prevent the command from executing further event.preventDefault = true; From 023a2dd8554fa65b3baf8106a64686db6b7e729e Mon Sep 17 00:00:00 2001 From: radmanplays <95340057+radmanplays@users.noreply.github.com> Date: Sun, 29 Sep 2024 16:46:57 +0330 Subject: [PATCH 8/8] Update mobnpcspawner.js --- examplemods/mobnpcspawner.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/examplemods/mobnpcspawner.js b/examplemods/mobnpcspawner.js index 8e24aae..b16caff 100644 --- a/examplemods/mobnpcspawner.js +++ b/examplemods/mobnpcspawner.js @@ -1,8 +1,6 @@ (() => { PluginAPI.dedicatedServer.appendCode(function () { PluginAPI.addEventListener("processcommand", (event) => { - // Check if the sender is a player - if (!ModAPI.reflect.getClassById("net.minecraft.entity.player.EntityPlayerMP").instanceOf(event.sender.getRef())) { return; } // Check if the command is "/spawnnpc" if (event.command.toLowerCase().startsWith("/spawnnpc")) {