Use new reflective access to OldEnum subclasses

This commit is contained in:
fullwall 2024-07-07 12:40:04 +08:00
parent 4ba83dd520
commit 4143097d05
6 changed files with 32 additions and 21 deletions

View File

@ -112,13 +112,11 @@ public class Settings {
"The default background color for holograms, specified as an RGB or RGBA value<br>For example 0,255,123,0 would be green", "The default background color for holograms, specified as an RGB or RGBA value<br>For example 0,255,123,0 would be green",
"npc.hologram.default-background-color", ""), "npc.hologram.default-background-color", ""),
DEFAULT_HOLOGRAM_RENDERER( DEFAULT_HOLOGRAM_RENDERER(
"The default renderer for holograms, must be one of the following:<br>interaction - matches inbuilt nametags most closely<br>display - allows for different colored backgrounds<br>armorstand - the most stable option, very very small hit to client FPS compared to other options", "The default renderer for holograms, must be one of the following:<br>interaction - matches inbuilt nametags most closely<br>display - allows for different colored backgrounds<br>display_vehicle - mounts the display on the NPC<br>armorstand - the safest option, very very small hit to client FPS compared to other options<br>armorstand_vehicle - mounts the armorstand on the NPC",
"npc.hologram.default-renderer", "display"), "npc.hologram.default-renderer", "display"),
DEFAULT_LOOK_CLOSE("Enable look close by default", "npc.default.look-close.enabled", false), DEFAULT_LOOK_CLOSE("Enable look close by default", "npc.default.look-close.enabled", false),
DEFAULT_LOOK_CLOSE_RANGE("Default look close range in blocks", "npc.default.look-close.range", 10), DEFAULT_LOOK_CLOSE_RANGE("Default look close range in blocks", "npc.default.look-close.range", 10),
DEFAULT_NAME_HOLOGRAM_RENDERER( DEFAULT_NAME_HOLOGRAM_RENDERER("npc.hologram.default-name-renderer", ""),
"The default renderer for name holograms, must be one of the following:<br>interaction - matches inbuilt nametags most closely<br>display - allows for different colored backgrounds<br>display_vehicle - allows for different colored backgrounds<br>armorstand - the most stable option, very very small hit to client FPS compared to other options<br>armorstand_vehicle - the most stable option, very very small hit to client FPS compared to other options",
"npc.hologram.default-name-renderer", ""),
DEFAULT_NPC_HOLOGRAM_LINE_HEIGHT("Default distance between hologram lines", "npc.hologram.default-line-height", DEFAULT_NPC_HOLOGRAM_LINE_HEIGHT("Default distance between hologram lines", "npc.hologram.default-line-height",
0.4D), 0.4D),
DEFAULT_NPC_LIMIT( DEFAULT_NPC_LIMIT(

View File

@ -107,6 +107,7 @@ import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.EntityDim; import net.citizensnpcs.api.util.EntityDim;
import net.citizensnpcs.api.util.MemoryDataKey; import net.citizensnpcs.api.util.MemoryDataKey;
import net.citizensnpcs.api.util.Messaging; import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.OldEnumCompat.VillagerProfessionEnum;
import net.citizensnpcs.api.util.Paginator; import net.citizensnpcs.api.util.Paginator;
import net.citizensnpcs.api.util.Placeholders; import net.citizensnpcs.api.util.Placeholders;
import net.citizensnpcs.api.util.SpigotUtil; import net.citizensnpcs.api.util.SpigotUtil;
@ -2553,7 +2554,7 @@ public class NPCCommands {
if (parsed == null) if (parsed == null)
throw new CommandException(Messages.INVALID_PROFESSION, args.getString(1), throw new CommandException(Messages.INVALID_PROFESSION, args.getString(1),
Util.listValuesPretty(Profession.values())); Util.listValuesPretty(VillagerProfessionEnum.values()));
npc.getOrAddTrait(VillagerProfession.class).setProfession(parsed); npc.getOrAddTrait(VillagerProfession.class).setProfession(parsed);
Messaging.sendTr(sender, Messages.PROFESSION_SET, npc.getName(), parsed); Messaging.sendTr(sender, Messages.PROFESSION_SET, npc.getName(), parsed);

View File

@ -8,11 +8,12 @@ import net.citizensnpcs.api.exception.NPCLoadException;
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.api.util.DataKey;
import net.citizensnpcs.api.util.OldEnumCompat.VillagerProfessionEnum;
/** /**
* Persists the Villager profession metadata. * Persists the Villager profession metadata.
* *
* @see Profession * @see Villager.Profession
*/ */
@TraitName("profession") @TraitName("profession")
public class VillagerProfession extends Trait { public class VillagerProfession extends Trait {
@ -29,8 +30,9 @@ public class VillagerProfession extends Trait {
@Override @Override
public void load(DataKey key) throws NPCLoadException { public void load(DataKey key) throws NPCLoadException {
try { try {
profession = Profession.valueOf(key.getString("")); VillagerProfessionEnum vpe = VillagerProfessionEnum.valueOf(key.getString(""));
if ("NORMAL".equals(profession.name())) { profession = vpe.getInstance();
if ("NORMAL".equals(vpe.name())) {
profession = Profession.FARMER; profession = Profession.FARMER;
} }
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
@ -47,23 +49,19 @@ public class VillagerProfession extends Trait {
return; return;
} }
if (SUPPORT_ZOMBIE_VILLAGER) { if (SUPPORT_ZOMBIE_VILLAGER) {
try { if (npc.getEntity() instanceof ZombieVillager) {
if (npc.getEntity() instanceof ZombieVillager) { ((ZombieVillager) npc.getEntity()).setVillagerProfession(profession);
((ZombieVillager) npc.getEntity()).setVillagerProfession(profession);
}
} catch (Throwable t) {
SUPPORT_ZOMBIE_VILLAGER = false;
} }
} }
} }
@Override @Override
public void save(DataKey key) { public void save(DataKey key) {
key.setString("", profession.name()); key.setString("", new VillagerProfessionEnum(profession).name());
} }
public void setProfession(Profession profession) { public void setProfession(Profession profession) {
if ("NORMAL".equals(profession.name())) { if ("NORMAL".equals(new VillagerProfessionEnum(profession).name())) {
profession = Profession.FARMER; profession = Profession.FARMER;
} }
this.profession = profession; this.profession = profession;
@ -75,5 +73,14 @@ public class VillagerProfession extends Trait {
return "Profession{" + profession + "}"; return "Profession{" + profession + "}";
} }
private static boolean SUPPORT_ZOMBIE_VILLAGER = true; private static boolean SUPPORT_ZOMBIE_VILLAGER = false;
static {
try {
Class.forName("org.bukkit.entity.ZombieVillager").getMethod("setVillagerProfession",
Villager.Profession.class);
SUPPORT_ZOMBIE_VILLAGER = true;
} catch (Throwable e) {
e.printStackTrace();
}
}
} }

View File

@ -17,6 +17,7 @@ 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.Messaging; import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.OldEnumCompat.CatTypeEnum;
import net.citizensnpcs.util.Messages; import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.NMS; import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util; import net.citizensnpcs.util.Util;
@ -109,7 +110,7 @@ public class CatTrait extends Trait {
String output = ""; String output = "";
if (args.hasValueFlag("type")) { if (args.hasValueFlag("type")) {
if (type == null) if (type == null)
throw new CommandUsageException(Messages.INVALID_CAT_TYPE, Util.listValuesPretty(Cat.Type.values())); throw new CommandUsageException(Messages.INVALID_CAT_TYPE, Util.listValuesPretty(CatTypeEnum.values()));
trait.setType(type); trait.setType(type);
output += ' ' + Messaging.tr(Messages.CAT_TYPE_SET, args.getFlag("type")); output += ' ' + Messaging.tr(Messages.CAT_TYPE_SET, args.getFlag("type"));
} }

View File

@ -15,6 +15,7 @@ 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.Messaging; import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.OldEnumCompat.FrogVariantEnum;
import net.citizensnpcs.util.Messages; import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Util; import net.citizensnpcs.util.Util;
@ -58,7 +59,8 @@ public class FrogTrait extends Trait {
String output = ""; String output = "";
if (args.hasValueFlag("variant")) { if (args.hasValueFlag("variant")) {
if (variant == null) if (variant == null)
throw new CommandException(Messages.INVALID_FROG_VARIANT, Util.listValuesPretty(Frog.Variant.values())); throw new CommandException(Messages.INVALID_FROG_VARIANT,
Util.listValuesPretty(FrogVariantEnum.values()));
trait.setVariant(variant); trait.setVariant(variant);
output += Messaging.tr(Messages.FROG_VARIANT_SET, variant); output += Messaging.tr(Messages.FROG_VARIANT_SET, variant);
} }

View File

@ -18,6 +18,8 @@ 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.Messaging; import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.OldEnumCompat.VillagerProfessionEnum;
import net.citizensnpcs.api.util.OldEnumCompat.VillagerTypeEnum;
import net.citizensnpcs.trait.VillagerProfession; import net.citizensnpcs.trait.VillagerProfession;
import net.citizensnpcs.util.Messages; import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Util; import net.citizensnpcs.util.Util;
@ -84,14 +86,14 @@ public class VillagerTrait extends Trait {
if (args.hasValueFlag("type")) { if (args.hasValueFlag("type")) {
if (type == null) if (type == null)
throw new CommandException(Messages.INVALID_VILLAGER_TYPE, throw new CommandException(Messages.INVALID_VILLAGER_TYPE,
Util.listValuesPretty(Villager.Type.values())); Util.listValuesPretty(VillagerTypeEnum.values()));
trait.setType(type); trait.setType(type);
output += " " + Messaging.tr(Messages.VILLAGER_TYPE_SET, args.getFlag("type")); output += " " + Messaging.tr(Messages.VILLAGER_TYPE_SET, args.getFlag("type"));
} }
if (args.hasValueFlag("profession")) { if (args.hasValueFlag("profession")) {
if (profession == null) if (profession == null)
throw new CommandException(Messages.INVALID_PROFESSION, args.getFlag("profession"), throw new CommandException(Messages.INVALID_PROFESSION, args.getFlag("profession"),
Joiner.on(',').join(Profession.values())); Joiner.on(',').join(VillagerProfessionEnum.values()));
npc.getOrAddTrait(VillagerProfession.class).setProfession(profession); npc.getOrAddTrait(VillagerProfession.class).setProfession(profession);
output += " " + Messaging.tr(Messages.PROFESSION_SET, npc.getName(), args.getFlag("profession")); output += " " + Messaging.tr(Messages.PROFESSION_SET, npc.getName(), args.getFlag("profession"));
} }