Skip to content

Commit

Permalink
Minor Optimization to addEntity()
Browse files Browse the repository at this point in the history
  • Loading branch information
Axionize committed Nov 17, 2024
1 parent deb2628 commit 0f3bf60
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions src/main/java/ac/grim/grimac/utils/latency/CompensatedEntities.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,20 @@ public void addEntity(int entityID, UUID uuid, EntityType entityType, Vector3d p
packetEntity = new PacketEntityHorse(player, uuid, entityType, position.getX(), position.getY(), position.getZ(), xRot);
} else if (entityType == EntityTypes.SLIME || entityType == EntityTypes.MAGMA_CUBE || entityType == EntityTypes.PHANTOM) {
packetEntity = new PacketEntitySizeable(player, uuid, entityType, position.getX(), position.getY(), position.getZ());
} else if (EntityTypes.PIG.equals(entityType)) {
packetEntity = new PacketEntityRideable(player, uuid, entityType, position.getX(), position.getY(), position.getZ());
} else if (EntityTypes.SHULKER.equals(entityType)) {
packetEntity = new PacketEntityShulker(player, uuid, entityType, position.getX(), position.getY(), position.getZ());
} else if (EntityTypes.STRIDER.equals(entityType)) {
packetEntity = new PacketEntityStrider(player, uuid, entityType, position.getX(), position.getY(), position.getZ());
} else if (EntityTypes.isTypeInstanceOf(entityType, EntityTypes.BOAT) || EntityTypes.CHICKEN.equals(entityType)) {
packetEntity = new PacketEntityTrackXRot(player, uuid, entityType, position.getX(), position.getY(), position.getZ(), xRot);
} else if (EntityTypes.FISHING_BOBBER.equals(entityType)) {
packetEntity = new PacketEntityHook(player, uuid, entityType, position.getX(), position.getY(), position.getZ(), data);
} else if (EntityTypes.ENDER_DRAGON.equals(entityType)) {
packetEntity = new PacketEntityEnderDragon(player, uuid, entityID, position.getX(), position.getY(), position.getZ());
} else {
if (EntityTypes.PIG.equals(entityType)) {
packetEntity = new PacketEntityRideable(player, uuid, entityType, position.getX(), position.getY(), position.getZ());
} else if (EntityTypes.SHULKER.equals(entityType)) {
packetEntity = new PacketEntityShulker(player, uuid, entityType, position.getX(), position.getY(), position.getZ());
} else if (EntityTypes.STRIDER.equals(entityType)) {
packetEntity = new PacketEntityStrider(player, uuid, entityType, position.getX(), position.getY(), position.getZ());
} else if (EntityTypes.isTypeInstanceOf(entityType, EntityTypes.BOAT) || EntityTypes.CHICKEN.equals(entityType)) {
packetEntity = new PacketEntityTrackXRot(player, uuid, entityType, position.getX(), position.getY(), position.getZ(), xRot);
} else if (EntityTypes.FISHING_BOBBER.equals(entityType)) {
packetEntity = new PacketEntityHook(player, uuid, entityType, position.getX(), position.getY(), position.getZ(), data);
} else if (EntityTypes.ENDER_DRAGON.equals(entityType)) {
packetEntity = new PacketEntityEnderDragon(player, uuid, entityID, position.getX(), position.getY(), position.getZ());
} else {
packetEntity = new PacketEntity(player, uuid, entityType, position.getX(), position.getY(), position.getZ());
}
packetEntity = new PacketEntity(player, uuid, entityType, position.getX(), position.getY(), position.getZ());
}

entityMap.put(entityID, packetEntity);
Expand Down Expand Up @@ -301,7 +299,9 @@ public void updateEntityMetadata(int entityID, List<EntityData> watchableObjects
player.compensatedWorld.openShulkerBoxes.add(data);
}
}
} else if (entity instanceof PacketEntityRideable) {
}

if (entity instanceof PacketEntityRideable) {
int offset = 0;
if (PacketEvents.getAPI().getServerManager().getVersion().isOlderThanOrEquals(ServerVersion.V_1_8_8)) {
if (entity.getType() == EntityTypes.PIG) {
Expand Down Expand Up @@ -343,7 +343,9 @@ public void updateEntityMetadata(int entityID, List<EntityData> watchableObjects
((PacketEntityRideable) entity).hasSaddle = (boolean) striderSaddle.getValue();
}
}
} else if (entity instanceof PacketEntityHorse) {
}

if (entity instanceof PacketEntityHorse) {
if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_9_4)) {
int offset = 0;

Expand Down Expand Up @@ -387,7 +389,23 @@ public void updateEntityMetadata(int entityID, List<EntityData> watchableObjects
((PacketEntityHorse) entity).isRearing = (info & 0x40) != 0;
}
}
} else if (entity.getType() == EntityTypes.FIREWORK_ROCKET) {
}

if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_9_4)) {
EntityData gravity = WatchableIndexUtil.getIndex(watchableObjects, 5);

if (gravity != null) {
Object gravityObject = gravity.getValue();

if (gravityObject instanceof Boolean) {
// Vanilla uses hasNoGravity, which is a bad name IMO
// hasGravity > hasNoGravity
entity.hasGravity = !((Boolean) gravityObject);
}
}
}

if (entity.getType() == EntityTypes.FIREWORK_ROCKET) {
int offset = 0;
if (PacketEvents.getAPI().getServerManager().getVersion().isOlderThanOrEquals(ServerVersion.V_1_12_2)) {
offset = 2;
Expand All @@ -410,7 +428,9 @@ public void updateEntityMetadata(int entityID, List<EntityData> watchableObjects
player.compensatedFireworks.addNewFirework(entityID);
}
}
} else if (entity instanceof PacketEntityHook) {
}

if (entity instanceof PacketEntityHook) {
int index;
if (PacketEvents.getAPI().getServerManager().getVersion().isOlderThanOrEquals(ServerVersion.V_1_9_4)) {
index = 5;
Expand All @@ -428,19 +448,5 @@ public void updateEntityMetadata(int entityID, List<EntityData> watchableObjects
Integer attachedEntityID = (Integer) hookWatchableObject.getValue();
((PacketEntityHook) entity).attached = attachedEntityID - 1; // the server adds 1 to the ID
}

if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_9_4)) {
EntityData gravity = WatchableIndexUtil.getIndex(watchableObjects, 5);

if (gravity != null) {
Object gravityObject = gravity.getValue();

if (gravityObject instanceof Boolean) {
// Vanilla uses hasNoGravity, which is a bad name IMO
// hasGravity > hasNoGravity
entity.hasGravity = !((Boolean) gravityObject);
}
}
}
}
}

0 comments on commit 0f3bf60

Please sign in to comment.