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

Mod Material support, for Hybrid Server #7126

Open
wants to merge 13 commits into
base: dev/patch
Choose a base branch
from
11 changes: 8 additions & 3 deletions src/main/java/ch/njol/skript/aliases/Aliases.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import ch.njol.skript.config.Node;
import ch.njol.skript.config.SectionNode;
import ch.njol.skript.entity.EntityData;
import org.bukkit.entity.EntityType;
import org.skriptlang.skript.lang.script.Script;
import ch.njol.skript.lang.parser.ParserInstance;
import ch.njol.skript.localization.ArgsMessage;
Expand Down Expand Up @@ -403,8 +402,14 @@ private static void loadMissingAliases() {
for (Material material : Material.values()) {
if (!material.isLegacy() && !provider.hasAliasForMaterial(material)) {
NamespacedKey key = material.getKey();
String name = key.getKey().replace("_", " ");
parser.loadAlias(name + "¦s", key.toString());
// mod:an_item -> (mod's an item) | (an item from mod)
// minecraft:dirt -> dirt
if (NamespacedKey.MINECRAFT.equals(key.getNamespace())) {
parser.loadAlias(key.getKey().replace("_", " "), key.toString());
} else {
parser.loadAlias((key.getNamespace() + "'s " + key.getKey() + "¦s").replace("_", " "), key.toString());
parser.loadAlias((key.getKey() + "¦s from " + key.getNamespace()).replace("_", " "), key.toString());
}
Skript.debug(ChatColor.YELLOW + "Creating temporary alias for: " + key);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/aliases/AliasesProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public void addAlias(AliasName name, String id, @Nullable Map<String, Object> ta
datas = typeOfId.getTypes();
} else { // ... but quite often, we just got Vanilla id
// Prepare and modify ItemStack (using somewhat Unsafe methods)
Material material = BukkitUnsafe.getMaterialFromMinecraftId(id);
Material material = BukkitUnsafe.getMaterialFromNamespacedId(id);
if (material == null) { // If server doesn't recognize id, do not proceed
throw new InvalidMinecraftIdException(id);
}
Expand Down
34 changes: 27 additions & 7 deletions src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@

import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.UnsafeValues;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
Expand All @@ -39,7 +37,6 @@
import com.google.gson.reflect.TypeToken;
import ch.njol.util.EnumTypeAdapter;
import ch.njol.skript.Skript;
import ch.njol.skript.util.Version;

/**
* Contains helpers for Bukkit's not so safe stuff.
Expand Down Expand Up @@ -68,9 +65,32 @@ public class BukkitUnsafe {
@Nullable
private static Map<Integer,Material> idMappings;

@Nullable
public static Material getMaterialFromMinecraftId(String id) {
return Material.matchMaterial(id);
/**
* Get a material from a minecraft id.
*
* @param id Namespaced ID with or without a namespace. IDs without a namespace will be treated
* as minecraft namespaced IDs. ('minecraft:dirt' and 'dirt' are equivalent.)
* @return The Material which the id represents, or null if no material can be matched.
* @deprecated Prefer {@link BukkitUnsafe#getMaterialFromNamespacedId(String)} for including modded item support
*/
@Deprecated
public static @Nullable Material getMaterialFromMinecraftId(String id) {
return getMaterialFromNamespacedId(id);
}

/**
* Get a material from a namespaced ID.
* For example, 'minecraft:iron_ingot' -> Material.IRON_INGOT; 'mod:an_item' -> Material.MOD_AN_ITEM
*
* @param id Namespaced ID with or without a namespace. IDs without a namespace will be treated
* as minecraft namespaced IDs. ('minecraft:dirt' and 'dirt' are equivalent.)
* @return The Material which the id represents, or null if no material can be matched.
*/
public static @Nullable Material getMaterialFromNamespacedId(String id) {
return Material.matchMaterial(id.toLowerCase().startsWith(NamespacedKey.MINECRAFT + ":")
? id
: id.replace(":", "_") //For Hybrid Server
);
}

public static void modifyItemStack(ItemStack stack, String arguments) {
Expand Down