initial commit

This commit is contained in:
Evert Prants 2020-09-19 19:41:49 +03:00
commit 9911249dcc
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
5 changed files with 187 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/bin/
/.settings/
/.classpath
/.project

5
plugin.yml Normal file
View File

@ -0,0 +1,5 @@
name: ClickHarvest
main: ee.lunasqu.clickharvest.Main
version: 1.0
api-version: 1.13
commands:

View File

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

View File

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

View File

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