From 145aec9b7f3f3be214f3d2e1c574410442195d01 Mon Sep 17 00:00:00 2001 From: LoJoSho Date: Sun, 27 Aug 2023 12:44:53 -0500 Subject: [PATCH] feat: entity cooldown teleportation packet option --- .../hmccosmetics/config/Settings.java | 4 ++++ .../cosmetic/types/CosmeticBackpackType.java | 2 +- .../hmccosmetics/user/manager/UserEntity.java | 22 ++++++++++++++++--- common/src/main/resources/config.yml | 3 +++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/config/Settings.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/config/Settings.java index 3cc4232e..350e7d9b 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/config/Settings.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/config/Settings.java @@ -39,6 +39,7 @@ public class Settings { private static final String COSMETIC_EMOTE_INVINCIBLE_PATH = "emote-invincible"; private static final String COSMETIC_EMOTE_CAMERA_PATH = "emote-camera"; private static final String COSMETIC_EMOTE_MOVE_CHECK_PATH = "emote-move"; + private static final String COSMETIC_PACKET_ENTITY_TELEPORT_COOLDOWN_PATH = "entity-cooldown-teleport-packet"; private static final String COSMETIC_BACKPACK_FORCE_RIDING_PACKET_PATH = "backpack-force-riding-packet"; private static final String COSMETIC_ADD_ENCHANTS_HELMET_PATH = "helmet-add-enchantments"; private static final String COSMETIC_ADD_ENCHANTS_CHESTPLATE_PATH = "chest-add-enchantments"; @@ -107,6 +108,8 @@ public class Settings { @Getter private static int tickPeriod; @Getter + private static int packetEntityTeleportCooldown; + @Getter private static double emoteDistance; @Getter private static Vector balloonOffset; @@ -173,6 +176,7 @@ public class Settings { viewDistance = cosmeticSettings.node(VIEW_DISTANCE_PATH).getInt(-3); emoteCameraEnabled = cosmeticSettings.node(COSMETIC_EMOTE_CAMERA_PATH).getBoolean(true); emoteMoveCheck = cosmeticSettings.node(COSMETIC_EMOTE_MOVE_CHECK_PATH).getBoolean(false); + packetEntityTeleportCooldown = cosmeticSettings.node(COSMETIC_PACKET_ENTITY_TELEPORT_COOLDOWN_PATH).getInt(-1); ConfigurationNode menuSettings = source.node(MENU_SETTINGS_PATH); diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBackpackType.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBackpackType.java index 33b58b05..7682f2ed 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBackpackType.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBackpackType.java @@ -53,7 +53,7 @@ public class CosmeticBackpackType extends Cosmetic { List outsideViewers = user.getUserBackpackManager().getEntityManager().refreshViewers(loc); user.getUserBackpackManager().getEntityManager().teleport(loc); - user.getUserBackpackManager().getEntityManager().setRotation((int) loc.getYaw()); + user.getUserBackpackManager().getEntityManager().setRotation((int) loc.getYaw(), isFirstPersonCompadible()); PacketManager.sendEntitySpawnPacket(user.getEntity().getLocation(), user.getUserBackpackManager().getFirstArmorStandId(), EntityType.ARMOR_STAND, UUID.randomUUID(), outsideViewers); PacketManager.sendArmorstandMetadata(user.getUserBackpackManager().getFirstArmorStandId(), outsideViewers); diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/manager/UserEntity.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/manager/UserEntity.java index b627f454..e500d294 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/manager/UserEntity.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/manager/UserEntity.java @@ -1,5 +1,6 @@ package com.hibiscusmc.hmccosmetics.user.manager; +import com.hibiscusmc.hmccosmetics.config.Settings; import com.hibiscusmc.hmccosmetics.user.CosmeticUser; import com.hibiscusmc.hmccosmetics.user.CosmeticUsers; import com.hibiscusmc.hmccosmetics.util.PlayerUtils; @@ -20,7 +21,9 @@ public class UserEntity { @Getter private List viewers = new ArrayList<>(); @Getter @Setter - private Long lastUpdate = 0L; + private Long viewerLastUpdate = 0L; + @Getter @Setter + private Long lastPositionUpdate = 0L; @Getter @Setter private List ids = new ArrayList<>(); @Getter @@ -35,7 +38,7 @@ public class UserEntity { } public List refreshViewers(Location location) { - if (System.currentTimeMillis() - lastUpdate <= 1000) return List.of(); //Prevents mass refreshes + if (System.currentTimeMillis() - viewerLastUpdate <= 1000) return List.of(); //Prevents mass refreshes ArrayList newPlayers = new ArrayList<>(); ArrayList removePlayers = new ArrayList<>(); List players = PlayerUtils.getNearbyPlayers(location); @@ -61,20 +64,33 @@ public class UserEntity { } } viewers.removeAll(removePlayers); - lastUpdate = System.currentTimeMillis(); + setViewerLastUpdate(System.currentTimeMillis()); return newPlayers; } public void teleport(Location location) { + if (this.getLocation() != null && this.getLocation().getWorld() == location.getWorld()) { + // Was thinking about using schedulers to just send the packet later... but that would be a lot of tasks and + // would probably cause more lag. Furthermore, the server "ticks" the cosmetics every second by defualt. So it's fine like this. + if (System.currentTimeMillis() - getLastPositionUpdate() <= Settings.getPacketEntityTeleportCooldown()) return; + } this.location = location; for (Integer entity : ids) { PacketManager.sendTeleportPacket(entity, location, false, getViewers()); } + setLastPositionUpdate(System.currentTimeMillis()); } public void setRotation(int yaw) { + setRotation(yaw, false); + } + + public void setRotation(int yaw, boolean additonalPacket) { location.setYaw(yaw); for (Integer entity : ids) { + // First person backpacks need both packets to rotate properly, otherwise they look off + // Regular backpacks just need the look packet + if (additonalPacket) PacketManager.sendRotationPacket(entity, yaw, false, getViewers()); PacketManager.sendLookPacket(entity, location, getViewers()); } } diff --git a/common/src/main/resources/config.yml b/common/src/main/resources/config.yml index a15fb67e..d7f60c72 100644 --- a/common/src/main/resources/config.yml +++ b/common/src/main/resources/config.yml @@ -35,6 +35,9 @@ cosmetic-settings: emote-move: false # This make it so it always sends the riding packets for the backpack. This sends more packets but is more reliable for servers which modify player passengers. backpack-force-riding-packet: false + # This helps reduce the amount of packets sent for packet entities, but reduces accuracy of the entity. This is in milliseconds. -1 to disable. + # This is useful for servers with a lot of backpacks, as they are passengers, which means the client will update their position automatically within an area where the entity is located. + entity-cooldown-teleport-packet: 500 helmet-add-enchantments: false # If the plugin should keep enchants on helmets. This is useful as some enchantments are client side only. chest-add-enchantments: false # If the plugin should keep enchants on chestplate. This is useful as some enchantments are client side only.