From e1d701050ba403360337f08fff528eda90ad7b0c Mon Sep 17 00:00:00 2001 From: fullwall Date: Mon, 16 Dec 2024 03:23:20 +0800 Subject: [PATCH] Rework attribute fix --- .../citizensnpcs/commands/NPCCommands.java | 4 +-- .../npc/CitizensTraitFactory.java | 4 +-- .../citizensnpcs/trait/AttributeTrait.java | 22 ++++++-------- .../net/citizensnpcs/trait/PacketNPC.java | 1 + .../java/net/citizensnpcs/util/Messages.java | 1 + .../main/java/net/citizensnpcs/util/Util.java | 29 +++++++++---------- main/src/main/resources/en.json | 1 + 7 files changed, 28 insertions(+), 34 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java index a5ced5878..334cb93c9 100644 --- a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -26,7 +26,6 @@ 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; @@ -425,8 +424,7 @@ public class NPCCommands { @Arg(2) Double value) { final Attribute attr = Util.getAttribute(attribute); if (attr == null) { - // todo an translation key is necessary here - sender.sendMessage("Attribute not found"); + Messaging.sendErrorTr(sender, Messages.ATTRIBUTE_NOT_FOUND, attribute); return; } AttributeTrait trait = npc.getOrAddTrait(AttributeTrait.class); diff --git a/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java b/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java index e3fe0c3d4..1cb5fb7f4 100644 --- a/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java +++ b/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java @@ -20,7 +20,7 @@ import net.citizensnpcs.api.trait.trait.Inventory; import net.citizensnpcs.api.trait.trait.MobType; import net.citizensnpcs.api.trait.trait.Owner; import net.citizensnpcs.api.trait.trait.PlayerFilter; -import net.citizensnpcs.api.trait.trait.Spawned; +import net.citizensnpcs.api.trait.trait.Spawned; import net.citizensnpcs.trait.Age; import net.citizensnpcs.trait.Anchors; import net.citizensnpcs.trait.ArmorStandTrait; @@ -106,7 +106,7 @@ public class CitizensTraitFactory implements TraitFactory { registerTrait(TraitInfo.create(ItemFrameTrait.class)); registerTrait(TraitInfo.create(LookClose.class).optInToStats()); registerTrait(TraitInfo.create(PaintingTrait.class)); - registerTrait(TraitInfo.create(MirrorTrait.class).optInToStats()); + registerTrait(TraitInfo.create(MirrorTrait.class).optInToStats()); registerTrait(TraitInfo.create(MountTrait.class)); registerTrait(TraitInfo.create(MobType.class).asDefaultTrait()); registerTrait(TraitInfo.create(OcelotModifiers.class)); diff --git a/main/src/main/java/net/citizensnpcs/trait/AttributeTrait.java b/main/src/main/java/net/citizensnpcs/trait/AttributeTrait.java index 160e9bca5..480fd328b 100644 --- a/main/src/main/java/net/citizensnpcs/trait/AttributeTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/AttributeTrait.java @@ -2,18 +2,18 @@ package net.citizensnpcs.trait; import java.util.Map; -import net.citizensnpcs.api.exception.NPCLoadException; -import net.citizensnpcs.api.util.DataKey; -import net.citizensnpcs.util.Util; import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeInstance; import org.bukkit.entity.LivingEntity; import com.google.common.collect.Maps; +import net.citizensnpcs.api.exception.NPCLoadException; import net.citizensnpcs.api.persistence.Persist; import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.TraitName; +import net.citizensnpcs.api.util.DataKey; +import net.citizensnpcs.util.Util; @TraitName("attributetrait") public class AttributeTrait extends Trait { @@ -34,16 +34,12 @@ public class AttributeTrait extends Trait { @Override public void load(DataKey key) throws NPCLoadException { - for (Map.Entry entry : key.getValuesDeep().entrySet()) { - final String rawAttributeName = entry.getKey(); - final Attribute attribute = Util.getAttribute(rawAttributeName); - if (attribute != null) { - final Object rawValue = entry.getValue(); - if (rawValue instanceof Double) { - attributes.put(attribute, (Double) rawValue); - } + for (DataKey subkey : key.getRelative("attributes").getSubKeys()) { + if (Util.getAttribute(subkey.name()) == null) { + key.removeKey("attributes." + subkey.name()); } } + attributes.remove(null); } @Override @@ -54,9 +50,9 @@ public class AttributeTrait extends Trait { for (Map.Entry entry : attributes.entrySet()) { final Attribute key = entry.getKey(); final AttributeInstance attributeInstance = le.getAttribute(key); - if (attributeInstance == null) { // not applicable anymore so ignore + if (attributeInstance == null) continue; - } + attributeInstance.setBaseValue(entry.getValue()); } } diff --git a/main/src/main/java/net/citizensnpcs/trait/PacketNPC.java b/main/src/main/java/net/citizensnpcs/trait/PacketNPC.java index 8209a7e09..0f7211146 100644 --- a/main/src/main/java/net/citizensnpcs/trait/PacketNPC.java +++ b/main/src/main/java/net/citizensnpcs/trait/PacketNPC.java @@ -112,6 +112,7 @@ public class PacketNPC extends Trait { public boolean spawn(Location at) { NMS.setLocationDirectly(base.getBukkitEntity(), at); PlayerUpdateTask.registerPlayer(getBukkitEntity()); + System.out.println("SPAWN"); return true; } } diff --git a/main/src/main/java/net/citizensnpcs/util/Messages.java b/main/src/main/java/net/citizensnpcs/util/Messages.java index 5f59c4fa8..cd3792950 100644 --- a/main/src/main/java/net/citizensnpcs/util/Messages.java +++ b/main/src/main/java/net/citizensnpcs/util/Messages.java @@ -20,6 +20,7 @@ public class Messages { public static final String ANIMATION_ADDED = "citizens.editors.waypoints.triggers.animation.added"; public static final String ANIMATION_TRIGGER_PROMPT = "citizens.editors.waypoints.triggers.animation.prompt"; public static final String ARMADILLO_STATE_SET = "citizens.commands.npc.armadillo.state-set"; + public static final String ATTRIBUTE_NOT_FOUND = "citizens.commands.npc.attribute.not-found"; public static final String ATTRIBUTE_RESET = "citizens.commands.npc.attribute.reset"; public static final String ATTRIBUTE_SET = "citizens.commands.npc.attribute.set"; public static final String AVAILABLE_WAYPOINT_PROVIDERS = "citizens.waypoints.available-providers-header"; diff --git a/main/src/main/java/net/citizensnpcs/util/Util.java b/main/src/main/java/net/citizensnpcs/util/Util.java index 7ad93df66..daf2e3012 100644 --- a/main/src/main/java/net/citizensnpcs/util/Util.java +++ b/main/src/main/java/net/citizensnpcs/util/Util.java @@ -189,6 +189,17 @@ public class Util { NMS.look(entity, to, headOnly, immediate); } + public static Attribute getAttribute(String attribute) { + if (!SpigotUtil.isRegistryKeyed(Attribute.class)) { + try { + return Attribute.valueOf(attribute.toUpperCase(Locale.ROOT)); + } catch (IllegalArgumentException ignore) { + return null; + } + } + return getRegistryValue(Registry.ATTRIBUTE, attribute); + } + public static Location getCenterLocation(Block block) { Location bloc = block.getLocation(); Location center = new Location(bloc.getWorld(), bloc.getBlockX() + 0.5, bloc.getBlockY(), @@ -260,27 +271,13 @@ public class Util { for (String keyCandidate : keyCandidates) { final NamespacedKey key = SpigotUtil.getKey(keyCandidate); final T value = registry.get(key); - if (value != null) { + if (value != null) return value; - } + } return null; } - public static Attribute getAttribute(String... keyCandidates) { - for (String keyCandidate : keyCandidates) { - boolean isFullUpperCase = keyCandidate.toUpperCase(Locale.ENGLISH).equals(keyCandidate); - if (isFullUpperCase) { // we assume it is an enum key - try { - // Just imagine we're still on older API (1.21.3-, exclusive) - // noinspection deprecation - return Attribute.valueOf(keyCandidate); - } catch (IllegalArgumentException ignored) {} // huh, not? - } - } - return getRegistryValue(Registry.ATTRIBUTE, keyCandidates); - } - public static String getTeamName(UUID id) { return "CIT-" + id.toString().replace("-", "").substring(0, 12); } diff --git a/main/src/main/resources/en.json b/main/src/main/resources/en.json index bb49d8181..9a1de4813 100644 --- a/main/src/main/resources/en.json +++ b/main/src/main/resources/en.json @@ -55,6 +55,7 @@ "citizens.commands.npc.allay.dancing-unset" : "[[{0}]] is no longer dancing.", "citizens.commands.npc.allay.description" : "Sets allay modifiers", "citizens.commands.npc.allay.help" : "", + "citizens.commands.npc.attribute.not-found" : "[[{0}]] is not a valid attribute.", "citizens.commands.npc.armadillo.state-set": "State set to [[{0}]]", "citizens.commands.npc.anchor.added" : "Anchor added.", "citizens.commands.npc.anchor.already-exists" : "The anchor [[{0}]] already exists.",