From a7a8a4b0374d0604bb67552f93c8298994654310 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Sun, 9 May 2021 23:24:04 +0200 Subject: [PATCH] Implemented linking of beacons in crafting --- .../listener/OnPrepareItemCraftEvent.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/listener/OnPrepareItemCraftEvent.java b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/listener/OnPrepareItemCraftEvent.java index a2a5c3b..daeecf8 100644 --- a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/listener/OnPrepareItemCraftEvent.java +++ b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/listener/OnPrepareItemCraftEvent.java @@ -1,11 +1,37 @@ package eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.listener; +import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterItem; +import org.bukkit.Material; import org.bukkit.inventory.CraftingInventory; import org.bukkit.inventory.InventoryView; +import org.bukkit.inventory.ItemStack; public class OnPrepareItemCraftEvent { public static void onPrepareItemCraftEvent(CraftingInventory what, InventoryView view, boolean isRepair){ + ItemStack[] matrix = what.getMatrix(); + // Check if one of provided items is already LinkedBeaconTeleporterItem and use first if found + LinkedBeaconTeleporterItem lbtItem = null; + + for (ItemStack item : matrix) { + lbtItem = LinkedBeaconTeleporterItem.getFromItemStack(item); + if(lbtItem != null) break; + } + + if(lbtItem == null){ + // If none is found, create new LinkedBeaconTeleporterItem and set as crafting-result + ItemStack item = new ItemStack(Material.BEACON); + lbtItem = new LinkedBeaconTeleporterItem(item); + } + + // Generate ItemStack + ItemStack item = lbtItem.toItemStack(); + + // Exactly 2 + item.setAmount(2); + + // Set as crafting-result + what.setResult(item); } }