From b59b775490bd9a92d4ed09c4cda55739701c882a Mon Sep 17 00:00:00 2001 From: oddlama Date: Tue, 12 Dec 2023 20:27:55 +0100 Subject: [PATCH] feat: update to minecraft 1.20.4 --- build.gradle.kts | 6 +-- .../org/oddlama/vane/core/command/Param.java | 4 +- .../BukkitEnchantmentWrapper.java | 26 ------------- .../core/enchantments/CustomEnchantment.java | 17 ++------- .../core/enchantments/EnchantmentManager.java | 38 +++++++++---------- .../NativeEnchantmentWrapper.java | 4 ++ .../item/VanillaFunctionalityInhibitor.java | 2 +- .../java/org/oddlama/vane/util/BlockUtil.java | 2 +- .../java/org/oddlama/vane/util/ItemUtil.java | 10 ++--- .../org/oddlama/vane/util/MaterialUtil.java | 2 +- .../main/java/org/oddlama/vane/util/Nms.java | 16 ++++---- .../oddlama/vane/trifles/items/Trowel.java | 2 +- 12 files changed, 48 insertions(+), 81 deletions(-) delete mode 100644 vane-core/src/main/java/org/oddlama/vane/core/enchantments/BukkitEnchantmentWrapper.java diff --git a/build.gradle.kts b/build.gradle.kts index 2b5cdf792..7c2f157ae 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,11 +1,11 @@ plugins { `java-library` - id("io.papermc.paperweight.userdev") version "1.5.5" + id("io.papermc.paperweight.userdev") version "1.5.11" id("xyz.jpenilla.run-paper") version "2.2.0" // Adds runServer and runMojangMappedServer tasks for testing } dependencies { - paperDevBundle("1.20.2-R0.1-SNAPSHOT") + paperDevBundle("1.20.4-R0.1-SNAPSHOT") } java { @@ -53,7 +53,7 @@ configure(subprojects.filter { apply(plugin = "io.papermc.paperweight.userdev") dependencies { - paperDevBundle("1.20.2-R0.1-SNAPSHOT") + paperDevBundle("1.20.4-R0.1-SNAPSHOT") } tasks { diff --git a/vane-core/src/main/java/org/oddlama/vane/core/command/Param.java b/vane-core/src/main/java/org/oddlama/vane/core/command/Param.java index ddf5df1f3..d6c025886 100644 --- a/vane-core/src/main/java/org/oddlama/vane/core/command/Param.java +++ b/vane-core/src/main/java/org/oddlama/vane/core/command/Param.java @@ -292,14 +292,14 @@ public default DynamicChoiceParam choose_enchantment( return choice( "enchantment", sender -> - Arrays.stream(Enchantment.values()).filter(e -> filter.apply(sender, e)).collect(Collectors.toList()), + org.bukkit.Registry.ENCHANTMENT.stream().filter(e -> filter.apply(sender, e)).collect(Collectors.toList()), (sender, e) -> e.getKey().toString(), (sender, str) -> { var parts = str.split(":"); if (parts.length != 2) { return null; } - var e = Enchantment.getByKey(namespaced_key(parts[0], parts[1])); + var e = org.bukkit.Registry.ENCHANTMENT.get(namespaced_key(parts[0], parts[1])); if (!filter.apply(sender, e)) { return null; } diff --git a/vane-core/src/main/java/org/oddlama/vane/core/enchantments/BukkitEnchantmentWrapper.java b/vane-core/src/main/java/org/oddlama/vane/core/enchantments/BukkitEnchantmentWrapper.java deleted file mode 100644 index 3e00debad..000000000 --- a/vane-core/src/main/java/org/oddlama/vane/core/enchantments/BukkitEnchantmentWrapper.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.oddlama.vane.core.enchantments; - -import net.minecraft.world.item.enchantment.Enchantment; -import org.bukkit.craftbukkit.v1_20_R2.enchantments.CraftEnchantment; -import org.jetbrains.annotations.NotNull; - -public class BukkitEnchantmentWrapper extends CraftEnchantment { - - private CustomEnchantment custom_enchantment; - - public BukkitEnchantmentWrapper(CustomEnchantment custom_enchantment, Enchantment native_enchantment) { - super(native_enchantment); - this.custom_enchantment = custom_enchantment; - } - - @Deprecated - @NotNull - @Override - public String getName() { - return custom_enchantment.get_name(); - } - - public CustomEnchantment custom_enchantment() { - return custom_enchantment; - } -} diff --git a/vane-core/src/main/java/org/oddlama/vane/core/enchantments/CustomEnchantment.java b/vane-core/src/main/java/org/oddlama/vane/core/enchantments/CustomEnchantment.java index 2ece99104..706e5fb97 100644 --- a/vane-core/src/main/java/org/oddlama/vane/core/enchantments/CustomEnchantment.java +++ b/vane-core/src/main/java/org/oddlama/vane/core/enchantments/CustomEnchantment.java @@ -9,6 +9,7 @@ import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.EnchantmentTarget; import org.bukkit.inventory.ItemStack; +import org.bukkit.craftbukkit.v1_20_R3.enchantments.CraftEnchantment; import org.jetbrains.annotations.NotNull; import org.oddlama.vane.annotation.enchantment.Rarity; import org.oddlama.vane.annotation.enchantment.VaneEnchantment; @@ -36,7 +37,6 @@ public class CustomEnchantment> extends Listener { private String name; private NamespacedKey key; private NativeEnchantmentWrapper native_wrapper; - private BukkitEnchantmentWrapper bukkit_wrapper; private final Set supersedes = new HashSet<>(); @@ -72,27 +72,16 @@ public CustomEnchantment(Context context, boolean default_enabled) { native_wrapper = new NativeEnchantmentWrapper(this); Nms.register_enchantment(key(), native_wrapper); - // After registering in NMS we can create a wrapper for bukkit - bukkit_wrapper = new BukkitEnchantmentWrapper(this, native_wrapper); - Enchantment.registerEnchantment(bukkit_wrapper); - // Automatic recipes and loot table config and registration recipes = new Recipes(get_context(), this.key, this::default_recipes); loot_tables = new LootTables(get_context(), this.key, this::default_loot_tables); } - /** - * Returns the bukkit wrapper for the given custom enchantment. - */ - public static BukkitEnchantmentWrapper bukkit(Class> cls) { - return instances.get(cls).bukkit(); - } - /** * Returns the bukkit wrapper for this enchantment. */ - public final BukkitEnchantmentWrapper bukkit() { - return bukkit_wrapper; + public final Enchantment bukkit() { + return CraftEnchantment.minecraftToBukkit(native_wrapper); } /** diff --git a/vane-core/src/main/java/org/oddlama/vane/core/enchantments/EnchantmentManager.java b/vane-core/src/main/java/org/oddlama/vane/core/enchantments/EnchantmentManager.java index a96dc4d1a..5b29d8c7a 100644 --- a/vane-core/src/main/java/org/oddlama/vane/core/enchantments/EnchantmentManager.java +++ b/vane-core/src/main/java/org/oddlama/vane/core/enchantments/EnchantmentManager.java @@ -27,6 +27,8 @@ import org.oddlama.vane.util.ItemUtil; import org.oddlama.vane.util.StorageUtil; +import org.bukkit.craftbukkit.v1_20_R3.enchantments.CraftEnchantment; + import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TranslatableComponent; @@ -35,15 +37,6 @@ public class EnchantmentManager extends Listener { public EnchantmentManager(Context context) { super(context); - - try { - final var accepting = Enchantment.class.getDeclaredField("acceptingNew"); - accepting.setAccessible(true); - accepting.set(null, true); - } catch (NoSuchFieldException | IllegalAccessException e) { - get_module().log.severe("Could not re-enable enchantment registration! Shutting down."); - get_module().getServer().shutdown(); - } } public ItemStack update_enchanted_item(ItemStack item_stack) { @@ -88,8 +81,9 @@ private void remove_superseded(ItemStack item_stack, Map e // 1. Build a list of all enchantments that would be removed, because // they are superseded by some enchantment. final var to_remove_inclusive = enchantments.keySet().stream() - .filter(x -> x instanceof BukkitEnchantmentWrapper) - .map(x -> ((BukkitEnchantmentWrapper)x).custom_enchantment().supersedes()) + .map(x -> ((CraftEnchantment)x).getHandle()) + .filter(x -> x instanceof NativeEnchantmentWrapper) + .map(x -> ((NativeEnchantmentWrapper)x).custom().supersedes()) .flatMap(Set::stream) .collect(Collectors.toSet()); @@ -99,13 +93,19 @@ private void remove_superseded(ItemStack item_stack, Map e // enchantments to remove. Consider this: A supersedes B, and B supersedes C, but // A doesn't supersede C. Now an item with A B and C should get reduced to // A and C, not just to A. - enchantments.keySet().stream() - .filter(x -> x instanceof BukkitEnchantmentWrapper) - .filter(x -> !to_remove_inclusive.contains(x.getKey())) // Ignore enchantments that are themselves removed. - .map(x -> ((BukkitEnchantmentWrapper)x).custom_enchantment().supersedes()) + var to_remove = enchantments.keySet().stream() + .map(x -> ((CraftEnchantment)x).getHandle()) + .filter(x -> x instanceof NativeEnchantmentWrapper) + .filter(x -> !to_remove_inclusive.contains(((NativeEnchantmentWrapper)x).custom().key())) // Ignore enchantments that are themselves removed. + .map(x -> ((NativeEnchantmentWrapper)x).custom().supersedes()) .flatMap(Set::stream) - .map(Enchantment::getByKey) - .forEach(item_stack::removeEnchantment); + .map(x -> org.bukkit.Registry.ENCHANTMENT.get(x)) + .collect(Collectors.toSet()); + + for (var e : to_remove) { + item_stack.removeEnchantment(e); + enchantments.remove(e); + } } private void update_lore(ItemStack item_stack, Map enchantments) { @@ -114,7 +114,7 @@ private void update_lore(ItemStack item_stack, Map enchant final var vane_enchantments = enchantments .entrySet() .stream() - .filter(p -> p.getKey() instanceof BukkitEnchantmentWrapper) + .filter(p -> ((CraftEnchantment)p.getKey()).getHandle() instanceof NativeEnchantmentWrapper) .sorted(Map.Entry.comparingByKey(Comparator.comparing(x -> x.getKey().toString())) .thenComparing(Map.Entry.comparingByValue()) ) @@ -127,7 +127,7 @@ private void update_lore(ItemStack item_stack, Map enchant lore.removeIf(this::is_enchantment_lore); lore.addAll(0, vane_enchantments.stream().map(ench -> - ItemUtil.add_sentinel(((BukkitEnchantmentWrapper) ench.getKey()).custom_enchantment().display_name(ench.getValue()), SENTINEL) + ItemUtil.add_sentinel(((NativeEnchantmentWrapper)((CraftEnchantment) ench.getKey()).getHandle()).custom().display_name(ench.getValue()), SENTINEL) ).toList()); // Set lore diff --git a/vane-core/src/main/java/org/oddlama/vane/core/enchantments/NativeEnchantmentWrapper.java b/vane-core/src/main/java/org/oddlama/vane/core/enchantments/NativeEnchantmentWrapper.java index 71f865f9e..6758e2a3e 100644 --- a/vane-core/src/main/java/org/oddlama/vane/core/enchantments/NativeEnchantmentWrapper.java +++ b/vane-core/src/main/java/org/oddlama/vane/core/enchantments/NativeEnchantmentWrapper.java @@ -38,6 +38,10 @@ public Enchantment.Rarity getRarity() { } } + public CustomEnchantment custom() { + return this.enchantment; + } + @Override public int getMinLevel() { return enchantment.min_level(); diff --git a/vane-core/src/main/java/org/oddlama/vane/core/item/VanillaFunctionalityInhibitor.java b/vane-core/src/main/java/org/oddlama/vane/core/item/VanillaFunctionalityInhibitor.java index c9855956e..f97bbe77a 100644 --- a/vane-core/src/main/java/org/oddlama/vane/core/item/VanillaFunctionalityInhibitor.java +++ b/vane-core/src/main/java/org/oddlama/vane/core/item/VanillaFunctionalityInhibitor.java @@ -4,7 +4,7 @@ import static org.oddlama.vane.util.Nms.item_handle; import org.bukkit.Keyed; -import org.bukkit.craftbukkit.v1_20_R2.inventory.CraftItemStack; +import org.bukkit.craftbukkit.v1_20_R3.inventory.CraftItemStack; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.EntityType; import org.bukkit.entity.Item; diff --git a/vane-core/src/main/java/org/oddlama/vane/util/BlockUtil.java b/vane-core/src/main/java/org/oddlama/vane/util/BlockUtil.java index b6b62e46a..2dc61e84e 100644 --- a/vane-core/src/main/java/org/oddlama/vane/util/BlockUtil.java +++ b/vane-core/src/main/java/org/oddlama/vane/util/BlockUtil.java @@ -15,7 +15,7 @@ import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.Skull; -import org.bukkit.craftbukkit.v1_20_R2.CraftWorld; +import org.bukkit.craftbukkit.v1_20_R3.CraftWorld; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; diff --git a/vane-core/src/main/java/org/oddlama/vane/util/ItemUtil.java b/vane-core/src/main/java/org/oddlama/vane/util/ItemUtil.java index 9dc3d9c4c..33c7ae701 100644 --- a/vane-core/src/main/java/org/oddlama/vane/util/ItemUtil.java +++ b/vane-core/src/main/java/org/oddlama/vane/util/ItemUtil.java @@ -20,8 +20,8 @@ import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.OfflinePlayer; -import org.bukkit.craftbukkit.v1_20_R2.enchantments.CraftEnchantment; -import org.bukkit.craftbukkit.v1_20_R2.inventory.CraftItemStack; +import org.bukkit.craftbukkit.v1_20_R3.enchantments.CraftEnchantment; +import org.bukkit.craftbukkit.v1_20_R3.inventory.CraftItemStack; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -157,12 +157,12 @@ public static int compare_enchantments(final ItemStack item_a, final ItemStack i final var a_rarity = ae .keySet() .stream() - .mapToInt(e -> CraftEnchantment.getRaw(e).getRarity().getWeight()) + .mapToInt(e -> ((CraftEnchantment)e).getHandle().getRarity().getWeight()) .sum(); final var b_rarity = be .keySet() .stream() - .mapToInt(e -> CraftEnchantment.getRaw(e).getRarity().getWeight()) + .mapToInt(e -> ((CraftEnchantment)e).getHandle().getRarity().getWeight()) .sum(); if (a_rarity != b_rarity) { return b_rarity - a_rarity; @@ -334,7 +334,7 @@ private static ItemStack apply_enchants(final ItemStack item_stack, @Nullable St key = key.substring(0, level_delim); } - final var ench = Enchantment.getByKey(NamespacedKey.fromString(key)); + final var ench = org.bukkit.Registry.ENCHANTMENT.get(NamespacedKey.fromString(key)); if (ench == null) { throw new IllegalArgumentException( "Cannot apply unknown enchantment '" + key + "' to item '" + item_stack + "'"); diff --git a/vane-core/src/main/java/org/oddlama/vane/util/MaterialUtil.java b/vane-core/src/main/java/org/oddlama/vane/util/MaterialUtil.java index 76c3cd3a4..00c70a5f0 100644 --- a/vane-core/src/main/java/org/oddlama/vane/util/MaterialUtil.java +++ b/vane-core/src/main/java/org/oddlama/vane/util/MaterialUtil.java @@ -59,7 +59,7 @@ public static boolean is_replaceable_grass(Material type) { default: return false; case TALL_GRASS: - case GRASS: + case SHORT_GRASS: return true; } } diff --git a/vane-core/src/main/java/org/oddlama/vane/util/Nms.java b/vane-core/src/main/java/org/oddlama/vane/util/Nms.java index 615f1c362..99bd35aad 100644 --- a/vane-core/src/main/java/org/oddlama/vane/util/Nms.java +++ b/vane-core/src/main/java/org/oddlama/vane/util/Nms.java @@ -8,12 +8,13 @@ import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.NamespacedKey; -import org.bukkit.craftbukkit.v1_20_R2.CraftServer; -import org.bukkit.craftbukkit.v1_20_R2.CraftWorld; -import org.bukkit.craftbukkit.v1_20_R2.entity.CraftEntity; -import org.bukkit.craftbukkit.v1_20_R2.entity.CraftPlayer; -import org.bukkit.craftbukkit.v1_20_R2.inventory.CraftItemStack; -import org.bukkit.craftbukkit.v1_20_R2.util.CraftNamespacedKey; +import org.bukkit.craftbukkit.v1_20_R3.CraftServer; +import org.bukkit.craftbukkit.v1_20_R3.CraftWorld; +import org.bukkit.craftbukkit.v1_20_R3.enchantments.CraftEnchantment; +import org.bukkit.craftbukkit.v1_20_R3.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_20_R3.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_20_R3.inventory.CraftItemStack; +import org.bukkit.craftbukkit.v1_20_R3.util.CraftNamespacedKey; import org.bukkit.enchantments.EnchantmentTarget; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -51,8 +52,7 @@ public static void register_enchantment(NamespacedKey key, Enchantment enchantme } public static org.bukkit.enchantments.Enchantment bukkit_enchantment(Enchantment enchantment) { - final var key = BuiltInRegistries.ENCHANTMENT.getKey(enchantment); - return org.bukkit.enchantments.Enchantment.getByKey(CraftNamespacedKey.fromMinecraft(key)); + return CraftEnchantment.minecraftToBukkit(enchantment); } @NotNull diff --git a/vane-trifles/src/main/java/org/oddlama/vane/trifles/items/Trowel.java b/vane-trifles/src/main/java/org/oddlama/vane/trifles/items/Trowel.java index 02e96ac15..2e1c61ded 100644 --- a/vane-trifles/src/main/java/org/oddlama/vane/trifles/items/Trowel.java +++ b/vane-trifles/src/main/java/org/oddlama/vane/trifles/items/Trowel.java @@ -14,7 +14,7 @@ import org.bukkit.Sound; import org.bukkit.SoundCategory; import org.bukkit.Tag; -import org.bukkit.craftbukkit.v1_20_R2.block.CraftBlock; +import org.bukkit.craftbukkit.v1_20_R3.block.CraftBlock; import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.EventHandler;