Skip to content

Commit

Permalink
xpspawner
Browse files Browse the repository at this point in the history
  • Loading branch information
ZXMushroom63 committed Oct 4, 2024
1 parent 1e1c0be commit 6954b68
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 92 deletions.
29 changes: 18 additions & 11 deletions examplemods/mobnpcspawner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
Expand Down
50 changes: 16 additions & 34 deletions examplemods/npcspawner.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -31,31 +13,31 @@
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
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;
Expand Down
47 changes: 0 additions & 47 deletions examplemods/npcspawnertest.js

This file was deleted.

36 changes: 36 additions & 0 deletions examplemods/xpspawner.js
Original file line number Diff line number Diff line change
@@ -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;
}
});
});
})();

0 comments on commit 6954b68

Please sign in to comment.