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

Feature/registry classes #6695

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 10 additions & 2 deletions src/main/java/ch/njol/skript/classes/data/BukkitClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.Registry;
import org.bukkit.SoundCategory;
import org.bukkit.World;
import org.bukkit.World.Environment;
Expand Down Expand Up @@ -90,6 +91,7 @@
import ch.njol.skript.classes.EnumClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.classes.Serializer;
import ch.njol.skript.classes.registry.RegistryClassInfo;
import ch.njol.skript.entity.EntityData;
import ch.njol.skript.expressions.ExprDamageCause;
import ch.njol.skript.expressions.base.EventValueExpression;
Expand Down Expand Up @@ -976,8 +978,14 @@ public String toVariableNameString(final ItemStack i) {
.name(ClassInfo.NO_DOC)
.since("2.0")
.changer(DefaultChangers.itemChanger));

Classes.registerClass(new EnumClassInfo<>(Biome.class, "biome", "biomes")

ClassInfo<?> biomeClassInfo;
if (Skript.classExists("org.bukkit.Registry") && Skript.fieldExists(Registry.class, "BIOME")) {
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
biomeClassInfo = new RegistryClassInfo<>(Biome.class, Registry.BIOME, "biome", "biomes");
} else {
biomeClassInfo = new EnumClassInfo<>(Biome.class, "biome", "biomes");
}
Classes.registerClass(biomeClassInfo
.user("biomes?")
.name("Biome")
.description("All possible biomes Minecraft uses to generate a world.")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
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.eclipse.jdt.annotation.Nullable;
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
import org.jetbrains.annotations.NotNull;

public class RegistryClassInfo<R extends Keyed> extends ClassInfo<R> {
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved

public RegistryClassInfo(Class<R> registryClass, Registry<R> registry, String codeName, String languageNode) {
this(registryClass, registry, codeName, languageNode, new EventValueExpression<>(registryClass));
}

/**
* @param registry The registry
* @param codeName The name used in patterns
*/
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);
usage(registryUtils.getAllNames())
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
.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);
}
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.classes.registry;

import ch.njol.skript.classes.Serializer;
import ch.njol.yggdrasil.Fields;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;

import java.io.StreamCorruptedException;

public class RegistrySerializer<R extends Keyed> extends Serializer<R> {

private final Registry<R> registry;

public RegistrySerializer(Registry<R> registry) {
this.registry = registry;
}

@Override
public Fields serialize(R o) {
Fields fields = new Fields();
fields.putPrimitive("name", o.getKey().toString());
return null;
}

@Override
protected R deserialize(Fields fields) {
try {
String name = fields.getAndRemovePrimitive("name", String.class);
NamespacedKey namespacedKey;
if (!name.contains(":")) {
// Old variables
namespacedKey = NamespacedKey.minecraft(name);
} else {
namespacedKey = NamespacedKey.fromString(name);
}
if (namespacedKey == null)
return null;

ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
return registry.get(namespacedKey);
} catch (StreamCorruptedException e) {
return null;
}
}

@Override
public boolean mustSyncDeserialization() {
return false;
}

@Override
protected boolean canBeInstantiated() {
return false;
}

@Override
public void deserialize(R o, Fields f) {
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
}

}
129 changes: 129 additions & 0 deletions src/main/java/ch/njol/skript/classes/registry/RegistryUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.classes.registry;

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.eclipse.jdt.annotation.Nullable;

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

public class RegistryUtils<R extends Keyed> {
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved

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) {
assert !languageNode.isEmpty() && !languageNode.endsWith(".") : languageNode;
this.registry = registry;
this.languageNode = languageNode;
refresh();
Language.addListener(this::refresh);
}

void refresh() {
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
names.clear();
parseMap.clear();
for (R registryObject : registry) {
NamespacedKey namespacedKey = registryObject.getKey();
String namespace = namespacedKey.getNamespace();
String key = namespacedKey.getKey();
String keyWithSpaces = key.replace("_", " ");
String languageKey = languageNode + "." + key;

// Put the full namespaced key as a pattern
parseMap.put(namespacedKey.toString(), registryObject);

// If the object is a vanilla Minecraft object, we'll add the key with spaces as a pattern
if (namespace.equalsIgnoreCase("minecraft")) {
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
parseMap.put(keyWithSpaces, registryObject);
}

String[] options = Language.getList(languageKey);
// Missing/Custom registry objects
if (options.length == 1 && options[0].equals(languageKey.toLowerCase(Locale.ENGLISH))) {
if (namespace.equalsIgnoreCase("minecraft")) {
// If the object is a vanilla Minecraft object, we'll use the key with spaces as a name
names.put(registryObject, keyWithSpaces);
} else {
// If the object is a custom object, we'll use the full namespaced key as a name
names.put(registryObject, namespacedKey.toString());
}
} else {
for (String option : options) {
option = option.toLowerCase(Locale.ENGLISH);

// Isolate the gender if one is present
NonNullPair<String, Integer> strippedOption = Noun.stripGender(option, languageKey);
String first = strippedOption.getFirst();
Integer second = strippedOption.getSecond();

// Add to name map if needed
names.putIfAbsent(registryObject, first);

parseMap.put(first, registryObject);
if (second != -1) { // There is a gender present
parseMap.put(Noun.getArticleWithSpace(second, Language.F_INDEFINITE_ARTICLE) + first, registryObject);
}
}
}
}
}

/**
* This method attempts to match the string input against one of the string representations of the enumerators.
*
* @param input a string to attempt to match against one the enumerators.
* @return The enumerator matching the input, or null if no match could be made.
*/
@Nullable
public R parse(String input) {
return parseMap.get(input.toLowerCase(Locale.ENGLISH));
}

/**
* This method returns the string representation of an enumerator.
*
* @param object The object to represent as a string.
* @param flags not currently used
* @return A string representation of the enumerator.
*/
public String toString(R object, int flags) {
return names.get(object);
}

/**
* @return A comma-separated string containing a list of all names representing the registry.
* Note that some entries may represent the same registry object.
*/
public String getAllNames() {
return StringUtils.join(parseMap.keySet(), ", ");
}

}