From b1e10d1edf5fbeeb889b26a9011e7579dbfd43e4 Mon Sep 17 00:00:00 2001 From: Brianna O'Keefe Date: Thu, 17 May 2018 03:13:44 -0400 Subject: [PATCH] You can now give farm items as console. You can now specify a player when giving farm items. You can now specify the level when giving farm items. FarmItems will now output to hoppers placed beneath them. --- pom.xml | 2 +- .../com/songoda/epicfarming/api/IFarm.java | 16 ----- .../com/songoda/epicfarming/api/ILevel.java | 23 ------- .../epicfarming/events/BlockListeners.java | 2 +- .../com/songoda/epicfarming/farming/Farm.java | 11 +--- .../songoda/epicfarming/farming/Level.java | 11 +--- .../epicfarming/farming/LevelManager.java | 4 ++ .../epicfarming/handlers/CommandHandler.java | 30 ++++++++-- .../epicfarming/handlers/FarmingHandler.java | 60 +++++++++++++++++++ .../songoda/epicfarming/utils/Methods.java | 5 +- src/main/resources/plugin.yml | 2 +- 11 files changed, 97 insertions(+), 69 deletions(-) delete mode 100644 src/main/java/com/songoda/epicfarming/api/IFarm.java delete mode 100644 src/main/java/com/songoda/epicfarming/api/ILevel.java diff --git a/pom.xml b/pom.xml index 1b418c6..df06ceb 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.songoda EpicFarming - 1.2.1 + 1.3 jar diff --git a/src/main/java/com/songoda/epicfarming/api/IFarm.java b/src/main/java/com/songoda/epicfarming/api/IFarm.java deleted file mode 100644 index 2ede20d..0000000 --- a/src/main/java/com/songoda/epicfarming/api/IFarm.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.songoda.epicfarming.api; - -import org.bukkit.Location; -import org.bukkit.entity.Player; -import org.bukkit.inventory.InventoryHolder; - -public interface IFarm extends InventoryHolder { - - void view(Player player); - - void upgrade(UpgradeType type, Player player); - - Location getLocation(); - - ILevel getLevel(); -} diff --git a/src/main/java/com/songoda/epicfarming/api/ILevel.java b/src/main/java/com/songoda/epicfarming/api/ILevel.java deleted file mode 100644 index b0a0bc6..0000000 --- a/src/main/java/com/songoda/epicfarming/api/ILevel.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.songoda.epicfarming.api; - -import java.util.List; - -public interface ILevel { - - List getDescription(); - - int getLevel(); - - int getRadius(); - - boolean isAutoHarvest(); - - boolean isAutoReplant(); - - double getSpeedMultiplier(); - - int getCostExperiance(); - - int getCostEconomy(); - -} diff --git a/src/main/java/com/songoda/epicfarming/events/BlockListeners.java b/src/main/java/com/songoda/epicfarming/events/BlockListeners.java index 0b2cc0b..b2541dd 100644 --- a/src/main/java/com/songoda/epicfarming/events/BlockListeners.java +++ b/src/main/java/com/songoda/epicfarming/events/BlockListeners.java @@ -117,7 +117,7 @@ public class BlockListeners implements Listener { event.setCancelled(true); - ItemStack item = Methods.makeFarmItem(farm.getLevel().getLevel()); + ItemStack item = Methods.makeFarmItem(farm.getLevel()); Block block = event.getBlock(); diff --git a/src/main/java/com/songoda/epicfarming/farming/Farm.java b/src/main/java/com/songoda/epicfarming/farming/Farm.java index 07e7c74..f500440 100644 --- a/src/main/java/com/songoda/epicfarming/farming/Farm.java +++ b/src/main/java/com/songoda/epicfarming/farming/Farm.java @@ -2,8 +2,6 @@ package com.songoda.epicfarming.farming; import com.songoda.arconix.plugin.Arconix; import com.songoda.epicfarming.EpicFarming; -import com.songoda.epicfarming.api.IFarm; -import com.songoda.epicfarming.api.ILevel; import com.songoda.epicfarming.api.UpgradeType; import com.songoda.epicfarming.player.PlayerData; import com.songoda.epicfarming.utils.Debugger; @@ -22,7 +20,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Random; -public class Farm implements IFarm { +public class Farm { private Location location; private Level level; @@ -34,7 +32,6 @@ public class Farm implements IFarm { this.inventory = Bukkit.createInventory(null, 54, Methods.formatName(level.getLevel(),false)); } - @Override public void view(Player player) { try { if (!player.hasPermission("epicfarming.view")) @@ -134,7 +131,6 @@ public class Farm implements IFarm { inventory.setItem(26, Methods.getBackgroundGlass(true)); } - @Override public Inventory getInventory() { return inventory; } @@ -157,7 +153,6 @@ public class Farm implements IFarm { return items; } - @Override public void upgrade(UpgradeType type, Player player) { try { EpicFarming instance = EpicFarming.getInstance(); @@ -287,7 +282,6 @@ public class Farm implements IFarm { return false; } - @Override public Location getLocation() { return location.clone(); } @@ -296,8 +290,7 @@ public class Farm implements IFarm { this.location = location; } - @Override - public ILevel getLevel() { + public Level getLevel() { return level; } } \ No newline at end of file diff --git a/src/main/java/com/songoda/epicfarming/farming/Level.java b/src/main/java/com/songoda/epicfarming/farming/Level.java index 5a58110..af2705f 100644 --- a/src/main/java/com/songoda/epicfarming/farming/Level.java +++ b/src/main/java/com/songoda/epicfarming/farming/Level.java @@ -1,12 +1,11 @@ package com.songoda.epicfarming.farming; import com.songoda.epicfarming.EpicFarming; -import com.songoda.epicfarming.api.ILevel; import java.util.ArrayList; import java.util.List; -public class Level implements ILevel { +public class Level { private int level, costExperiance, costEconomy, radius; @@ -38,42 +37,34 @@ public class Level implements ILevel { } - @Override public List getDescription() { return new ArrayList<>(description); } - @Override public int getLevel() { return level; } - @Override public int getRadius() { return radius; } - @Override public boolean isAutoHarvest() { return autoHarvest; } - @Override public boolean isAutoReplant() { return autoReplant; } - @Override public double getSpeedMultiplier() { return speedMultiplier; } - @Override public int getCostExperiance() { return costExperiance; } - @Override public int getCostEconomy() { return costEconomy; } diff --git a/src/main/java/com/songoda/epicfarming/farming/LevelManager.java b/src/main/java/com/songoda/epicfarming/farming/LevelManager.java index 29fa5d2..6b165d8 100644 --- a/src/main/java/com/songoda/epicfarming/farming/LevelManager.java +++ b/src/main/java/com/songoda/epicfarming/farming/LevelManager.java @@ -25,6 +25,10 @@ public class LevelManager { return registeredLevels.lastEntry().getValue(); } + public boolean isLevel(int level) { + return registeredLevels.containsKey(level); + } + public Map getLevels() { return Collections.unmodifiableMap(registeredLevels); } diff --git a/src/main/java/com/songoda/epicfarming/handlers/CommandHandler.java b/src/main/java/com/songoda/epicfarming/handlers/CommandHandler.java index 808832e..89b4150 100644 --- a/src/main/java/com/songoda/epicfarming/handlers/CommandHandler.java +++ b/src/main/java/com/songoda/epicfarming/handlers/CommandHandler.java @@ -2,6 +2,7 @@ package com.songoda.epicfarming.handlers; import com.songoda.arconix.plugin.Arconix; import com.songoda.epicfarming.EpicFarming; +import com.songoda.epicfarming.farming.Level; import com.songoda.epicfarming.utils.Debugger; import com.songoda.epicfarming.utils.Methods; import org.bukkit.Bukkit; @@ -31,7 +32,7 @@ public class CommandHandler implements CommandExecutor { sender.sendMessage(Arconix.pl().getApi().format().formatText(" &8- &aEFA help &7Displays this page.")); sender.sendMessage(Arconix.pl().getApi().format().formatText(" &8- &aEFA settings &7Edit the EpicFarming Settings.")); sender.sendMessage(Arconix.pl().getApi().format().formatText(" &8- &aEFA reload &7Reloads Configuration and Language files.")); - sender.sendMessage(Arconix.pl().getApi().format().formatText(" &8- &aEFA givefarmitem [player] &7Give a farm item to a player.")); + sender.sendMessage(Arconix.pl().getApi().format().formatText(" &8- &aEFA givefarmitem [player] [level] &7Give a farm item to a player.")); sender.sendMessage(""); } else if (args[0].equalsIgnoreCase("reload")) { if (!sender.hasPermission("epicfarming.admin")) { @@ -46,13 +47,30 @@ public class CommandHandler implements CommandExecutor { return true; } //ToDo: add the ability to specify level. - if (args.length == 1) { - if (sender instanceof Player) - ((Player) sender).getInventory().addItem(Methods.makeFarmItem(1)); + if (args.length >= 1) { + if (!(sender instanceof Player) && args.length == 1) return true; + + Level level = instance.getLevelManager().getLowestLevel(); + Player player; + if (args.length != 1 && Bukkit.getPlayer(args[1]) == null) { + sender.sendMessage("Not a player..."); + return true; + } else if (args.length == 1) { + player = (Player)sender; + } else { + player = Bukkit.getPlayer(args[1]); + } + + + if (args.length >= 3 && !instance.getLevelManager().isLevel(Integer.parseInt(args[2]))) { + sender.sendMessage("Not a level..."); + return true; + } else { + level = instance.getLevelManager().getLevel(Integer.parseInt(args[2])); + } + player.getInventory().addItem(Methods.makeFarmItem(level)); } else if (Bukkit.getPlayerExact(args[1]) == null) { sender.sendMessage(instance.references.getPrefix() + Arconix.pl().getApi().format().formatText("&cThat username does not exist, or the user is not online!")); - } else { - Bukkit.getPlayerExact(args[1]).getInventory().addItem(Methods.makeFarmItem(1)); } } else if (sender instanceof Player) { if (args[0].equalsIgnoreCase("settings")) { diff --git a/src/main/java/com/songoda/epicfarming/handlers/FarmingHandler.java b/src/main/java/com/songoda/epicfarming/handlers/FarmingHandler.java index 6f53b5a..73017f7 100644 --- a/src/main/java/com/songoda/epicfarming/handlers/FarmingHandler.java +++ b/src/main/java/com/songoda/epicfarming/handlers/FarmingHandler.java @@ -9,7 +9,9 @@ import org.bukkit.Bukkit; import org.bukkit.CropState; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; +import org.bukkit.block.Hopper; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.material.Crops; @@ -25,6 +27,7 @@ public class FarmingHandler { public FarmingHandler(EpicFarming instance) { this.instance = instance; Bukkit.getScheduler().scheduleSyncRepeatingTask(EpicFarming.getInstance(), this::farmRunner, 0, instance.getConfig().getInt("Main.Farm Tick Speed")); + Bukkit.getScheduler().scheduleSyncRepeatingTask(EpicFarming.getInstance(), this::hopRunner, 0, 8); } @@ -58,6 +61,63 @@ public class FarmingHandler { } } + private void hopRunner() { + + for (Farm farm : instance.getFarmManager().getFarms().values()) { + Block block = farm.getLocation().getBlock(); + + if (block.getRelative(BlockFace.DOWN).getType() == Material.HOPPER) { + Inventory inventory = farm.getInventory(); + Inventory hopperInventory = ((Hopper) block.getRelative(BlockFace.DOWN).getState()).getInventory(); + + for (int i = 27; i < inventory.getSize(); i++) { + if (inventory.getItem(i) == null || inventory.getItem(i).getType() == Material.AIR) continue; + + int amtToMove = 1; + + ItemStack item = inventory.getItem(i); + + ItemStack toMove = item.clone(); + toMove.setAmount(amtToMove); + + if (canHop(hopperInventory, toMove)) { + + hopperInventory.addItem(toMove); + item.setAmount(item.getAmount() - amtToMove); + } + break; + } + } + } + + } + + public boolean canHop(Inventory i, ItemStack item) { + try { + if (i.firstEmpty() != -1) { + return true; + } + boolean can = false; + for (ItemStack it : i.getContents()) { + if (it == null) { + can = true; + break; + } else { + if (it.isSimilar(item)) { + if (it.getAmount() <= it.getMaxStackSize()) { + can = true; + break; + } + } + } + } + return can; + } catch (Exception e) { + Debugger.runReport(e); + } + return false; + } + private boolean doDrop(Farm farm, Material material) { Random random = new Random(); diff --git a/src/main/java/com/songoda/epicfarming/utils/Methods.java b/src/main/java/com/songoda/epicfarming/utils/Methods.java index 00b9493..d47d891 100644 --- a/src/main/java/com/songoda/epicfarming/utils/Methods.java +++ b/src/main/java/com/songoda/epicfarming/utils/Methods.java @@ -2,6 +2,7 @@ package com.songoda.epicfarming.utils; import com.songoda.arconix.plugin.Arconix; import com.songoda.epicfarming.EpicFarming; +import com.songoda.epicfarming.farming.Level; import org.apache.commons.lang.math.NumberUtils; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; @@ -60,10 +61,10 @@ public class Methods { return null; } - public static ItemStack makeFarmItem(int level) { + public static ItemStack makeFarmItem(Level level) { ItemStack item = new ItemStack(Material.valueOf(EpicFarming.getInstance().getConfig().getString("Main.Farm Block Material")), 1); ItemMeta meta = item.getItemMeta(); - meta.setDisplayName(Arconix.pl().getApi().format().formatText(Methods.formatName(level, true))); + meta.setDisplayName(Arconix.pl().getApi().format().formatText(Methods.formatName(level.getLevel(), true))); item.setItemMeta(meta); return item; } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index b781c81..0a0b28f 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,7 +1,7 @@ name: EpicFarming description: EpicFarming main: com.songoda.epicfarming.EpicFarming -version: 1.2.1 +version: 1.3 depend: [Arconix] author: Songoda commands: