Skip to content
Open
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
28 changes: 18 additions & 10 deletions src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;

import io.papermc.paper.registry.RegistryKey;
import org.bukkit.Keyed;
import org.bukkit.Registry;
import org.bukkit.inventory.EquipmentSlot;
Expand Down Expand Up @@ -89,8 +90,25 @@ public static EquipmentSlot getEquipmentSlotFromIndex(int slotIndex) {
String codeName,
String languageNode
) {
// validate class
if (!Skript.classExists(classPath))
return null;
Class<R> registryClass;
try {
//noinspection unchecked
registryClass = (Class<R>) Class.forName(classPath);
} catch (ClassNotFoundException e) {
Skript.debug("Could not retrieve the class with the path: '" + classPath + "'.");
throw new RuntimeException(e);
}

// first, try better RegistryKey
if (PaperUtils.registryExists(registryName)) {
RegistryKey<R> registryKey = PaperUtils.getBukkitRegistryKey(registryName);
return new RegistryClassInfo<>(registryClass, registryKey, codeName, languageNode);
}

// otherwise, standard Registry
Registry<R> registry = null;
if (BukkitUtils.registryExists(registryName)) {
try {
Expand All @@ -99,18 +117,8 @@ public static EquipmentSlot getEquipmentSlotFromIndex(int slotIndex) {
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
} else if (PaperUtils.registryExists(registryName)) {
registry = PaperUtils.getBukkitRegistry(registryName);
}
if (registry != null) {
Class<R> registryClass;
try {
//noinspection unchecked
registryClass = (Class<R>) Class.forName(classPath);
} catch (ClassNotFoundException e) {
Skript.debug("Could not retrieve the class with the path: '" + classPath + "'.");
throw new RuntimeException(e);
}
return new RegistryClassInfo<>(registryClass, registry, codeName, languageNode);
}
Skript.debug("There were no registries found for '" + registryName + "'.");
Expand Down
1 change: 0 additions & 1 deletion src/main/java/ch/njol/skript/classes/ClassInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ public ClassInfo<T> user(final String... userInputPatterns) throws PatternSyntax
* @see SimpleLiteral
*/
public ClassInfo<T> defaultExpression(final DefaultExpression<T> defaultExpression) {
assert this.defaultExpression == null;
if (!defaultExpression.isDefault())
throw new IllegalArgumentException("defaultExpression.isDefault() must return true for the default expression of a class");
this.defaultExpression = defaultExpression;
Expand Down
31 changes: 6 additions & 25 deletions src/main/java/ch/njol/skript/classes/data/BukkitClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.util.BlockUtils;
import ch.njol.yggdrasil.Fields;
import io.papermc.paper.registry.RegistryKey;
import io.papermc.paper.world.MoonPhase;
import net.kyori.adventure.text.Component;
import org.bukkit.*;
Expand Down Expand Up @@ -326,7 +327,7 @@ public String toVariableNameString(InventoryHolder holder) {
.since("2.0")
.changer(DefaultChangers.itemChanger));

Classes.registerClass(new RegistryClassInfo<>(Biome.class, Registry.BIOME, "biome", "biomes")
Classes.registerClass(new RegistryClassInfo<>(Biome.class, RegistryKey.BIOME, "biome", "biomes")
.user("biomes?")
.name("Biome")
.description("All possible biomes Minecraft uses to generate a world.",
Expand Down Expand Up @@ -600,7 +601,7 @@ public String[] getPatterns() {
ExpressionPropertyHandler.of(GameRule::getName, String.class)
));

Classes.registerClass(new RegistryClassInfo<>(Attribute.class, Registry.ATTRIBUTE, "attributetype", "attribute types")
Classes.registerClass(new RegistryClassInfo<>(Attribute.class, RegistryKey.ATTRIBUTE, "attributetype", "attribute types")
.user("attribute ?types?")
.name("Attribute Type")
.description("Represents the type of an attribute. Note that this type does not contain any numerical values." +
Expand Down Expand Up @@ -659,14 +660,14 @@ public String[] getPatterns() {
.description("Represents a change reason of an <a href='#experience cooldown change event'>experience cooldown change event</a>.")
.since("2.10"));

Classes.registerClass(new RegistryClassInfo<>(Villager.Type.class, Registry.VILLAGER_TYPE, "villagertype", "villager types")
Classes.registerClass(new RegistryClassInfo<>(Villager.Type.class, RegistryKey.VILLAGER_TYPE, "villagertype", "villager types")
.user("villager ?types?")
.name("Villager Type")
.description("Represents the different types of villagers. These are usually the biomes a villager can be from.")
.after("biome")
.since("2.10"));

Classes.registerClass(new RegistryClassInfo<>(Villager.Profession.class, Registry.VILLAGER_PROFESSION, "villagerprofession", "villager professions")
Classes.registerClass(new RegistryClassInfo<>(Villager.Profession.class, RegistryKey.VILLAGER_PROFESSION, "villagerprofession", "villager professions")
.user("villager ?professions?")
.name("Villager Profession")
.description("Represents the different professions of villagers.")
Expand Down Expand Up @@ -705,27 +706,7 @@ public String toVariableNameString(EntitySnapshot snapshot) {
.description("Represents a banner pattern.")
.since("2.10"));

ClassInfo<?> patternTypeInfo;
Registry<PatternType> patternRegistry = Bukkit.getRegistry(PatternType.class);
if (patternRegistry != null) {
patternTypeInfo = new RegistryClassInfo<>(PatternType.class, patternRegistry, "bannerpatterntype", "banner pattern types");
} else {
try {
Class<?> patternClass = Class.forName("org.bukkit.block.banner.PatternType");
if (patternClass.isEnum()) {
//noinspection unchecked,rawtypes
Class<? extends Enum> enumClass = (Class<? extends Enum>) patternClass;
//noinspection rawtypes,unchecked
patternTypeInfo = new EnumClassInfo<>(enumClass, "bannerpatterntype", "banner pattern types");
} else {
throw new IllegalStateException("PatternType is neither an enum nor a valid registry.");
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

Classes.registerClass(patternTypeInfo
Classes.registerClass(new RegistryClassInfo<>(PatternType.class, RegistryKey.BANNER_PATTERN, "bannerpatterntype", "banner pattern types")
.user("banner ?pattern ?types?")
.name("Banner Pattern Type")
.description("Represents the various banner patterns that can be applied to a banner.")
Expand Down
105 changes: 95 additions & 10 deletions src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.expressions.base.EventValueExpression;
import ch.njol.skript.lang.DefaultExpression;
import io.papermc.paper.registry.RegistryAccess;
import io.papermc.paper.registry.RegistryKey;
import org.bukkit.Keyed;
import org.bukkit.Registry;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
* This class can be used for easily creating ClassInfos for {@link Registry}s.
Expand All @@ -16,12 +22,87 @@
*/
public class RegistryClassInfo<R extends Keyed> extends ClassInfo<R> {

private final @Nullable RegistryKey<R> registryKey;

/**
* @param registryClass The registry class
* @param registryKey The registry key
* @param codeName The name used in patterns
* @param languageNode The language node of the type
*/
public RegistryClassInfo(Class<R> registryClass, RegistryKey<R> registryKey, String codeName, String languageNode) {
this(registryClass, registryKey, RegistryAccess.registryAccess().getRegistry(registryKey), codeName,
languageNode, null, null);
}

/**
* @param registryClass The registry class
* @param registryKey The registry key
* @param codeName The name used in patterns
* @param languageNode The language node of the type
* @param parseCallback A consumer to run on a successful parse.
*/
public RegistryClassInfo(Class<R> registryClass, RegistryKey<R> registryKey, String codeName, String languageNode,
Consumer<R> parseCallback) {
this(registryClass, registryKey, RegistryAccess.registryAccess().getRegistry(registryKey), codeName,
languageNode, null, parseCallback);
}

private RegistryClassInfo(Class<R> registryClass, @Nullable RegistryKey<R> registryKey, Registry<R> registry,
String codeName, String languageNode, @Nullable DefaultExpression<R> defaultExpression,
@Nullable Consumer<R> parseCallback) {
super(registryClass, codeName);

if (defaultExpression == null) {
defaultExpression = new EventValueExpression<>(registryClass);
}
if (parseCallback == null) {
parseCallback = ignored -> { };
}

this.registryKey = registryKey;
RegistryParser<R> registryParser = new RegistryParser<>(registry, languageNode, parseCallback);
usage(registryParser.getCombinedPatterns())
.supplier(registry::iterator)
.serializer(new RegistrySerializer<>(registry))
.defaultExpression(defaultExpression)
.parser(registryParser);
}

public @Nullable RegistryKey<R> registryKey() {
return registryKey;
}

@Override
public @NotNull RegistryParser<R> getParser() {
//noinspection ConstantConditions, unchecked
return (RegistryParser<R>) super.getParser();
}

@Override
public @NotNull RegistrySerializer<R> getSerializer() {
//noinspection ConstantConditions, unchecked
return (RegistrySerializer<R>) super.getSerializer();
}

@Override
public @NotNull Supplier<Iterator<R>> getSupplier() {
//noinspection ConstantConditions
return super.getSupplier();
}

/*
* Legacy Constructors
*/

/**
* @param registryClass The registry class
* @param registry The registry
* @param codeName The name used in patterns
* @param languageNode The language node of the type
* @deprecated Use {@link #RegistryClassInfo(Class, RegistryKey, String, String)}.
*/
@Deprecated(since = "INSERT VERSION", forRemoval = true)
public RegistryClassInfo(Class<R> registryClass, Registry<R> registry, String codeName, String languageNode) {
this(registryClass, registry, codeName, languageNode, new EventValueExpression<>(registryClass));
}
Expand All @@ -32,7 +113,9 @@ public RegistryClassInfo(Class<R> registryClass, Registry<R> registry, String co
* @param codeName The name used in patterns
* @param languageNode The language node of the type
* @param parseCallback A consumer to run on a successful parse.
* @deprecated Use {@link #RegistryClassInfo(Class, RegistryKey, String, String, Consumer)}.
*/
@Deprecated(since = "INSERT VERSION", forRemoval = true)
public RegistryClassInfo(Class<R> registryClass, Registry<R> registry, String codeName, String languageNode, Consumer<R> parseCallback) {
this(registryClass, registry, codeName, languageNode, new EventValueExpression<>(registryClass), parseCallback);
}
Expand All @@ -43,8 +126,11 @@ public RegistryClassInfo(Class<R> registryClass, Registry<R> registry, String co
* @param codeName The name used in patterns
* @param languageNode The language node of the type
* @param defaultExpression The default expression of the type
* @deprecated Use {@link #RegistryClassInfo(Class, RegistryKey, String, String)} with {@link #defaultExpression(DefaultExpression)}.
*/
public RegistryClassInfo(Class<R> registryClass, Registry<R> registry, String codeName, String languageNode, DefaultExpression<R> defaultExpression) {
@Deprecated(since = "INSERT VERSION", forRemoval = true)
public RegistryClassInfo(Class<R> registryClass, Registry<R> registry, String codeName, String languageNode,
DefaultExpression<R> defaultExpression) {
this(registryClass, registry, codeName, languageNode, defaultExpression, ignored -> {});
}

Expand All @@ -55,15 +141,13 @@ public RegistryClassInfo(Class<R> registryClass, Registry<R> registry, String co
* @param languageNode The language node of the type
* @param defaultExpression The default expression of the type
* @param parseCallback A consumer to run on a successful parse.
* @deprecated Use {@link #RegistryClassInfo(Class, RegistryKey, String, String, Consumer)}
* with {@link #defaultExpression(DefaultExpression)}.
*/
public RegistryClassInfo(Class<R> registryClass, Registry<R> registry, String codeName, String languageNode, DefaultExpression<R> defaultExpression, Consumer<R> parseCallback) {
super(registryClass, codeName);
RegistryParser<R> registryParser = new RegistryParser<>(registry, languageNode, parseCallback);
usage(registryParser.getCombinedPatterns())
.supplier(registry::iterator)
.serializer(new RegistrySerializer<>(registry))
.defaultExpression(defaultExpression)
.parser(registryParser);
@Deprecated(since = "INSERT VERSION", forRemoval = true)
public RegistryClassInfo(Class<R> registryClass, Registry<R> registry, String codeName, String languageNode,
DefaultExpression<R> defaultExpression, Consumer<R> parseCallback) {
this(registryClass, null, registry, codeName, languageNode, defaultExpression, parseCallback);
}

/**
Expand All @@ -89,7 +173,8 @@ public RegistryClassInfo(Class<R> registryClass, Registry<R> registry, String co
* @deprecated {@code registerComparator} is no longer necessary.
*/
@Deprecated(since = "2.16", forRemoval = true)
public RegistryClassInfo(Class<R> registryClass, Registry<R> registry, String codeName, String languageNode, DefaultExpression<R> defaultExpression, boolean registerComparator) {
public RegistryClassInfo(Class<R> registryClass, Registry<R> registry, String codeName, String languageNode,
DefaultExpression<R> defaultExpression, boolean registerComparator) {
this(registryClass, registry, codeName, languageNode, defaultExpression);
}

Expand Down
13 changes: 2 additions & 11 deletions src/main/java/ch/njol/skript/entity/CatData.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package ch.njol.skript.entity;

import ch.njol.skript.bukkitutil.BukkitUtils;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.EnumClassInfo;
import ch.njol.skript.classes.registry.RegistryClassInfo;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.registrations.Classes;
import ch.njol.util.coll.CollectionUtils;
import com.google.common.collect.Iterators;
import org.bukkit.Registry;
import io.papermc.paper.registry.RegistryKey;
import org.bukkit.entity.Cat;
import org.bukkit.entity.Cat.Type;
import org.jetbrains.annotations.NotNull;
Expand All @@ -22,13 +19,7 @@ public class CatData extends EntityData<Cat> {
private static final Type[] TYPES;

static {
ClassInfo<Type> catTypeClassInfo;
if (BukkitUtils.registryExists("CAT_VARIANT")) {
catTypeClassInfo = new RegistryClassInfo<>(Cat.Type.class, Registry.CAT_VARIANT, "cattype", "cat types");
} else {
//noinspection unchecked, rawtypes - it is an enum on other versions
catTypeClassInfo = new EnumClassInfo<>((Class) Cat.Type.class, "cattype", "cat types");
}
var catTypeClassInfo = new RegistryClassInfo<>(Cat.Type.class, RegistryKey.CAT_VARIANT, "cattype", "cat types");
Classes.registerClass(catTypeClassInfo
.user("cat ?(type|race)s?")
.name("Cat Type")
Expand Down
Loading
Loading