Add compatbility with 1.21 enums change

This commit is contained in:
libraryaddict 2024-07-14 00:02:04 +12:00
parent 134f8931c8
commit 4c8337cefa
17 changed files with 145 additions and 80 deletions

View File

@ -313,11 +313,12 @@ public class LibsDisguises extends JavaPlugin {
"Please update from MC 1.19.0! You should be using at least 1.19.3! 1.19.1 is the lowest supported 1.19 version!");
}
if (NmsVersion.v1_21_R1.isSupported()) {
// Creates more problems than it solves
/* if (NmsVersion.v1_21_R1.isSupported()) {
getLogger().info(
"Detected you are running 1.21, you may need to enable `enum-compatibility-mode` in your server's bukkit.yml if you are " +
"experiencing errors on Lib's Disguises load");
}
}*/
try {
Class cl = Class.forName("org.bukkit.Server$Spigot");

View File

@ -7,6 +7,7 @@ import me.libraryaddict.disguise.disguisetypes.watchers.PaintingWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.SplashPotionWatcher;
import me.libraryaddict.disguise.utilities.DisguiseValues;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.Art;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
@ -92,7 +93,9 @@ public class MiscDisguise extends TargetedDisguise {
switch (getType()) {
// The only disguises which should use a custom data.
case PAINTING:
((PaintingWatcher) getWatcher()).setArt(Art.values()[Math.max(0, id) % Art.values().length]);
if (!NmsVersion.v1_21_R1.isSupported()) {
((PaintingWatcher) getWatcher()).setArt(ReflectionManager.fromEnum(Art.class, id));
}
break;
case FALLING_BLOCK:
((FallingBlockWatcher) getWatcher()).setBlock(itemStack);

View File

@ -11,6 +11,7 @@ import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
import me.libraryaddict.disguise.disguisetypes.MetaIndex;
import me.libraryaddict.disguise.utilities.parser.RandomDefaultValue;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import me.libraryaddict.disguise.utilities.reflection.annotations.MethodDescription;
import me.libraryaddict.disguise.utilities.reflection.annotations.NmsAddedIn;
import me.libraryaddict.disguise.utilities.reflection.annotations.NmsRemovedIn;
@ -100,7 +101,7 @@ public class AreaEffectCloudWatcher extends FlagWatcher {
if (NmsVersion.v1_13.isSupported()) {
return (Particle) SpigotConversionUtil.toBukkitParticle(getData(MetaIndex.AREA_EFFECT_PARTICLE).getType());
} else {
return Particle.values()[getData(MetaIndex.AREA_EFFECT_PARTICLE_OLD)];
return ReflectionManager.fromEnum(Particle.class, getData(MetaIndex.AREA_EFFECT_PARTICLE_OLD));
}
}
@ -111,7 +112,7 @@ public class AreaEffectCloudWatcher extends FlagWatcher {
setParticle(
new com.github.retrooper.packetevents.protocol.particle.Particle(SpigotConversionUtil.fromBukkitParticle(particle)));
} else {
setData(MetaIndex.AREA_EFFECT_PARTICLE_OLD, particle.ordinal());
setData(MetaIndex.AREA_EFFECT_PARTICLE_OLD, ReflectionManager.enumOrdinal(particle));
sendData(MetaIndex.AREA_EFFECT_PARTICLE_OLD);
}
}

View File

@ -4,9 +4,9 @@ import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.AnimalColor;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.MetaIndex;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.parser.RandomDefaultValue;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import me.libraryaddict.disguise.utilities.reflection.annotations.MethodDescription;
import me.libraryaddict.disguise.utilities.reflection.annotations.NmsAddedIn;
import org.bukkit.DyeColor;
@ -21,9 +21,7 @@ public class CatWatcher extends TameableWatcher {
super(disguise);
if (DisguiseConfig.isRandomDisguises()) {
Cat.Type[] values = Cat.Type.values();
setType(values[DisguiseUtilities.getRandom().nextInt(values.length)]);
setType(ReflectionManager.randomEnum(Cat.Type.class));
}
}

View File

@ -6,6 +6,7 @@ import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
import me.libraryaddict.disguise.disguisetypes.MetaIndex;
import me.libraryaddict.disguise.utilities.parser.RandomDefaultValue;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.Color;
import org.bukkit.entity.Display;
import org.bukkit.util.Transformation;
@ -146,13 +147,13 @@ public abstract class DisplayWatcher extends FlagWatcher {
}
public Display.Billboard getBillboard() {
return Display.Billboard.values()[getData(MetaIndex.DISPLAY_BILLBOARD_RENDER_CONSTRAINTS)];
return ReflectionManager.fromEnum(Display.Billboard.class, getData(MetaIndex.DISPLAY_BILLBOARD_RENDER_CONSTRAINTS));
}
// Because TextDisplayWatcher modifies this on startup..
@RandomDefaultValue
public void setBillboard(Display.Billboard billboard) {
setData(MetaIndex.DISPLAY_BILLBOARD_RENDER_CONSTRAINTS, (byte) billboard.ordinal());
setData(MetaIndex.DISPLAY_BILLBOARD_RENDER_CONSTRAINTS, (byte) ReflectionManager.enumOrdinal(billboard));
sendData(MetaIndex.DISPLAY_BILLBOARD_RENDER_CONSTRAINTS);
}

View File

@ -5,11 +5,10 @@ import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.MetaIndex;
import me.libraryaddict.disguise.utilities.parser.RandomDefaultValue;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import me.libraryaddict.disguise.utilities.reflection.annotations.NmsAddedIn;
import org.bukkit.entity.Fox;
import java.util.Random;
/**
* Created by libraryaddict on 6/05/2019.
*/
@ -19,7 +18,7 @@ public class FoxWatcher extends AgeableWatcher {
super(disguise);
if (DisguiseConfig.isRandomDisguises()) {
setType(Fox.Type.values()[new Random().nextInt(Fox.Type.values().length)]);
setType(ReflectionManager.randomEnum(Fox.Type.class));
}
}

View File

@ -4,16 +4,15 @@ import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.MetaIndex;
import me.libraryaddict.disguise.utilities.parser.RandomDefaultValue;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.entity.Frog;
import java.util.Random;
public class FrogWatcher extends AgeableWatcher {
public FrogWatcher(Disguise disguise) {
super(disguise);
if (DisguiseConfig.isRandomDisguises()) {
setVariant(Frog.Variant.values()[new Random().nextInt(Frog.Variant.values().length)]);
setVariant(ReflectionManager.randomEnum(Frog.Variant.class));
}
}

View File

@ -3,9 +3,9 @@ package me.libraryaddict.disguise.disguisetypes.watchers;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.MetaIndex;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.parser.RandomDefaultValue;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.Material;
import org.bukkit.entity.Horse.Color;
import org.bukkit.entity.Horse.Style;
@ -17,13 +17,13 @@ public class HorseWatcher extends AbstractHorseWatcher {
super(disguise);
if (DisguiseConfig.isRandomDisguises()) {
setStyle(Style.values()[DisguiseUtilities.random.nextInt(Style.values().length)]);
setColor(Color.values()[DisguiseUtilities.random.nextInt(Color.values().length)]);
setStyle(ReflectionManager.randomEnum(Style.class));
setColor(ReflectionManager.randomEnum(Color.class));
}
}
public Color getColor() {
return Color.values()[(getData(MetaIndex.HORSE_COLOR_STYLE) & 0xFF)];
return ReflectionManager.fromEnum(Color.class, getData(MetaIndex.HORSE_COLOR_STYLE) & 0xFF);
}
@RandomDefaultValue
@ -33,7 +33,7 @@ public class HorseWatcher extends AbstractHorseWatcher {
}
public Style getStyle() {
return Style.values()[(getData(MetaIndex.HORSE_COLOR_STYLE) >>> 8)];
return ReflectionManager.fromEnum(Style.class, (getData(MetaIndex.HORSE_COLOR_STYLE) >>> 8));
}
@RandomDefaultValue

View File

@ -3,6 +3,7 @@ package me.libraryaddict.disguise.disguisetypes.watchers;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.MetaIndex;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import me.libraryaddict.disguise.utilities.reflection.annotations.NmsAddedIn;
import org.bukkit.entity.MushroomCow;
@ -18,12 +19,12 @@ public class MushroomCowWatcher extends AgeableWatcher {
@NmsAddedIn(NmsVersion.v1_14)
public MushroomCow.Variant getVariant() {
return MushroomCow.Variant.valueOf(getData(MetaIndex.MUSHROOM_COW_TYPE).toUpperCase(Locale.ENGLISH));
return ReflectionManager.fromEnum(MushroomCow.Variant.class, getData(MetaIndex.MUSHROOM_COW_TYPE));
}
@NmsAddedIn(NmsVersion.v1_14)
public void setVariant(MushroomCow.Variant variant) {
setData(MetaIndex.MUSHROOM_COW_TYPE, variant.name().toLowerCase(Locale.ENGLISH));
setData(MetaIndex.MUSHROOM_COW_TYPE, ReflectionManager.enumName(variant).toLowerCase(Locale.ENGLISH));
sendData(MetaIndex.MUSHROOM_COW_TYPE);
}
}

View File

@ -5,12 +5,11 @@ import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.MetaIndex;
import me.libraryaddict.disguise.utilities.parser.RandomDefaultValue;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import me.libraryaddict.disguise.utilities.reflection.annotations.MethodOnlyUsedBy;
import me.libraryaddict.disguise.utilities.reflection.annotations.NmsAddedIn;
import org.bukkit.entity.Panda;
import java.util.Random;
/**
* Created by libraryaddict on 6/05/2019.
*/
@ -21,8 +20,8 @@ public class PandaWatcher extends AgeableWatcher {
if (DisguiseConfig.isRandomDisguises()) {
// We don't do 'setGene' here so it's just as random as it would be as if it was natural.
setMainGene(Panda.Gene.values()[new Random().nextInt(Panda.Gene.values().length)]);
setHiddenGene(Panda.Gene.values()[new Random().nextInt(Panda.Gene.values().length)]);
setMainGene(ReflectionManager.randomEnum(Panda.Gene.class));
setHiddenGene(ReflectionManager.randomEnum(Panda.Gene.class));
}
}

View File

@ -3,8 +3,8 @@ package me.libraryaddict.disguise.disguisetypes.watchers;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.MetaIndex;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.parser.RandomDefaultValue;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.entity.Rabbit;
public class RabbitWatcher extends AgeableWatcher {
@ -13,7 +13,7 @@ public class RabbitWatcher extends AgeableWatcher {
super(disguise);
if (DisguiseConfig.isRandomDisguises()) {
setType(Rabbit.Type.values()[DisguiseUtilities.random.nextInt(Rabbit.Type.values().length)]);
setType(ReflectionManager.randomEnum(Rabbit.Type.class));
}
}

View File

@ -4,7 +4,7 @@ import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.MetaIndex;
import me.libraryaddict.disguise.utilities.parser.RandomDefaultValue;
import org.apache.commons.lang.math.RandomUtils;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.DyeColor;
import org.bukkit.entity.TropicalFish;
@ -60,9 +60,9 @@ public class TropicalFishWatcher extends FishWatcher {
super(disguise);
if (DisguiseConfig.isRandomDisguises()) {
this.setPattern(TropicalFish.Pattern.values()[RandomUtils.nextInt(TropicalFish.Pattern.values().length)]);
this.setBodyColor(DyeColor.values()[RandomUtils.nextInt(DyeColor.values().length)]);
this.setPatternColor(DyeColor.values()[RandomUtils.nextInt(DyeColor.values().length)]);
setPattern(ReflectionManager.randomEnum(TropicalFish.Pattern.class));
setBodyColor(ReflectionManager.randomEnum(DyeColor.class));
setPatternColor(ReflectionManager.randomEnum(DyeColor.class));
}
}

View File

@ -9,22 +9,21 @@ import me.libraryaddict.disguise.disguisetypes.MetaIndex;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.parser.RandomDefaultValue;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import me.libraryaddict.disguise.utilities.reflection.annotations.NmsAddedIn;
import org.bukkit.entity.Villager;
import org.bukkit.entity.Villager.Profession;
import java.util.Locale;
public class VillagerWatcher extends AbstractVillagerWatcher {
public VillagerWatcher(Disguise disguise) {
super(disguise);
if (DisguiseConfig.isRandomDisguises()) {
setProfession(Profession.values()[DisguiseUtilities.random.nextInt(Profession.values().length)]);
setProfession(ReflectionManager.randomEnum(Villager.Profession.class));
if (NmsVersion.v1_14.isSupported()) {
setBiome(Villager.Type.values()[DisguiseUtilities.random.nextInt(Villager.Type.values().length)]);
setBiome(ReflectionManager.randomEnum(Villager.Type.class));
setLevel(DisguiseUtilities.random.nextInt(5) + 1);
}
}
@ -42,20 +41,20 @@ public class VillagerWatcher extends AbstractVillagerWatcher {
}
public Profession getProfession() {
if (NmsVersion.v1_14.isSupported()) {
return Profession.valueOf(getVillagerData().getProfession().getName().getKey().toUpperCase(Locale.ENGLISH));
if (!NmsVersion.v1_14.isSupported()) {
return ReflectionManager.fromEnum(Profession.class, getData(MetaIndex.VILLAGER_PROFESSION) + 1);
}
return Profession.values()[getData(MetaIndex.VILLAGER_PROFESSION) + 1];
return ReflectionManager.fromEnum(Profession.class, getVillagerData().getProfession().getName().toString());
}
@RandomDefaultValue
public void setProfession(Profession profession) {
if (NmsVersion.v1_14.isSupported()) {
setVillagerData(new VillagerData(VillagerTypes.getByName(getType().getKey().toString()),
VillagerProfessions.getByName(profession.getKey().toString()), getLevel()));
setVillagerData(new VillagerData(VillagerTypes.getByName(ReflectionManager.keyedName(getType())),
VillagerProfessions.getByName(ReflectionManager.keyedName(profession)), getLevel()));
} else {
setData(MetaIndex.VILLAGER_PROFESSION, profession.ordinal() - 1);
setData(MetaIndex.VILLAGER_PROFESSION, ReflectionManager.enumOrdinal(profession) - 1);
sendData(MetaIndex.VILLAGER_PROFESSION);
}
}
@ -63,14 +62,14 @@ public class VillagerWatcher extends AbstractVillagerWatcher {
@Deprecated
@NmsAddedIn(NmsVersion.v1_14)
public Villager.Type getType() {
return Villager.Type.valueOf(getVillagerData().getType().getName().getKey().toUpperCase(Locale.ENGLISH));
return ReflectionManager.fromEnum(Villager.Type.class, getVillagerData().getType().getName().getKey());
}
@Deprecated
@NmsAddedIn(NmsVersion.v1_14)
public void setType(Villager.Type type) {
setVillagerData(new VillagerData(VillagerTypes.getByName(type.getKey().toString()),
VillagerProfessions.getByName(getProfession().getKey().toString()), getLevel()));
setVillagerData(new VillagerData(VillagerTypes.getByName(ReflectionManager.keyedName(type)),
VillagerProfessions.getByName(ReflectionManager.keyedName(getProfession())), getLevel()));
}
@NmsAddedIn(NmsVersion.v1_14)
@ -90,7 +89,7 @@ public class VillagerWatcher extends AbstractVillagerWatcher {
@NmsAddedIn(NmsVersion.v1_14)
public void setLevel(int level) {
setVillagerData(new VillagerData(VillagerTypes.getByName(getType().getKey().toString()),
VillagerProfessions.getByName(getProfession().getKey().toString()), Math.max(1, Math.min(5, level))));
setVillagerData(new VillagerData(VillagerTypes.getByName(ReflectionManager.keyedName(getType())),
VillagerProfessions.getByName(ReflectionManager.keyedName(getProfession())), Math.max(1, Math.min(5, level))));
}
}

View File

@ -1,9 +1,11 @@
package me.libraryaddict.disguise.disguisetypes.watchers;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.AnimalColor;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.MetaIndex;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import me.libraryaddict.disguise.utilities.reflection.annotations.NmsAddedIn;
import me.libraryaddict.disguise.utilities.reflection.annotations.NmsRemovedIn;
import org.bukkit.DyeColor;
@ -13,6 +15,12 @@ public class WolfWatcher extends TameableWatcher {
public WolfWatcher(Disguise disguise) {
super(disguise);
if (DisguiseConfig.isRandomDisguises()) {
if (NmsVersion.v1_20_R4.isSupported()) {
setVariant(ReflectionManager.randomEnum(Wolf.Variant.class));
}
}
}
public DyeColor getCollarColor() {

View File

@ -8,20 +8,18 @@ import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.MetaIndex;
import me.libraryaddict.disguise.utilities.parser.RandomDefaultValue;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import me.libraryaddict.disguise.utilities.reflection.annotations.NmsAddedIn;
import org.bukkit.entity.Villager;
import org.bukkit.entity.Villager.Profession;
import java.util.Locale;
import java.util.Random;
public class ZombieVillagerWatcher extends ZombieWatcher {
public ZombieVillagerWatcher(Disguise disguise) {
super(disguise);
if (DisguiseConfig.isRandomDisguises()) {
setProfession(Profession.values()[new Random().nextInt(Profession.values().length)]);
setProfession(ReflectionManager.randomEnum(Profession.class));
}
}
@ -60,19 +58,19 @@ public class ZombieVillagerWatcher extends ZombieWatcher {
public Profession getProfession() {
if (NmsVersion.v1_14.isSupported()) {
return Profession.valueOf(getVillagerData().getProfession().getName().getKey().toUpperCase(Locale.ENGLISH));
return ReflectionManager.fromEnum(Profession.class, getVillagerData().getProfession().getName().getKey());
}
return Profession.values()[getData(MetaIndex.ZOMBIE_VILLAGER_PROFESSION_OLD) + 1];
return ReflectionManager.fromEnum(Profession.class, getData(MetaIndex.ZOMBIE_VILLAGER_PROFESSION_OLD) + 1);
}
@RandomDefaultValue
public void setProfession(Profession profession) {
if (NmsVersion.v1_14.isSupported()) {
setVillagerData(new VillagerData(VillagerTypes.getByName(getType().getKey().toString()),
VillagerProfessions.getByName(profession.getKey().toString()), getLevel()));
setVillagerData(new VillagerData(VillagerTypes.getByName(ReflectionManager.keyedName(getType())),
VillagerProfessions.getByName(ReflectionManager.keyedName(profession)), getLevel()));
} else {
setData(MetaIndex.ZOMBIE_VILLAGER_PROFESSION_OLD, profession.ordinal() - 1);
setData(MetaIndex.ZOMBIE_VILLAGER_PROFESSION_OLD, ReflectionManager.enumOrdinal(profession) - 1);
sendData(MetaIndex.ZOMBIE_VILLAGER_PROFESSION_OLD);
}
}
@ -80,14 +78,14 @@ public class ZombieVillagerWatcher extends ZombieWatcher {
@Deprecated
@NmsAddedIn(NmsVersion.v1_14)
public Villager.Type getType() {
return Villager.Type.valueOf(getVillagerData().getType().getName().getKey().toUpperCase(Locale.ENGLISH));
return ReflectionManager.fromEnum(Villager.Type.class, getVillagerData().getType().getName().getKey());
}
@Deprecated
@NmsAddedIn(NmsVersion.v1_14)
public void setType(Villager.Type type) {
setVillagerData(new VillagerData(VillagerTypes.getByName(type.getKey().toString()),
VillagerProfessions.getByName(getProfession().getKey().toString()), getLevel()));
setVillagerData(new VillagerData(VillagerTypes.getByName(ReflectionManager.keyedName(type)),
VillagerProfessions.getByName(ReflectionManager.keyedName(getProfession())), getLevel()));
}
@NmsAddedIn(NmsVersion.v1_14)
@ -107,7 +105,7 @@ public class ZombieVillagerWatcher extends ZombieWatcher {
@NmsAddedIn(NmsVersion.v1_14)
public void setLevel(int level) {
setVillagerData(new VillagerData(VillagerTypes.getByName(getType().getKey().toString()),
VillagerProfessions.getByName(getProfession().getKey().toString()), Math.max(1, Math.min(5, level))));
setVillagerData(new VillagerData(VillagerTypes.getByName(ReflectionManager.keyedName(getType())),
VillagerProfessions.getByName(ReflectionManager.keyedName(getProfession())), Math.max(1, Math.min(5, level))));
}
}

View File

@ -60,6 +60,7 @@ import org.apache.commons.lang.StringUtils;
import org.bukkit.Art;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Keyed;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
@ -1413,13 +1414,13 @@ public class ReflectionManager {
return (T) nmsReflection.getCatTypeFromInt((int) value);
}
return (T) Cat.Type.values()[(int) value];
return (T) fromEnum(Cat.Type.class, (int) value);
} else if (index == MetaIndex.FROG_VARIANT) {
if (nmsReflection != null) {
return (T) nmsReflection.getFrogVariantFromInt((int) value);
}
return (T) Frog.Variant.values()[(int) value];
return (T) fromEnum(Frog.Variant.class, (int) value);
} else if (index == MetaIndex.PAINTING) {
return (T) nmsReflection.getPaintingFromInt((int) value);
} else if (index == MetaIndex.WOLF_VARIANT) {
@ -1450,23 +1451,23 @@ public class ReflectionManager {
}*/ else if (index == MetaIndex.AREA_EFFECT_CLOUD_COLOR) {
return (T) Color.fromRGB((int) value);
} else if (index == MetaIndex.PARROT_VARIANT) {
return (T) Parrot.Variant.values()[(int) value];
return (T) fromEnum(Parrot.Variant.class, (int) value);
} else if (index == MetaIndex.AXOLOTL_VARIANT) {
return (T) Axolotl.Variant.values()[(int) value];
return (T) fromEnum(Axolotl.Variant.class, (int) value);
} else if (index == MetaIndex.OCELOT_TYPE) {
return (T) Ocelot.Type.values()[(int) value];
return (T) fromEnum(Ocelot.Type.class, (int) value);
} else if (index == MetaIndex.FOX_TYPE) {
return (T) Fox.Type.values()[(int) value];
return (T) fromEnum(Fox.Type.class, (int) value);
} else if (index == MetaIndex.RABBIT_TYPE) {
return (T) RabbitType.getType((int) value);
} else if (index == MetaIndex.LLAMA_COLOR) {
return (T) Llama.Color.values()[(int) value];
return (T) fromEnum(Llama.Color.class, (int) value);
} else if (index == MetaIndex.PANDA_MAIN_GENE || index == MetaIndex.PANDA_HIDDEN_GENE) {
return (T) Panda.Gene.values()[(byte) value];
return (T) fromEnum(Panda.Gene.class, (byte) value);
} else if (index == MetaIndex.ITEM_DISPLAY_TRANSFORM) {
return (T) ItemDisplay.ItemDisplayTransform.values()[(byte) value];
return (T) fromEnum(ItemDisplay.ItemDisplayTransform.class, (byte) value);
} else if (index == MetaIndex.BOAT_TYPE_NEW) {
return (T) Boat.Type.values()[(int) value];
return (T) fromEnum(Boat.Type.class, (int) value);
}
return (T) value;
@ -1499,7 +1500,7 @@ public class ReflectionManager {
return nmsReflection.getCatVariantAsInt((Cat.Type) value);
}
return ((Cat.Type) value).ordinal();
return enumOrdinal(value);
}
if (NmsVersion.v1_19_R1.isSupported()) {
@ -1508,7 +1509,7 @@ public class ReflectionManager {
} else if (value instanceof Art) {
return nmsReflection.getPaintingAsInt((Art) value);
} else if (value instanceof Boat.Type) {
return ((Boat.Type) value).ordinal();
return enumOrdinal(value);
}
if (NmsVersion.v1_20_R4.isSupported()) {
@ -1529,7 +1530,7 @@ public class ReflectionManager {
return RabbitType.getTypeId((Rabbit.Type) value);
} else if (value instanceof Enum && !(value instanceof SnifferState || value instanceof EntityPose || value instanceof BlockFace ||
value instanceof ArmadilloState)) {
int v = ((Enum) value).ordinal();
int v = enumOrdinal(value);
if (index.isByteValues()) {
return (byte) v;
@ -1807,7 +1808,7 @@ public class ReflectionManager {
try {
if (!NmsVersion.v1_13.isSupported()) {
return itemStack.getType().ordinal() + (itemStack.getDurability() << 12);
return enumOrdinal(itemStack.getType()) + (itemStack.getDurability() << 12);
}
Object nmsBlock = magicGetBlock.invoke(null, itemStack.getType());
@ -2514,4 +2515,62 @@ public class ReflectionManager {
return null;
}
public static int enumOrdinal(Object obj) {
return ((Enum) obj).ordinal();
}
public static String enumName(Object obj) {
if (obj instanceof Enum) {
return ((Enum) obj).name();
}
return ((Keyed) obj).getKey().getKey();
}
public static String keyedName(Object obj) {
return ((Keyed) obj).getKey().toString();
}
public static <T> T randomEnum(Class<T> clss) {
if (clss.isEnum()) {
T[] enums = clss.getEnumConstants();
return enums[DisguiseUtilities.getRandom().nextInt(enums.length)];
}
List<Keyed> enums = Bukkit.getRegistry((Class<Keyed>) clss).stream().collect(Collectors.toList());
return (T) enums.get(DisguiseUtilities.getRandom().nextInt(enums.size()));
}
public static <T> T fromEnum(Class<T> clss, int ordinal) {
if (!clss.isEnum()) {
throw new IllegalStateException("Attempted to convert " + clss +
" to an enum and use the ordinal, but that shouldn't be used in newer versions of Minecraft. This is a bug in Lib's " +
"Disguises, please report to libraryaddict");
}
return clss.getEnumConstants()[Math.max(0, ordinal) % clss.getEnumConstants().length];
}
public static <T> T fromEnum(Class<T> clss, String name) {
String[] split = name.split(":");
if (split.length != 2) {
split = new String[]{"minecraft", name};
}
if (clss.isEnum()) {
for (T e : clss.getEnumConstants()) {
if (!((Enum) e).name().equalsIgnoreCase(split[1])) {
continue;
}
return e;
}
}
return (T) Bukkit.getRegistry((Class<Keyed>) clss).get(new NamespacedKey(split[0], split[1]));
}
}

View File

@ -24,8 +24,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<lombok.version>1.18.32</lombok.version>
<packetevents.version>2.4.0-SNAPSHOT</packetevents.version>
<spigot-api.version>1.21-R0.1-20240704.105621-53</spigot-api.version>
<packetevents.version>2.4.0</packetevents.version>
<spigot.version>1.21-R0.1-SNAPSHOT</spigot.version>
<junit.version>4.13.2</junit.version>
<junit-jupiter.version>5.9.3</junit-jupiter.version>
@ -82,7 +81,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>${spigot-api.version}</version>
<version>${spigot.version}</version>
<optional>true</optional>
</dependency>
<dependency>