From 734bc9e88c06c6432fb734983add7ecd3ef11039 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Fri, 3 Feb 2023 11:02:22 -0500 Subject: [PATCH] Add armor toggle patch for Bukkit (#3210) --- .../api/configuration/ViaVersionConfig.java | 2 + .../ArmorToggleListener.java | 56 +++++++++++++++++++ .../bukkit/platform/BukkitViaConfig.java | 7 +++ .../bukkit/platform/BukkitViaLoader.java | 5 ++ .../configuration/AbstractViaConfig.java | 5 ++ .../resources/assets/viaversion/config.yml | 3 + 6 files changed, 78 insertions(+) create mode 100644 bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/protocol1_19_4To1_19_3/ArmorToggleListener.java diff --git a/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java b/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java index eee4564ff..bad8c22c2 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java +++ b/api/src/main/java/com/viaversion/viaversion/api/configuration/ViaVersionConfig.java @@ -458,4 +458,6 @@ public interface ViaVersionConfig { boolean cache1_17Light(); @Nullable String chatTypeFormat(String translationKey); + + boolean isArmorToggleFix(); } diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/protocol1_19_4To1_19_3/ArmorToggleListener.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/protocol1_19_4To1_19_3/ArmorToggleListener.java new file mode 100644 index 000000000..4caaf573e --- /dev/null +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/protocol1_19_4To1_19_3/ArmorToggleListener.java @@ -0,0 +1,56 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2023 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.protocol1_19_4To1_19_3; + +import com.viaversion.viaversion.ViaVersionPlugin; +import com.viaversion.viaversion.bukkit.listeners.ViaBukkitListener; +import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.Protocol1_19_4To1_19_3; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemStack; + +public final class ArmorToggleListener extends ViaBukkitListener { + + public ArmorToggleListener(ViaVersionPlugin plugin) { + super(plugin, Protocol1_19_4To1_19_3.class); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void itemUse(final PlayerInteractEvent event) { + final Player player = event.getPlayer(); + if (!isOnPipe(player)) return; + + final ItemStack item = event.getItem(); + if (item == null) { + return; + } + final EquipmentSlot equipmentSlot = item.getType().getEquipmentSlot(); + // Name comparison for OFF_HAND so 1.8 doesn't complain + if (equipmentSlot != EquipmentSlot.HAND && !equipmentSlot.name().equals("OFF_HAND")) { + final ItemStack armor = player.getInventory().getItem(equipmentSlot); + // If two pieces of armor are equal, the client will do nothing. + if (armor != null && armor.getType() != Material.AIR && !armor.equals(item)) { + player.updateInventory(); + } + } + } +} diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaConfig.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaConfig.java index 38d5be247..e6a6da0bf 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaConfig.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaConfig.java @@ -33,6 +33,7 @@ public class BukkitViaConfig extends AbstractViaConfig { private boolean hitboxFix1_9; private boolean hitboxFix1_14; private String blockConnectionMethod; + private boolean armorToggleFix; public BukkitViaConfig() { super(new File(((Plugin) Via.getPlatform()).getDataFolder(), "config.yml")); @@ -47,6 +48,7 @@ public class BukkitViaConfig extends AbstractViaConfig { hitboxFix1_9 = getBoolean("change-1_9-hitbox", false); hitboxFix1_14 = getBoolean("change-1_14-hitbox", false); blockConnectionMethod = getString("blockconnection-method", "packet"); + armorToggleFix = getBoolean("armor-toggle-fix", true); } @Override @@ -78,6 +80,11 @@ public class BukkitViaConfig extends AbstractViaConfig { return blockConnectionMethod; } + @Override + public boolean isArmorToggleFix() { + return armorToggleFix; + } + @Override public List getUnsupportedOptions() { return UNSUPPORTED; 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 54f8e2b80..bafddaad2 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 @@ -28,6 +28,7 @@ import com.viaversion.viaversion.bukkit.listeners.JoinListener; import com.viaversion.viaversion.bukkit.listeners.UpdateListener; import com.viaversion.viaversion.bukkit.listeners.multiversion.PlayerSneakListener; import com.viaversion.viaversion.bukkit.listeners.protocol1_15to1_14_4.EntityToggleGlideListener; +import com.viaversion.viaversion.bukkit.listeners.protocol1_19_4To1_19_3.ArmorToggleListener; import com.viaversion.viaversion.bukkit.listeners.protocol1_19to1_18_2.BlockBreakListener; import com.viaversion.viaversion.bukkit.listeners.protocol1_9to1_8.ArmorListener; import com.viaversion.viaversion.bukkit.listeners.protocol1_9to1_8.BlockListener; @@ -142,6 +143,10 @@ public class BukkitViaLoader implements ViaPlatformLoader { } } + if (serverProtocolVersion < ProtocolVersion.v1_19_4.getVersion() && plugin.getConf().isArmorToggleFix()) { + storeListener(new ArmorToggleListener(plugin)).register(); + } + /* Providers */ if (serverProtocolVersion < ProtocolVersion.v1_9.getVersion()) { Via.getManager().getProviders().use(MovementTransmitterProvider.class, new BukkitViaMovementTransmitter()); diff --git a/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java b/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java index ad84bcd69..516423cf6 100644 --- a/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java +++ b/common/src/main/java/com/viaversion/viaversion/configuration/AbstractViaConfig.java @@ -533,4 +533,9 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf public @Nullable String chatTypeFormat(final String translationKey) { return chatTypeFormats.get(translationKey); } + + @Override + public boolean isArmorToggleFix() { + return false; + } } diff --git a/common/src/main/resources/assets/viaversion/config.yml b/common/src/main/resources/assets/viaversion/config.yml index b74390628..69dbda161 100644 --- a/common/src/main/resources/assets/viaversion/config.yml +++ b/common/src/main/resources/assets/viaversion/config.yml @@ -172,6 +172,9 @@ chat-types-1_19: chat.type.team.text: "%s <%s> %s" chat.type.emote: "* %s %s" # +# Force-update 1.19.4+ player's inventory when they try to swap armor in a pre-occupied slot. +armor-toggle-fix: true +# #----------------------------------------------------------# # 1.9+ CLIENTS ON 1.8 SERVERS OPTIONS # #----------------------------------------------------------#