commit 9911249dccedcc32be2d00c271538f4d4ff8d16c Author: Evert Prants Date: Sat Sep 19 19:41:49 2020 +0300 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8e540a5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/bin/ +/.settings/ +/.classpath +/.project diff --git a/plugin.yml b/plugin.yml new file mode 100644 index 0000000..8499165 --- /dev/null +++ b/plugin.yml @@ -0,0 +1,5 @@ +name: ClickHarvest +main: ee.lunasqu.clickharvest.Main +version: 1.0 +api-version: 1.13 +commands: diff --git a/src/ee/lunasqu/clickharvest/CropEvent.java b/src/ee/lunasqu/clickharvest/CropEvent.java new file mode 100644 index 0000000..3007bfa --- /dev/null +++ b/src/ee/lunasqu/clickharvest/CropEvent.java @@ -0,0 +1,119 @@ +package ee.lunasqu.clickharvest; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.block.Block; +import org.bukkit.block.data.Ageable; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; + +public class CropEvent implements Listener { + private List cropList = new ArrayList(); + + public void registerHarvestable(Harvestable crop) { + cropList.add(crop); + } + + public boolean isCrop(Material m) { + for (Harvestable h : cropList) { + if (h.getCrop().equals(m)) return true; + } + return false; + } + + public Material getSeed(Material m) { + for (Harvestable h : cropList) { + if (h.getCrop().equals(m)) return h.getSeed(); + } + return Material.AIR; + } + + public int getMaxGrowth(Material m) { + for (Harvestable h : cropList) { + if (h.getCrop().equals(m)) return h.getGrowth(); + } + return 0; + } + + public CropEvent () { + super(); + this.registerHarvestable(new Harvestable(Material.WHEAT, Material.WHEAT_SEEDS, 7)); + this.registerHarvestable(new Harvestable(Material.POTATOES, Material.POTATO, 7)); + this.registerHarvestable(new Harvestable(Material.CARROTS, Material.CARROT, 7)); + this.registerHarvestable(new Harvestable(Material.BEETROOTS, Material.BEETROOT_SEEDS, 3)); + this.registerHarvestable(new Harvestable(Material.NETHER_WART, Material.NETHER_WART, 3)); + } + + @EventHandler + public void onRightClick(PlayerInteractEvent e) { + Block block = e.getClickedBlock(); + + if (e.getAction() == Action.RIGHT_CLICK_BLOCK) { + if (!isCrop(block.getType())) + return; + + harvestCrop(block.getType(), e); + } + } + + public void harvestCrop(Material m, PlayerInteractEvent e) { + if (!isCrop(m)) return; + + Block block = e.getClickedBlock(); + Ageable cropData = (Ageable) block.getBlockData(); + if (cropData.getAge() != getMaxGrowth(m)) return; + + boolean seedInDrop = false; + boolean replant = true; + for (ItemStack is : block.getDrops()) { + if (is.getType() == getSeed(m)) { + seedInDrop = true; + break; + } + } + + if (!e.getPlayer().getInventory().containsAtLeast(new ItemStack(getSeed(m)), 1) && !seedInDrop) { + replant = false; + } + + Collection drops = block.getDrops(e.getPlayer().getInventory().getItemInMainHand()); + if (seedInDrop) { + for (ItemStack is : drops) { + if (is.getType() == getSeed(m)) { + is.setAmount(is.getAmount() - 1); + break; + } + } + } + + if (replant && !seedInDrop) { + e.getPlayer().getInventory().remove(new ItemStack(getSeed(m), 1)); + } + + for (ItemStack is : drops) { + if (is.getType() == Material.AIR || is.getAmount() < 1) continue; + block.getWorld().dropItemNaturally(block.getLocation(), is); + } + + Bukkit.getPluginManager().callEvent(new BlockBreakEvent(block, e.getPlayer())); + if (replant) block.setType(m); + else block.setType(Material.AIR); + + if (block.getType() == Material.NETHER_WART) { + e.getPlayer().playSound(e.getPlayer().getLocation(), Sound.BLOCK_NETHER_WART_BREAK, 10, 1); + if (replant) e.getPlayer().playSound(e.getPlayer().getLocation(), Sound.ITEM_NETHER_WART_PLANT, 8, 1); + } else { + e.getPlayer().playSound(e.getPlayer().getLocation(), Sound.BLOCK_CROP_BREAK, 10, 1); + if (replant) e.getPlayer().playSound(e.getPlayer().getLocation(), Sound.ITEM_CROP_PLANT, 8, 1); + } + } +} \ No newline at end of file diff --git a/src/ee/lunasqu/clickharvest/Harvestable.java b/src/ee/lunasqu/clickharvest/Harvestable.java new file mode 100644 index 0000000..f8f7210 --- /dev/null +++ b/src/ee/lunasqu/clickharvest/Harvestable.java @@ -0,0 +1,41 @@ +package ee.lunasqu.clickharvest; + +import org.bukkit.Material; + +public class Harvestable { + private Material crop; + private Material seed; + private int growth; + + public Harvestable(Material crop, Material seed, int growth) { + this.setCrop(crop); + this.setSeed(seed); + this.setGrowth(growth); + } + + public Material getCrop() { + return crop; + } + + public void setCrop(Material crop) { + this.crop = crop; + } + + public Material getSeed() { + return seed; + } + + public void setSeed(Material seed) { + this.seed = seed; + } + + public int getGrowth() { + return growth; + } + + public void setGrowth(int growth) { + this.growth = growth; + } + + +} diff --git a/src/ee/lunasqu/clickharvest/Main.java b/src/ee/lunasqu/clickharvest/Main.java new file mode 100644 index 0000000..ae0a41c --- /dev/null +++ b/src/ee/lunasqu/clickharvest/Main.java @@ -0,0 +1,18 @@ +package ee.lunasqu.clickharvest; + +import org.bukkit.plugin.java.JavaPlugin; + +public class Main extends JavaPlugin { + public static Main plugin; + + @Override + public void onEnable() { + plugin = this; + getServer().getPluginManager().registerEvents(new CropEvent(), this); + } + + @Override + public void onDisable() { + + } +}