From a3b5f5993eedc32bfc7905275c318d84d49a79a3 Mon Sep 17 00:00:00 2001 From: c0508383 Date: Wed, 6 Jul 2022 01:29:43 -0700 Subject: [PATCH] IWorld: -Implemented spawnClone() with a boolean parameter to check entity protection from mods & plugins correctly (for real this time). ICloneHandler: -Implemented ignoreProtection boolean value for the spawn() function. --- .../java/noppes/npcs/NoppesUtilServer.java | 23 ++++++++++++++++++- .../controllers/ServerCloneController.java | 13 +++++++++-- .../noppes/npcs/scripted/ScriptWorld.java | 17 ++++++++++---- .../npcs/scripted/interfaces/IWorld.java | 3 +++ .../interfaces/handler/ICloneHandler.java | 3 +++ 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/main/java/noppes/npcs/NoppesUtilServer.java b/src/main/java/noppes/npcs/NoppesUtilServer.java index 6b4f5ccfb..dd0012d60 100644 --- a/src/main/java/noppes/npcs/NoppesUtilServer.java +++ b/src/main/java/noppes/npcs/NoppesUtilServer.java @@ -636,9 +636,29 @@ public class NoppesUtilServer { Server.sendData(player, EnumPacketClient.GUI_CLOSE, i, comp); } - public static Entity spawnClone(NBTTagCompound compound, int x, int y, + public static Entity spawnCloneWithProtection(NBTTagCompound compound, int x, int y, int z, World worldObj) { ServerCloneController.Instance.cleanTags(compound); + compound.setTag("Pos", NBTTags.nbtDoubleList(x + 0.5, y, z + 0.5)); + Entity entity = EntityList.createEntityFromNBT(compound, worldObj); + if(entity == null){ + return null; + } + entity.setPosition((double) x + 0.5, (double) y, (double) z + 0.5); + if(entity instanceof EntityNPCInterface){ + EntityNPCInterface npc = (EntityNPCInterface) entity; + npc.ai.startPos = new int[]{MathHelper.floor_double(npc.posX),MathHelper.floor_double(npc.posY),MathHelper.floor_double(npc.posZ)}; + npc.ticksExisted = 0; + npc.totalTicksAlive = 0; + } + worldObj.spawnEntityInWorld(entity); + entity.prevPosY = entity.lastTickPosY = entity.posY = y + 1; + return entity; + } + + public static Entity spawnClone(NBTTagCompound compound, int x, int y, + int z, World worldObj) { + ServerCloneController.Instance.cleanTags(compound); compound.setTag("Pos", NBTTags.nbtDoubleList(x + 0.5, y + 1, z + 0.5)); Entity entity = EntityList.createEntityFromNBT(compound, worldObj); if(entity == null){ @@ -653,6 +673,7 @@ public class NoppesUtilServer { worldObj.spawnEntityInWorld(entity); return entity; } + public static boolean isOp(EntityPlayer player) { return MinecraftServer.getServer().getConfigurationManager().func_152596_g(player.getGameProfile()); } diff --git a/src/main/java/noppes/npcs/controllers/ServerCloneController.java b/src/main/java/noppes/npcs/controllers/ServerCloneController.java index 8eac9cd5d..5fe56c42f 100644 --- a/src/main/java/noppes/npcs/controllers/ServerCloneController.java +++ b/src/main/java/noppes/npcs/controllers/ServerCloneController.java @@ -194,16 +194,25 @@ public class ServerCloneController implements ICloneHandler { } } - public IEntity spawn(double x, double y, double z, int tab, String name, IWorld world) { + public IEntity spawn(double x, double y, double z, int tab, String name, IWorld world, boolean ignoreProtection) { NBTTagCompound compound = this.getCloneData((ICommandSender)null, name, tab); if (compound == null) { throw new CustomNPCsException("Unknown clone tab:" + tab + " name:" + name, new Object[0]); } else { - Entity entity = NoppesUtilServer.spawnClone(compound, (int)x, (int)y, (int)z, world.getMCWorld()); + Entity entity; + if (!ignoreProtection) { + entity = NoppesUtilServer.spawnCloneWithProtection(compound, (int) x, (int) y, (int) z, world.getMCWorld()); + } else { + entity = NoppesUtilServer.spawnClone(compound, (int) x, (int) y, (int) z, world.getMCWorld()); + } return entity == null ? null : NpcAPI.Instance().getIEntity(entity); } } + public IEntity spawn(double x, double y, double z, int tab, String name, IWorld world) { + return spawn(x, y, z, tab, name, world, true); + } + public IEntity[] getTab(int tab, IWorld world) { File dir = new File(getDir(), tab + ""); if (!dir.exists() || !dir.isDirectory() || dir.listFiles() == null) { diff --git a/src/main/java/noppes/npcs/scripted/ScriptWorld.java b/src/main/java/noppes/npcs/scripted/ScriptWorld.java index 7bff4c428..376c6d0e7 100644 --- a/src/main/java/noppes/npcs/scripted/ScriptWorld.java +++ b/src/main/java/noppes/npcs/scripted/ScriptWorld.java @@ -588,14 +588,21 @@ public class ScriptWorld implements IWorld { * @param name Name of the cloned entity * @return Returns the entity which was spawned */ - public IEntity spawnClone(int x, int y, int z, int tab, String name){ + public IEntity spawnClone(int x, int y, int z, int tab, String name, boolean ignoreProtection){ NBTTagCompound compound = ServerCloneController.Instance.getCloneData(null, name, tab); if(compound == null) return null; - Entity entity = NoppesUtilServer.spawnClone(compound, x, y, z, world); - if(entity == null) - return null; - return NpcAPI.Instance().getIEntity(entity); + Entity entity; + if (!ignoreProtection) { + entity = NoppesUtilServer.spawnCloneWithProtection(compound, x, y, z, world); + } else { + entity = NoppesUtilServer.spawnClone(compound, x, y, z, world); + } + return entity == null ? null : NpcAPI.Instance().getIEntity(entity); + } + + public IEntity spawnClone(int x, int y, int z, int tab, String name) { + return this.spawnClone(x,y,z,tab,name,true); } public ScriptScoreboard getScoreboard(){ diff --git a/src/main/java/noppes/npcs/scripted/interfaces/IWorld.java b/src/main/java/noppes/npcs/scripted/interfaces/IWorld.java index 275d51e0f..27eb3392e 100644 --- a/src/main/java/noppes/npcs/scripted/interfaces/IWorld.java +++ b/src/main/java/noppes/npcs/scripted/interfaces/IWorld.java @@ -346,8 +346,11 @@ public interface IWorld { * @param z The z position the clone will be spawned at * @param tab The tab in which the clone is * @param name Name of the cloned entity + * @param ignoreProtection Whether the spawning of this clone skips protection checks. * @return Returns the entity which was spawned */ + IEntity spawnClone(int x, int y, int z, int tab, String name, boolean ignoreProtection); + IEntity spawnClone(int x, int y, int z, int tab, String name); ScriptScoreboard getScoreboard(); diff --git a/src/main/java/noppes/npcs/scripted/interfaces/handler/ICloneHandler.java b/src/main/java/noppes/npcs/scripted/interfaces/handler/ICloneHandler.java index effc81b74..9fe8108fe 100644 --- a/src/main/java/noppes/npcs/scripted/interfaces/handler/ICloneHandler.java +++ b/src/main/java/noppes/npcs/scripted/interfaces/handler/ICloneHandler.java @@ -9,6 +9,9 @@ import noppes.npcs.scripted.interfaces.IWorld; import noppes.npcs.scripted.interfaces.entity.IEntity; public interface ICloneHandler { + + IEntity spawn(double x, double y, double z, int tab, String name, IWorld world, boolean ignoreProtection); + IEntity spawn(double x, double y, double z, int tab, String name, IWorld world); IEntity[] getTab(int tab, IWorld world);