fix: search attributes using different key for backwards compatibility (#3178)

This commit is contained in:
ZX夏夜之风 2024-11-06 00:13:41 +08:00 committed by GitHub
parent 63b0125be7
commit 7cc2b0658d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 20 additions and 9 deletions

View File

@ -45,7 +45,6 @@ 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;
@ -400,7 +399,7 @@ public class CitizensNPC extends AbstractNPC {
if (type == EntityType.PLAYER || Util.isHorse(type)) {
if (SUPPORT_ATTRIBUTES && !hasTrait(AttributeTrait.class) || !getTrait(AttributeTrait.class)
.hasAttribute(Registry.ATTRIBUTE.get(SpigotUtil.getKey("step_height")))) {
.hasAttribute(Util.getRegistryValue(Registry.ATTRIBUTE, "generic.step_height", "step_height"))) {
NMS.setStepHeight(entity, 1);
}
}

View File

@ -916,7 +916,7 @@ public class HologramTrait extends Trait {
}
if (SpigotUtil.getVersion()[1] >= 21 && base.getEntity() instanceof LivingEntity) {
AttributeInstance inst = ((LivingEntity) base.getEntity())
.getAttribute(Registry.ATTRIBUTE.get(SpigotUtil.getKey("scale")));
.getAttribute(Util.getRegistryValue(Registry.ATTRIBUTE, "generic.scale", "scale"));
if (inst != null) {
Transformation tf = disp.getTransformation();
tf.getScale().set(inst.getValue());

View File

@ -2,6 +2,7 @@ package net.citizensnpcs.trait;
import java.math.BigDecimal;
import net.citizensnpcs.util.Util;
import org.bukkit.Registry;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
@ -12,7 +13,6 @@ import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitEventHandler;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.SpigotUtil;
@TraitName("scaledhealthtrait")
public class ScaledMaxHealthTrait extends Trait {
@ -41,7 +41,7 @@ public class ScaledMaxHealthTrait extends Trait {
public void onSpawn() {
if (maxHealth != null && npc.getEntity() instanceof LivingEntity) {
if (SUPPORTS_ATTRIBUTES) {
((LivingEntity) npc.getEntity()).getAttribute(Registry.ATTRIBUTE.get(SpigotUtil.getKey("max_health")))
((LivingEntity) npc.getEntity()).getAttribute(Util.getRegistryValue(Registry.ATTRIBUTE, "generic.max_health", "max_health"))
.setBaseValue(Math.min(MAX_VALUE, maxHealth));
} else {
((LivingEntity) npc.getEntity()).setMaxHealth(maxHealth);

View File

@ -33,7 +33,6 @@ 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;
@ -143,7 +142,7 @@ public class BossBarTrait extends Trait {
LivingEntity entity = (LivingEntity) npc.getEntity();
double maxHealth = entity.getMaxHealth();
if (SUPPORT_ATTRIBUTES) {
maxHealth = entity.getAttribute(Registry.ATTRIBUTE.get(SpigotUtil.getKey("max_health")))
maxHealth = entity.getAttribute(Util.getRegistryValue(Registry.ATTRIBUTE, "generic.max_health", "max_health"))
.getValue();
}
bar.setProgress(entity.getHealth() / maxHealth);

View File

@ -61,7 +61,6 @@ 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;
@ -131,7 +130,7 @@ public class NMS {
return;
if (SUPPORTS_ATTRIBUTABLE && npc.getEntity() instanceof Attributable) {
AttributeInstance attribute = ((Attributable) npc.getEntity())
.getAttribute(Registry.ATTRIBUTE.get(SpigotUtil.getKey("knockback_resistance")));
.getAttribute(Util.getRegistryValue(Registry.ATTRIBUTE, "generic.knockback_resistance", "knockback_resistance"));
if (attribute != null) {
strength *= 1 - attribute.getValue();
}

View File

@ -18,8 +18,11 @@ import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.Keyed;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
@ -252,6 +255,17 @@ public class Util {
return new XORShiftRNG();
}
public static <T extends Keyed> T getRegistryValue(Registry<T> registry, String... keyCandidates) {
for (String keyCandidate : keyCandidates) {
final NamespacedKey key = SpigotUtil.getKey(keyCandidate);
final T value = registry.get(key);
if (value != null) {
return value;
}
}
return null;
}
public static String getTeamName(UUID id) {
return "CIT-" + id.toString().replace("-", "").substring(0, 12);
}