From 8878c238c043967b9f27b29081dab5348ff40e22 Mon Sep 17 00:00:00 2001 From: fullwall Date: Tue, 29 Oct 2024 20:35:25 +0800 Subject: [PATCH] Add back compat for attribute --- .../citizensnpcs/commands/NPCCommands.java | 7 +++-- .../net/citizensnpcs/npc/CitizensNPC.java | 7 +++-- .../citizensnpcs/trait/AttributeTrait.java | 2 +- .../net/citizensnpcs/trait/Controllable.java | 7 ++--- .../net/citizensnpcs/trait/HologramTrait.java | 5 +-- .../trait/versioned/BossBarTrait.java | 17 ++++++---- .../main/java/net/citizensnpcs/util/NMS.java | 31 ++++++++++--------- 7 files changed, 41 insertions(+), 35 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java index 0c9e2a91f..e25c69cb9 100644 --- a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -26,10 +26,10 @@ import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.OfflinePlayer; +import org.bukkit.Registry; import org.bukkit.Rotation; import org.bukkit.Sound; import org.bukkit.World; -import org.bukkit.attribute.Attribute; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.command.CommandSender; @@ -420,12 +420,13 @@ public class NPCCommands { public void attribute(CommandContext args, CommandSender sender, NPC npc, @Arg(value = 1, completionsProvider = OptionalAttributeCompletions.class) String attribute, @Arg(2) Double value) { + AttributeTrait trait = npc.getOrAddTrait(AttributeTrait.class); if (value == null) { - trait.setDefaultAttribute(Attribute.valueOf(attribute)); + trait.setDefaultAttribute(Registry.ATTRIBUTE.get(SpigotUtil.getKey(attribute))); Messaging.sendTr(sender, Messages.ATTRIBUTE_RESET, attribute); } else { - trait.setAttributeValue(Attribute.valueOf(attribute), value); + trait.setAttributeValue(Registry.ATTRIBUTE.get(SpigotUtil.getKey(attribute)), value); Messaging.sendTr(sender, Messages.ATTRIBUTE_SET, attribute, value); } } diff --git a/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java b/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java index 45de9a50e..56e925aaf 100644 --- a/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java +++ b/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java @@ -9,7 +9,7 @@ import java.util.function.Consumer; import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.attribute.Attribute; +import org.bukkit.Registry; import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; @@ -45,6 +45,7 @@ import net.citizensnpcs.api.trait.trait.MobType; import net.citizensnpcs.api.trait.trait.Spawned; import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.api.util.Messaging; +import net.citizensnpcs.api.util.SpigotUtil; import net.citizensnpcs.npc.ai.CitizensNavigator; import net.citizensnpcs.npc.skin.SkinnableEntity; import net.citizensnpcs.trait.AttributeTrait; @@ -398,8 +399,8 @@ public class CitizensNPC extends AbstractNPC { entity.setRemoveWhenFarAway(false); if (type == EntityType.PLAYER || Util.isHorse(type)) { - if (SUPPORT_ATTRIBUTES && !hasTrait(AttributeTrait.class) - || !getTrait(AttributeTrait.class).hasAttribute(Attribute.GENERIC_STEP_HEIGHT)) { + if (SUPPORT_ATTRIBUTES && !hasTrait(AttributeTrait.class) || !getTrait(AttributeTrait.class) + .hasAttribute(Registry.ATTRIBUTE.get(SpigotUtil.getKey("step_height")))) { NMS.setStepHeight(entity, 1); } } diff --git a/main/src/main/java/net/citizensnpcs/trait/AttributeTrait.java b/main/src/main/java/net/citizensnpcs/trait/AttributeTrait.java index 156317590..b98150496 100644 --- a/main/src/main/java/net/citizensnpcs/trait/AttributeTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/AttributeTrait.java @@ -15,7 +15,7 @@ import net.citizensnpcs.api.trait.TraitName; @TraitName("attributetrait") public class AttributeTrait extends Trait { @Persist(keyType = Attribute.class) - private final Map attributes = Maps.newEnumMap(Attribute.class); + private final Map attributes = Maps.newHashMap(); public AttributeTrait() { super("attributetrait"); diff --git a/main/src/main/java/net/citizensnpcs/trait/Controllable.java b/main/src/main/java/net/citizensnpcs/trait/Controllable.java index e80f8a9a5..889e6b866 100644 --- a/main/src/main/java/net/citizensnpcs/trait/Controllable.java +++ b/main/src/main/java/net/citizensnpcs/trait/Controllable.java @@ -38,7 +38,6 @@ public class Controllable extends Trait { private BuiltInControls controls; @Persist private boolean enabled = true; - private ControllableInput input; @Persist("owner_required") private boolean ownerRequired; @@ -132,7 +131,7 @@ public class Controllable extends Trait { if (npc.getNavigator().isNavigating() || passengers.size() == 0 || !(passengers.get(0) instanceof Player)) return; Player player = (Player) passengers.get(0); - input = new ControllableInput(); + ControllableInput input = new ControllableInput(); if (SUPPORTS_PLAYER_INPUT_EVENT) { input.forward = player.getCurrentInput().isForward() ? 1 : player.getCurrentInput().isBackward() ? -1 : 0; input.horizontal = player.getCurrentInput().isLeft() ? 1 : player.getCurrentInput().isRight() ? -1 : 0; @@ -404,10 +403,8 @@ public class Controllable extends Trait { yaw = Math.toRadians(yaw); Vector vel = handle.getVelocity(); double oldSpeed = Math.sqrt(vel.getX() * vel.getX() + vel.getZ() * vel.getZ()); - double nxsin = -Math.sin(yaw); - double xcos = Math.cos(yaw); if (input.forward > 0) { - vel = vel.setX(nxsin * speed * speedMod).setZ(xcos * speed * speedMod); + vel = vel.setX(-Math.sin(yaw) * speed * speedMod).setZ(Math.cos(yaw) * speed * speedMod); } vel.add(new Vector(Math.sin(yaw + Math.PI / 2), 0D, -Math.cos(yaw + Math.PI / 2)) .multiply(speedMod * Setting.CONTROLLABLE_GROUND_DIRECTION_MODIFIER.asDouble() * input.horizontal)); diff --git a/main/src/main/java/net/citizensnpcs/trait/HologramTrait.java b/main/src/main/java/net/citizensnpcs/trait/HologramTrait.java index 9542c3c92..53a949e39 100644 --- a/main/src/main/java/net/citizensnpcs/trait/HologramTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/HologramTrait.java @@ -17,7 +17,7 @@ import org.bukkit.Color; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Particle; -import org.bukkit.attribute.Attribute; +import org.bukkit.Registry; import org.bukkit.attribute.AttributeInstance; import org.bukkit.command.CommandSender; import org.bukkit.entity.AreaEffectCloud; @@ -918,7 +918,8 @@ public class HologramTrait extends Trait { disp.setBackgroundColor(color); } if (SpigotUtil.getVersion()[1] >= 21 && base.getEntity() instanceof LivingEntity) { - AttributeInstance inst = ((LivingEntity) base.getEntity()).getAttribute(Attribute.GENERIC_SCALE); + AttributeInstance inst = ((LivingEntity) base.getEntity()) + .getAttribute(Registry.ATTRIBUTE.get(SpigotUtil.getKey("scale"))); if (inst != null) { Transformation tf = disp.getTransformation(); tf.getScale().set(inst.getValue()); diff --git a/main/src/main/java/net/citizensnpcs/trait/versioned/BossBarTrait.java b/main/src/main/java/net/citizensnpcs/trait/versioned/BossBarTrait.java index b145eb058..50aaeaa83 100644 --- a/main/src/main/java/net/citizensnpcs/trait/versioned/BossBarTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/versioned/BossBarTrait.java @@ -5,7 +5,7 @@ import java.util.List; import java.util.function.Supplier; import org.bukkit.Bukkit; -import org.bukkit.attribute.Attribute; +import org.bukkit.Registry; import org.bukkit.boss.BarColor; import org.bukkit.boss.BarFlag; import org.bukkit.boss.BarStyle; @@ -33,6 +33,7 @@ import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.TraitName; import net.citizensnpcs.api.util.Messaging; import net.citizensnpcs.api.util.Placeholders; +import net.citizensnpcs.api.util.SpigotUtil; import net.citizensnpcs.util.NMS; import net.citizensnpcs.util.Util; @@ -142,11 +143,8 @@ public class BossBarTrait extends Trait { LivingEntity entity = (LivingEntity) npc.getEntity(); double maxHealth = entity.getMaxHealth(); if (SUPPORT_ATTRIBUTES) { - try { - maxHealth = entity.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); - } catch (Throwable t) { - SUPPORT_ATTRIBUTES = false; - } + maxHealth = entity.getAttribute(Registry.ATTRIBUTE.get(SpigotUtil.getKey("max_health"))) + .getValue(); } bar.setProgress(entity.getHealth() / maxHealth); } @@ -277,4 +275,11 @@ public class BossBarTrait extends Trait { } private static boolean SUPPORT_ATTRIBUTES = true; + static { + try { + Class.forName("org.bukkit.attribute.Attribute"); + } catch (ClassNotFoundException e) { + SUPPORT_ATTRIBUTES = false; + } + } } diff --git a/main/src/main/java/net/citizensnpcs/util/NMS.java b/main/src/main/java/net/citizensnpcs/util/NMS.java index bc14659ce..005f6baae 100644 --- a/main/src/main/java/net/citizensnpcs/util/NMS.java +++ b/main/src/main/java/net/citizensnpcs/util/NMS.java @@ -18,9 +18,9 @@ import java.util.stream.Collectors; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.Registry; import org.bukkit.Sound; import org.bukkit.attribute.Attributable; -import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeInstance; import org.bukkit.block.Block; import org.bukkit.block.data.BlockData; @@ -61,6 +61,7 @@ import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.util.BoundingBox; import net.citizensnpcs.api.util.EntityDim; import net.citizensnpcs.api.util.Messaging; +import net.citizensnpcs.api.util.SpigotUtil; import net.citizensnpcs.api.util.SpigotUtil.InventoryViewAPI; import net.citizensnpcs.npc.ai.MCNavigationStrategy.MCNavigator; import net.citizensnpcs.npc.ai.MCTargetStrategy.TargetNavigator; @@ -128,9 +129,9 @@ public class NMS { Consumer cb) { if (npc.getEntity() == null) return; - if (SUPPORT_KNOCKBACK_RESISTANCE && npc.getEntity() instanceof Attributable) { + if (SUPPORTS_ATTRIBUTABLE && npc.getEntity() instanceof Attributable) { AttributeInstance attribute = ((Attributable) npc.getEntity()) - .getAttribute(Attribute.GENERIC_KNOCKBACK_RESISTANCE); + .getAttribute(Registry.ATTRIBUTE.get(SpigotUtil.getKey("knockback_resistance"))); if (attribute != null) { strength *= 1 - attribute.getValue(); } @@ -506,6 +507,10 @@ public class NMS { return null; } + public static float getForwardBackwardMovement(org.bukkit.entity.Entity bukkitEntity) { + return BRIDGE.getForwardBackwardMovement(bukkitEntity); + } + public static MethodHandle getGetter(Class clazz, String name) { return getGetter(clazz, name, true); } @@ -528,10 +533,6 @@ public class NMS { return BRIDGE.getHeadYaw(entity); } - public static float getForwardBackwardMovement(org.bukkit.entity.Entity bukkitEntity) { - return BRIDGE.getForwardBackwardMovement(bukkitEntity); - } - public static float getJumpPower(NPC npc, float original) { if (npc == null) return original; @@ -674,10 +675,6 @@ public class NMS { return BRIDGE.getVehicle(entity); } - public static float getXZMovement(org.bukkit.entity.Entity bukkitEntity) { - return BRIDGE.getXZMovement(bukkitEntity); - } - public static Collection getViewingPlayers(org.bukkit.entity.Entity entity) { return BRIDGE.getViewingPlayers(entity); } @@ -686,6 +683,10 @@ public class NMS { return BRIDGE.getWidth(entity); } + public static float getXZMovement(org.bukkit.entity.Entity bukkitEntity) { + return BRIDGE.getXZMovement(bukkitEntity); + } + public static float getYaw(Entity entity) { return BRIDGE.getYaw(entity); } @@ -1047,7 +1048,7 @@ public class NMS { private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup(); private static Field MODIFIERS_FIELD; private static boolean PAPER_KNOCKBACK_EVENT_EXISTS = true; - private static boolean SUPPORT_KNOCKBACK_RESISTANCE = true; + private static boolean SUPPORTS_ATTRIBUTABLE = true; private static boolean SUPPORTS_FIND_PROFILES_BY_NAME = true; private static MethodHandle UNSAFE_FIELD_OFFSET; private static MethodHandle UNSAFE_PUT_BOOLEAN; @@ -1065,9 +1066,9 @@ public class NMS { PAPER_KNOCKBACK_EVENT_EXISTS = false; } try { - Class.forName("org.bukkit.attribute.Attribute").getField("GENERIC_KNOCKBACK_RESISTANCE"); - } catch (Exception e) { - SUPPORT_KNOCKBACK_RESISTANCE = false; + Class.forName("org.bukkit.attribute.Attributable"); + } catch (ClassNotFoundException e) { + SUPPORTS_ATTRIBUTABLE = false; } try { GameProfileRepository.class.getMethod("findProfilesByNames", String[].class, ProfileLookupCallback.class);