From 5b59ad98d5271f41fb27360f995d4353594e4701 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sat, 24 Apr 2021 13:40:02 +1200 Subject: [PATCH] Fix max health not working properly, implement proper cracked support for iron golem. Fixes #549 --- .../disguise/disguisetypes/FlagWatcher.java | 4 ++ .../disguise/disguisetypes/GolemCrack.java | 11 ++++ .../watchers/IronGolemWatcher.java | 48 ++++++++++++++ .../disguisetypes/watchers/LivingWatcher.java | 14 +++-- .../PacketHandlerAttributes.java | 21 +++---- .../packethandlers/PacketHandlerSpawn.java | 62 +++++++++---------- .../utilities/params/ParamInfoTypes.java | 2 + 7 files changed, 114 insertions(+), 48 deletions(-) create mode 100644 src/main/java/me/libraryaddict/disguise/disguisetypes/GolemCrack.java diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java index ba58c972..83c54653 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java @@ -282,6 +282,10 @@ public class FlagWatcher { } value = entityValues.get(id); + + if (id == MetaIndex.LIVING_HEALTH.getIndex() && (float) watch.getRawValue() <= 0) { + value = watch.getRawValue(); + } } else if (backupEntityValues.containsKey(id)) { if (backupEntityValues.get(id) == null) { continue; diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/GolemCrack.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/GolemCrack.java new file mode 100644 index 00000000..a629fd56 --- /dev/null +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/GolemCrack.java @@ -0,0 +1,11 @@ +package me.libraryaddict.disguise.disguisetypes; + +/** + * Created by libraryaddict on 24/04/2021. + */ +public enum GolemCrack { + HEALTH_100, + HEALTH_75, + HEALTH_50, + HEALTH_25 +} diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/IronGolemWatcher.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/IronGolemWatcher.java index 75b6452d..098fd1d5 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/IronGolemWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/IronGolemWatcher.java @@ -1,9 +1,57 @@ package me.libraryaddict.disguise.disguisetypes.watchers; +import lombok.Getter; import me.libraryaddict.disguise.disguisetypes.Disguise; +import me.libraryaddict.disguise.disguisetypes.GolemCrack; +import me.libraryaddict.disguise.utilities.reflection.NmsAddedIn; +import me.libraryaddict.disguise.utilities.reflection.NmsVersion; public class IronGolemWatcher extends InsentientWatcher { + private GolemCrack cracks; + public IronGolemWatcher(Disguise disguise) { super(disguise); } + + @NmsAddedIn(NmsVersion.v1_16) + public GolemCrack getCracks() { + return cracks; + } + + @NmsAddedIn(NmsVersion.v1_16) + public void setCracks(GolemCrack cracks) { + if (cracks == getCracks() || cracks == null) { + return; + } + + this.cracks = cracks; + + switch (cracks) { + case HEALTH_25: + setHealth(24); + break; + case HEALTH_50: + setHealth(49); + break; + case HEALTH_75: + setHealth(74); + break; + case HEALTH_100: + setHealth(100); + break; + } + + if (!isMaxHealthSet() || getMaxHealth() != 100) { + setMaxHealth(100); + } + } + + @Override + public IronGolemWatcher clone(Disguise disguise) { + IronGolemWatcher watcher = (IronGolemWatcher) super.clone(disguise); + + watcher.setCracks(getCracks()); + + return watcher; + } } diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/LivingWatcher.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/LivingWatcher.java index 71712524..ff3254ae 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/LivingWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/LivingWatcher.java @@ -11,6 +11,7 @@ import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.FlagWatcher; import me.libraryaddict.disguise.disguisetypes.MetaIndex; import me.libraryaddict.disguise.utilities.DisguiseUtilities; +import me.libraryaddict.disguise.utilities.parser.RandomDefaultValue; import me.libraryaddict.disguise.utilities.reflection.NmsAddedIn; import me.libraryaddict.disguise.utilities.reflection.NmsVersion; import org.bukkit.Color; @@ -129,7 +130,7 @@ public class LivingWatcher extends FlagWatcher { Builder builder; builder = WrappedAttribute.newBuilder(); - builder.attributeKey("generic.maxHealth"); + builder.attributeKey(NmsVersion.v1_16.isSupported() ? "generic.max_health" : "generic.maxHealth"); builder.baseValue(getMaxHealth()); builder.packet(packet); @@ -142,9 +143,14 @@ public class LivingWatcher extends FlagWatcher { for (Player player : DisguiseUtilities.getPerverts(getDisguise())) { try { - ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); - } - catch (InvocationTargetException e) { + if (player == getDisguise().getEntity()) { + PacketContainer p = packet.shallowClone(); + p.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId()); + ProtocolLibrary.getProtocolManager().sendServerPacket(player, p, false); + } else { + ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); + } + } catch (InvocationTargetException e) { e.printStackTrace(); } } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerAttributes.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerAttributes.java index a9bbbd9e..0dd9db30 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerAttributes.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerAttributes.java @@ -23,16 +23,14 @@ import java.util.List; * Created by libraryaddict on 3/01/2019. */ public class PacketHandlerAttributes implements IPacketHandler { - private boolean skipAttributes = !NmsVersion.v1_14.isSupported() && - ProtocolLibrary.getPlugin().getDescription().getVersion().equals("4.5.0"); + private boolean skipAttributes = !NmsVersion.v1_14.isSupported() && ProtocolLibrary.getPlugin().getDescription().getVersion().equals("4.5.0"); public PacketHandlerAttributes() { if (!skipAttributes) { return; } - DisguiseUtilities.getLogger() - .info("You are running ProtocolLib 4.5.0, attributes will not be handled; Update if you can."); + DisguiseUtilities.getLogger().info("You are running ProtocolLib 4.5.0, attributes will not be handled; Update if you can."); } @Override @@ -41,8 +39,7 @@ public class PacketHandlerAttributes implements IPacketHandler { } @Override - public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer, - Entity entity) { + public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer, Entity entity) { packets.clear(); // Skip due to a bug in ProtocolLib @@ -58,27 +55,25 @@ public class PacketHandlerAttributes implements IPacketHandler { PacketContainer updateAttributes = new PacketContainer(PacketType.Play.Server.UPDATE_ATTRIBUTES); for (WrappedAttribute attribute : sentPacket.getAttributeCollectionModifier().read(0)) { - if (attribute.getAttributeKey().equals("generic.maxHealth")) { + if (attribute.getAttributeKey().equals(NmsVersion.v1_16.isSupported() ? "generic.max_health" : "generic.maxHealth")) { WrappedAttribute.Builder builder; - if (disguise.getWatcher() instanceof LivingWatcher && - ((LivingWatcher) disguise.getWatcher()).isMaxHealthSet()) { + if (disguise.getWatcher() instanceof LivingWatcher && ((LivingWatcher) disguise.getWatcher()).isMaxHealthSet()) { builder = WrappedAttribute.newBuilder(); - builder.attributeKey("generic.maxHealth"); + builder.attributeKey(attribute.getAttributeKey()); builder.baseValue(((LivingWatcher) disguise.getWatcher()).getMaxHealth()); } else if (DisguiseConfig.isMaxHealthDeterminedByDisguisedEntity()) { builder = WrappedAttribute.newBuilder(attribute); } else { builder = WrappedAttribute.newBuilder(); - builder.attributeKey("generic.maxHealth"); + builder.attributeKey(attribute.getAttributeKey()); builder.baseValue(DisguiseValues.getDisguiseValues(disguise.getType()).getMaxHealth()); } builder.packet(updateAttributes); attributes.add(builder.build()); - } else if (attribute.getAttributeKey().equals("generic.movementSpeed") && - disguise.getWatcher() instanceof AbstractHorseWatcher) { + } else if (attribute.getAttributeKey().equals(NmsVersion.v1_16.isSupported() ? "generic.movement_speed" : "generic.movementSpeed") && disguise.getWatcher() instanceof AbstractHorseWatcher) { WrappedAttribute.Builder builder = WrappedAttribute.newBuilder(attribute); builder.packet(updateAttributes); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java index f098f7aa..89b2c66c 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java @@ -71,33 +71,6 @@ public class PacketHandlerSpawn implements IPacketHandler { Disguise disguise = packets.getDisguise(); boolean sendArmor = true; - if (DisguiseConfig.isMiscDisguisesForLivingEnabled()) { - if (disguise.getWatcher() instanceof LivingWatcher) { - ArrayList attributes = new ArrayList<>(); - - WrappedAttribute.Builder builder = WrappedAttribute.newBuilder().attributeKey("generic.maxHealth"); - - if (((LivingWatcher) disguise.getWatcher()).isMaxHealthSet()) { - builder.baseValue(((LivingWatcher) disguise.getWatcher()).getMaxHealth()); - } else if (DisguiseConfig.isMaxHealthDeterminedByDisguisedEntity() && disguisedEntity instanceof Damageable) { - builder.baseValue(((Damageable) disguisedEntity).getMaxHealth()); - } else { - builder.baseValue(DisguiseValues.getDisguiseValues(disguise.getType()).getMaxHealth()); - } - - PacketContainer packet = new PacketContainer(PacketType.Play.Server.UPDATE_ATTRIBUTES); - - builder.packet(packet); - - attributes.add(builder.build()); - - packet.getIntegers().write(0, disguisedEntity.getEntityId()); - packet.getAttributeCollectionModifier().write(0, attributes); - - packets.addPacket(packet); - } - } - Location loc = disguisedEntity.getLocation().clone().add(0, DisguiseUtilities.getYModifier(disguise) + disguise.getWatcher().getYModifier(), 0); Float pitchLock = DisguiseConfig.isMovementPacketsEnabled() ? disguise.getWatcher().getPitchLock() : null; @@ -174,7 +147,7 @@ public class PacketHandlerSpawn implements IPacketHandler { sendTab.getModifier().write(0, ReflectionManager.getEnumPlayerInfoAction(0)); List playerList = Collections.singletonList(ReflectionManager.getPlayerInfoData(sendTab.getHandle(), ReflectionManager - .getGameProfileWithThisSkin(playerDisguise.getUUID(), playerDisguise.getProfileName(), playerDisguise.getGameProfile()))); + .getGameProfileWithThisSkin(playerDisguise.getUUID(), playerDisguise.getProfileName(), playerDisguise.getGameProfile()))); sendTab.getModifier().write(1, playerList); packets.addPacket(sendTab); @@ -356,9 +329,8 @@ public class PacketHandlerSpawn implements IPacketHandler { entityType = ReflectionManager.getEntityType(disguise.getType().getEntityType()); } - Object[] params = - new Object[]{disguisedEntity.getEntityId(), disguise.getUUID(), x, y, z, loc.getPitch(), loc.getYaw(), entityType, data, - ReflectionManager.getVec3D(disguisedEntity.getVelocity())}; + Object[] params = new Object[]{disguisedEntity.getEntityId(), disguise.getUUID(), x, y, z, loc.getPitch(), loc.getYaw(), entityType, data, + ReflectionManager.getVec3D(disguisedEntity.getVelocity())}; spawnEntity = ProtocolLibrary.getProtocolManager().createPacketConstructor(PacketType.Play.Server.SPAWN_ENTITY, params).createPacket(params); } else { @@ -430,6 +402,34 @@ public class PacketHandlerSpawn implements IPacketHandler { packets.addPacket(newPacket); } + if (DisguiseConfig.isMiscDisguisesForLivingEnabled()) { + if (disguise.getWatcher() instanceof LivingWatcher) { + ArrayList attributes = new ArrayList<>(); + + WrappedAttribute.Builder builder = + WrappedAttribute.newBuilder().attributeKey(NmsVersion.v1_16.isSupported() ? "generic.max_health" : "generic.maxHealth"); + + if (((LivingWatcher) disguise.getWatcher()).isMaxHealthSet()) { + builder.baseValue(((LivingWatcher) disguise.getWatcher()).getMaxHealth()); + } else if (DisguiseConfig.isMaxHealthDeterminedByDisguisedEntity() && disguisedEntity instanceof Damageable) { + builder.baseValue(((Damageable) disguisedEntity).getMaxHealth()); + } else { + builder.baseValue(DisguiseValues.getDisguiseValues(disguise.getType()).getMaxHealth()); + } + + PacketContainer packet = new PacketContainer(PacketType.Play.Server.UPDATE_ATTRIBUTES); + + builder.packet(packet); + + attributes.add(builder.build()); + + packet.getIntegers().write(0, disguisedEntity.getEntityId()); + packet.getAttributeCollectionModifier().write(0, attributes); + + packets.addPacket(packet); + } + } + if (!disguise.isPlayerDisguise() || normalPlayerDisguise) { DisguiseUtilities.getNamePackets(disguise, new String[0]).forEach(packets::addPacket); } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/params/ParamInfoTypes.java b/src/main/java/me/libraryaddict/disguise/utilities/params/ParamInfoTypes.java index bf7fdac6..211de362 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/params/ParamInfoTypes.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/params/ParamInfoTypes.java @@ -5,6 +5,7 @@ import com.comphenix.protocol.wrappers.WrappedGameProfile; import com.comphenix.protocol.wrappers.WrappedParticle; import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.disguisetypes.EntityPose; +import me.libraryaddict.disguise.disguisetypes.GolemCrack; import me.libraryaddict.disguise.disguisetypes.RabbitType; import me.libraryaddict.disguise.utilities.params.types.ParamInfoEnum; import me.libraryaddict.disguise.utilities.params.types.base.*; @@ -129,6 +130,7 @@ public class ParamInfoTypes { "etc. 30m20secs = 30 minutes, 20 seconds")); paramInfos.add(new ParamInfoEnum(ChatColor.class, "ChatColor", "A chat color")); + paramInfos.add(new ParamInfoEnum(GolemCrack.class, "Golem Cracked", "The stage a golem has been cracked")); // Register base types Map booleanMap = new HashMap<>();