From b0318c5dddb76a630c8953a2631ece969b71361e Mon Sep 17 00:00:00 2001 From: xoy Date: Sun, 23 Jun 2024 01:12:31 +0200 Subject: [PATCH] [Add Crop Harvest with Hoes] --- .../java/dev/xoy/xoyshowoff/XoyShowOff.java | 5 +- .../listeners/CropHarvestListener.java | 83 +++++++++++++++++++ .../{items => listeners}/SandGunListener.java | 3 +- 3 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 src/main/java/dev/xoy/xoyshowoff/listeners/CropHarvestListener.java rename src/main/java/dev/xoy/xoyshowoff/{items => listeners}/SandGunListener.java (96%) diff --git a/src/main/java/dev/xoy/xoyshowoff/XoyShowOff.java b/src/main/java/dev/xoy/xoyshowoff/XoyShowOff.java index 9c72ddf..03c2fe2 100644 --- a/src/main/java/dev/xoy/xoyshowoff/XoyShowOff.java +++ b/src/main/java/dev/xoy/xoyshowoff/XoyShowOff.java @@ -2,10 +2,10 @@ package dev.xoy.xoyshowoff; import dev.xoy.xoyshowoff.commands.GiveSandArrowCommand; import dev.xoy.xoyshowoff.commands.GiveSandGunCommand; -import dev.xoy.xoyshowoff.items.SandGunListener; +import dev.xoy.xoyshowoff.listeners.CropHarvestListener; +import dev.xoy.xoyshowoff.listeners.SandGunListener; import org.bukkit.plugin.java.JavaPlugin; -import java.util.Objects; import java.util.logging.Logger; public final class XoyShowOff extends JavaPlugin { @@ -16,6 +16,7 @@ public final class XoyShowOff extends JavaPlugin { public void onEnable() { LOGGER.info("xoyShowOff is now enabled!"); getServer().getPluginManager().registerEvents(new SandGunListener(), this); + getServer().getPluginManager().registerEvents(new CropHarvestListener(), this); getCommand("givesandgun").setExecutor(new GiveSandGunCommand()); getCommand("givesandarrow").setExecutor(new GiveSandArrowCommand()); } diff --git a/src/main/java/dev/xoy/xoyshowoff/listeners/CropHarvestListener.java b/src/main/java/dev/xoy/xoyshowoff/listeners/CropHarvestListener.java new file mode 100644 index 0000000..9f5c737 --- /dev/null +++ b/src/main/java/dev/xoy/xoyshowoff/listeners/CropHarvestListener.java @@ -0,0 +1,83 @@ +package dev.xoy.xoyshowoff.listeners; + +import dev.xoy.xoyshowoff.XoyShowOff; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.data.Ageable; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; + +import java.util.HashMap; + +public class CropHarvestListener implements Listener { + private static final Material[] cropHarvestTools = new Material[]{ + Material.WOODEN_HOE, + Material.STONE_HOE, + Material.IRON_HOE, + Material.GOLDEN_HOE, + Material.DIAMOND_HOE, + Material.NETHERITE_HOE + }; + + private static final Material[] crops = new Material[] { + Material.POTATOES, + Material.CARROTS, + Material.BEETROOTS, + Material.WHEAT + }; + + private static final Material[] cropDrop = new Material[] { + Material.POTATO, + Material.CARROT, + Material.BEETROOT, + Material.WHEAT + }; + + private boolean isCrop(Block block) { + for (Material material : crops) { + if(block.getType().equals(material)) return true; + } + return false; + } + + private boolean cropHarvestToolInHand(Player player) { + ItemStack itemInHand = player.getInventory().getItemInMainHand(); + Material material = itemInHand.getType(); + for(Material toolMaterial : cropHarvestTools) { + if(toolMaterial.equals(material)) return true; + } + return false; + } + + private ItemStack getCropDrop(Block block) { + for(int i = 0; i < crops.length; i++) { + if(block.getType().equals(crops[i])) return cropDrop[i].asItemType().createItemStack(); + } + return null; + } + + private void tryCropHarvest(Block block, Player player) { + Ageable cropAge = (Ageable) block.getBlockData(); + if(cropAge.getAge() == cropAge.getMaximumAge()) { + block.setType(block.getType()); + ItemStack drop = getCropDrop(block); + if(drop != null) player.getInventory().addItem(drop); + XoyShowOff.LOGGER.info("Set block type!"); + } + } + + @EventHandler + public void onPlayerUse(PlayerInteractEvent event) { + Player player = event.getPlayer(); + + if(cropHarvestToolInHand(player)) { + Block block = event.getClickedBlock(); + if(block != null && isCrop(block)) { + tryCropHarvest(block, player); + } + } + } +} diff --git a/src/main/java/dev/xoy/xoyshowoff/items/SandGunListener.java b/src/main/java/dev/xoy/xoyshowoff/listeners/SandGunListener.java similarity index 96% rename from src/main/java/dev/xoy/xoyshowoff/items/SandGunListener.java rename to src/main/java/dev/xoy/xoyshowoff/listeners/SandGunListener.java index b62e0bf..5a0214d 100644 --- a/src/main/java/dev/xoy/xoyshowoff/items/SandGunListener.java +++ b/src/main/java/dev/xoy/xoyshowoff/listeners/SandGunListener.java @@ -1,6 +1,7 @@ -package dev.xoy.xoyshowoff.items; +package dev.xoy.xoyshowoff.listeners; import dev.xoy.xoyshowoff.XoyShowOff; +import dev.xoy.xoyshowoff.items.Keys; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.NamespacedKey;