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.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);

View File

@ -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<String, Object> 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<Attribute, Double> 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());
}
}

View File

@ -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;
}
}

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_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";

View File

@ -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);
}

View File

@ -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.",