From 915eeab020e370768a22624638ff27b1b1394cec Mon Sep 17 00:00:00 2001 From: Bixilon Date: Wed, 18 Nov 2020 21:05:32 +0100 Subject: [PATCH] reduce exceptions if server sends bullshit --- .../data/entities/entities/Entity.java | 4 + .../protocol/protocol/PacketHandler.java | 85 ++++++++++++++++--- 2 files changed, 77 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/data/entities/entities/Entity.java b/src/main/java/de/bixilon/minosoft/data/entities/entities/Entity.java index b616a6afd..580469970 100644 --- a/src/main/java/de/bixilon/minosoft/data/entities/entities/Entity.java +++ b/src/main/java/de/bixilon/minosoft/data/entities/entities/Entity.java @@ -225,6 +225,10 @@ public abstract class Entity { return metaData.getSets().getPose(EntityMetaDataFields.ENTITY_POSE); } + @EntityMetaDataFunction(identifier = "ticksFrozen") + public int getTicksFrozen(){ + return metaData.getSets().getInt(EntityMetaDataFields.ENTITY_TICKS_FROZEN); + } @Override public String toString() { return String.format("%s:%s", information.getMod(), information.getIdentifier()); diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketHandler.java b/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketHandler.java index e1d62c36a..8ece786c2 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketHandler.java +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketHandler.java @@ -291,16 +291,31 @@ public class PacketHandler { } public void handle(PacketEntityMovementAndRotation pkg) { - connection.getPlayer().getWorld().getEntity(pkg.getEntityId()).setLocation(pkg.getRelativeLocation()); - connection.getPlayer().getWorld().getEntity(pkg.getEntityId()).setRotation(pkg.getYaw(), pkg.getPitch()); + Entity entity = connection.getPlayer().getWorld().getEntity(pkg.getEntityId()); + if(entity == null){ + // thanks mojang + return; + } + entity.setLocation(pkg.getRelativeLocation()); + entity.setRotation(pkg.getYaw(), pkg.getPitch()); } public void handle(PacketEntityMovement pkg) { - connection.getPlayer().getWorld().getEntity(pkg.getEntityId()).setLocation(pkg.getRelativeLocation()); + Entity entity = connection.getPlayer().getWorld().getEntity(pkg.getEntityId()); + if(entity == null){ + // thanks mojang + return; + } + entity.setLocation(pkg.getRelativeLocation()); } public void handle(PacketEntityRotation pkg) { - connection.getPlayer().getWorld().getEntity(pkg.getEntityId()).setRotation(pkg.getYaw(), pkg.getPitch()); + Entity entity = connection.getPlayer().getWorld().getEntity(pkg.getEntityId()); + if(entity == null){ + // thanks mojang + return; + } + entity.setRotation(pkg.getYaw(), pkg.getPitch()); } public void handle(PacketDestroyEntity pkg) { @@ -319,6 +334,10 @@ public class PacketHandler { } else { entity = connection.getPlayer().getWorld().getEntity(pkg.getEntityId()); } + if(entity == null){ + // thanks mojang + return; + } connection.getVelocityHandler().handleVelocity(entity, pkg.getVelocity()); } @@ -330,12 +349,22 @@ public class PacketHandler { } public void handle(PacketEntityTeleport pkg) { - connection.getPlayer().getWorld().getEntity(pkg.getEntityId()).setLocation(pkg.getRelativeLocation()); - connection.getPlayer().getWorld().getEntity(pkg.getEntityId()).setRotation(pkg.getYaw(), pkg.getPitch()); + Entity entity = connection.getPlayer().getWorld().getEntity(pkg.getEntityId()); + if(entity == null){ + // thanks mojang + return; + } + entity.setLocation(pkg.getRelativeLocation()); + entity.setRotation(pkg.getYaw(), pkg.getPitch()); } public void handle(PacketEntityHeadRotation pkg) { - connection.getPlayer().getWorld().getEntity(pkg.getEntityId()).setHeadRotation(pkg.getHeadYaw()); + Entity entity = connection.getPlayer().getWorld().getEntity(pkg.getEntityId()); + if(entity == null){ + // thanks mojang + return; + } + entity.setHeadRotation(pkg.getHeadYaw()); } public void handle(PacketWindowItems pkg) { @@ -352,25 +381,39 @@ public class PacketHandler { } else { entity = connection.getPlayer().getWorld().getEntity(pkg.getEntityId()); } + if(entity == null){ + // thanks mojang + return; + } entity.setMetaData(pkg.getEntityData()); } public void handle(PacketEntityEquipment pkg) { connection.fireEvent(new EntityEquipmentChangeEvent(connection, pkg)); - connection.getPlayer().getWorld().getEntity(pkg.getEntityId()).setEquipment(pkg.getSlots()); + Entity entity = connection.getPlayer().getWorld().getEntity(pkg.getEntityId()); + if(entity == null){ + // thanks mojang + return; + } + entity.setEquipment(pkg.getSlots()); } public void handle(PacketBlockChange pkg) { + Chunk chunk = connection.getPlayer().getWorld().getChunk(pkg.getPosition().getChunkLocation()); + if(chunk == null){ + // thanks mojang + return; + } connection.fireEvent(new BlockChangeEvent(connection, pkg)); - connection.getPlayer().getWorld().setBlock(pkg.getPosition(), pkg.getBlock()); + chunk.setBlock(pkg.getPosition().getInChunkLocation(), pkg.getBlock()); } public void handle(PacketMultiBlockChange pkg) { Chunk chunk = connection.getPlayer().getWorld().getChunk(pkg.getLocation()); if (chunk == null) { - Log.warn(String.format("Server tried to change blocks in unloaded chunks! (location=%s)", pkg.getLocation())); + // thanks mojang return; } connection.fireEvent(new MultiBlockChangeEvent(connection, pkg)); @@ -430,6 +473,10 @@ public class PacketHandler { } else { entity = connection.getPlayer().getWorld().getEntity(pkg.getEntityId()); } + if(entity == null){ + // thanks mojang + return; + } entity.addEffect(pkg.getEffect()); } @@ -441,6 +488,10 @@ public class PacketHandler { } else { entity = connection.getPlayer().getWorld().getEntity(pkg.getEntityId()); } + if(entity == null){ + // thanks mojang + return; + } entity.removeEffect(pkg.getEffect()); } @@ -474,8 +525,18 @@ public class PacketHandler { } public void handle(PacketAttachEntity pkg) { - // ToDo check if it is us - connection.getPlayer().getWorld().getEntity(pkg.getEntityId()).attachTo(pkg.getVehicleId()); + Entity entity; + if (pkg.getEntityId() == connection.getPlayer().getEntity().getEntityId()) { + // that's us! + entity = connection.getPlayer().getEntity(); + } else { + entity = connection.getPlayer().getWorld().getEntity(pkg.getEntityId()); + } + if(entity == null){ + // thanks mojang + return; + } + entity.attachTo(pkg.getVehicleId()); // ToDo leash support }