From 2b51eef4789560a58604321958cae88a5750b4be Mon Sep 17 00:00:00 2001 From: TomTom <93038247+AverageGithub@users.noreply.github.com> Date: Mon, 1 Jan 2024 13:14:27 +0100 Subject: [PATCH] fix: fix glitch, add config option for listening to interact event --- build.gradle | 2 +- .../gui/impl/SmithingTable_V1_16.java | 10 +++++-- .../gui/impl/SmithingTable_V1_20.java | 16 ++++++++--- .../axsmithing/listener/InteractListener.java | 28 +++++++++++++++++++ src/main/resources/config.yml | 4 ++- 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/build.gradle b/build.gradle index bf5bd6c..7e65207 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group = 'com.artillexstudios.axsmithing' -version = '1.4' +version = '1.5' repositories { mavenCentral() diff --git a/src/main/java/com/artillexstudios/axsmithing/gui/impl/SmithingTable_V1_16.java b/src/main/java/com/artillexstudios/axsmithing/gui/impl/SmithingTable_V1_16.java index 8f7b654..161e37a 100644 --- a/src/main/java/com/artillexstudios/axsmithing/gui/impl/SmithingTable_V1_16.java +++ b/src/main/java/com/artillexstudios/axsmithing/gui/impl/SmithingTable_V1_16.java @@ -10,6 +10,7 @@ import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryDragEvent; @@ -217,9 +218,6 @@ private boolean checkRecipes(Inventory inv, ItemStack finalBase, ItemStack final } private boolean checkRecipe(Inventory inventory, ItemStack finalBase, ItemStack finalAddition) { - if (inventory.getItem(outputSlot) != null && !inventory.getItem(outputSlot).getType().isAir()) { - return false; - } Iterator recipeIterator = Bukkit.getServer().recipeIterator(); while (recipeIterator.hasNext()) { inventory.setItem(outputSlot, new ItemStack(Material.AIR)); @@ -247,6 +245,12 @@ private boolean checkRecipe(Inventory inventory, ItemStack finalBase, ItemStack return false; } + + ItemStack it; + if ((it = inventory.getItem(outputSlot)) != null && it.getType() == item.getType()) { + return false; + } + item.setItemMeta(baseItemMeta); inventory.setItem(outputSlot, item); return true; diff --git a/src/main/java/com/artillexstudios/axsmithing/gui/impl/SmithingTable_V1_20.java b/src/main/java/com/artillexstudios/axsmithing/gui/impl/SmithingTable_V1_20.java index eb65203..250e55e 100644 --- a/src/main/java/com/artillexstudios/axsmithing/gui/impl/SmithingTable_V1_20.java +++ b/src/main/java/com/artillexstudios/axsmithing/gui/impl/SmithingTable_V1_20.java @@ -108,7 +108,6 @@ public void handleClick(InventoryClickEvent event) { if (!(event.getInventory().getHolder() instanceof SmithingTable_V1_20)) return; if ((event.getSlot() == upgradeSlot || event.getSlot() == itemSlot || event.getSlot() == templateSlot) && event.getInventory().getItem(outputSlot) != null) { - event.getInventory().setItem(outputSlot, null); if (checkRecipes(event.getInventory(), event.getInventory().getItem(templateSlot), event.getInventory().getItem(itemSlot), event.getInventory().getItem(upgradeSlot))) { event.getInventory().setItem(outputSlot, null); } @@ -277,14 +276,12 @@ private boolean checkRecipes(Inventory inv, ItemStack finalTemplate, ItemStack f } private boolean checkRecipe(Inventory inventory, ItemStack finalTemplate, ItemStack finalBase, ItemStack finalAddition) { - if (inventory.getItem(outputSlot) != null && !inventory.getItem(outputSlot).getType().isAir()) { - return false; - } Iterator recipeIterator = Bukkit.getServer().recipeIterator(); while (recipeIterator.hasNext()) { Recipe recipe = recipeIterator.next(); if (recipe instanceof SmithingTrimRecipe trimRecipe) { + if (finalBase == null || finalAddition == null || finalTemplate == null) return false; boolean test1 = trimRecipe.getTemplate().test(finalTemplate); boolean test2 = trimRecipe.getBase().test(finalBase); boolean test3 = trimRecipe.getAddition().test(finalAddition); @@ -299,6 +296,11 @@ private boolean checkRecipe(Inventory inventory, ItemStack finalTemplate, ItemSt clone.setItemMeta(armorMeta); } + ItemStack it; + if ((it = inventory.getItem(outputSlot)) != null && it.getType() == clone.getType()) { + return false; + } + inventory.setItem(outputSlot, clone); return true; } else { @@ -329,6 +331,12 @@ private boolean checkRecipe(Inventory inventory, ItemStack finalTemplate, ItemSt if (item == null) { return false; } + + ItemStack it; + if ((it = inventory.getItem(outputSlot)) != null && it.getType() == item.getType()) { + return false; + } + item.setItemMeta(baseItemMeta); inventory.setItem(outputSlot, item); return true; diff --git a/src/main/java/com/artillexstudios/axsmithing/listener/InteractListener.java b/src/main/java/com/artillexstudios/axsmithing/listener/InteractListener.java index 11f29bb..23dad0b 100644 --- a/src/main/java/com/artillexstudios/axsmithing/listener/InteractListener.java +++ b/src/main/java/com/artillexstudios/axsmithing/listener/InteractListener.java @@ -4,15 +4,43 @@ import com.artillexstudios.axapi.utils.Version; import com.artillexstudios.axsmithing.AxSmithingPlugin; import com.viaversion.viaversion.api.Via; +import org.bukkit.Material; import org.bukkit.entity.Player; +import org.bukkit.event.Event; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.InventoryType; +import org.bukkit.event.player.PlayerInteractEvent; import org.jetbrains.annotations.NotNull; public class InteractListener implements Listener { + @EventHandler + public void onPlayerInteractEvent(@NotNull final PlayerInteractEvent event) { + if (event.getClickedBlock() == null) return; + if (event.getClickedBlock().getType() != Material.SMITHING_TABLE) return; + if (event.getAction().equals(Action.LEFT_CLICK_BLOCK)) return; + if (!AxSmithingPlugin.getConfiguration().getBoolean("listen-to-interact-event")) return; + + if (AxSmithingPlugin.is1_20()) { + if (Via.getAPI().getPlayerVersion(event.getPlayer()) >= Version.v1_20_1.protocolId && !AxSmithingPlugin.getConfiguration().getBoolean("menu.1_20.force-for-1_20-clients")) { + return; + } + } else { + if (Via.getAPI().getPlayerVersion(event.getPlayer()) != Version.v1_20_1.protocolId && Via.getAPI().getPlayerVersion(event.getPlayer()) != Version.v1_20_2.protocolId) { + return; + } + } + + event.setCancelled(true); + event.setUseInteractedBlock(Event.Result.DENY); + event.setUseItemInHand(Event.Result.DENY); + event.getPlayer().closeInventory(); + AxSmithingPlugin.getSmithingTableImpl().open(event.getPlayer()); + } + @EventHandler public void onInventoryOpenEvent(@NotNull final InventoryOpenEvent event) { if (event.getInventory().getType() != InventoryType.SMITHING) return; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 2c8a0fe..12ac5a3 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -43,9 +43,11 @@ menu: - 14 material: air +listen-to-interact-event: true + messages: prefix: "&#AA3428&lAxSmithing &7ยป " no-permission: "&cYou do not have permission to do this!" reload: "&aPlugin reloaded in %time%ms!" -config-version: 1 \ No newline at end of file +config-version: 2 \ No newline at end of file