[Add Crop Harvest with Hoes]

This commit is contained in:
xoy 2024-06-23 01:12:31 +02:00
parent 9139dd49cb
commit b0318c5ddd
3 changed files with 88 additions and 3 deletions

View File

@ -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());
}

View File

@ -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);
}
}
}
}

View File

@ -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;