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.
This commit is contained in:
Brianna O'Keefe 2018-05-17 03:13:44 -04:00
parent 9a7c589321
commit b1e10d1edf
11 changed files with 97 additions and 69 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.songoda</groupId>
<artifactId>EpicFarming</artifactId>
<version>1.2.1</version>
<version>1.3</version>
<packaging>jar</packaging>
<build>

View File

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

View File

@ -1,23 +0,0 @@
package com.songoda.epicfarming.api;
import java.util.List;
public interface ILevel {
List<String> getDescription();
int getLevel();
int getRadius();
boolean isAutoHarvest();
boolean isAutoReplant();
double getSpeedMultiplier();
int getCostExperiance();
int getCostEconomy();
}

View File

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

View File

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

View File

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

View File

@ -25,6 +25,10 @@ public class LevelManager {
return registeredLevels.lastEntry().getValue();
}
public boolean isLevel(int level) {
return registeredLevels.containsKey(level);
}
public Map<Integer, Level> getLevels() {
return Collections.unmodifiableMap(registeredLevels);
}

View File

@ -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")) {

View File

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

View File

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

View File

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