From 007660f16f2b713e760e859445b5e7a4c54204f3 Mon Sep 17 00:00:00 2001 From: luisc Date: Fri, 14 Jan 2022 19:07:41 -0600 Subject: [PATCH] Added collision options for CNPCs. --- src/main/java/noppes/npcs/DataDisplay.java | 10 +++ .../client/gui/mainmenu/GuiNpcDisplay.java | 6 ++ .../npcs/entity/EntityNPCInterface.java | 65 +++++++++++++++++-- .../npcs/scripted/entity/ScriptNpc.java | 9 ++- .../npcs/scripted/interfaces/ICustomNpc.java | 4 ++ 5 files changed, 88 insertions(+), 6 deletions(-) diff --git a/src/main/java/noppes/npcs/DataDisplay.java b/src/main/java/noppes/npcs/DataDisplay.java index 1bda8ee33..e0218ae33 100644 --- a/src/main/java/noppes/npcs/DataDisplay.java +++ b/src/main/java/noppes/npcs/DataDisplay.java @@ -52,6 +52,9 @@ public class DataDisplay { public ArrayList invisibleToList = new ArrayList<>(); + //0 - Yes, 1 - No, 2 - Only NPCs, 3 - Only Players, 4 - NPCs and Players + public int collidesWith = 1; + public DataDisplay(EntityNPCInterface npc){ this.npc = npc; @@ -120,6 +123,8 @@ public class DataDisplay { nbttagcompound.setBoolean("NoLivingAnimation", disableLivingAnimation); nbttagcompound.setByte("BossBar", showBossBar); + nbttagcompound.setInteger("CollidesWith",collidesWith); + NBTTagList list = new NBTTagList(); for(UUID uuid : invisibleToList){ list.appendTag(new NBTTagString(uuid.toString())); @@ -179,6 +184,11 @@ public class DataDisplay { invisibleToList = new ArrayList<>(); } + if(nbttagcompound.hasKey("CollidesWith")) + collidesWith = nbttagcompound.getInteger("CollidesWith"); + else + collidesWith = 1; + if(prevSkinType != skinType || !texture.equals(prevTexture)) npc.textureLocation = null; npc.textureGlowLocation = null; diff --git a/src/main/java/noppes/npcs/client/gui/mainmenu/GuiNpcDisplay.java b/src/main/java/noppes/npcs/client/gui/mainmenu/GuiNpcDisplay.java index 4503f4a31..4794d6593 100644 --- a/src/main/java/noppes/npcs/client/gui/mainmenu/GuiNpcDisplay.java +++ b/src/main/java/noppes/npcs/client/gui/mainmenu/GuiNpcDisplay.java @@ -82,6 +82,9 @@ public class GuiNpcDisplay extends GuiNPCInterface2 implements ITextfieldListene addLabel(new GuiNpcLabel(5,"display.livingAnimation", guiLeft + 5, y + 5)); this.addButton(new GuiNpcButton(5, guiLeft + 120, y, 50, 20, new String[]{"gui.yes","gui.no"}, display.disableLivingAnimation?1:0)); + addLabel(new GuiNpcLabel(6,"Collides With Entities", guiLeft + 185, y + 5)); + this.addButton(new GuiNpcButton(6, guiLeft + 300, y, 100, 20, new String[]{"gui.yes","gui.no","Only CNPCs","Only Players","CNPCs & Players"}, display.collidesWith)); + y+=23; addLabel(new GuiNpcLabel(7,"display.visible", guiLeft + 5, y + 5)); this.addButton(new GuiNpcButton(7, guiLeft + 120, y, 50, 20, new String[]{"gui.yes","gui.no","gui.partly"}, display.visible)); @@ -155,6 +158,9 @@ public class GuiNpcDisplay extends GuiNPCInterface2 implements ITextfieldListene else if(button.id == 5){ display.disableLivingAnimation = button.getValue() == 1; } + else if(button.id == 6){ + display.collidesWith = button.getValue(); + } else if(button.id == 7){ display.visible = button.getValue(); } diff --git a/src/main/java/noppes/npcs/entity/EntityNPCInterface.java b/src/main/java/noppes/npcs/entity/EntityNPCInterface.java index 17af11e56..4c7aed224 100644 --- a/src/main/java/noppes/npcs/entity/EntityNPCInterface.java +++ b/src/main/java/noppes/npcs/entity/EntityNPCInterface.java @@ -1604,17 +1604,72 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti // obviously we dont want this @Override public boolean canBePushed() { - return false; + return this.display.collidesWith == 0; + } + + // checks for any entity within a certain boundingbox, eg minecarts + @Override + protected void collideWithNearbyEntities() { + if(this.display.collidesWith != 1) { + List list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.boundingBox.expand(0.20000000298023224D, 0.0D, 0.20000000298023224D)); + + if (list != null && !list.isEmpty()) { + for (int i = 0; i < list.size(); ++i) { + Entity entity = (Entity) list.get(i); + + if (this.canBePushed() || + (entity instanceof EntityNPCInterface && (this.display.collidesWith == 2 || this.display.collidesWith == 4)) || + (entity instanceof EntityPlayerMP && (this.display.collidesWith == 3 || this.display.collidesWith == 4)) + ) { + super.collideWithEntity(entity); + } + } + } + } + } + + public void applyEntityCollision(Entity entity) + { + if (entity.riddenByEntity != this && entity.ridingEntity != this) + { + double d0 = entity.posX - this.posX; + double d1 = entity.posZ - this.posZ; + double d2 = MathHelper.abs_max(d0, d1); + + if (d2 >= 0.009999999776482582D) + { + d2 = (double)MathHelper.sqrt_double(d2); + d0 /= d2; + d1 /= d2; + double d3 = 1.0D / d2; + + if (d3 > 1.0D) + { + d3 = 1.0D; + } + + d0 *= d3; + d1 *= d3; + d0 *= 0.05000000074505806D; + d1 *= 0.05000000074505806D; + d0 *= (double)(1.0F - this.entityCollisionReduction); + d1 *= (double)(1.0F - this.entityCollisionReduction); + if (this.canBePushed() || + (entity instanceof EntityNPCInterface && (this.display.collidesWith == 2 || this.display.collidesWith == 4)) || + (entity instanceof EntityPlayerMP && (this.display.collidesWith == 3 || this.display.collidesWith == 4)) + ) + this.addVelocity(-d0, 0.0D, -d1); + entity.addVelocity(d0, 0.0D, d1); + } + } } // not any entity can collide. if you want projectiles to still be // effective, you will have to handle those yourself @Override - protected void collideWithEntity(Entity p_82167_1_) {} + protected void collideWithEntity(Entity p_82167_1_) { - // checks for any entity within a certain boundingbox, eg minecarts - @Override - protected void collideWithNearbyEntities() {} + } public boolean canFly(){ return false; diff --git a/src/main/java/noppes/npcs/scripted/entity/ScriptNpc.java b/src/main/java/noppes/npcs/scripted/entity/ScriptNpc.java index bf43b5a38..7145c6bb7 100644 --- a/src/main/java/noppes/npcs/scripted/entity/ScriptNpc.java +++ b/src/main/java/noppes/npcs/scripted/entity/ScriptNpc.java @@ -1105,5 +1105,12 @@ public class ScriptNpc extends ScriptLiving imp return npc.display.glowTexture; } - public void updateClient() { this.npc.updateClient(); }; + public void setCollisionType(int type){ + npc.display.collidesWith = type; + } + public int getCollisionType(){ + return npc.display.collidesWith; + } + + public void updateClient() { this.npc.updateClient(); } } diff --git a/src/main/java/noppes/npcs/scripted/interfaces/ICustomNpc.java b/src/main/java/noppes/npcs/scripted/interfaces/ICustomNpc.java index 9618104fa..27bf7121c 100644 --- a/src/main/java/noppes/npcs/scripted/interfaces/ICustomNpc.java +++ b/src/main/java/noppes/npcs/scripted/interfaces/ICustomNpc.java @@ -729,5 +729,9 @@ public interface ICustomNpc extends IEntityLiving { public String getOverlayTexture(); + public void setCollisionType(int type); + + public int getCollisionType(); + public void updateClient(); }