Skip to content

Commit

Permalink
fix: fix glitch, add config option for listening to interact event
Browse files Browse the repository at this point in the history
  • Loading branch information
AverageGithub committed Jan 1, 2024
1 parent abd52fb commit 2b51eef
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = 'com.artillexstudios.axsmithing'
version = '1.4'
version = '1.5'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Recipe> recipeIterator = Bukkit.getServer().recipeIterator();
while (recipeIterator.hasNext()) {
inventory.setItem(outputSlot, new ItemStack(Material.AIR));
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<Recipe> 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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
config-version: 2

0 comments on commit 2b51eef

Please sign in to comment.