From 040f85659d8a0b2a4df56b5ed34682374424b4f7 Mon Sep 17 00:00:00 2001 From: EnZaXD Date: Fri, 2 Aug 2024 11:05:17 +0200 Subject: [PATCH] Fix enchantment emulation in 1.20.5->1.21 on non Paper servers (#4060) --- .../PaperPlayerChangeItemListener.java | 51 +++++++++++++++++++ .../PlayerChangeItemListener.java | 45 +++++++--------- .../bukkit/platform/BukkitViaLoader.java | 9 +++- gradle.properties | 2 +- 4 files changed, 76 insertions(+), 31 deletions(-) create mode 100644 bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/v1_20_5to1_21/PaperPlayerChangeItemListener.java diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/v1_20_5to1_21/PaperPlayerChangeItemListener.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/v1_20_5to1_21/PaperPlayerChangeItemListener.java new file mode 100644 index 000000000..620aaf7ce --- /dev/null +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/v1_20_5to1_21/PaperPlayerChangeItemListener.java @@ -0,0 +1,51 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * This program 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. + * + * This program 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 this program. If not, see . + */ +package com.viaversion.viaversion.bukkit.listeners.v1_20_5to1_21; + +import com.viaversion.viaversion.ViaVersionPlugin; +import io.papermc.paper.event.player.PlayerInventorySlotChangeEvent; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; + +public final class PaperPlayerChangeItemListener extends PlayerChangeItemListener { + + public PaperPlayerChangeItemListener(final ViaVersionPlugin plugin) { + super(plugin); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onPlayerInventorySlotChangedEvent(final PlayerInventorySlotChangeEvent event) { + final Player player = event.getPlayer(); + final ItemStack item = event.getNewItemStack(); + final PlayerInventory inventory = player.getInventory(); + final int slot = event.getSlot(); + if (slot == inventory.getHeldItemSlot()) { + sendAttributeUpdate(player, item, Slot.HAND); + } else if (slot == 36) { + sendAttributeUpdate(player, item, Slot.BOOTS); + } else if (slot == 37) { + sendAttributeUpdate(player, item, Slot.LEGGINGS); + } else if (slot == 39) { + sendAttributeUpdate(player, item, Slot.HELMET); + } + } + +} diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/v1_20_5to1_21/PlayerChangeItemListener.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/v1_20_5to1_21/PlayerChangeItemListener.java index b2f776659..3af953882 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/v1_20_5to1_21/PlayerChangeItemListener.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/v1_20_5to1_21/PlayerChangeItemListener.java @@ -23,50 +23,31 @@ import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.bukkit.listeners.ViaBukkitListener; import com.viaversion.viaversion.protocols.v1_20_5to1_21.Protocol1_20_5To1_21; import com.viaversion.viaversion.protocols.v1_20_5to1_21.storage.EfficiencyAttributeStorage; -import io.papermc.paper.event.player.PlayerInventorySlotChangeEvent; -import org.bukkit.NamespacedKey; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerItemHeldEvent; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.PlayerInventory; import org.checkerframework.checker.nullness.qual.Nullable; /** * For some reason, mining efficiency is not calculated by the client anymore, but by the server, * then sending the current value to the client every time the item changes. This roughly emulates that behavior. */ -public final class PlayerChangeItemListener extends ViaBukkitListener { +public class PlayerChangeItemListener extends ViaBukkitListener { - private final Enchantment efficiency = Enchantment.getByKey(NamespacedKey.minecraft("efficiency")); - private final Enchantment aquaAffinity = Enchantment.getByKey(NamespacedKey.minecraft("aqua_affinity")); - private final Enchantment depthStrider = Enchantment.getByKey(NamespacedKey.minecraft("depth_strider")); - private final Enchantment soulSpeed = Enchantment.getByKey(NamespacedKey.minecraft("soul_speed")); - private final Enchantment swiftSneak = Enchantment.getByKey(NamespacedKey.minecraft("swift_sneak")); + // Use legacy function and names here to support all versions + private final Enchantment efficiency = getByName("efficiency", "DIG_SPEED"); + private final Enchantment aquaAffinity = getByName("aqua_affinity", "WATER_WORKER"); + private final Enchantment depthStrider = getByName("depth_strider", "DEPTH_STRIDER"); + private final Enchantment soulSpeed = getByName("soul_speed", "SOUL_SPEED"); + private final Enchantment swiftSneak = getByName("swift_sneak", "SWIFT_SNEAK"); public PlayerChangeItemListener(final ViaVersionPlugin plugin) { super(plugin, Protocol1_20_5To1_21.class); } - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onPlayerInventorySlotChangedEvent(final PlayerInventorySlotChangeEvent event) { - final Player player = event.getPlayer(); - final ItemStack item = event.getNewItemStack(); - final PlayerInventory inventory = player.getInventory(); - final int slot = event.getSlot(); - if (slot == inventory.getHeldItemSlot()) { - sendAttributeUpdate(player, item, Slot.HAND); - } else if (slot == 36) { - sendAttributeUpdate(player, item, Slot.BOOTS); - } else if (slot == 37) { - sendAttributeUpdate(player, item, Slot.LEGGINGS); - } else if (slot == 39) { - sendAttributeUpdate(player, item, Slot.HELMET); - } - } - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onPlayerItemHeld(final PlayerItemHeldEvent event) { final Player player = event.getPlayer(); @@ -74,7 +55,7 @@ public final class PlayerChangeItemListener extends ViaBukkitListener { sendAttributeUpdate(player, item, Slot.HAND); } - private void sendAttributeUpdate(final Player player, @Nullable final ItemStack item, final Slot slot) { + void sendAttributeUpdate(final Player player, @Nullable final ItemStack item, final Slot slot) { final UserConnection connection = Via.getAPI().getConnection(player.getUniqueId()); if (connection == null || !isOnPipe(player)) { return; @@ -105,7 +86,15 @@ public final class PlayerChangeItemListener extends ViaBukkitListener { storage.setEnchants(player.getEntityId(), connection, efficiencyLevel, soulSpeedLevel, swiftSneakLevel, aquaAffinityLevel, depthStriderLevel); } - private enum Slot { + enum Slot { HAND, BOOTS, LEGGINGS, HELMET } + + private Enchantment getByName(final String newName, final String oldName) { + final Enchantment enchantment = Enchantment.getByName(newName); + if (enchantment == null) { + return Enchantment.getByName(oldName); + } + return enchantment; + } } diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaLoader.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaLoader.java index fe549ae35..c36d4bce8 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaLoader.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaLoader.java @@ -33,6 +33,7 @@ import com.viaversion.viaversion.bukkit.listeners.protocol1_9to1_8.BlockListener import com.viaversion.viaversion.bukkit.listeners.protocol1_9to1_8.DeathListener; import com.viaversion.viaversion.bukkit.listeners.protocol1_9to1_8.HandItemCache; import com.viaversion.viaversion.bukkit.listeners.protocol1_9to1_8.PaperPatch; +import com.viaversion.viaversion.bukkit.listeners.v1_20_5to1_21.PaperPlayerChangeItemListener; import com.viaversion.viaversion.bukkit.listeners.v1_20_5to1_21.PlayerChangeItemListener; import com.viaversion.viaversion.bukkit.providers.BukkitAckSequenceProvider; import com.viaversion.viaversion.bukkit.providers.BukkitBlockConnectionProvider; @@ -179,8 +180,12 @@ public class BukkitViaLoader implements ViaPlatformLoader { Via.getManager().getProviders().use(AckSequenceProvider.class, new BukkitAckSequenceProvider(plugin)); new BlockBreakListener(plugin).register(); } - if (serverProtocolVersion.olderThan(ProtocolVersion.v1_21) && PaperViaInjector.hasClass("io.papermc.paper.event.player.PlayerInventorySlotChangeEvent")) { - new PlayerChangeItemListener(plugin).register(); + if (serverProtocolVersion.olderThan(ProtocolVersion.v1_21)) { + if (PaperViaInjector.hasClass("io.papermc.paper.event.player.PlayerInventorySlotChangeEvent")) { + new PaperPlayerChangeItemListener(plugin).register(); + } else { + new PlayerChangeItemListener(plugin).register(); + } } } diff --git a/gradle.properties b/gradle.properties index d4feb08aa..291040f85 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ # Project properties - we put these here so they can be modified without causing a recompile of the build scripts -projectVersion=5.0.2 +projectVersion=5.0.3-SNAPSHOT # Smile emoji mcVersions=1.21,1.20.6,1.20.5,1.20.4, 1.20.3, 1.20.2, 1.20.1, 1.20, 1.19.4, 1.19.3, 1.19.2, 1.19.1, 1.19, 1.18.2, 1.18.1, 1.18, 1.17.1, 1.17, 1.16.5, 1.16.4, 1.16.3, 1.16.2, 1.16.1, 1.16, 1.15.2, 1.15.1, 1.15, 1.14.4, 1.14.3, 1.14.2, 1.14.1, 1.14, 1.13.2, 1.13.1, 1.13, 1.12.2, 1.12.1, 1.12, 1.11.2, 1.11.1, 1.11, 1.10.2, 1.10.1, 1.10, 1.9.4, 1.9.3, 1.9.2, 1.9.1, 1.9, 1.8.9