Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interfaceification fixes #2635

Merged
merged 10 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.utilities.LegacyNamingHelper;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import org.bukkit.entity.Villager;
Expand Down Expand Up @@ -56,8 +57,9 @@ public boolean matches(ScriptPath path) {

@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (exactMatchesEnum(determinationObj.toString(), Villager.Profession.values())) {
Villager.Profession newProfession = Villager.Profession.valueOf(determinationObj.toString().toUpperCase());
// TODO This technically has registries on all supported versions
Villager.Profession newProfession = LegacyNamingHelper.convert(Villager.Profession.class, determinationObj.toString());
if (newProfession != null) {
event.setProfession(newProfession);
return true;
}
Expand All @@ -70,9 +72,9 @@ public ObjectTag getContext(String name) {
case "entity":
return entity;
case "reason":
return new ElementTag(event.getReason().toString());
return new ElementTag(event.getReason());
case "profession":
return new ElementTag(event.getProfession().toString());
return new ElementTag(String.valueOf(event.getProfession()), true);
}
return super.getContext(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.properties.bukkit.BukkitColorExtensions;
import com.denizenscript.denizen.utilities.LegacyNamingHelper;
import com.denizenscript.denizen.utilities.MultiVersionHelper1_19;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ColorTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Color;
import org.bukkit.DyeColor;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.*;
import org.bukkit.entity.*;

import java.util.Arrays;
Expand Down Expand Up @@ -150,12 +149,13 @@ else if (type == EntityType.FOX && mechanism.requireEnum(Fox.Type.class)) {
else if (type == EntityType.CAT && mechanism.requireObject(ListTag.class)) {
Cat cat = as(Cat.class);
ListTag list = mechanism.valueAsType(ListTag.class);
ElementTag catType = new ElementTag(list.get(0));
if (catType.matchesEnum(Cat.Type.class)) {
cat.setCatType(catType.asEnum(Cat.Type.class));
String catTypeStr = list.get(0);
Cat.Type catType = LegacyNamingHelper.convert(Cat.Type.class, catTypeStr);
if (catType != null) {
cat.setCatType(catType);
}
else {
mechanism.echoError("Invalid cat type specified: " + catType);
mechanism.echoError("Invalid cat type specified: " + catTypeStr);
}
if (list.size() > 1) {
ElementTag collarColor = new ElementTag(list.get(1));
Expand Down Expand Up @@ -187,11 +187,12 @@ else if (type == EntityType.PANDA && mechanism.requireObject(ListTag.class)) {
}
}
}
else if (type == EntityType.VILLAGER && mechanism.requireEnum(Villager.Type.class)) {
as(Villager.class).setVillagerType(color.asEnum(Villager.Type.class));
// TODO This technically has registries on all supported versions
else if (type == EntityType.VILLAGER) {
LegacyNamingHelper.requireType(mechanism, Villager.Type.class).ifPresent(as(Villager.class)::setVillagerType);
}
else if (type == EntityType.ZOMBIE_VILLAGER && mechanism.requireEnum(Villager.Type.class)) {
as(ZombieVillager.class).setVillagerType(color.asEnum(Villager.Type.class));
else if (type == EntityType.ZOMBIE_VILLAGER) {
LegacyNamingHelper.requireType(mechanism, Villager.Type.class).ifPresent(as(ZombieVillager.class)::setVillagerType);
}
else if (type == EntityType.ARROW && mechanism.requireObject(ColorTag.class)) {
as(Arrow.class).setColor(BukkitColorExtensions.getColor(mechanism.valueAsType(ColorTag.class)));
Expand Down Expand Up @@ -242,14 +243,16 @@ public String getColor(boolean includeDeprecated) {
case FOX -> as(Fox.class).getFoxType().name();
case CAT -> {
Cat cat = as(Cat.class);
yield cat.getCatType().name() + "|" + cat.getCollarColor().name();
// TODO once 1.21 is the minimum supported version, replace with direct registry-based handling
yield cat.getCatType() + "|" + cat.getCollarColor().name();
}
case PANDA -> {
Panda panda = as(Panda.class);
yield panda.getMainGene().name() + "|" + panda.getHiddenGene().name();
}
case VILLAGER -> as(Villager.class).getVillagerType().name();
case ZOMBIE_VILLAGER -> as(ZombieVillager.class).getVillagerType().name();
// TODO This technically has registries on all supported versions
case VILLAGER -> String.valueOf(as(Villager.class).getVillagerType());
case ZOMBIE_VILLAGER -> String.valueOf(as(ZombieVillager.class).getVillagerType());
case ARROW -> {
try {
yield BukkitColorExtensions.fromColor(as(Arrow.class).getColor()).identify();
Expand All @@ -264,12 +267,13 @@ public String getColor(boolean includeDeprecated) {
};
}

public static ListTag listForEnum(Enum<?>[] values) {
ListTag list = new ListTag(values.length);
for (Enum<?> obj : values) {
list.addObject(new ElementTag(obj));
// TODO once 1.21 is the minimum supported version, replace with direct registry-based handling
@SuppressWarnings({"unchecked", "DataFlowIssue"})
public static ListTag listTypes(Class<?> type) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_21) && Keyed.class.isAssignableFrom(type)) {
return Utilities.registryKeys(Bukkit.getRegistry((Class<? extends Keyed>) type));
}
return list;
return new ListTag(Arrays.asList(((Class<? extends Enum<?>>) type).getEnumConstants()), ElementTag::new);
}

public ListTag getAllowedColors() {
Expand All @@ -278,34 +282,35 @@ public ListTag getAllowedColors() {
return MultiVersionHelper1_19.getAllowedColors(type);
}
if (type == MOOSHROOM_ENTITY_TYPE) {
return listForEnum(MushroomCow.Variant.values());
return listTypes(MushroomCow.Variant.class);
}
return switch (type) {
case HORSE -> {
ListTag horseColors = listForEnum(Horse.Color.values());
horseColors.addAll(listForEnum(Horse.Style.values()));
ListTag horseColors = listTypes(Horse.Color.class);
horseColors.addAll(listTypes(Horse.Style.class));
yield horseColors;
}
case SHEEP, WOLF, SHULKER -> listForEnum(DyeColor.values());
case RABBIT -> listForEnum(Rabbit.Type.values());
case LLAMA, TRADER_LLAMA -> listForEnum(Llama.Color.values());
case PARROT -> listForEnum(Parrot.Variant.values());
case SHEEP, WOLF, SHULKER -> listTypes(DyeColor.class);
case RABBIT -> listTypes(Rabbit.Type.class);
case LLAMA, TRADER_LLAMA -> listTypes(Llama.Color.class);
case PARROT -> listTypes(Parrot.Variant.class);
case TROPICAL_FISH -> {
ListTag patterns = listForEnum(TropicalFish.Pattern.values());
patterns.addAll(listForEnum(DyeColor.values()));
ListTag patterns = listTypes(TropicalFish.Pattern.class);
patterns.addAll(listTypes(DyeColor.class));
yield patterns;
}
case FOX -> listForEnum(Fox.Type.values());
case CAT -> listForEnum(Cat.Type.values());
case PANDA -> listForEnum(Panda.Gene.values());
case VILLAGER, ZOMBIE_VILLAGER -> listForEnum(Villager.Type.values());
case FOX -> listTypes(Fox.Type.class);
case CAT -> listTypes(Cat.Type.class);
case PANDA -> listTypes(Panda.Gene.class);
// TODO This technically has registries on all supported versions
case VILLAGER, ZOMBIE_VILLAGER -> listTypes(Villager.Type.class);
case GOAT -> {
ListTag result = new ListTag();
result.add("screaming");
result.add("normal");
yield result;
}
case AXOLOTL -> EntityColor.listForEnum(Axolotl.Variant.values());
case AXOLOTL -> EntityColor.listTypes(Axolotl.Variant.class);
default -> null; // includes Ocelot (deprecated) and arrow (ColorTag)
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.denizenscript.denizen.objects.properties.entity;

import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.utilities.LegacyNamingHelper;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
Expand All @@ -13,6 +14,7 @@

public class EntityProfession implements Property {

// TODO This technically has registries on all supported versions
public static boolean describes(ObjectTag entity) {
if (!(entity instanceof EntityTag)) {
return false;
Expand Down Expand Up @@ -62,7 +64,7 @@ public void setProfession(Villager.Profession profession) {

@Override
public String getPropertyString() {
return CoreUtilities.toLowerCase(getProfession().name());
return CoreUtilities.toLowerCase(String.valueOf(getProfession()));
}

@Override
Expand All @@ -88,7 +90,7 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
// For the list of possible professions, refer to <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/Villager.Profession.html>
// -->
if (attribute.startsWith("profession")) {
return new ElementTag(getProfession())
return new ElementTag(String.valueOf(getProfession()), true)
.getObjectAttribute(attribute.fulfill(1));
}

Expand All @@ -109,8 +111,8 @@ public void adjust(Mechanism mechanism) {
// @tags
// <EntityTag.profession>
// -->
if (mechanism.matches("profession") && mechanism.requireEnum(Villager.Profession.class)) {
setProfession(Villager.Profession.valueOf(mechanism.getValue().asString().toUpperCase()));
if (mechanism.matches("profession")) {
LegacyNamingHelper.requireType(mechanism, Villager.Profession.class).ifPresent(this::setProfession);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,16 @@ else if (param.shouldBeType(EntityTag.class)) {
// Generally used with <@link command map> and <@link language Map Script Containers>.
// This is only their Bukkit enum names, as seen at <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/map/MapCursor.Type.html>.
// -->
registerEnumListTag("map_cursor_types", MapCursor.Type.class, "list_map_cursor_types");
// TODO once 1.20 is the minimum supported version, replace with direct registry-based handling
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_20)) {
tagProcessor.registerStaticTag(ListTag.class, "map_cursor_types", (attribute, object) -> {
listDeprecateWarn(attribute);
return Utilities.registryKeys(Registry.MAP_DECORATION_TYPE);
}, "list_map_cursor_types");
}
else {
registerEnumListTag("map_cursor_types", (Class<? extends Enum<?>>) (Class<?>) MapCursor.Type.class, "list_map_cursor_types");
}

// <--[tag]
// @attribute <server.world_types>
Expand Down Expand Up @@ -972,7 +981,7 @@ else if (param.shouldBeType(EntityTag.class)) {
// For locating specific structures, see <@link language Structure lookups>.
// -->
tagProcessor.registerTag(ListTag.class, "structures", (attribute, object) -> {
return new ListTag(Registry.STRUCTURE.stream().toList(), structure -> new ElementTag(Utilities.namespacedKeyToString(structure.getKey()), true));
return Utilities.registryKeys(Registry.STRUCTURE);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,38 @@

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.DebugInternals;
import org.bukkit.Bukkit;
import org.bukkit.Keyed;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

public class LegacyNamingHelper<T extends Enum<T>> {

// TODO once 1.21 is the minimum supported version, replace with direct registry-based handling
@SuppressWarnings({"unchecked", "rawtypes", "DataFlowIssue"})
public static <T extends Keyed> T convert(Class<T> type, String string) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_21)) {
return Bukkit.getRegistry(type).get(Utilities.parseNamespacedKey(string));
}
return (T) ElementTag.asEnum((Class<? extends Enum>) type, string);
}

public static <T extends Keyed> Optional<T> requireType(Mechanism mechanism, Class<T> type) {
T converted = convert(type, mechanism.getValue().asString());
if (converted == null) {
mechanism.echoError("Invalid " + DebugInternals.getClassNameOpti(type) + " specified: must specify a valid name.");
return Optional.empty();
}
return Optional.of(converted);
}

private final Set<String> modernNames;
private final Class<T> enumType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public static boolean colorIsApplicable(EntityType type) {
return type == EntityType.FROG || type == EntityType.BOAT || type == EntityType.CHEST_BOAT;
}

// TODO Frog variants technically have registries on all supported versions
public static String getColor(Entity entity) {
if (entity instanceof Frog frog) {
return frog.getVariant().name();
return String.valueOf(frog.getVariant());
}
else if (entity instanceof Boat boat) {
return boat.getBoatType().name();
Expand All @@ -28,17 +29,17 @@ else if (entity instanceof Boat boat) {

public static ListTag getAllowedColors(EntityType type) {
if (type == EntityType.FROG) {
return EntityColor.listForEnum(Frog.Variant.values());
return EntityColor.listTypes(Frog.Variant.class);
}
else if (type == EntityType.BOAT || type == EntityType.CHEST_BOAT) {
return EntityColor.listForEnum(Boat.Type.values());
return EntityColor.listTypes(Boat.Type.class);
}
return null;
}

public static void setColor(Entity entity, Mechanism mech) {
if (entity instanceof Frog frog && mech.requireEnum(Frog.Variant.class)) {
frog.setVariant(mech.getValue().asEnum(Frog.Variant.class));
if (entity instanceof Frog frog) {
LegacyNamingHelper.requireType(mech, Frog.Variant.class).ifPresent(frog::setVariant);
}
else if (entity instanceof Boat boat && mech.requireEnum(Boat.Type.class)) {
boat.setBoatType(mech.getValue().asEnum(Boat.Type.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.core.ScriptTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.tags.TagManager;
Expand Down Expand Up @@ -54,6 +56,10 @@ public static String namespacedKeyToString(NamespacedKey key) {
return key.getNamespace().equals(NamespacedKey.MINECRAFT) ? key.getKey() : key.toString();
}

public static ListTag registryKeys(Registry<?> registry) {
return new ListTag(registry.stream().toList(), keyed -> new ElementTag(namespacedKeyToString(keyed.getKey()), true));
}

public static boolean matchesNamespacedKeyButCaseInsensitive(String input) {
int colonIndex = input.indexOf(':');
if (colonIndex == -1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.denizenscript.denizen.utilities.maps;

import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.LegacyNamingHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapView;
Expand All @@ -26,7 +27,7 @@ public byte getDirection(PlayerTag player) {
}

public org.bukkit.map.MapCursor.Type getType(PlayerTag player) {
return org.bukkit.map.MapCursor.Type.valueOf(tag(typeTag, player).toUpperCase());
return LegacyNamingHelper.convert(org.bukkit.map.MapCursor.Type.class, tag(typeTag, player));
}

private byte yawToDirection(double yaw) {
Expand Down