Skip to content

Commit

Permalink
Registry - more suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneBeee committed May 25, 2024
1 parent ae46237 commit ee3558a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@
package ch.njol.skript.classes.registry;

import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.expressions.base.EventValueExpression;
import ch.njol.skript.lang.DefaultExpression;
import ch.njol.skript.lang.ParseContext;
import org.bukkit.Keyed;
import org.bukkit.Registry;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* This class can be used for easily creating ClassInfos for {@link Registry}s.
Expand All @@ -46,28 +42,12 @@ public RegistryClassInfo(Class<R> registryClass, Registry<R> registry, String co
*/
public RegistryClassInfo(Class<R> registryClass, Registry<R> registry, String codeName, String languageNode, DefaultExpression<R> defaultExpression) {
super(registryClass, codeName);
RegistryUtils<R> registryUtils = new RegistryUtils<>(registry, languageNode);
RegistryParser<R> registryUtils = new RegistryParser<>(registry, languageNode);
usage(registryUtils.getAllNames())
.supplier(registry::iterator)
.serializer(new RegistrySerializer<R>(registry))
.defaultExpression(defaultExpression)
.parser(new Parser<R>() {

@Override
public @Nullable R parse(String string, ParseContext context) {
return registryUtils.parse(string);
}

@Override
public @NotNull String toString(R object, int flags) {
return registryUtils.toString(object, flags);
}

@Override
public @NotNull String toVariableNameString(R object) {
return toString(object, 0);
}
});
.parser(new RegistryParser<R>(registry, codeName));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,35 @@
*/
package ch.njol.skript.classes.registry;

import ch.njol.skript.classes.Parser;
import ch.njol.skript.localization.Language;
import ch.njol.skript.localization.Noun;
import ch.njol.util.NonNullPair;
import ch.njol.util.StringUtils;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
* Utility class for {@link Registry}
* A parser based on a {@link Registry} used to parse data from a string or turn data into a string.
*
* @param <R> Registry class
*/
public class RegistryUtils<R extends Keyed> {
public class RegistryParser<R extends Keyed> extends Parser<R> {

private final Registry<R> registry;
private final String languageNode;

private final Map<R, String> names = new HashMap<>();
private final Map<String, R> parseMap = new HashMap<>();

public RegistryUtils(Registry<R> registry, String languageNode) {
public RegistryParser(Registry<R> registry, String languageNode) {
assert !languageNode.isEmpty() && !languageNode.endsWith(".") : languageNode;
this.registry = registry;
this.languageNode = languageNode;
Expand Down Expand Up @@ -119,10 +121,21 @@ public R parse(String input) {
* @param flags not currently used
* @return A string representation of the registry object.
*/
public String toString(R object, int flags) {
public @NotNull String toString(R object, int flags) {
return names.get(object);
}

/**
* Returns a registry object's string representation in a variable name.
*
* @param object Object to represent in a variable name.
* @return The given object's representation in a variable name.
*/
@Override
public @NotNull String toVariableNameString(R object) {
return toString(object, 0);
}

/**
* @return A comma-separated string containing a list of all names representing the registry.
* Note that some entries may represent the same registry object.
Expand Down

0 comments on commit ee3558a

Please sign in to comment.