Skip to content

Commit

Permalink
feat: update to minecraft 1.20.4
Browse files Browse the repository at this point in the history
  • Loading branch information
oddlama committed Dec 12, 2023
1 parent 7d3edb7 commit b59b775
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 81 deletions.
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,14 @@ public default DynamicChoiceParam<Enchantment> 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;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,7 +37,6 @@ public class CustomEnchantment<T extends Module<T>> extends Listener<T> {
private String name;
private NamespacedKey key;
private NativeEnchantmentWrapper native_wrapper;
private BukkitEnchantmentWrapper bukkit_wrapper;

private final Set<NamespacedKey> supersedes = new HashSet<>();

Expand Down Expand Up @@ -72,27 +72,16 @@ public CustomEnchantment(Context<T> 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<T>(get_context(), this.key, this::default_recipes);
loot_tables = new LootTables<T>(get_context(), this.key, this::default_loot_tables);
}

/**
* Returns the bukkit wrapper for the given custom enchantment.
*/
public static BukkitEnchantmentWrapper bukkit(Class<? extends CustomEnchantment<?>> 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -35,15 +37,6 @@ public class EnchantmentManager extends Listener<Core> {

public EnchantmentManager(Context<Core> 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) {
Expand Down Expand Up @@ -88,8 +81,9 @@ private void remove_superseded(ItemStack item_stack, Map<Enchantment, Integer> 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());

Expand All @@ -99,13 +93,19 @@ private void remove_superseded(ItemStack item_stack, Map<Enchantment, Integer> 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<Enchantment, Integer> enchantments) {
Expand All @@ -114,7 +114,7 @@ private void update_lore(ItemStack item_stack, Map<Enchantment, Integer> 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.<Enchantment, Integer>comparingByKey(Comparator.comparing(x -> x.getKey().toString()))
.thenComparing(Map.Entry.comparingByValue())
)
Expand All @@ -127,7 +127,7 @@ private void update_lore(ItemStack item_stack, Map<Enchantment, Integer> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public Enchantment.Rarity getRarity() {
}
}

public CustomEnchantment<?> custom() {
return this.enchantment;
}

@Override
public int getMinLevel() {
return enchantment.min_level();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions vane-core/src/main/java/org/oddlama/vane/util/ItemUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 + "'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
16 changes: 8 additions & 8 deletions vane-core/src/main/java/org/oddlama/vane/util/Nms.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b59b775

Please sign in to comment.