Merge branch 'development'

This commit is contained in:
Brianna 2020-09-09 10:31:55 -05:00
commit 76fbd992a6
24 changed files with 289 additions and 212 deletions

View File

@ -2,7 +2,7 @@
<groupId>com.songoda</groupId>
<artifactId>EpicFarming</artifactId>
<modelVersion>4.0.0</modelVersion>
<version>3.0.23</version>
<version>3.0.24</version>
<build>
<defaultGoal>clean install</defaultGoal>
<finalName>EpicFarming-${project.version}</finalName>

View File

@ -9,12 +9,14 @@ import com.songoda.core.gui.GuiManager;
import com.songoda.core.hooks.EconomyManager;
import com.songoda.core.hooks.EntityStackerManager;
import com.songoda.core.nms.NmsManager;
import com.songoda.core.nms.nbt.NBTCore;
import com.songoda.core.nms.nbt.NBTItem;
import com.songoda.core.utils.TextUtils;
import com.songoda.epicfarming.boost.BoostData;
import com.songoda.epicfarming.boost.BoostManager;
import com.songoda.epicfarming.commands.*;
import com.songoda.epicfarming.commands.CommandBoost;
import com.songoda.epicfarming.commands.CommandGiveFarmItem;
import com.songoda.epicfarming.commands.CommandReload;
import com.songoda.epicfarming.commands.CommandSettings;
import com.songoda.epicfarming.farming.Farm;
import com.songoda.epicfarming.farming.FarmManager;
import com.songoda.epicfarming.farming.FarmType;
@ -24,7 +26,11 @@ import com.songoda.epicfarming.farming.levels.modules.Module;
import com.songoda.epicfarming.farming.levels.modules.ModuleAutoBreeding;
import com.songoda.epicfarming.farming.levels.modules.ModuleAutoButcher;
import com.songoda.epicfarming.farming.levels.modules.ModuleAutoCollect;
import com.songoda.epicfarming.listeners.*;
import com.songoda.epicfarming.listeners.BlockListeners;
import com.songoda.epicfarming.listeners.EntityListeners;
import com.songoda.epicfarming.listeners.InteractListeners;
import com.songoda.epicfarming.listeners.InventoryListeners;
import com.songoda.epicfarming.listeners.UnloadListeners;
import com.songoda.epicfarming.settings.Settings;
import com.songoda.epicfarming.storage.Storage;
import com.songoda.epicfarming.storage.StorageRow;
@ -45,7 +51,11 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.PluginManager;
import java.io.File;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
/**
* Created by songoda on 1/23/2018.
@ -124,64 +134,6 @@ public class EpicFarming extends SongodaPlugin {
this.farmManager = new FarmManager(levelManager);
this.boostManager = new BoostManager();
/*
* Register Farms into FarmManger from configuration
*/
Bukkit.getScheduler().runTaskLaterAsynchronously(this, () -> {
if (storage.containsGroup("farms")) {
for (StorageRow row : storage.getRowsByGroup("farms")) {
Location location = Methods.unserializeLocation(row.getKey());
if (location == null || location.getWorld() == null) continue;
int level = 1;
int configLevel = row.get("level").asInt();
if (configLevel > 0) {
level = configLevel;
}
List<ItemStack> items = new ArrayList<ItemStack>();
List<ItemStack> configItems = row.get("contents").asItemStackList();
if (configItems != null && configItems.size() > 0) {
items = configItems;
}
UUID placedBY = null;
String configPlacedBy = row.get("placedby").asString();
if (configPlacedBy != null) {
placedBY = UUID.fromString(configPlacedBy);
}
FarmType farmType = FarmType.BOTH;
String farmTypeStr = row.get("farmtype").asString();
if (farmTypeStr != null)
farmType = FarmType.valueOf(farmTypeStr);
Farm farm = new Farm(location, levelManager.getLevel(level), placedBY);
farm.setFarmType(farmType);
farm.setItems(items);
Bukkit.getScheduler().runTask(EpicFarming.getInstance(), () ->
farmManager.addFarm(location, farm));
}
}
// Adding in Boosts
if (storage.containsGroup("boosts")) {
for (StorageRow row : storage.getRowsByGroup("boosts")) {
BoostData boostData = new BoostData(
row.get("amount").asInt(),
Long.parseLong(row.getKey()),
UUID.fromString(row.get("player").asString()));
Bukkit.getScheduler().runTask(EpicFarming.getInstance(), () -> {
this.boostManager.addBoostToPlayer(boostData);
});
}
}
// Save data initially so that if the person reloads again fast they don't lose all their data.
this.saveToFile();
}, 10);
// Register Listeners
guiManager.init();
PluginManager pluginManager = Bukkit.getPluginManager();
@ -207,12 +159,67 @@ public class EpicFarming extends SongodaPlugin {
Bukkit.getScheduler().runTaskLater(this, () -> {
if (!Bukkit.getPluginManager().isPluginEnabled("EpicHoppers"))
HopperTask.startTask(this);
}, 20L);
}, 30L);
// Start auto save
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, this::saveToFile, 6000, 6000);
}
@Override
public void onDataLoad() {
if (storage.containsGroup("farms")) {
for (StorageRow row : storage.getRowsByGroup("farms")) {
Location location = Methods.unserializeLocation(row.getKey());
if (location == null || location.getWorld() == null) continue;
int level = 1;
int configLevel = row.get("level").asInt();
if (configLevel > 0) {
level = configLevel;
}
List<ItemStack> items = new ArrayList<ItemStack>();
List<ItemStack> configItems = row.get("contents").asItemStackList();
if (configItems != null && configItems.size() > 0) {
items = configItems;
}
UUID placedBY = null;
String configPlacedBy = row.get("placedby").asString();
if (configPlacedBy != null) {
placedBY = UUID.fromString(configPlacedBy);
}
FarmType farmType = FarmType.BOTH;
String farmTypeStr = row.get("farmtype").asString();
if (farmTypeStr != null)
farmType = FarmType.valueOf(farmTypeStr);
Farm farm = new Farm(location, levelManager.getLevel(level), placedBY);
farm.setFarmType(farmType);
farm.setItems(items);
Bukkit.getScheduler().runTask(EpicFarming.getInstance(), () ->
farmManager.addFarm(location, farm));
}
}
// Adding in Boosts
if (storage.containsGroup("boosts")) {
for (StorageRow row : storage.getRowsByGroup("boosts")) {
BoostData boostData = new BoostData(
row.get("amount").asInt(),
Long.parseLong(row.getKey()),
UUID.fromString(row.get("player").asString()));
Bukkit.getScheduler().runTask(EpicFarming.getInstance(), () -> {
this.boostManager.addBoostToPlayer(boostData);
});
}
}
// Save data initially so that if the person reloads again fast they don't lose all their data.
this.saveToFile();
}
@Override
public void onConfigReload() {
this.setLocale(Settings.LANGUGE_MODE.getString(), true);
@ -287,8 +294,7 @@ public class EpicFarming extends SongodaPlugin {
}
public int getLevelFromItem(ItemStack item) {
NBTCore nbt = NmsManager.getNbt();
NBTItem nbtItem = nbt.of(item);
NBTItem nbtItem = NmsManager.getNbt().of(item);
if (nbtItem.has("level"))
return nbtItem.getNBTObject("level").asInt();

View File

@ -14,21 +14,21 @@ import java.util.List;
public class CommandBoost extends AbstractCommand {
final EpicFarming instance;
private final EpicFarming plugin;
public CommandBoost(EpicFarming instance) {
super(false, "boost");
this.instance = instance;
public CommandBoost(EpicFarming plugin) {
super(CommandType.CONSOLE_OK, "boost");
this.plugin = plugin;
}
@Override
protected ReturnType runCommand(CommandSender sender, String... args) {
if (args.length < 2) {
instance.getLocale().newMessage("&7Syntax error...").sendPrefixedMessage(sender);
plugin.getLocale().newMessage("&7Syntax error...").sendPrefixedMessage(sender);
return ReturnType.SYNTAX_ERROR;
}
if (!Methods.isInt(args[1])) {
instance.getLocale().newMessage("&6" + args[1] + " &7is not a number...").sendPrefixedMessage(sender);
plugin.getLocale().newMessage("&6" + args[1] + " &7is not a number...").sendPrefixedMessage(sender);
return ReturnType.SYNTAX_ERROR;
}
@ -45,14 +45,14 @@ public class CommandBoost extends AbstractCommand {
Player player = Bukkit.getPlayer(args[0]);
if (player == null) {
instance.getLocale().newMessage("&cThat player does not exist or is not online...").sendPrefixedMessage(sender);
plugin.getLocale().newMessage("&cThat player does not exist or is not online...").sendPrefixedMessage(sender);
return ReturnType.FAILURE;
}
BoostData boostData = new BoostData(Integer.parseInt(args[1]), duration == 0L ? Long.MAX_VALUE : System.currentTimeMillis() + duration, player.getUniqueId());
instance.getBoostManager().addBoostToPlayer(boostData);
instance.getLocale().newMessage("&7Successfully boosted &6" + Bukkit.getPlayer(args[0]).getName()
+ "'s &7farms by &6" + args[1] + "x" + (duration == 0L ? "" : (" for " + Methods.makeReadable(duration))) + "&7.").sendPrefixedMessage(sender);
plugin.getBoostManager().addBoostToPlayer(boostData);
plugin.getLocale().newMessage("&7Successfully boosted &6" + Bukkit.getPlayer(args[0]).getName()
+ "'s &7farms by &6" + args[1] + "x" + (duration == 0L ? "" : (" for " + Methods.makeReadable(duration))) + "&7.").sendPrefixedMessage(sender);
return ReturnType.SUCCESS;
}

View File

@ -13,25 +13,25 @@ import java.util.List;
public class CommandGiveFarmItem extends AbstractCommand {
final EpicFarming instance;
private final EpicFarming plugin;
public CommandGiveFarmItem(EpicFarming instance) {
super(false, "givefarmitem");
this.instance = instance;
public CommandGiveFarmItem(EpicFarming plugin) {
super(CommandType.CONSOLE_OK, "givefarmitem");
this.plugin = plugin;
}
@Override
protected ReturnType runCommand(CommandSender sender, String... args) {
if (args.length == 1) return ReturnType.SYNTAX_ERROR;
Level level = instance.getLevelManager().getLowestLevel();
Level level = plugin.getLevelManager().getLowestLevel();
Player player;
if (args.length != 0 && Bukkit.getPlayer(args[0]) == null) {
instance.getLocale().newMessage("&cThat player does not exist or is currently offline.").sendPrefixedMessage(sender);
plugin.getLocale().newMessage("&cThat player does not exist or is currently offline.").sendPrefixedMessage(sender);
return ReturnType.FAILURE;
} else if (args.length == 0) {
if (!(sender instanceof Player)) {
instance.getLocale().newMessage("&cYou need to be a player to give a farm item to yourself.").sendPrefixedMessage(sender);
plugin.getLocale().newMessage("&cYou need to be a player to give a farm item to yourself.").sendPrefixedMessage(sender);
return ReturnType.FAILURE;
}
player = (Player) sender;
@ -40,17 +40,17 @@ public class CommandGiveFarmItem extends AbstractCommand {
}
if (args.length >= 2 && !instance.getLevelManager().isLevel(Integer.parseInt(args[1]))) {
instance.getLocale().newMessage("&cNot a valid level... The current valid levels are: &4"
+ instance.getLevelManager().getLowestLevel().getLevel() + "-"
+ instance.getLevelManager().getHighestLevel().getLevel() + "&c.").sendPrefixedMessage(sender);
if (args.length >= 2 && !plugin.getLevelManager().isLevel(Integer.parseInt(args[1]))) {
plugin.getLocale().newMessage("&cNot a valid level... The current valid levels are: &4"
+ plugin.getLevelManager().getLowestLevel().getLevel() + "-"
+ plugin.getLevelManager().getHighestLevel().getLevel() + "&c.").sendPrefixedMessage(sender);
return ReturnType.FAILURE;
} else if (args.length != 0) {
level = instance.getLevelManager().getLevel(Integer.parseInt(args[1]));
level = plugin.getLevelManager().getLevel(Integer.parseInt(args[1]));
}
player.getInventory().addItem(instance.makeFarmItem(level));
instance.getLocale().getMessage("command.give.success")
player.getInventory().addItem(plugin.makeFarmItem(level));
plugin.getLocale().getMessage("command.give.success")
.processPlaceholder("level", level.getLevel()).sendPrefixedMessage(player);
return ReturnType.SUCCESS;

View File

@ -8,17 +8,17 @@ import java.util.List;
public class CommandReload extends AbstractCommand {
final EpicFarming instance;
private final EpicFarming plugin;
public CommandReload(EpicFarming instance) {
super(false, "reload");
this.instance = instance;
public CommandReload(EpicFarming plugin) {
super(CommandType.CONSOLE_OK, "reload");
this.plugin = plugin;
}
@Override
protected ReturnType runCommand(CommandSender sender, String... args) {
instance.reloadConfig();
instance.getLocale().getMessage("&7Configuration and Language files reloaded.").sendPrefixedMessage(sender);
plugin.reloadConfig();
plugin.getLocale().getMessage("&7Configuration and Language files reloaded.").sendPrefixedMessage(sender);
return AbstractCommand.ReturnType.SUCCESS;
}

View File

@ -10,16 +10,16 @@ import java.util.List;
public class CommandSettings extends AbstractCommand {
final EpicFarming instance;
private final EpicFarming plugin;
public CommandSettings(EpicFarming instance) {
super(true, "settings");
this.instance = instance;
public CommandSettings(EpicFarming plugin) {
super(CommandType.PLAYER_ONLY, "settings");
this.plugin = plugin;
}
@Override
protected ReturnType runCommand(CommandSender sender, String... args) {
instance.getGuiManager().showGUI((Player) sender, new PluginConfigGui(instance));
plugin.getGuiManager().showGUI((Player) sender, new PluginConfigGui(plugin));
return ReturnType.SUCCESS;
}

View File

@ -17,7 +17,13 @@ import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
public class Farm {

View File

@ -2,7 +2,11 @@ package com.songoda.epicfarming.farming.levels;
import com.songoda.epicfarming.farming.levels.modules.Module;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
public class LevelManager {

View File

@ -13,12 +13,20 @@ import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.*;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ModuleAutoBreeding extends Module {

View File

@ -13,12 +13,23 @@ import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.entity.*;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.Chicken;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Sheep;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Wool;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class ModuleAutoCollect extends Module {
@ -86,7 +97,7 @@ public class ModuleAutoCollect extends Module {
if (!isEnabled(farm)) {
ticksLived.remove(entity);
Bukkit.getScheduler().runTask(plugin, () ->
entity.getLocation().getWorld().dropItemNaturally(entity.getLocation(), new ItemStack(Material.EGG)));
entity.getLocation().getWorld().dropItemNaturally(entity.getLocation(), new ItemStack(Material.EGG)));
} else {
doLivestockDrop(farm, new ItemStack(Material.EGG, 1));
}
@ -100,7 +111,7 @@ public class ModuleAutoCollect extends Module {
ItemStack wool = woolColor.toItemStack((int) Math.round(1 + (Math.random() * 3)));
if (!isEnabled(farm)) {
Bukkit.getScheduler().runTask(plugin, () ->
entity.getLocation().getWorld().dropItemNaturally(entity.getLocation(), wool));
entity.getLocation().getWorld().dropItemNaturally(entity.getLocation(), wool));
} else {
doLivestockDrop(farm, wool);
}

View File

@ -18,7 +18,11 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class OverviewGui extends Gui {

View File

@ -3,10 +3,7 @@ package com.songoda.epicfarming.listeners;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.epicfarming.EpicFarming;
import com.songoda.epicfarming.farming.Farm;
import com.songoda.epicfarming.farming.FarmManager;
import com.songoda.epicfarming.farming.FarmType;
import com.songoda.epicfarming.farming.levels.Level;
import com.songoda.epicfarming.farming.levels.modules.ModuleAutoCollect;
import com.songoda.epicfarming.settings.Settings;
import com.songoda.epicfarming.tasks.FarmTask;
import org.bukkit.Bukkit;
@ -17,7 +14,12 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.*;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.block.BlockFadeEvent;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.block.BlockGrowEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.PermissionAttachmentInfo;
@ -27,22 +29,22 @@ import org.bukkit.permissions.PermissionAttachmentInfo;
@SuppressWarnings("Duplicates")
public class BlockListeners implements Listener {
private final EpicFarming instance;
private final EpicFarming plugin;
public BlockListeners(EpicFarming instance) {
this.instance = instance;
public BlockListeners(EpicFarming plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockFade(BlockFadeEvent e) {
Farm farm = instance.getFarmManager().checkForFarm(e.getBlock().getLocation());
Farm farm = plugin.getFarmManager().checkForFarm(e.getBlock().getLocation());
if (farm != null && farm.getFarmType() != FarmType.LIVESTOCK)
e.setCancelled(true);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onGrow(BlockGrowEvent e) {
Farm farm = instance.getFarmManager().checkForFarm(e.getBlock().getLocation());
Farm farm = plugin.getFarmManager().checkForFarm(e.getBlock().getLocation());
if (farm != null && farm.getFarmType() != FarmType.LIVESTOCK)
e.setCancelled(true);
}
@ -63,13 +65,13 @@ public class BlockListeners implements Listener {
Material farmBlock = Settings.FARM_BLOCK_MATERIAL.getMaterial(CompatibleMaterial.END_ROD).getBlockMaterial();
if (e.getPlayer().getItemInHand().getType() != farmBlock
|| instance.getLevelFromItem(e.getItemInHand()) == 0 && !Settings.NON_COMMAND_FARMS.getBoolean())
|| plugin.getLevelFromItem(e.getItemInHand()) == 0 && !Settings.NON_COMMAND_FARMS.getBoolean())
return;
if (e.getBlockAgainst().getType() == farmBlock) e.setCancelled(true);
int amt = 0;
for (Farm farmm : instance.getFarmManager().getFarms().values()) {
for (Farm farmm : plugin.getFarmManager().getFarms().values()) {
if (farmm.getPlacedBy() == null || !farmm.getPlacedBy().equals(e.getPlayer().getUniqueId())) continue;
amt++;
}
@ -77,23 +79,23 @@ public class BlockListeners implements Listener {
if (limit != -1 && amt >= limit) {
e.setCancelled(true);
instance.getLocale().getMessage("event.limit.hit")
plugin.getLocale().getMessage("event.limit.hit")
.processPlaceholder("limit", limit).sendPrefixedMessage(e.getPlayer());
return;
}
Location location = e.getBlock().getLocation();
if (e.getBlockPlaced().getType().equals(Material.MELON_SEEDS) || e.getBlockPlaced().getType().equals(Material.PUMPKIN_SEEDS)) {
if (instance.getFarmManager().checkForFarm(location) != null) {
instance.getLocale().getMessage("event.warning.noauto").sendPrefixedMessage(e.getPlayer());
if (plugin.getFarmManager().checkForFarm(location) != null) {
plugin.getLocale().getMessage("event.warning.noauto").sendPrefixedMessage(e.getPlayer());
}
}
int level = instance.getLevelFromItem(e.getItemInHand());
Bukkit.getScheduler().runTaskLater(instance, () -> {
int level = plugin.getLevelFromItem(e.getItemInHand());
Bukkit.getScheduler().runTaskLater(plugin, () -> {
if (location.getBlock().getType() != farmBlock) return;
Farm farm = new Farm(location, instance.getLevelManager().getLevel(level == 0 ? 1 : level), e.getPlayer().getUniqueId());
instance.getFarmManager().addFarm(location, farm);
Farm farm = new Farm(location, plugin.getLevelManager().getLevel(level == 0 ? 1 : level), e.getPlayer().getUniqueId());
plugin.getFarmManager().addFarm(location, farm);
farm.tillLand();
}, 1);
@ -104,7 +106,7 @@ public class BlockListeners implements Listener {
if (event.getBlock().getType() != Settings.FARM_BLOCK_MATERIAL.getMaterial(CompatibleMaterial.END_ROD).getMaterial())
return;
Farm farm = instance.getFarmManager().removeFarm(event.getBlock().getLocation());
Farm farm = plugin.getFarmManager().removeFarm(event.getBlock().getLocation());
if (farm == null) return;
@ -112,7 +114,7 @@ public class BlockListeners implements Listener {
event.setCancelled(true);
ItemStack item = instance.makeFarmItem(farm.getLevel());
ItemStack item = plugin.makeFarmItem(farm.getLevel());
Block block = event.getBlock();
@ -129,14 +131,14 @@ public class BlockListeners implements Listener {
if (event.getBlock().getType() != Settings.FARM_BLOCK_MATERIAL.getMaterial(CompatibleMaterial.END_ROD).getMaterial())
return;
Farm farm = instance.getFarmManager().removeFarm(event.getBlock().getLocation());
Farm farm = plugin.getFarmManager().removeFarm(event.getBlock().getLocation());
if (farm == null) return;
FarmTask.getCrops(farm, false);
event.setCancelled(true);
ItemStack item = instance.makeFarmItem(farm.getLevel());
ItemStack item = plugin.makeFarmItem(farm.getLevel());
Block block = event.getBlock();
@ -151,7 +153,7 @@ public class BlockListeners implements Listener {
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onBlockFromToEventMonitor(BlockFromToEvent event) {
// prevent water/lava/egg griefs
if (instance.getFarmManager().getFarm(event.getToBlock()) != null) {
if (plugin.getFarmManager().getFarm(event.getToBlock()) != null) {
event.setCancelled(true);
}
}

View File

@ -11,7 +11,11 @@ import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.*;
import org.bukkit.entity.Chicken;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
@ -28,10 +32,10 @@ import java.util.List;
public class EntityListeners implements Listener {
private final EpicFarming instance;
private final EpicFarming plugin;
public EntityListeners(EpicFarming instance) {
this.instance = instance;
public EntityListeners(EpicFarming plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.HIGHEST)
@ -39,7 +43,7 @@ public class EntityListeners implements Listener {
LivingEntity entity = event.getEntity();
if (!entity.hasMetadata("EFA-TAGGED")) return;
Location location = (Location) entity.getMetadata("EFA-TAGGED").get(0).value();
Farm farm = instance.getFarmManager().getFarm(location);
Farm farm = plugin.getFarmManager().getFarm(location);
boolean autoCollect = false;
for (Module module : farm.getLevel().getRegisteredModules()) {
@ -95,7 +99,7 @@ public class EntityListeners implements Listener {
if (block.getType() != Settings.FARM_BLOCK_MATERIAL.getMaterial(CompatibleMaterial.END_ROD).getMaterial())
continue;
Farm farm = instance.getFarmManager().getFarm(block.getLocation());
Farm farm = plugin.getFarmManager().getFarm(block.getLocation());
if (farm == null) continue;
toCancel.add(block);
@ -104,11 +108,11 @@ public class EntityListeners implements Listener {
for (Block block : toCancel) {
event.blockList().remove(block);
Farm farm = instance.getFarmManager().removeFarm(block.getLocation());
Farm farm = plugin.getFarmManager().removeFarm(block.getLocation());
FarmTask.getCrops(farm, false);
ItemStack item = instance.makeFarmItem(farm.getLevel());
ItemStack item = plugin.makeFarmItem(farm.getLevel());
block.setType(Material.AIR);
block.getLocation().getWorld().dropItemNaturally(block.getLocation().add(.5, .5, .5), item);

View File

@ -17,10 +17,10 @@ import org.bukkit.event.player.PlayerInteractEvent;
*/
public class InteractListeners implements Listener {
private final EpicFarming instance;
private final EpicFarming plugin;
public InteractListeners(EpicFarming instance) {
this.instance = instance;
public InteractListeners(EpicFarming plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.HIGHEST)
@ -29,7 +29,7 @@ public class InteractListeners implements Listener {
Location location = e.getClickedBlock().getLocation();
if (e.getItem() != null && CompatibleMaterial.getMaterial(e.getItem()) == CompatibleMaterial.BONE_MEAL
&& instance.getFarmManager().checkForFarm(location) != null)
&& plugin.getFarmManager().checkForFarm(location) != null)
e.setCancelled(true);
if (e.getClickedBlock().getType() != Settings.FARM_BLOCK_MATERIAL.getMaterial().getMaterial())
@ -47,9 +47,9 @@ public class InteractListeners implements Listener {
return;
}
if (instance.getFarmManager().getFarms().containsKey(location)) {
if (plugin.getFarmManager().getFarms().containsKey(location)) {
e.setCancelled(true);
instance.getFarmManager().getFarm(location).view(e.getPlayer(), false);
plugin.getFarmManager().getFarm(location).view(e.getPlayer(), false);
}
}
}

View File

@ -17,7 +17,6 @@ public class InventoryListeners implements Listener {
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
if (event.getCurrentItem() == null) return;
if (event.getRawSlot() > event.getView().getTopInventory().getSize() - 1) return;

View File

@ -13,21 +13,21 @@ import java.util.ArrayList;
public class UnloadListeners implements Listener {
private final EpicFarming instance;
private final EpicFarming plugin;
public UnloadListeners(EpicFarming instance) {
this.instance = instance;
public UnloadListeners(EpicFarming plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onUnload(ChunkUnloadEvent event) {
Material type = Settings.FARM_BLOCK_MATERIAL.getMaterial().getMaterial();
for (Farm farm : new ArrayList<>(instance.getFarmManager().getFarms().values())) {
for (Farm farm : new ArrayList<>(plugin.getFarmManager().getFarms().values())) {
int x = farm.getLocation().getBlockX() >> 4;
int z = farm.getLocation().getBlockZ() >> 4;
if (event.getChunk().getX() == x && event.getChunk().getZ() == z) {
if (farm.getLocation().getBlock().getType() != type)
instance.getFarmManager().removeFarm(farm.getLocation());
plugin.getFarmManager().removeFarm(farm.getLocation());
}
}
}

View File

@ -40,17 +40,17 @@ public class StorageItem {
public String asString() {
if (object == null) return null;
return (String)object;
return (String) object;
}
public boolean asBoolean() {
if (object == null) return false;
return (boolean)object;
return (boolean) object;
}
public int asInt() {
if (object == null) return 0;
return (int)object;
return (int) object;
}
public Object asObject() {
@ -61,7 +61,7 @@ public class StorageItem {
List<ItemStack> list = new ArrayList<>();
if (object == null) return list;
String obj = (String) object;
if (obj.equals("[]"))return list;
if (obj.equals("[]")) return list;
List<String> sers = new ArrayList<>(Arrays.asList(obj.split(";;")));
for (String ser : sers) {
list.add(Serializers.deserialize(ser));

View File

@ -7,8 +7,19 @@ import com.songoda.epicfarming.storage.StorageRow;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemorySection;
import java.io.*;
import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class StorageYaml extends Storage {

View File

@ -16,7 +16,13 @@ import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
public class FarmTask extends BukkitRunnable {

View File

@ -10,7 +10,11 @@ import com.songoda.epicfarming.utils.CropType;
import org.bukkit.Location;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class GrowthTask extends BukkitRunnable {
@ -31,7 +35,7 @@ public class GrowthTask extends BukkitRunnable {
}
@Override
public void run() {
public synchronized void run() {
List<Crop> toRemove = new ArrayList<>();
for (Crop crop : liveCrops.values()) {
@ -63,12 +67,12 @@ public class GrowthTask extends BukkitRunnable {
}
public void addLiveCrop(Location location, Crop crop) {
public synchronized void addLiveCrop(Location location, Crop crop) {
if (!liveCrops.containsKey(location))
liveCrops.put(location, crop);
}
public void removeCropByLocation(Location location) {
public synchronized void removeCropByLocation(Location location) {
liveCrops.remove(location);
}

View File

@ -19,7 +19,7 @@ public class CachedChunk {
}
public CachedChunk(Location location) {
this(location.getWorld().getName(), (int)location.getX() >> 4, (int)location.getZ() >> 4);
this(location.getWorld().getName(), (int) location.getX() >> 4, (int) location.getZ() >> 4);
}
public CachedChunk(String world, int x, int z) {

View File

@ -5,7 +5,12 @@ import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class EntityUtils {

View File

@ -1,6 +1,5 @@
package com.songoda.epicfarming.utils;
import com.songoda.core.utils.TextUtils;
import com.songoda.epicfarming.EpicFarming;
import com.songoda.epicfarming.settings.Settings;
import org.bukkit.Bukkit;

View File

@ -10,41 +10,40 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.SkullMeta;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Serializers {
public static String serialize(ItemStack item){
public static String serialize(ItemStack item) {
StringBuilder builder = new StringBuilder();
builder.append(item.getType().toString());
if(item.getDurability() != 0) builder.append(":" + item.getDurability());
if (item.getDurability() != 0) builder.append(":" + item.getDurability());
builder.append(" " + item.getAmount());
for(Enchantment enchant:item.getEnchantments().keySet())
for (Enchantment enchant : item.getEnchantments().keySet())
builder.append(" " + enchant.getName() + ":" + item.getEnchantments().get(enchant));
String name = getName(item);
if(name != null) builder.append(" name:" + name);
if (name != null) builder.append(" name:" + name);
String lore = getLore(item);
if(lore != null) builder.append(" lore:" + lore);
if (lore != null) builder.append(" lore:" + lore);
Color color = getArmorColor(item);
if(color != null) builder.append(" rgb:" + color.getRed() + "|" + color.getGreen() + "|" + color.getBlue());
if (color != null) builder.append(" rgb:" + color.getRed() + "|" + color.getGreen() + "|" + color.getBlue());
String owner = getOwner(item);
if(owner != null) builder.append(" owner:" + owner);
if (owner != null) builder.append(" owner:" + owner);
return builder.toString();
}
public static ItemStack deserialize(String serializedItem){
public static ItemStack deserialize(String serializedItem) {
String[] strings = serializedItem.split(" ");
Map<Enchantment, Integer> enchants = new HashMap<Enchantment, Integer>();
String[] args;
ItemStack item = new ItemStack(Material.AIR);
for (String str: strings) {
for (String str : strings) {
args = str.split(":");
if(Material.matchMaterial(args[0]) != null && item.getType() == Material.AIR){
if (Material.matchMaterial(args[0]) != null && item.getType() == Material.AIR) {
item.setType(Material.matchMaterial(args[0]));
if(args.length == 2) item.setDurability(Short.parseShort(args[1]));
if (args.length == 2) item.setDurability(Short.parseShort(args[1]));
break;
}
}
@ -52,27 +51,27 @@ public class Serializers {
Bukkit.getLogger().info("Could not find a valid material for the item in \"" + serializedItem + "\"");
return null;
}
for(String str:strings){
for (String str : strings) {
args = str.split(":", 2);
if(isNumber(args[0])) item.setAmount(Integer.parseInt(args[0]));
if(args.length == 1) continue;
if(args[0].equalsIgnoreCase("name:")){
if (isNumber(args[0])) item.setAmount(Integer.parseInt(args[0]));
if (args.length == 1) continue;
if (args[0].equalsIgnoreCase("name:")) {
setName(item, ChatColor.translateAlternateColorCodes('&', args[1]));
continue;
}
if(args[0].equalsIgnoreCase("lore:")){
if (args[0].equalsIgnoreCase("lore:")) {
setLore(item, ChatColor.translateAlternateColorCodes('&', args[1]));
continue;
}
if(args[0].equalsIgnoreCase("rgb:")){
if (args[0].equalsIgnoreCase("rgb:")) {
setArmorColor(item, args[1]);
continue;
}
if(args[0].equalsIgnoreCase("owner:")){
if (args[0].equalsIgnoreCase("owner:")) {
setOwner(item, args[1]);
continue;
}
if(Enchantment.getByName(args[0].toUpperCase()) != null){
if (Enchantment.getByName(args[0].toUpperCase()) != null) {
enchants.put(Enchantment.getByName(args[0].toUpperCase()), Integer.parseInt(args[1]));
continue;
}
@ -80,52 +79,60 @@ public class Serializers {
item.addUnsafeEnchantments(enchants);
return item.getType().equals(Material.AIR) ? null : item;
}
private static String getOwner(ItemStack item){
if(!(item.getItemMeta() instanceof SkullMeta)) return null;
return ((SkullMeta)item.getItemMeta()).getOwner();
private static String getOwner(ItemStack item) {
if (!(item.getItemMeta() instanceof SkullMeta)) return null;
return ((SkullMeta) item.getItemMeta()).getOwner();
}
private static void setOwner(ItemStack item, String owner){
try{
private static void setOwner(ItemStack item, String owner) {
try {
SkullMeta meta = (SkullMeta) item.getItemMeta();
meta.setOwner(owner);
item.setItemMeta(meta);
}catch(Exception exception){
} catch (Exception exception) {
return;
}
}
private static String getName(ItemStack item){
if(!item.hasItemMeta()) return null;
if(!item.getItemMeta().hasDisplayName()) return null;
private static String getName(ItemStack item) {
if (!item.hasItemMeta()) return null;
if (!item.getItemMeta().hasDisplayName()) return null;
return item.getItemMeta().getDisplayName().replace(" ", "_").replace(ChatColor.COLOR_CHAR, '&');
}
private static void setName(ItemStack item, String name){
private static void setName(ItemStack item, String name) {
name = name.replace("_", " ");
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(name);
item.setItemMeta(meta);
}
private static String getLore(ItemStack item){
if(!item.hasItemMeta()) return null;
if(!item.getItemMeta().hasLore()) return null;
private static String getLore(ItemStack item) {
if (!item.hasItemMeta()) return null;
if (!item.getItemMeta().hasLore()) return null;
StringBuilder builder = new StringBuilder();
List<String> lore = item.getItemMeta().getLore();
for(int ind = 0;ind<lore.size();ind++){
for (int ind = 0; ind < lore.size(); ind++) {
builder.append((ind > 0 ? "|" : "") + lore.get(ind).replace(" ", "_").replace(ChatColor.COLOR_CHAR, '&'));
}
return builder.toString();
}
private static void setLore(ItemStack item, String lore){
private static void setLore(ItemStack item, String lore) {
lore = lore.replace("_", " ");
ItemMeta meta = item.getItemMeta();
meta.setLore(Arrays.asList(lore.split("\\|")));
item.setItemMeta(meta);
}
private static Color getArmorColor(ItemStack item){
if(!(item.getItemMeta() instanceof LeatherArmorMeta)) return null;
return ((LeatherArmorMeta)item.getItemMeta()).getColor();
private static Color getArmorColor(ItemStack item) {
if (!(item.getItemMeta() instanceof LeatherArmorMeta)) return null;
return ((LeatherArmorMeta) item.getItemMeta()).getColor();
}
private static void setArmorColor(ItemStack item, String str){
try{
private static void setArmorColor(ItemStack item, String str) {
try {
String[] colors = str.split("\\|");
int red = Integer.parseInt(colors[0]);
int green = Integer.parseInt(colors[1]);
@ -133,14 +140,15 @@ public class Serializers {
LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
meta.setColor(Color.fromRGB(red, green, blue));
item.setItemMeta(meta);
}catch(Exception exception){
} catch (Exception exception) {
return;
}
}
private static boolean isNumber(String str){
try{
private static boolean isNumber(String str) {
try {
Integer.parseInt(str);
}catch(NumberFormatException exception){
} catch (NumberFormatException exception) {
return false;
}
return true;