Rework attribute fix

This commit is contained in:
fullwall 2024-12-16 03:23:20 +08:00
parent 0239d5c8de
commit e1d701050b
7 changed files with 28 additions and 34 deletions

View File

@ -26,7 +26,6 @@ import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.Registry;
import org.bukkit.Rotation; import org.bukkit.Rotation;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.World; import org.bukkit.World;
@ -425,8 +424,7 @@ public class NPCCommands {
@Arg(2) Double value) { @Arg(2) Double value) {
final Attribute attr = Util.getAttribute(attribute); final Attribute attr = Util.getAttribute(attribute);
if (attr == null) { if (attr == null) {
// todo an translation key is necessary here Messaging.sendErrorTr(sender, Messages.ATTRIBUTE_NOT_FOUND, attribute);
sender.sendMessage("Attribute not found");
return; return;
} }
AttributeTrait trait = npc.getOrAddTrait(AttributeTrait.class); AttributeTrait trait = npc.getOrAddTrait(AttributeTrait.class);

View File

@ -2,18 +2,18 @@ package net.citizensnpcs.trait;
import java.util.Map; 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.Attribute;
import org.bukkit.attribute.AttributeInstance; import org.bukkit.attribute.AttributeInstance;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.persistence.Persist; import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName; import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.util.Util;
@TraitName("attributetrait") @TraitName("attributetrait")
public class AttributeTrait extends Trait { public class AttributeTrait extends Trait {
@ -34,16 +34,12 @@ public class AttributeTrait extends Trait {
@Override @Override
public void load(DataKey key) throws NPCLoadException { public void load(DataKey key) throws NPCLoadException {
for (Map.Entry<String, Object> entry : key.getValuesDeep().entrySet()) { for (DataKey subkey : key.getRelative("attributes").getSubKeys()) {
final String rawAttributeName = entry.getKey(); if (Util.getAttribute(subkey.name()) == null) {
final Attribute attribute = Util.getAttribute(rawAttributeName); key.removeKey("attributes." + subkey.name());
if (attribute != null) {
final Object rawValue = entry.getValue();
if (rawValue instanceof Double) {
attributes.put(attribute, (Double) rawValue);
}
} }
} }
attributes.remove(null);
} }
@Override @Override
@ -54,9 +50,9 @@ public class AttributeTrait extends Trait {
for (Map.Entry<Attribute, Double> entry : attributes.entrySet()) { for (Map.Entry<Attribute, Double> entry : attributes.entrySet()) {
final Attribute key = entry.getKey(); final Attribute key = entry.getKey();
final AttributeInstance attributeInstance = le.getAttribute(key); final AttributeInstance attributeInstance = le.getAttribute(key);
if (attributeInstance == null) { // not applicable anymore so ignore if (attributeInstance == null)
continue; continue;
}
attributeInstance.setBaseValue(entry.getValue()); attributeInstance.setBaseValue(entry.getValue());
} }
} }

View File

@ -112,6 +112,7 @@ public class PacketNPC extends Trait {
public boolean spawn(Location at) { public boolean spawn(Location at) {
NMS.setLocationDirectly(base.getBukkitEntity(), at); NMS.setLocationDirectly(base.getBukkitEntity(), at);
PlayerUpdateTask.registerPlayer(getBukkitEntity()); PlayerUpdateTask.registerPlayer(getBukkitEntity());
System.out.println("SPAWN");
return true; return true;
} }
} }

View File

@ -20,6 +20,7 @@ public class Messages {
public static final String ANIMATION_ADDED = "citizens.editors.waypoints.triggers.animation.added"; 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 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 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_RESET = "citizens.commands.npc.attribute.reset";
public static final String ATTRIBUTE_SET = "citizens.commands.npc.attribute.set"; public static final String ATTRIBUTE_SET = "citizens.commands.npc.attribute.set";
public static final String AVAILABLE_WAYPOINT_PROVIDERS = "citizens.waypoints.available-providers-header"; public static final String AVAILABLE_WAYPOINT_PROVIDERS = "citizens.waypoints.available-providers-header";

View File

@ -189,6 +189,17 @@ public class Util {
NMS.look(entity, to, headOnly, immediate); 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) { public static Location getCenterLocation(Block block) {
Location bloc = block.getLocation(); Location bloc = block.getLocation();
Location center = new Location(bloc.getWorld(), bloc.getBlockX() + 0.5, bloc.getBlockY(), Location center = new Location(bloc.getWorld(), bloc.getBlockX() + 0.5, bloc.getBlockY(),
@ -260,27 +271,13 @@ public class Util {
for (String keyCandidate : keyCandidates) { for (String keyCandidate : keyCandidates) {
final NamespacedKey key = SpigotUtil.getKey(keyCandidate); final NamespacedKey key = SpigotUtil.getKey(keyCandidate);
final T value = registry.get(key); final T value = registry.get(key);
if (value != null) { if (value != null)
return value; return value;
}
} }
return null; 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) { public static String getTeamName(UUID id) {
return "CIT-" + id.toString().replace("-", "").substring(0, 12); return "CIT-" + id.toString().replace("-", "").substring(0, 12);
} }

View File

@ -55,6 +55,7 @@
"citizens.commands.npc.allay.dancing-unset" : "[[{0}]] is no longer dancing.", "citizens.commands.npc.allay.dancing-unset" : "[[{0}]] is no longer dancing.",
"citizens.commands.npc.allay.description" : "Sets allay modifiers", "citizens.commands.npc.allay.description" : "Sets allay modifiers",
"citizens.commands.npc.allay.help" : "", "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.armadillo.state-set": "State set to [[{0}]]",
"citizens.commands.npc.anchor.added" : "Anchor added.", "citizens.commands.npc.anchor.added" : "Anchor added.",
"citizens.commands.npc.anchor.already-exists" : "The anchor [[{0}]] already exists.", "citizens.commands.npc.anchor.already-exists" : "The anchor [[{0}]] already exists.",