From 4e844070ca3e9a7ab33f6bf90396ae66c9744702 Mon Sep 17 00:00:00 2001 From: jojo Date: Fri, 22 May 2020 14:38:32 +0200 Subject: [PATCH 1/3] Add BlockBreakEvent --- .../listener/InFightInventoryListener.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/listener/InFightInventoryListener.java b/FightSystem_Main/src/de/steamwar/fightsystem/listener/InFightInventoryListener.java index 359116c..ae86109 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/listener/InFightInventoryListener.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/listener/InFightInventoryListener.java @@ -3,6 +3,7 @@ package de.steamwar.fightsystem.listener; import de.steamwar.fightsystem.states.FightState; import org.bukkit.Material; import org.bukkit.event.EventHandler; +import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.event.inventory.InventoryPickupItemEvent; @@ -62,4 +63,36 @@ public class InFightInventoryListener extends BasicListener { public void onDropping(PlayerDropItemEvent e){ e.setCancelled(true); } + + @EventHandler + public void onBlockBreak(BlockBreakEvent event) { + Inventory inventory = event.getPlayer().getInventory(); + int inventorySize = inventory.getSize(); + + Material type = event.getBlock().getType(); + ItemStack itemStack; + + for (int i = 0; i < inventorySize; i++) { + itemStack = inventory.getItem(i); + if (itemStack != null && itemStack.getType().equals(type) && itemStack.getAmount() != itemStack.getMaxStackSize()) { + itemStack.setType(type); + itemStack.setAmount(itemStack.getAmount() + 1); + inventory.setItem(i, itemStack); + event.getPlayer().updateInventory(); + return; + } + } + + for (int i = 0; i < inventorySize; i++) { + itemStack = inventory.getItem(i); + if (itemStack != null && itemStack.getType().equals(Material.AIR)) { + itemStack.setType(type); + itemStack.setAmount(1); + inventory.setItem(i, itemStack); + event.getPlayer().updateInventory(); + return; + } + } + } + } From ad077e8eb2c4e1358693dee1a6b66af5434f4769 Mon Sep 17 00:00:00 2001 From: jojo Date: Fri, 22 May 2020 16:18:40 +0200 Subject: [PATCH 2/3] Add BlockBreakEvent Debug Messages --- .../fightsystem/listener/InFightInventoryListener.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/listener/InFightInventoryListener.java b/FightSystem_Main/src/de/steamwar/fightsystem/listener/InFightInventoryListener.java index ae86109..c36c06d 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/listener/InFightInventoryListener.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/listener/InFightInventoryListener.java @@ -67,14 +67,20 @@ public class InFightInventoryListener extends BasicListener { @EventHandler public void onBlockBreak(BlockBreakEvent event) { Inventory inventory = event.getPlayer().getInventory(); + System.out.println("Inventory: " + inventory); int inventorySize = inventory.getSize(); + System.out.println("Inventory Size: " + inventorySize); Material type = event.getBlock().getType(); + System.out.println("Type: " + type); ItemStack itemStack; + System.out.println("Searching Type Slot"); + for (int i = 0; i < inventorySize; i++) { itemStack = inventory.getItem(i); if (itemStack != null && itemStack.getType().equals(type) && itemStack.getAmount() != itemStack.getMaxStackSize()) { + System.out.println("Slot Found: " + i); itemStack.setType(type); itemStack.setAmount(itemStack.getAmount() + 1); inventory.setItem(i, itemStack); @@ -83,9 +89,12 @@ public class InFightInventoryListener extends BasicListener { } } + System.out.println("Searching Empty Slot"); + for (int i = 0; i < inventorySize; i++) { itemStack = inventory.getItem(i); if (itemStack != null && itemStack.getType().equals(Material.AIR)) { + System.out.println("Slot Found: " + i); itemStack.setType(type); itemStack.setAmount(1); inventory.setItem(i, itemStack); From f8a2585621b8e5796a7b93cdf30bab66a916b0b3 Mon Sep 17 00:00:00 2001 From: jojo Date: Fri, 22 May 2020 16:40:27 +0200 Subject: [PATCH 3/3] Add BlockBreakEvent Add item not in Inventory --- .../src/de/steamwar/fightsystem/FightSystem.java | 1 + .../listener/InFightInventoryListener.java | 14 ++------------ 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java index fa6e642..bbb6e2d 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java @@ -57,6 +57,7 @@ public class FightSystem extends JavaPlugin { new PlayerStateListener(); new ProjectileLaunchListener(); new InFightDamageListener(); + new InFightInventoryListener(); new FreezeWorldStateListener(); new EventJoinListener(); new CheckListener(); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/listener/InFightInventoryListener.java b/FightSystem_Main/src/de/steamwar/fightsystem/listener/InFightInventoryListener.java index c36c06d..8cef341 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/listener/InFightInventoryListener.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/listener/InFightInventoryListener.java @@ -67,20 +67,14 @@ public class InFightInventoryListener extends BasicListener { @EventHandler public void onBlockBreak(BlockBreakEvent event) { Inventory inventory = event.getPlayer().getInventory(); - System.out.println("Inventory: " + inventory); int inventorySize = inventory.getSize(); - System.out.println("Inventory Size: " + inventorySize); Material type = event.getBlock().getType(); - System.out.println("Type: " + type); ItemStack itemStack; - System.out.println("Searching Type Slot"); - for (int i = 0; i < inventorySize; i++) { itemStack = inventory.getItem(i); if (itemStack != null && itemStack.getType().equals(type) && itemStack.getAmount() != itemStack.getMaxStackSize()) { - System.out.println("Slot Found: " + i); itemStack.setType(type); itemStack.setAmount(itemStack.getAmount() + 1); inventory.setItem(i, itemStack); @@ -89,14 +83,10 @@ public class InFightInventoryListener extends BasicListener { } } - System.out.println("Searching Empty Slot"); - for (int i = 0; i < inventorySize; i++) { itemStack = inventory.getItem(i); - if (itemStack != null && itemStack.getType().equals(Material.AIR)) { - System.out.println("Slot Found: " + i); - itemStack.setType(type); - itemStack.setAmount(1); + if (itemStack == null || itemStack.getType().equals(Material.AIR)) { + itemStack = new ItemStack(type, 1); inventory.setItem(i, itemStack); event.getPlayer().updateInventory(); return;