diff --git a/examplemods/mobnpcspawner.js b/examplemods/mobnpcspawner.js index 4d681dd..292b965 100644 --- a/examplemods/mobnpcspawner.js +++ b/examplemods/mobnpcspawner.js @@ -4,8 +4,8 @@ // 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")) { + // Check if the command is "/spawnsheep" + if (event.command.toLowerCase().startsWith("/spawnsheep")) { const world = event.sender.getServerForPlayer(); const senderPos = event.sender.getPosition(); @@ -17,21 +17,28 @@ sheep.$setLocationAndAngles(senderPos.getX(), senderPos.getY(), senderPos.getZ(), senderPos.rotationYaw, senderPos.rotationPitch); // Disable AI (no AI behavior) - sheep.$setNoAI(1) + //sheep.$setNoAI(1) // Disable gravity - sheep.$noGravity = 1; + //sheep.$noGravity = 1; // Make sheep invincible - sheep.$invulnerable = 1 + //sheep.$invulnerable = 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!"))); + if (globalThis.AsyncSink) { //If we can, start the AsyncSink debugger to see filesystem requests + AsyncSink.startDebuggingFS(); + } + // Add the sheep to the world + ModAPI.promisify(ModAPI.hooks.methods.nmw_World_spawnEntityInWorld)(world.getRef(), sheep).then(result => { + // 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!"))); + + if (globalThis.AsyncSink) { //Stop debugging when we are done, otherwise the console will get filled up. + AsyncSink.stopDebuggingFS(); + } + }); // Prevent the command from executing further event.preventDefault = true; diff --git a/examplemods/npcspawner.js b/examplemods/npcspawner.js index 1663ba2..b2b8a1e 100644 --- a/examplemods/npcspawner.js +++ b/examplemods/npcspawner.js @@ -1,28 +1,10 @@ (() => { PluginAPI.dedicatedServer.appendCode(function () { - var ready = false; - var killFS = false; - function setup_filesystem_middleware() { - if (!ready) { - AsyncSink.MIDDLEWARE.push((ev)=>{ - if (killFS) { - ev.shim = true; - if (typeof ev.shimOutput === "boolean") { - ev.shimOutput = true; - } - } - }); - ready = true; - } - } PluginAPI.addEventListener("processcommand", (event) => { if (!ModAPI.reflect.getClassById("net.minecraft.entity.player.EntityPlayerMP").instanceOf(event.sender.getRef())) { return; } - if (event.command.toLowerCase().startsWith("/spawnnpc")) { - if (!globalThis.AsyncSink) { - return console.error("NPC Spawner relies on the AsyncSink library."); - } - setup_filesystem_middleware(); + if (event.command.toLowerCase().startsWith("/spawnnpc2")) { + AsyncSink.startDebuggingFS(); const world = event.sender.getServerForPlayer(); const senderPos = event.sender.getPosition(); @@ -31,7 +13,7 @@ var UUID = ModAPI.hooks.methods[ModAPI.util.getMethodFromPackage("net.lax1dude.eaglercraft.v1_8.EaglercraftUUID", "randomUUID")](); //Not using UUID to make patching easier for now - + const fakeProfile = GameProfileClass.constructors[1](null, ModAPI.util.str("Steve")); // Get the PlayerInteractionManager class @@ -39,23 +21,23 @@ const playerInteractionManager = PlayerInteractionManagerClass.constructors[0](world.getRef()); // Get the EntityPlayerMP class to spawn the fake player - killFS = true; const EntityPlayerMPClass = ModAPI.reflect.getClassById("net.minecraft.entity.player.EntityPlayerMP"); - const fakePlayer = ModAPI.util.wrap(EntityPlayerMPClass.constructors[0]( - ModAPI.server.getRef(), world.getRef(), fakeProfile, playerInteractionManager - )); - killFS = false; + ModAPI.promisify(EntityPlayerMPClass.constructors[0])(ModAPI.server.getRef(), world.getRef(), fakeProfile, playerInteractionManager).then(result => { + console.log(result); + var fakePlayer = ModAPI.util.wrap(result); - // Set the fake player position to be near the command sender - console.log(senderPos); - fakePlayer.setPosition(senderPos.getX(), senderPos.getY(), senderPos.getZ()); + // Set the fake player position to be near the command sender + console.log(senderPos); + fakePlayer.setPosition(senderPos.getX(), senderPos.getY(), senderPos.getZ()); - // Add the fake player to the world - world.spawnEntityInWorld(fakePlayer.getRef()); + // Add the fake player to the world + world.spawnEntityInWorld(fakePlayer.getRef()); + + // 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!"))); + }); - // 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; diff --git a/examplemods/npcspawnertest.js b/examplemods/npcspawnertest.js deleted file mode 100644 index b2b8a1e..0000000 --- a/examplemods/npcspawnertest.js +++ /dev/null @@ -1,47 +0,0 @@ -(() => { - 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("/spawnnpc2")) { - AsyncSink.startDebuggingFS(); - const world = event.sender.getServerForPlayer(); - const senderPos = event.sender.getPosition(); - - // Create a fake player GameProfile - const GameProfileClass = ModAPI.reflect.getClassById("net.lax1dude.eaglercraft.v1_8.mojang.authlib.GameProfile"); - var UUID = ModAPI.hooks.methods[ModAPI.util.getMethodFromPackage("net.lax1dude.eaglercraft.v1_8.EaglercraftUUID", "randomUUID")](); - - //Not using UUID to make patching easier for now - - const fakeProfile = GameProfileClass.constructors[1](null, ModAPI.util.str("Steve")); - - // Get the PlayerInteractionManager class - const PlayerInteractionManagerClass = ModAPI.reflect.getClassById("net.minecraft.server.management.ItemInWorldManager"); - const playerInteractionManager = PlayerInteractionManagerClass.constructors[0](world.getRef()); - - // Get the EntityPlayerMP class to spawn the fake player - const EntityPlayerMPClass = ModAPI.reflect.getClassById("net.minecraft.entity.player.EntityPlayerMP"); - ModAPI.promisify(EntityPlayerMPClass.constructors[0])(ModAPI.server.getRef(), world.getRef(), fakeProfile, playerInteractionManager).then(result => { - console.log(result); - var fakePlayer = ModAPI.util.wrap(result); - - // Set the fake player position to be near the command sender - console.log(senderPos); - fakePlayer.setPosition(senderPos.getX(), senderPos.getY(), senderPos.getZ()); - - // Add the fake player to the world - world.spawnEntityInWorld(fakePlayer.getRef()); - - // 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; - } - }); - }); -})(); diff --git a/examplemods/xpspawner.js b/examplemods/xpspawner.js new file mode 100644 index 0000000..0f81082 --- /dev/null +++ b/examplemods/xpspawner.js @@ -0,0 +1,36 @@ +(() => { + 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 "/spawnsheep" + if (event.command.toLowerCase().startsWith("/spawnxp")) { + const world = event.sender.getServerForPlayer(); + const senderPos = event.sender.getPosition(); + + // Create a sheep entity + const EntityXP = ModAPI.reflect.getClassByName("EntityXPOrb"); + const xporb = EntityXP.constructors[0](world.getRef(), senderPos.getX(), senderPos.getY(), senderPos.getZ(), 10); + + if (globalThis.AsyncSink) { //If we can, start the AsyncSink debugger to see filesystem requests + AsyncSink.startDebuggingFS(); + } + + // Add the sheep to the world + ModAPI.promisify(ModAPI.hooks.methods.nmw_World_spawnEntityInWorld)(world.getRef(), xporb).then(result => { + // 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("An xp orb has been spawned!"))); + + if (globalThis.AsyncSink) { //Stop debugging when we are done, otherwise the console will get filled up. + AsyncSink.stopDebuggingFS(); + } + }); + + // Prevent the command from executing further + event.preventDefault = true; + } + }); + }); +})(); \ No newline at end of file