119 lines
3.9 KiB
Java
119 lines
3.9 KiB
Java
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<Harvestable> cropList = new ArrayList<Harvestable>();
|
|
|
|
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<ItemStack> 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);
|
|
}
|
|
}
|
|
} |