commit 4925aa5b14e6ba3f5fc689dc69fc7bf2a3256d1b
parent 0e661a0aa3fa834c03a674d3ba0f7f27c00e1dbf
Author: chunksize <reisingerluca@gmail.com>
Date: Fri, 7 Jul 2023 15:54:16 +0200
Added BlockDetectorModule and tweaked AutoBreakerModule
Diffstat:
3 files changed, 106 insertions(+), 2 deletions(-)
diff --git a/src/poppy/Main.java b/src/poppy/Main.java
@@ -19,6 +19,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import poppy.modules.AutoBreakerModule;
import poppy.modules.AutoPlacerModule;
import poppy.modules.AutoWorkbenchModule;
+import poppy.modules.BlockDetectorModule;
import poppy.modules.CauldronModule;
import poppy.modules.ChairModule;
import poppy.modules.DoubleDoorModule;
@@ -46,6 +47,7 @@ public class Main extends JavaPlugin
private AutoPlacerModule autoPlacerModule;
private AutoBreakerModule autoBreakerModule;
private PlayerMountModule playerMountModule;
+ private BlockDetectorModule blockDetectorModule;
@Override
public void onEnable()
@@ -87,6 +89,9 @@ public class Main extends JavaPlugin
playerMountModule = new PlayerMountModule();
pluginManager.registerEvents(playerMountModule, this);
+
+ blockDetectorModule = new BlockDetectorModule();
+ pluginManager.registerEvents(blockDetectorModule, this);
}
@Override
diff --git a/src/poppy/modules/AutoBreakerModule.java b/src/poppy/modules/AutoBreakerModule.java
@@ -1,6 +1,7 @@
package poppy.modules;
import java.util.HashSet;
+import java.util.Iterator;
import org.bukkit.Bukkit;
import org.bukkit.Material;
@@ -10,6 +11,7 @@ import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.block.Dispenser;
+import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.Directional;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.EventHandler;
@@ -54,6 +56,37 @@ public class AutoBreakerModule implements Listener
final HashSet<ItemStack> drops = new HashSet<ItemStack>(faceBlock.getDrops(item));
+ if(Tag.CROPS.isTagged(faceBlock.getType()) || Tag.MINEABLE_AXE.isTagged(faceBlock.getType()))
+ {
+ if(faceBlock.getBlockData() instanceof Ageable)
+ {
+ final Ageable ageable = (Ageable) faceBlock.getBlockData();
+
+ if(ageable.getAge() == ageable.getMaximumAge())
+ {
+ if(!addItemToChest(direction, dispenser, drops))
+ {
+ faceBlock.breakNaturally(item);
+ }
+ else
+ {
+ faceBlock.setType(Material.AIR);
+ }
+ }
+ else if(!Tag.CROPS.isTagged(faceBlock.getType()))
+ {
+ if(!addItemToChest(direction, dispenser, drops))
+ {
+ faceBlock.breakNaturally(item);
+ }
+ else
+ {
+ faceBlock.setType(Material.AIR);
+ }
+ }
+ }
+ }
+
if(item.getType().toString().endsWith("_PICKAXE"))
{
if(Tag.MINEABLE_PICKAXE.isTagged(faceBlock.getType()))
@@ -139,12 +172,15 @@ public class AutoBreakerModule implements Listener
public boolean isInventoryFullList(Inventory inventory, HashSet<ItemStack> items)
{
final HashSet<ItemStack> itemsCopy = new HashSet<ItemStack>(items);
+ final Iterator<ItemStack> it = itemsCopy.iterator();
- for(ItemStack item : itemsCopy)
+ while(it.hasNext())
{
+ ItemStack item = it.next();
+
if(!Utils.isInventoryFull(inventory, item))
{
- itemsCopy.remove(item);
+ it.remove();
}
}
return itemsCopy.size() != 0;
diff --git a/src/poppy/modules/BlockDetectorModule.java b/src/poppy/modules/BlockDetectorModule.java
@@ -0,0 +1,63 @@
+package poppy.modules;
+
+import java.util.HashSet;
+
+import org.bukkit.Location;
+import org.bukkit.Material;
+import org.bukkit.block.Block;
+import org.bukkit.block.data.Directional;
+import org.bukkit.block.data.type.Observer;
+import org.bukkit.entity.ItemFrame;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.Listener;
+import org.bukkit.event.block.BlockRedstoneEvent;
+import org.bukkit.inventory.ItemStack;
+
+
+public class BlockDetectorModule implements Listener
+{
+ @EventHandler
+ public void onBlockRedstone(BlockRedstoneEvent event)
+ {
+ final Block block = event.getBlock();
+
+ if(block.getType() != Material.OBSERVER) return;
+
+ final ItemFrame[] itemframes = HopperSorterModule.getBlockItemFrames(block);
+ final ItemStack[] itemstacks = getItemsFromItemframes(itemframes);
+
+ if(itemstacks.length <= 0) return;
+
+ final Observer observer = (Observer) block.getBlockData();
+
+ if(block.getBlockData() instanceof Directional)
+ {
+ final Location location = observer.getFacing().getDirection().toLocation(block.getWorld());
+ final Block faceblock = block.getLocation().add(location.toVector()).getBlock();
+
+ boolean check = false;
+
+ for(ItemStack item : itemstacks)
+ {
+ if(item.getType() == faceblock.getType())
+ {
+ observer.setPowered(true);
+ check = true;
+ }
+ }
+
+ if(!check) event.setNewCurrent(0);
+ }
+ }
+
+ public static ItemStack[] getItemsFromItemframes(ItemFrame[] itemframes)
+ {
+ final HashSet<ItemStack> itemstacks = new HashSet <ItemStack>();
+
+ for(ItemFrame itemframe : itemframes)
+ {
+ itemstacks.add(itemframe.getItem());
+ }
+ return itemstacks.toArray(new ItemStack[itemstacks.size()]);
+ }
+}