From 243950759d4c99439794a814e6d8d8507fdb84a5 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Thu, 3 Oct 2024 00:51:35 +0800 Subject: [PATCH 01/17] fix: Mod items support, for Hybrid Server --- src/main/java/ch/njol/skript/aliases/Aliases.java | 5 ++++- src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/aliases/Aliases.java b/src/main/java/ch/njol/skript/aliases/Aliases.java index 3e67971698a..9d7aef2fe24 100644 --- a/src/main/java/ch/njol/skript/aliases/Aliases.java +++ b/src/main/java/ch/njol/skript/aliases/Aliases.java @@ -403,7 +403,10 @@ private static void loadMissingAliases() { for (Material material : Material.values()) { if (!material.isLegacy() && !provider.hasAliasForMaterial(material)) { NamespacedKey key = material.getKey(); - String name = key.getKey().replace("_", " "); + String name = (key.getNamespace().equals(NamespacedKey.MINECRAFT) ? key.getKey() : key.toString()) + .replace("_", " "); + // mod:an_item → mod:an item + // minecraft:dirt → dirt parser.loadAlias(name + "¦s", key.toString()); Skript.debug(ChatColor.YELLOW + "Creating temporary alias for: " + key); } diff --git a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java index f47a3f5d8d7..1c28093eb21 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java +++ b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java @@ -70,7 +70,10 @@ public class BukkitUnsafe { @Nullable public static Material getMaterialFromMinecraftId(String id) { - return Material.matchMaterial(id); + return Material.matchMaterial(id.toLowerCase().startsWith("minecraft:") + ? id + : id.replace(":", "_") //For Hybrid Server + ); } public static void modifyItemStack(ItemStack stack, String arguments) { From dc1957e8647b63a5e604432afe4a4bc9121e1d9f Mon Sep 17 00:00:00 2001 From: XPYEX Date: Thu, 3 Oct 2024 01:05:20 +0800 Subject: [PATCH 02/17] style: style --- src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java index 1c28093eb21..d84452f3a0b 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java +++ b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java @@ -30,6 +30,7 @@ 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; @@ -70,7 +71,7 @@ public class BukkitUnsafe { @Nullable public static Material getMaterialFromMinecraftId(String id) { - return Material.matchMaterial(id.toLowerCase().startsWith("minecraft:") + return Material.matchMaterial(id.toLowerCase().startsWith(NamespacedKey.MINECRAFT + ":") ? id : id.replace(":", "_") //For Hybrid Server ); From acf70fb90dfa0ca96c965c7c87fb01c9a81f53fc Mon Sep 17 00:00:00 2001 From: XPYEX Date: Thu, 3 Oct 2024 01:20:07 +0800 Subject: [PATCH 03/17] perf: rename function name and aliases name --- src/main/java/ch/njol/skript/aliases/Aliases.java | 7 ++++--- src/main/java/ch/njol/skript/aliases/AliasesProvider.java | 2 +- src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java | 6 +----- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/main/java/ch/njol/skript/aliases/Aliases.java b/src/main/java/ch/njol/skript/aliases/Aliases.java index 9d7aef2fe24..a6aa2b46905 100644 --- a/src/main/java/ch/njol/skript/aliases/Aliases.java +++ b/src/main/java/ch/njol/skript/aliases/Aliases.java @@ -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; @@ -403,8 +402,10 @@ private static void loadMissingAliases() { for (Material material : Material.values()) { if (!material.isLegacy() && !provider.hasAliasForMaterial(material)) { NamespacedKey key = material.getKey(); - String name = (key.getNamespace().equals(NamespacedKey.MINECRAFT) ? key.getKey() : key.toString()) - .replace("_", " "); + String name = (key.getNamespace().equals(NamespacedKey.MINECRAFT) + ? key.getKey() + : key.getNamespace() + "'s " + key.getKey() + ).replace("_", " "); // mod:an_item → mod:an item // minecraft:dirt → dirt parser.loadAlias(name + "¦s", key.toString()); diff --git a/src/main/java/ch/njol/skript/aliases/AliasesProvider.java b/src/main/java/ch/njol/skript/aliases/AliasesProvider.java index 06ae5317f78..c09501636ed 100644 --- a/src/main/java/ch/njol/skript/aliases/AliasesProvider.java +++ b/src/main/java/ch/njol/skript/aliases/AliasesProvider.java @@ -290,7 +290,7 @@ public void addAlias(AliasName name, String id, @Nullable Map 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); } diff --git a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java index d84452f3a0b..363036bcb78 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java +++ b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java @@ -20,9 +20,6 @@ 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; @@ -40,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. @@ -70,7 +66,7 @@ public class BukkitUnsafe { private static Map idMappings; @Nullable - public static Material getMaterialFromMinecraftId(String id) { + public static Material getMaterialFromNamespacedId(String id) { return Material.matchMaterial(id.toLowerCase().startsWith(NamespacedKey.MINECRAFT + ":") ? id : id.replace(":", "_") //For Hybrid Server From e8e8074bd3f9e6c20ce9c82118defbbda3d90281 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Thu, 3 Oct 2024 01:22:02 +0800 Subject: [PATCH 04/17] perf: notes --- src/main/java/ch/njol/skript/aliases/Aliases.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/aliases/Aliases.java b/src/main/java/ch/njol/skript/aliases/Aliases.java index a6aa2b46905..c3bb3491ced 100644 --- a/src/main/java/ch/njol/skript/aliases/Aliases.java +++ b/src/main/java/ch/njol/skript/aliases/Aliases.java @@ -406,7 +406,7 @@ private static void loadMissingAliases() { ? key.getKey() : key.getNamespace() + "'s " + key.getKey() ).replace("_", " "); - // mod:an_item → mod:an item + // mod:an_item → mod's an item // minecraft:dirt → dirt parser.loadAlias(name + "¦s", key.toString()); Skript.debug(ChatColor.YELLOW + "Creating temporary alias for: " + key); From bc7740e62cea95fe88827c692d1e51b11aa5e303 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Thu, 3 Oct 2024 01:34:54 +0800 Subject: [PATCH 05/17] perf: not a break change --- src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java index 363036bcb78..32e1712ff30 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java +++ b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java @@ -65,6 +65,11 @@ public class BukkitUnsafe { @Nullable private static Map idMappings; + @Deprecated + public static Material getMaterialFromMinecraftId(String id) { + return getMaterialFromNamespacedId(id); + } + @Nullable public static Material getMaterialFromNamespacedId(String id) { return Material.matchMaterial(id.toLowerCase().startsWith(NamespacedKey.MINECRAFT + ":") From fd28c5c380dcdd9cbb80e4d0aa475ea0af74c217 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Thu, 3 Oct 2024 01:35:51 +0800 Subject: [PATCH 06/17] perf: the @Nullable annotation --- src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java index 32e1712ff30..fd11c49cdb1 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java +++ b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java @@ -65,6 +65,7 @@ public class BukkitUnsafe { @Nullable private static Map idMappings; + @Nullable @Deprecated public static Material getMaterialFromMinecraftId(String id) { return getMaterialFromNamespacedId(id); From d49018bbe478cbad21a0786ecbb0598129caeb88 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Thu, 3 Oct 2024 01:41:53 +0800 Subject: [PATCH 07/17] docs: javadoc --- .../ch/njol/skript/bukkitutil/BukkitUnsafe.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java index fd11c49cdb1..30c9fe11dd9 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java +++ b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java @@ -65,12 +65,26 @@ public class BukkitUnsafe { @Nullable private static Map idMappings; + /** + * @deprecated You should use {@link BukkitUnsafe#getMaterialFromNamespacedId(String)} + * Get a material from a minecraft id. + * + * @param id Namespaced id (just like 'minecraft:dirt'), or normally just a material name (just like 'dirt') + * @return Material or null + */ @Nullable @Deprecated public static Material getMaterialFromMinecraftId(String id) { return getMaterialFromNamespacedId(id); } + /** + * Get a material from a namespaced id. + * Such as, minecraft:iron_ingot -> IRON_INGOT; mod:an_item -> mod_an_item + * + * @param id Namespaced id (just like 'minecraft:dirt'), or normally just a material name (just like 'dirt') + * @return Material or null + */ @Nullable public static Material getMaterialFromNamespacedId(String id) { return Material.matchMaterial(id.toLowerCase().startsWith(NamespacedKey.MINECRAFT + ":") From c04c6a3c2e3230a29742172afa8cdb080aff7b0a Mon Sep 17 00:00:00 2001 From: XPYEX Date: Thu, 3 Oct 2024 01:48:47 +0800 Subject: [PATCH 08/17] style: to upper cases --- src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java index 30c9fe11dd9..37d68c11741 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java +++ b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java @@ -80,7 +80,7 @@ public static Material getMaterialFromMinecraftId(String id) { /** * Get a material from a namespaced id. - * Such as, minecraft:iron_ingot -> IRON_INGOT; mod:an_item -> mod_an_item + * Such as, minecraft:iron_ingot -> IRON_INGOT; mod:an_item -> MOD_AN_ITEM * * @param id Namespaced id (just like 'minecraft:dirt'), or normally just a material name (just like 'dirt') * @return Material or null From b9a37edd9bd479544c5f3cb52c070be9920e80bf Mon Sep 17 00:00:00 2001 From: XPYEX Date: Thu, 3 Oct 2024 02:02:06 +0800 Subject: [PATCH 09/17] perf: resolved mentioned --- .../java/ch/njol/skript/aliases/Aliases.java | 4 ++-- .../njol/skript/bukkitutil/BukkitUnsafe.java | 22 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/ch/njol/skript/aliases/Aliases.java b/src/main/java/ch/njol/skript/aliases/Aliases.java index c3bb3491ced..2198a5c3c74 100644 --- a/src/main/java/ch/njol/skript/aliases/Aliases.java +++ b/src/main/java/ch/njol/skript/aliases/Aliases.java @@ -406,8 +406,8 @@ private static void loadMissingAliases() { ? key.getKey() : key.getNamespace() + "'s " + key.getKey() ).replace("_", " "); - // mod:an_item → mod's an item - // minecraft:dirt → dirt + // mod:an_item -> mod's an item + // minecraft:dirt -> dirt parser.loadAlias(name + "¦s", key.toString()); Skript.debug(ChatColor.YELLOW + "Creating temporary alias for: " + key); } diff --git a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java index 37d68c11741..4ba3a18e4a9 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java +++ b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java @@ -66,27 +66,27 @@ public class BukkitUnsafe { private static Map idMappings; /** - * @deprecated You should use {@link BukkitUnsafe#getMaterialFromNamespacedId(String)} * Get a material from a minecraft id. * - * @param id Namespaced id (just like 'minecraft:dirt'), or normally just a material name (just like 'dirt') - * @return Material or null + * @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 the id represents, or null if no material can be matched. + * @deprecated Prefer {@link BukkitUnsafe#getMaterialFromNamespacedId(String)} */ - @Nullable @Deprecated - public static Material getMaterialFromMinecraftId(String id) { + public static @Nullable Material getMaterialFromMinecraftId(String id) { return getMaterialFromNamespacedId(id); } /** - * Get a material from a namespaced id. - * Such as, minecraft:iron_ingot -> IRON_INGOT; mod:an_item -> MOD_AN_ITEM + * 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 (just like 'minecraft:dirt'), or normally just a material name (just like 'dirt') - * @return Material or null + * @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 the id represents, or null if no material can be matched. */ - @Nullable - public static Material getMaterialFromNamespacedId(String id) { + public static @Nullable Material getMaterialFromNamespacedId(String id) { return Material.matchMaterial(id.toLowerCase().startsWith(NamespacedKey.MINECRAFT + ":") ? id : id.replace(":", "_") //For Hybrid Server From 070b0bab0fea00f5d4bedda8cc4be353d653c99d Mon Sep 17 00:00:00 2001 From: XPYEX Date: Thu, 3 Oct 2024 02:05:48 +0800 Subject: [PATCH 10/17] style: style --- src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java index 4ba3a18e4a9..bea439f1753 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java +++ b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java @@ -70,7 +70,7 @@ public class BukkitUnsafe { * * @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 the id represents, or null if no material can be matched. + * @return The Material which the id represents, or null if no material can be matched. * @deprecated Prefer {@link BukkitUnsafe#getMaterialFromNamespacedId(String)} */ @Deprecated @@ -84,7 +84,7 @@ public class BukkitUnsafe { * * @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 the id represents, or null if no material can be matched. + * @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 + ":") From a8e13fff603c8af1018436e3009b3dc76c93ec3a Mon Sep 17 00:00:00 2001 From: XPYEX Date: Thu, 3 Oct 2024 02:36:30 +0800 Subject: [PATCH 11/17] perf: resolved mentioned --- src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java index bea439f1753..66b7fa13a34 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java +++ b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java @@ -71,7 +71,7 @@ public class BukkitUnsafe { * @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)} + * @deprecated Prefer {@link BukkitUnsafe#getMaterialFromNamespacedId(String)} for including modded item support */ @Deprecated public static @Nullable Material getMaterialFromMinecraftId(String id) { From 3c2057f47490bdbb49ca78ce6e05fe779356159f Mon Sep 17 00:00:00 2001 From: XPYEX Date: Thu, 3 Oct 2024 02:45:27 +0800 Subject: [PATCH 12/17] feat: the expression: from --- src/main/java/ch/njol/skript/aliases/Aliases.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/njol/skript/aliases/Aliases.java b/src/main/java/ch/njol/skript/aliases/Aliases.java index 2198a5c3c74..ae2a1067d79 100644 --- a/src/main/java/ch/njol/skript/aliases/Aliases.java +++ b/src/main/java/ch/njol/skript/aliases/Aliases.java @@ -402,13 +402,14 @@ private static void loadMissingAliases() { for (Material material : Material.values()) { if (!material.isLegacy() && !provider.hasAliasForMaterial(material)) { NamespacedKey key = material.getKey(); - String name = (key.getNamespace().equals(NamespacedKey.MINECRAFT) - ? key.getKey() - : key.getNamespace() + "'s " + key.getKey() - ).replace("_", " "); - // mod:an_item -> mod's an item + // mod:an_item -> (mod's an item) | (an item from mod) // minecraft:dirt -> dirt - parser.loadAlias(name + "¦s", key.toString()); + 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); } } From d8a2d768edea92732ea8713832c8fe627dc361fa Mon Sep 17 00:00:00 2001 From: XPYEX Date: Thu, 3 Oct 2024 02:52:50 +0800 Subject: [PATCH 13/17] fix: wrong space --- src/main/java/ch/njol/skript/aliases/Aliases.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/aliases/Aliases.java b/src/main/java/ch/njol/skript/aliases/Aliases.java index ae2a1067d79..5e9efa20d4d 100644 --- a/src/main/java/ch/njol/skript/aliases/Aliases.java +++ b/src/main/java/ch/njol/skript/aliases/Aliases.java @@ -407,7 +407,7 @@ private static void loadMissingAliases() { 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.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); From 034f70b2f18c8186f46455f50489810123dee3b5 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Fri, 4 Oct 2024 01:59:15 +0800 Subject: [PATCH 14/17] fix: ..s --- src/main/java/ch/njol/skript/aliases/Aliases.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/aliases/Aliases.java b/src/main/java/ch/njol/skript/aliases/Aliases.java index 5e9efa20d4d..84234052724 100644 --- a/src/main/java/ch/njol/skript/aliases/Aliases.java +++ b/src/main/java/ch/njol/skript/aliases/Aliases.java @@ -405,7 +405,7 @@ private static void loadMissingAliases() { // 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()); + parser.loadAlias(key.getKey().replace("_", " ") + "¦s", 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()); From 8f57fe9a1b64033a066502c1ee9760bc3ab776c7 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Mon, 28 Oct 2024 00:44:58 +0800 Subject: [PATCH 15/17] perf: add warnings --- src/main/java/ch/njol/skript/aliases/Aliases.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/ch/njol/skript/aliases/Aliases.java b/src/main/java/ch/njol/skript/aliases/Aliases.java index 84234052724..df565698b34 100644 --- a/src/main/java/ch/njol/skript/aliases/Aliases.java +++ b/src/main/java/ch/njol/skript/aliases/Aliases.java @@ -399,6 +399,7 @@ public static void load() { private static void loadMissingAliases() { if (!Skript.methodExists(Material.class, "getKey")) return; + boolean modItemRegistered = false; for (Material material : Material.values()) { if (!material.isLegacy() && !provider.hasAliasForMaterial(material)) { NamespacedKey key = material.getKey(); @@ -407,12 +408,25 @@ private static void loadMissingAliases() { if (NamespacedKey.MINECRAFT.equals(key.getNamespace())) { parser.loadAlias(key.getKey().replace("_", " ") + "¦s", key.toString()); } else { + if (!modItemRegistered) modItemRegistered = true; 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); } } + if (modItemRegistered) { + Skript.warning("=============================================================="); + Skript.warning("We found some Materials, which are not registered by Minecraft."); + Skript.warning("For comfort of your coding, we register aliases from to or "); + Skript.warning("Please PAY ATTENTION: The Hybrid Servers are NOT supported now. We don't have enough energy to handle these now(maybe someday), sorry"); + Skript.warning("So, DO NOT report any issues caused by this to us"); + Skript.warning("The server will keep loading after 5 seconds"); + Skript.warning("=============================================================="); + try { + Thread.sleep(5000L); + } catch (InterruptedException ignored) {} + } } private static void loadInternal() throws IOException { From 1c4c1ddbc4e7e818ea36e152dae710755dc952ac Mon Sep 17 00:00:00 2001 From: XPYEX Date: Mon, 28 Oct 2024 00:53:26 +0800 Subject: [PATCH 16/17] style: just sort codes, using IDEA --- .../java/ch/njol/skript/aliases/Aliases.java | 207 +++++------ .../njol/skript/aliases/AliasesProvider.java | 328 ++++++++---------- .../njol/skript/bukkitutil/BukkitUnsafe.java | 72 ++-- 3 files changed, 280 insertions(+), 327 deletions(-) diff --git a/src/main/java/ch/njol/skript/aliases/Aliases.java b/src/main/java/ch/njol/skript/aliases/Aliases.java index df565698b34..a69f2cfac9c 100644 --- a/src/main/java/ch/njol/skript/aliases/Aliases.java +++ b/src/main/java/ch/njol/skript/aliases/Aliases.java @@ -1,21 +1,3 @@ -/** - * 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 . - * - * Copyright Peter Güttinger, SkriptLang team and contributors - */ package ch.njol.skript.aliases; import ch.njol.skript.Skript; @@ -25,7 +7,6 @@ import ch.njol.skript.config.Node; import ch.njol.skript.config.SectionNode; import ch.njol.skript.entity.EntityData; -import org.skriptlang.skript.lang.script.Script; import ch.njol.skript.lang.parser.ParserInstance; import ch.njol.skript.localization.ArgsMessage; import ch.njol.skript.localization.Language; @@ -36,11 +17,6 @@ import ch.njol.skript.util.EnchantmentType; import ch.njol.skript.util.Utils; import ch.njol.skript.util.Version; -import org.bukkit.ChatColor; -import org.bukkit.Material; -import org.bukkit.NamespacedKey; -import org.jetbrains.annotations.Nullable; - import java.io.IOException; import java.io.UncheckedIOException; import java.net.URI; @@ -55,14 +31,51 @@ import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.jetbrains.annotations.Nullable; +import org.skriptlang.skript.lang.script.Script; public abstract class Aliases { - static final boolean USING_ITEM_COMPONENTS = Skript.isRunningMinecraft(1, 20, 5); private static final AliasesProvider provider = createProvider(10000, null); private static final AliasesParser parser = createParser(provider); - + // this is not an alias! + private final static ItemType everything = new ItemType(); + private final static Message m_empty_string = new Message("aliases.empty string"); + private final static ArgsMessage m_invalid_item_type = new ArgsMessage("aliases.invalid item type"); + private final static Message m_outside_section = new Message("aliases.outside section"); + private final static RegexMessage p_any = new RegexMessage("aliases.any", "", " (.+)", Pattern.CASE_INSENSITIVE); + private final static RegexMessage p_every = new RegexMessage("aliases.every", "", " (.+)", Pattern.CASE_INSENSITIVE); + private final static RegexMessage p_of_every = new RegexMessage("aliases.of every", "(\\d+) ", " (.+)", Pattern.CASE_INSENSITIVE); + private final static RegexMessage p_of = new RegexMessage("aliases.of", "(\\d+) (?:", " )?(.+)", Pattern.CASE_INSENSITIVE); + /** + * Go through these whenever aliases are reloaded, and update them. + */ + private static final Map trackedTypes = new HashMap<>(); + /** + * If user had an obscure config option set, don't crash due to missing + * Java item types. + */ + private static final boolean noHardExceptions = SkriptConfig.apiSoftExceptions.value(); + static String itemSingular = "item"; + static String itemPlural = "items"; + @Nullable + static String itemGender = null; + static String blockSingular = "block"; + static String blockPlural = "blocks"; + @Nullable + static String blockGender = null; + + static { + everything.setAll(true); + ItemData all = new ItemData(Material.AIR); + all.isAnything = true; + everything.add(all); + } + @Nullable private static ItemType getAlias_i(final String s) { // Check script aliases first @@ -70,10 +83,10 @@ private static ItemType getAlias_i(final String s) { if (aliases != null) { return aliases.provider.getAlias(s); // Delegates to global provider if needed } - + return provider.getAlias(s); } - + /** * Creates an aliases provider with Skript's default configuration. * @param expectedCount Expected alias count. @@ -83,14 +96,14 @@ private static ItemType getAlias_i(final String s) { private static AliasesProvider createProvider(int expectedCount, @Nullable AliasesProvider parent) { return new AliasesProvider(expectedCount, parent); } - + /** * Creates an aliases parser with Skript's default configuration. * @return Aliases parser. */ private static AliasesParser createParser(AliasesProvider provider) { AliasesParser parser = new AliasesParser(provider); - + // Register standard conditions parser.registerCondition("minecraft version", (str) -> { int orNewer = str.indexOf("or newer"); // For example: 1.12 or newer @@ -99,14 +112,14 @@ private static AliasesParser createParser(AliasesProvider provider) { Version ver = new Version(str.substring(0, orNewer - 1)); return Skript.getMinecraftVersion().compareTo(ver) >= 0; } - + int orOlder = str.indexOf("or older"); // For example: 1.11 or older if (orOlder != -1) { @SuppressWarnings("null") Version ver = new Version(str.substring(0, orOlder - 1)); return Skript.getMinecraftVersion().compareTo(ver) <= 0; } - + int to = str.indexOf("to"); // For example: 1.11 to 1.12 if (to != -1) { @SuppressWarnings("null") @@ -116,39 +129,17 @@ private static AliasesParser createParser(AliasesProvider provider) { Version current = Skript.getMinecraftVersion(); return current.compareTo(first) >= 0 && current.compareTo(second) <= 0; } - + return Skript.getMinecraftVersion().equals(new Version(str)); }); - + return parser; } - static String itemSingular = "item"; - static String itemPlural = "items"; - @Nullable - static String itemGender = null; - static String blockSingular = "block"; - static String blockPlural = "blocks"; - @Nullable - static String blockGender = null; - - // this is not an alias! - private final static ItemType everything = new ItemType(); - static { - everything.setAll(true); - ItemData all = new ItemData(Material.AIR); - all.isAnything = true; - everything.add(all); - } - - private final static Message m_empty_string = new Message("aliases.empty string"); - private final static ArgsMessage m_invalid_item_type = new ArgsMessage("aliases.invalid item type"); - private final static Message m_outside_section = new Message("aliases.outside section"); - /** * Concatenates parts of an alias's name. This currently 'lowercases' the first character of any part if there's no space in front of it. It also replaces double spaces with a * single one and trims the resulting string. - * + * * @param parts */ static String concatenate(final String... parts) { @@ -170,7 +161,7 @@ static String concatenate(final String... parts) { } return "" + b.toString().replace(" ", " ").trim(); } - + @Nullable private static MaterialName getMaterialNameData(ItemData type) { // Check script aliases first @@ -178,11 +169,11 @@ private static MaterialName getMaterialNameData(ItemData type) { if (aliases != null) { return aliases.provider.getMaterialName(type); } - + // Then global aliases return provider.getMaterialName(type); } - + public static String getMaterialName(ItemData type, boolean plural) { MaterialName name = getMaterialNameData(type); if (name == null) { @@ -190,7 +181,7 @@ public static String getMaterialName(ItemData type, boolean plural) { } return name.toString(plural); } - + /** * @return The item's gender or -1 if no name is found */ @@ -200,10 +191,10 @@ public static int getGender(ItemData item) { return n.gender; return -1; } - + /** * Parses an ItemType to be used as an alias, i.e. it doesn't parse 'all'/'every' and the amount. - * + * * @param s mixed case string * @return A new ItemType representing the given value */ @@ -215,28 +206,23 @@ public static ItemType parseAlias(final String s) { } if (s.equals("*")) return everything; - + final ItemType t = new ItemType(); - + final String[] types = s.split("\\s*,\\s*"); for (final String type : types) { if (type == null || parseType(type, t, true) == null) return null; } - + return t; } - - private final static RegexMessage p_any = new RegexMessage("aliases.any", "", " (.+)", Pattern.CASE_INSENSITIVE); - private final static RegexMessage p_every = new RegexMessage("aliases.every", "", " (.+)", Pattern.CASE_INSENSITIVE); - private final static RegexMessage p_of_every = new RegexMessage("aliases.of every", "(\\d+) ", " (.+)", Pattern.CASE_INSENSITIVE); - private final static RegexMessage p_of = new RegexMessage("aliases.of", "(\\d+) (?:", " )?(.+)", Pattern.CASE_INSENSITIVE); - + /** * Parses an ItemType. *

* Prints errors. - * + * * @param s * @return The parsed ItemType or null if the input is invalid. */ @@ -244,10 +230,10 @@ public static ItemType parseAlias(final String s) { public static ItemType parseItemType(String s) { if (s.isEmpty()) return null; - s = "" + s.trim(); - + s = s.trim(); + final ItemType t = new ItemType(); - + Matcher m; if ((m = p_of_every.matcher(s)).matches()) { t.setAmount(Utils.parseInt("" + m.group(1))); @@ -265,11 +251,12 @@ public static ItemType parseItemType(String s) { if (s.length() != l) // had indefinite article t.setAmount(1); } - + String lc = s.toLowerCase(Locale.ENGLISH); String of = Language.getSpaced("of").toLowerCase(); int c = -1; - outer: while ((c = lc.indexOf(of, c + 1)) != -1) { + outer: + while ((c = lc.indexOf(of, c + 1)) != -1) { ItemType t2 = t.clone(); try (BlockingLogHandler ignored = new BlockingLogHandler().start()) { if (parseType("" + s.substring(0, c), t2, false) == null) @@ -286,19 +273,19 @@ public static ItemType parseItemType(String s) { } return t2; } - + if (parseType(s, t, false) == null) return null; - + if (t.numTypes() == 0) return null; - + return t; } - + /** * Prints errors. - * + * * @param s The string holding the type, can be either a number or an alias, plus an optional data part. Case does not matter. * @param t The ItemType to add the parsed ItemData(s) to (i.e. this ItemType will be modified) * @param isAlias Whether this type is parsed for an alias. @@ -322,10 +309,10 @@ private static ItemType parseType(final String s, final ItemType t, final boolea Skript.error(m_invalid_item_type.toString(s)); return null; } - + /** * Gets an alias from the aliases defined in the config. - * + * * @param s The alias to get, case does not matter * @return A copy of the ItemType represented by the given alias or null if no such alias exists. */ @@ -371,14 +358,14 @@ private static ItemType getAlias(final String s) { } return null; } - + /** * Clears aliases. Make sure to load them after this! */ public static void clear() { provider.clearAliases(); } - + /** * Loads aliases from Skript's standard locations. * Exceptions will be logged, but not thrown. @@ -425,13 +412,14 @@ private static void loadMissingAliases() { Skript.warning("=============================================================="); try { Thread.sleep(5000L); - } catch (InterruptedException ignored) {} + } catch (InterruptedException ignored) { + } } } - + private static void loadInternal() throws IOException { Path dataFolder = Skript.getInstance().getDataFolder().toPath(); - + // Load aliases.zip OR aliases from jar (never both) Path zipPath = dataFolder.resolve("aliases-english.zip"); if (!SkriptConfig.loadDefaultAliases.value()) { @@ -455,9 +443,9 @@ private static void loadInternal() throws IOException { } catch (URISyntaxException e) { assert false; } - + } - + // Load everything from aliases folder (user aliases) Path aliasesFolder = dataFolder.resolve("aliases"); if (Files.exists(aliasesFolder)) { @@ -467,19 +455,19 @@ private static void loadInternal() throws IOException { // generate aliases from item names for any missing items loadMissingAliases(); - + // Update tracked item types for (Map.Entry entry : trackedTypes.entrySet()) { @SuppressWarnings("null") // No null keys in this map ItemType type = parseItemType(entry.getKey()); if (type == null) Skript.warning("Alias '" + entry.getKey() + "' is required by Skript, but does not exist anymore. " - + "Make sure to fix this before restarting the server."); + + "Make sure to fix this before restarting the server."); else entry.getValue().setTo(type); } } - + /** * Loads aliases from given directory. * @param dir Directory of aliases. @@ -503,7 +491,7 @@ else if (name.endsWith(".sk")) throw e.getCause(); } } - + /** * Loads aliases from given path. * @param f Path of alias file. @@ -513,7 +501,7 @@ public static void load(Path f) throws IOException { Config config = new Config(f, false, false, "="); load(config); } - + /** * Loads aliases from configuration. * @param config Configuration containing the aliases. @@ -524,7 +512,7 @@ public static void load(Config config) { Skript.error(m_outside_section.toString()); continue; } - + parser.load((SectionNode) n); } } @@ -542,7 +530,7 @@ public static String getMinecraftId(ItemData data) { } return provider.getMinecraftId(data); } - + /** * Gets an entity type related to given item. For example, an armor stand * item is related with armor stand entity. @@ -557,22 +545,11 @@ public static EntityData getRelatedEntity(ItemData data) { } return provider.getRelatedEntity(data); } - - /** - * Go through these whenever aliases are reloaded, and update them. - */ - private static final Map trackedTypes = new HashMap<>(); - - /** - * If user had an obscure config option set, don't crash due to missing - * Java item types. - */ - private static final boolean noHardExceptions = SkriptConfig.apiSoftExceptions.value(); - + /** * Gets an item type that matches the given name. * If it doesn't exist, an exception is thrown instead. - * + * *

Item types provided by this method are updated when aliases are * reloaded. However, this also means they are tracked by aliases system * and NOT necessarily garbage-collected. @@ -599,7 +576,7 @@ public static ItemType javaItemType(String name) { trackedTypes.put(name, type); return type; } - + /** * Creates an aliases provider to be used by given addon. It can be used to * register aliases and variations to be used in scripts. @@ -610,11 +587,11 @@ public static AliasesProvider getAddonProvider(@Nullable SkriptAddon addon) { if (addon == null) { throw new IllegalArgumentException("addon needed"); } - + // TODO in future, maybe record and allow unloading addon-provided aliases? return provider; // For now, just allow loading aliases easily } - + /** * Creates script aliases for the provided Script. * @return Script aliases that are ready to be added to. diff --git a/src/main/java/ch/njol/skript/aliases/AliasesProvider.java b/src/main/java/ch/njol/skript/aliases/AliasesProvider.java index c09501636ed..1cbaa0c9cfd 100644 --- a/src/main/java/ch/njol/skript/aliases/AliasesProvider.java +++ b/src/main/java/ch/njol/skript/aliases/AliasesProvider.java @@ -1,45 +1,24 @@ -/** - * 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 . - * - * Copyright Peter Güttinger, SkriptLang team and contributors - */ package ch.njol.skript.aliases; +import ch.njol.skript.Skript; +import ch.njol.skript.bukkitutil.BukkitUnsafe; +import ch.njol.skript.bukkitutil.ItemUtils; +import ch.njol.skript.bukkitutil.block.BlockCompat; +import ch.njol.skript.bukkitutil.block.BlockValues; +import ch.njol.skript.entity.EntityData; +import com.google.gson.Gson; +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.Set; import java.util.List; import java.util.Map; - -import ch.njol.skript.Skript; -import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; +import java.util.Set; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.Nullable; -import com.google.gson.Gson; - -import ch.njol.skript.bukkitutil.BukkitUnsafe; -import ch.njol.skript.bukkitutil.ItemUtils; -import ch.njol.skript.bukkitutil.block.BlockCompat; -import ch.njol.skript.bukkitutil.block.BlockValues; -import ch.njol.skript.entity.EntityData; - /** * Provides aliases on Bukkit/Spigot platform. */ @@ -47,14 +26,14 @@ public class AliasesProvider { // not supported on Spigot versions older than 1.18 private static final boolean FASTER_SET_SUPPORTED = Skript.classExists("it.unimi.dsi.fastutil.objects.ObjectOpenHashSet"); - + /** * When an alias is not found, it will requested from this provider. * Null when this is global aliases provider. */ @Nullable private final AliasesProvider parent; - + /** * All aliases that are currently loaded by this provider. */ @@ -64,112 +43,21 @@ public class AliasesProvider { * All materials that are currently loaded by this provider. */ private final Set materials; - + /** * Tags are in JSON format. We may need GSON when merging tags * (which might be done if variations are used). */ private final Gson gson; - - /** - * Represents a variation of material. It could, for example, define one - * more tag or change base id, but keep tag intact. - */ - public static class Variation { - - @Nullable - private final String id; - private final int insertPoint; - - private final Map tags; - private final Map states; - - public Variation(@Nullable String id, int insertPoint, Map tags, Map states) { - this.id = id; - this.insertPoint = insertPoint; - this.tags = tags; - this.states = states; - } - - @Nullable - public String getId() { - return id; - } - - public int getInsertPoint() { - return insertPoint; - } - - @Nullable - public String insertId(@Nullable String inserted) { - if (inserted == null) // Nothing to insert - return id; - inserted = inserted.substring(0, inserted.length() - 1); // Strip out - - if (id == null) // Inserting to nothing - return inserted; - - String id = this.id; - assert id != null; - if (insertPoint == -1) // No place where to insert - return inserted; - - // Insert given string to in middle of our id - String before = id.substring(0, insertPoint); - String after = id.substring(insertPoint + 1); - return before + inserted + after; - } - - public Map getTags() { - return tags; - } - - - public Map getBlockStates() { - return states; - } - - - public Variation merge(Variation other) { - // Merge tags and block states - Map mergedTags = new HashMap<>(other.tags); - mergedTags.putAll(tags); - Map mergedStates = new HashMap<>(other.states); - mergedStates.putAll(states); - - // Potentially merge ids - String id = insertId(other.id); - - return new Variation(id, -1, mergedTags, mergedStates); - } - } - - public static class VariationGroup { - - public final List keys; - - public final List values; - - public VariationGroup() { - this.keys = new ArrayList<>(); - this.values = new ArrayList<>(); - } - - public void put(String key, Variation value) { - keys.add(key); - values.add(value); - } - } - /** * Contains all variations. */ private final Map variations; - /** * Allows looking up aliases based on item datas created runtime. */ private final AliasesMap aliasesMap; - + /** * Constructs a new aliases provider with no data. */ @@ -184,20 +72,20 @@ public AliasesProvider(int expectedCount, @Nullable AliasesProvider parent) { } else { this.materials = new HashSet<>(); } - + this.gson = new Gson(); } - + /** * Uses GSON to parse Mojang's JSON format to a map. * @param raw Raw JSON. - * @return String,Object map. + * @return String, Object map. */ @SuppressWarnings({"null", "unchecked"}) public Map parseMojangson(String raw) { return (Map) gson.fromJson(raw, Object.class); } - + /** * Applies given tags to an item stack. * @param stack Item stack. @@ -213,10 +101,10 @@ public int applyTags(ItemStack stack, Map tags) { tags.remove("Damage"); flags |= ItemFlags.CHANGED_DURABILITY; } - + if (tags.isEmpty()) // No real tags to apply return flags; - + // Apply random tags using JSON if (Aliases.USING_ITEM_COMPONENTS) { String components = (String) tags.get("components"); // only components are supported for modifying a stack @@ -231,40 +119,10 @@ public int applyTags(ItemStack stack, Map tags) { BukkitUnsafe.modifyItemStack(stack, json); } flags |= ItemFlags.CHANGED_TAGS; - + return flags; } - - /** - * Name of an alias used by {@link #addAlias(AliasName, String, Map, Map)} - * for registration. - */ - public static class AliasName { - - /** - * Singular for of alias name. - */ - public final String singular; - - /** - * Plural form of alias name. - */ - public final String plural; - - /** - * Gender of alias name. - */ - public final int gender; - public AliasName(String singular, String plural, int gender) { - super(); - this.singular = singular; - this.plural = plural; - this.gender = gender; - } - - } - /** * Adds an alias to this provider. * @param name Name of alias without any patterns or variation blocks. @@ -296,13 +154,13 @@ public void addAlias(AliasName name, String id, @Nullable Map ta } materials.add(material); - + // Hacky: get related entity from block states String entityName = blockStates.remove("relatedEntity"); if (entityName != null) { related = EntityData.parse(entityName); } - + // Apply (NBT) tags to item stack ItemStack stack = null; int itemFlags = 0; @@ -312,14 +170,14 @@ public void addAlias(AliasName name, String id, @Nullable Map ta itemFlags = applyTags(stack, new HashMap<>(tags)); } } - + // Parse block state to block values BlockValues blockValues = BlockCompat.INSTANCE.createBlockValues(material, blockStates, stack, itemFlags); - + ItemData data = stack != null ? new ItemData(stack, blockValues) : new ItemData(material, blockValues); data.isAlias = true; data.itemFlags = itemFlags; - + // Deduplicate item data if this has been loaded before if (deduplicate) { AliasesMap.Match canonical = aliasesMap.exactMatch(data); @@ -329,10 +187,10 @@ public void addAlias(AliasName name, String id, @Nullable Map ta data = aliasData.getItem(); } } - + datas = Collections.singletonList(data); } - + // If this is first time we're defining an item, store additional data about it if (typeOfId == null) { ItemData data = datas.get(0); @@ -351,7 +209,8 @@ public void addAlias(AliasName name, String id, @Nullable Map ta aliases.put(name.plural, type); // Plural form type.addAll(datas); } else { // There is already an item type with this name, we need to *only* add new data - newDataLoop: for (ItemData newData : datas) { + newDataLoop: + for (ItemData newData : datas) { for (ItemData existingData : type.getTypes()) { if (newData == existingData || newData.matchAlias(existingData).isAtLeast(MatchQuality.EXACT)) // Don't add this data, the item type already contains it! continue newDataLoop; @@ -360,11 +219,11 @@ public void addAlias(AliasName name, String id, @Nullable Map ta } } } - + public void addVariationGroup(String name, VariationGroup group) { variations.put(name, group); } - + @Nullable public VariationGroup getVariationGroup(String name) { return variations.get(name); @@ -378,7 +237,7 @@ public ItemType getAlias(String alias) { } return item; } - + public AliasesMap.@Nullable AliasData getAliasData(ItemData item) { AliasesMap.AliasData data = aliasesMap.matchAlias(item).getData(); if (data == null && parent != null) { @@ -395,7 +254,7 @@ public String getMinecraftId(ItemData item) { } return null; } - + @Nullable public MaterialName getMaterialName(ItemData item) { AliasesMap.AliasData data = getAliasData(item); @@ -404,7 +263,7 @@ public MaterialName getMaterialName(ItemData item) { } return null; } - + @Nullable public EntityData getRelatedEntity(ItemData item) { AliasesMap.AliasData data = getAliasData(item); @@ -434,4 +293,123 @@ public boolean hasAliasForMaterial(Material material) { return materials.contains(material); } + /** + * Represents a variation of material. It could, for example, define one + * more tag or change base id, but keep tag intact. + */ + public static class Variation { + + @Nullable + private final String id; + private final int insertPoint; + + private final Map tags; + private final Map states; + + public Variation(@Nullable String id, int insertPoint, Map tags, Map states) { + this.id = id; + this.insertPoint = insertPoint; + this.tags = tags; + this.states = states; + } + + @Nullable + public String getId() { + return id; + } + + public int getInsertPoint() { + return insertPoint; + } + + @Nullable + public String insertId(@Nullable String inserted) { + if (inserted == null) // Nothing to insert + return id; + inserted = inserted.substring(0, inserted.length() - 1); // Strip out - + if (id == null) // Inserting to nothing + return inserted; + + String id = this.id; + assert id != null; + if (insertPoint == -1) // No place where to insert + return inserted; + + // Insert given string to in middle of our id + String before = id.substring(0, insertPoint); + String after = id.substring(insertPoint + 1); + return before + inserted + after; + } + + public Map getTags() { + return tags; + } + + + public Map getBlockStates() { + return states; + } + + + public Variation merge(Variation other) { + // Merge tags and block states + Map mergedTags = new HashMap<>(other.tags); + mergedTags.putAll(tags); + Map mergedStates = new HashMap<>(other.states); + mergedStates.putAll(states); + + // Potentially merge ids + String id = insertId(other.id); + + return new Variation(id, -1, mergedTags, mergedStates); + } + } + + public static class VariationGroup { + + public final List keys; + + public final List values; + + public VariationGroup() { + this.keys = new ArrayList<>(); + this.values = new ArrayList<>(); + } + + public void put(String key, Variation value) { + keys.add(key); + values.add(value); + } + } + + /** + * Name of an alias used by {@link #addAlias(AliasName, String, Map, Map)} + * for registration. + */ + public static class AliasName { + + /** + * Singular for of alias name. + */ + public final String singular; + + /** + * Plural form of alias name. + */ + public final String plural; + + /** + * Gender of alias name. + */ + public final int gender; + + public AliasName(String singular, String plural, int gender) { + super(); + this.singular = singular; + this.plural = plural; + this.gender = gender; + } + + } + } diff --git a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java index 66b7fa13a34..b3b5c7c92c9 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java +++ b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java @@ -1,30 +1,34 @@ /** - * 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 . - * + * 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 . + *

* Copyright Peter Güttinger, SkriptLang team and contributors */ package ch.njol.skript.bukkitutil; +import ch.njol.skript.Skript; +import ch.njol.util.EnumTypeAdapter; +import com.google.common.io.ByteStreams; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; import java.io.IOException; import java.io.InputStream; 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; @@ -32,39 +36,32 @@ import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.Nullable; -import com.google.common.io.ByteStreams; -import com.google.gson.GsonBuilder; -import com.google.gson.reflect.TypeToken; -import ch.njol.util.EnumTypeAdapter; -import ch.njol.skript.Skript; - /** * Contains helpers for Bukkit's not so safe stuff. */ @SuppressWarnings("deprecation") public class BukkitUnsafe { - + /** * Bukkit's UnsafeValues allows us to do stuff that would otherwise * require NMS. It has existed for a long time, too, so 1.9 support is * not particularly hard to achieve. - * + * * UnsafeValues' existence and behavior is not guaranteed across future versions. */ @Nullable private static final UnsafeValues unsafe = Bukkit.getUnsafe(); + /** + * Maps pre 1.12 ids to materials for variable conversions. + */ + @Nullable + private static Map idMappings; static { if (unsafe == null) throw new Error("UnsafeValues are not available."); } - /** - * Maps pre 1.12 ids to materials for variable conversions. - */ - @Nullable - private static Map idMappings; - /** * Get a material from a minecraft id. * @@ -98,19 +95,20 @@ public static void modifyItemStack(ItemStack stack, String arguments) { throw new IllegalStateException("modifyItemStack could not be performed as UnsafeValues are not available."); unsafe.modifyItemStack(stack, arguments); } - + private static void initIdMappings() { try (InputStream is = Skript.getInstance().getResource("materials/ids.json")) { if (is == null) { throw new AssertionError("missing id mappings"); } String data = new String(ByteStreams.toByteArray(is), StandardCharsets.UTF_8); - - Type type = new TypeToken>(){}.getType(); + + Type type = new TypeToken>() { + }.getType(); Map rawMappings = new GsonBuilder(). - registerTypeAdapterFactory(EnumTypeAdapter.factory) - .create().fromJson(data, type); - + registerTypeAdapterFactory(EnumTypeAdapter.factory) + .create().fromJson(data, type); + // Process raw mappings Map parsed = new HashMap<>(rawMappings.size()); // Legacy material conversion API @@ -122,7 +120,7 @@ private static void initIdMappings() { throw new AssertionError(e); } } - + @Nullable public static Material getMaterialFromId(int id) { if (idMappings == null) { From 2f59fb69af9c1b09ab89d9e7eacf60240098ff20 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Tue, 29 Oct 2024 23:55:50 +0800 Subject: [PATCH 17/17] perf: resolved mentioned --- .../java/ch/njol/skript/aliases/Aliases.java | 13 ++++++----- .../njol/skript/bukkitutil/BukkitUnsafe.java | 22 ++----------------- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/src/main/java/ch/njol/skript/aliases/Aliases.java b/src/main/java/ch/njol/skript/aliases/Aliases.java index a69f2cfac9c..b92a4196f84 100644 --- a/src/main/java/ch/njol/skript/aliases/Aliases.java +++ b/src/main/java/ch/njol/skript/aliases/Aliases.java @@ -51,10 +51,12 @@ public abstract class Aliases { private final static RegexMessage p_every = new RegexMessage("aliases.every", "", " (.+)", Pattern.CASE_INSENSITIVE); private final static RegexMessage p_of_every = new RegexMessage("aliases.of every", "(\\d+) ", " (.+)", Pattern.CASE_INSENSITIVE); private final static RegexMessage p_of = new RegexMessage("aliases.of", "(\\d+) (?:", " )?(.+)", Pattern.CASE_INSENSITIVE); + /** * Go through these whenever aliases are reloaded, and update them. */ private static final Map trackedTypes = new HashMap<>(); + /** * If user had an obscure config option set, don't crash due to missing * Java item types. @@ -402,13 +404,14 @@ private static void loadMissingAliases() { Skript.debug(ChatColor.YELLOW + "Creating temporary alias for: " + key); } } + if (modItemRegistered) { Skript.warning("=============================================================="); - Skript.warning("We found some Materials, which are not registered by Minecraft."); - Skript.warning("For comfort of your coding, we register aliases from to or "); - Skript.warning("Please PAY ATTENTION: The Hybrid Servers are NOT supported now. We don't have enough energy to handle these now(maybe someday), sorry"); - Skript.warning("So, DO NOT report any issues caused by this to us"); - Skript.warning("The server will keep loading after 5 seconds"); + Skript.warning("Some materials were found that seem to be modded."); + Skript.warning("An item that has the id 'mod:item' can be used as 'mod's item' or 'item from mod'."); + Skript.warning("WARNING: Skript does not officially support any modded servers."); + Skript.warning("Any issues you encounter related to modded items will be your responsibility to fix."); + Skript.warning("The server will keep loading after 5 seconds."); Skript.warning("=============================================================="); try { Thread.sleep(5000L); diff --git a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java index b3b5c7c92c9..e7e255f1e72 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java +++ b/src/main/java/ch/njol/skript/bukkitutil/BukkitUnsafe.java @@ -1,21 +1,3 @@ -/** - * 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 . - *

- * Copyright Peter Güttinger, SkriptLang team and contributors - */ package ch.njol.skript.bukkitutil; import ch.njol.skript.Skript; @@ -51,11 +33,11 @@ public class BukkitUnsafe { */ @Nullable private static final UnsafeValues unsafe = Bukkit.getUnsafe(); + /** * Maps pre 1.12 ids to materials for variable conversions. */ - @Nullable - private static Map idMappings; + private static @Nullable Map idMappings; static { if (unsafe == null)