New PluginConfig Option & Fix For Paper Hoppers

This commit is contained in:
jameslfc19 2020-08-01 20:58:23 +01:00
parent b2925e44a6
commit d1cc5d8289
16 changed files with 259 additions and 191 deletions

View File

@ -12,7 +12,7 @@ import com.jamesdpeters.minecraft.chests.listeners.InventoryListener;
import com.jamesdpeters.minecraft.chests.listeners.WorldListener;
import com.jamesdpeters.minecraft.chests.maventemplates.BuildConstants;
import com.jamesdpeters.minecraft.chests.misc.Permissions;
import com.jamesdpeters.minecraft.chests.misc.Settings;
import com.jamesdpeters.minecraft.chests.serialize.PluginConfig;
import com.jamesdpeters.minecraft.chests.misc.Stats;
import com.jamesdpeters.minecraft.chests.misc.Utils;
import com.jamesdpeters.minecraft.chests.storage.autocraft.AutoCraftingStorage;
@ -80,12 +80,12 @@ public class ChestsPlusPlus extends JavaPlugin {
public void onEnable() {
int pluginId = 7166;
Metrics metrics = new Metrics(this, pluginId);
Stats.addCharts(metrics);
PLUGIN = this;
LangFileProperties.moveLangFiles(getFile());
Settings.initConfig(this);
LangFileProperties.loadLangFile(Settings.getLangFileName());
Utils.copyFromResources(getFile(), "lang");
PluginConfig.load(this);
LangFileProperties.loadLangFile(PluginConfig.LANG_FILE.get());
Stats.addCharts(metrics);
//API initialisation
API.register(this);
@ -107,7 +107,7 @@ public class ChestsPlusPlus extends JavaPlugin {
if(isDev) getLogger().warning("You are currently running a Dev build - update checker disabled! Build: "+BuildConstants.VERSION);
if(isBeta) getLogger().warning("You are currently running a Beta build - update checker disabled! Build: "+BuildConstants.VERSION);
if(Settings.isUpdateCheckEnabled() && !isDev && !isBeta) {
if(PluginConfig.IS_UPDATE_CHECKER_ENABLED.get() && !isDev && !isBeta) {
String BUKKIT_URL = "https://dev.bukkit.org/projects/chests-plus-plus/files";
UpdateChecker.init(this, 71355, UpdateChecker.VERSION_SCHEME_DECIMAL);
Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> {
@ -123,7 +123,7 @@ public class ChestsPlusPlus extends JavaPlugin {
}
boot = true;
});
}, 0, Settings.getUpdateCheckerPeriodTicks());
}, 0, PluginConfig.UPDATE_CHECKER_PERIOD.get()*20);
}
//Load storages after load.
@ -145,8 +145,8 @@ public class ChestsPlusPlus extends JavaPlugin {
public void onDisable() {
super.onDisable();
Config.save();
//Remove entities that could have been left behind from bad save files/crashes etc.
Utils.fixEntities();
// //Remove entities that could have been left behind from bad save files/crashes etc.
// Utils.fixEntities();
}
}

View File

@ -4,7 +4,6 @@ import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
import com.jamesdpeters.minecraft.chests.lang.Message;
import com.jamesdpeters.minecraft.chests.misc.Messages;
import com.jamesdpeters.minecraft.chests.misc.Permissions;
import com.jamesdpeters.minecraft.chests.misc.Settings;
import com.jamesdpeters.minecraft.chests.misc.Utils;
import com.jamesdpeters.minecraft.chests.storage.autocraft.AutoCraftingStorage;
import com.jamesdpeters.minecraft.chests.serialize.Config;
@ -79,7 +78,7 @@ public class AutoCraftCommand extends ServerCommand {
return true;
case ADD:
if (args.length > 1) {
if (player.hasPermission(Permissions.AUTOCRAFT_ADD) && !Settings.isBlacklistedWorld(player.getWorld())) {
if (player.hasPermission(Permissions.AUTOCRAFT_ADD) && !Utils.isBlacklistedWorld(player.getWorld())) {
Block targetBlock = player.getTargetBlockExact(5);
if (targetBlock != null)
Config.getAutoCraft().createStorage(player, targetBlock, args[1], true);
@ -95,7 +94,7 @@ public class AutoCraftCommand extends ServerCommand {
}
case OPEN:
if (args.length > 1) {
if (sender.hasPermission(Permissions.AUTOCRAFT_OPEN) && sender.hasPermission(Permissions.AUTOCRAFT_OPEN_REMOTE) && !Settings.isBlacklistedWorld(player.getWorld())) {
if (sender.hasPermission(Permissions.AUTOCRAFT_OPEN) && sender.hasPermission(Permissions.AUTOCRAFT_OPEN_REMOTE) && !Utils.isBlacklistedWorld(player.getWorld())) {
AutoCraftingStorage invs;
if (args[1].contains(":")) {
invs = Config.getAutoCraft().getStorage(player, args[1]);

View File

@ -5,7 +5,6 @@ import com.jamesdpeters.minecraft.chests.inventories.ChestLinkMenu;
import com.jamesdpeters.minecraft.chests.lang.Message;
import com.jamesdpeters.minecraft.chests.misc.Messages;
import com.jamesdpeters.minecraft.chests.misc.Permissions;
import com.jamesdpeters.minecraft.chests.misc.Settings;
import com.jamesdpeters.minecraft.chests.misc.Utils;
import com.jamesdpeters.minecraft.chests.serialize.Config;
import com.jamesdpeters.minecraft.chests.storage.chestlink.ChestLinkStorage;
@ -18,7 +17,6 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@ -83,7 +81,7 @@ public class ChestLinkCommand extends ServerCommand {
return true;
case ADD:
if (args.length > 1) {
if (sender.hasPermission(Permissions.ADD) && !Settings.isBlacklistedWorld(player.getWorld())) {
if (sender.hasPermission(Permissions.ADD) && !Utils.isBlacklistedWorld(player.getWorld())) {
Block targetBlock = player.getTargetBlockExact(5);
if (targetBlock != null)
Config.getChestLink().createStorage(player, targetBlock, args[1], true);
@ -100,7 +98,7 @@ public class ChestLinkCommand extends ServerCommand {
}
case OPEN:
if (args.length > 1) {
if (sender.hasPermission(Permissions.OPEN) && sender.hasPermission(Permissions.OPEN_REMOTE) && !Settings.isBlacklistedWorld(player.getWorld())) {
if (sender.hasPermission(Permissions.OPEN) && sender.hasPermission(Permissions.OPEN_REMOTE) && !Utils.isBlacklistedWorld(player.getWorld())) {
ChestLinkStorage invs;
if (args[1].contains(":")) {
invs = Config.getChestLink().getStorage(player, args[1]);
@ -119,7 +117,7 @@ public class ChestLinkCommand extends ServerCommand {
return true;
}
case MENU:
if (sender.hasPermission(Permissions.MENU) && !Settings.isBlacklistedWorld(player.getWorld())) {
if (sender.hasPermission(Permissions.MENU) && !Utils.isBlacklistedWorld(player.getWorld())) {
ChestLinkMenu.getMenu(player).open(player);
return true;
} else {

View File

@ -73,27 +73,6 @@ public class LangFileProperties {
return file;
}
public static void moveLangFiles(File jarFile){
String directory = "lang";
try (JarFile jar = new JarFile(jarFile)) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if (!name.startsWith(directory + "/") || entry.isDirectory()) {
continue;
}
ChestsPlusPlus.PLUGIN.saveResource(name, true);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/*
IDE LANGUAGE GENERATOR
*/

View File

@ -14,11 +14,11 @@ import java.util.Properties;
public class LanguageFile extends Properties {
public void store(File file) throws IOException {
store0(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)), false);
store0(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "8859_1")), false);
}
public void storeGenerated(File file) throws IOException {
store0(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)), true);
store0(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"8859_1")), true);
}
private void store0(BufferedWriter bw, boolean generated)

View File

@ -3,7 +3,8 @@ package com.jamesdpeters.minecraft.chests.listeners;
import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
import com.jamesdpeters.minecraft.chests.api.ApiSpecific;
import com.jamesdpeters.minecraft.chests.filters.HopperFilter;
import com.jamesdpeters.minecraft.chests.misc.Settings;
import com.jamesdpeters.minecraft.chests.runnables.VirtualChestToHopper;
import com.jamesdpeters.minecraft.chests.serialize.PluginConfig;
import com.jamesdpeters.minecraft.chests.serialize.Config;
import com.jamesdpeters.minecraft.chests.misc.Utils;
import com.jamesdpeters.minecraft.chests.storage.chestlink.ChestLinkStorage;
@ -17,14 +18,9 @@ import org.bukkit.block.Block;
import org.bukkit.block.Hopper;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ItemFrame;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityEvent;
import org.bukkit.event.entity.EntityInteractEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.inventory.InventoryPickupItemEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
@ -39,7 +35,10 @@ public class HopperListener implements Listener {
if(event.getDestination().getLocation() != null){
if(event.getDestination().getLocation().getBlock().isBlockPowered()) return;
}
event.setCancelled(!HopperFilter.isInFilter(event.getDestination().getLocation().getBlock(),event.getItem()));
if(!event.isCancelled()) Bukkit.getScheduler().scheduleSyncDelayedTask(ChestsPlusPlus.PLUGIN, ()-> {
VirtualChestToHopper.move(event.getDestination().getLocation(), event.getSource(), event.getDestination());
}, 1);
event.setCancelled(true);
}
}
@ -57,7 +56,7 @@ public class HopperListener implements Listener {
public void run() {
if(location != null) {
int hopperAmount = SpigotConfig.getWorldSettings(location.getWorld()).getHopperAmount();
if (Utils.moveToOtherInventory(event.getSource(), hopperAmount, storage.getInventory())) {
if (Utils.hopperMove(event.getSource(), hopperAmount, storage.getInventory())) {
storage.updateDisplayItem();
}
if (event.getDestination().getHolder() != null) event.getDestination().getHolder().getInventory().clear();
@ -87,7 +86,7 @@ public class HopperListener implements Listener {
Rotation rotation = itemFrame.getRotation();
//Set ItemFrame invisible based on config.
ApiSpecific.getNmsProvider().setItemFrameVisible(itemFrame, !Settings.isFilterItemFrameInvisible());
ApiSpecific.getNmsProvider().setItemFrameVisible(itemFrame, !PluginConfig.INVISIBLE_FILTER_ITEM_FRAMES.get());
//ItemFrame event acts weird, it returns the values of the itemframe *before* the event. So we have to calculate what the next state will be.
if(!itemFrame.getItem().getType().equals(Material.AIR)) rotation = rotation.rotateClockwise();

View File

@ -1,116 +0,0 @@
package com.jamesdpeters.minecraft.chests.misc;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Settings {
private static String CHECK_UPDATE = "update-checker";
private static String CHECK_UPDATE_PERIOD = "update-checker-period";
private static String LIMIT_CHESTS = "limit-chestlinks";
private static String LIMIT_CHESTS_NUMBER = "limit-chestlinks-amount";
private static String SHOULD_ANIMATE_ALL_CHESTS = "should-animate-all-chests";
private static String RUN_HOPPERS_UNLOADED_CHUNKS = "run-hoppers-unloaded-chunks";
private static String SHOULD_CHEST_ARMOUR_STAND = "display_chestlink_armour_stands";
private static String SHOULD_AUTOCRAFT_ARMOUR_STAND = "display_autocraft_armour_stands";
private static String INVISIBLE_FILTER_ITEM_FRAMES = "set-filter-itemframe-invisible";
private static String WORLD_BLACKLIST = "world-blacklist";
private static String LANG_FILE = "language-file";
private static Settings cf;
private FileConfiguration configuration;
private Plugin plugin;
private static boolean isUpdateCheckEnabled;
private static int updateCheckerPeriod;
private static boolean limitChests;
private static int limitChestsAmount;
private static boolean shouldAnimateAllChests;
private static boolean runHoppersInUnloadedChunks;
private static boolean shouldDisplayChestLinkStand;
private static boolean shouldDisplayAutoCraftStand;
private static boolean filterItemFrameInvisible;
private static List<String> worldBlacklist;
private static String langFileName;
public static void initConfig(Plugin plugin){
cf = new Settings();
cf.plugin = plugin;
cf.configuration = plugin.getConfig();
//DEFAULT VALUES
cf.configuration.addDefault(CHECK_UPDATE,true);
cf.configuration.addDefault(CHECK_UPDATE_PERIOD,60*60);
cf.configuration.addDefault(LIMIT_CHESTS,false);
cf.configuration.addDefault(LIMIT_CHESTS_NUMBER,0);
cf.configuration.addDefault(SHOULD_ANIMATE_ALL_CHESTS,true);
cf.configuration.addDefault(RUN_HOPPERS_UNLOADED_CHUNKS,false);
cf.configuration.addDefault(SHOULD_CHEST_ARMOUR_STAND,true);
cf.configuration.addDefault(SHOULD_AUTOCRAFT_ARMOUR_STAND,true);
cf.configuration.addDefault(INVISIBLE_FILTER_ITEM_FRAMES, false);
cf.configuration.addDefault(WORLD_BLACKLIST, Collections.singletonList(""));
cf.configuration.addDefault(LANG_FILE, "default");
cf.configuration.options().copyDefaults(true);
cf.plugin.saveConfig();
reloadConfig();
}
private static void save(){
cf.plugin.saveConfig();
}
public static void reloadConfig(){
cf.configuration = cf.plugin.getConfig();
isUpdateCheckEnabled = cf.configuration.getBoolean(CHECK_UPDATE);
updateCheckerPeriod = cf.configuration.getInt(CHECK_UPDATE_PERIOD);
limitChests = cf.configuration.getBoolean(LIMIT_CHESTS);
limitChestsAmount = cf.configuration.getInt(LIMIT_CHESTS_NUMBER);
shouldAnimateAllChests = cf.configuration.getBoolean(SHOULD_ANIMATE_ALL_CHESTS);
runHoppersInUnloadedChunks = cf.configuration.getBoolean(RUN_HOPPERS_UNLOADED_CHUNKS);
shouldDisplayChestLinkStand = cf.configuration.getBoolean(SHOULD_CHEST_ARMOUR_STAND);
shouldDisplayAutoCraftStand = cf.configuration.getBoolean(SHOULD_AUTOCRAFT_ARMOUR_STAND);
filterItemFrameInvisible = cf.configuration.getBoolean(INVISIBLE_FILTER_ITEM_FRAMES);
worldBlacklist = cf.configuration.getStringList(WORLD_BLACKLIST);
langFileName = cf.configuration.getString(LANG_FILE);
}
/**
* GETTERS AND SETTERS
*/
public static boolean isUpdateCheckEnabled() {
return isUpdateCheckEnabled;
}
public static int getUpdateCheckerPeriodTicks() { return 20*updateCheckerPeriod;}
public static boolean isLimitChests() { return limitChests; }
public static int getLimitChestsAmount() { return limitChestsAmount; }
public static boolean isShouldAnimateAllChests() {
return shouldAnimateAllChests;
}
public static boolean isRunHoppersInUnloadedChunks() {
return runHoppersInUnloadedChunks;
}
public static boolean isShouldDisplayChestLinkStand() {
return shouldDisplayChestLinkStand;
}
public static boolean isShouldDisplayAutoCraftStand() {
return shouldDisplayAutoCraftStand;
}
public static boolean isFilterItemFrameInvisible() {
return filterItemFrameInvisible;
}
public static List<String> getWorldBlacklist(){ return worldBlacklist; }
public static String getLangFileName(){ return langFileName; }
public static boolean isBlacklistedWorld(World world){
return worldBlacklist.contains(world.getName());
}
}

View File

@ -1,5 +1,6 @@
package com.jamesdpeters.minecraft.chests.misc;
import com.jamesdpeters.minecraft.chests.serialize.PluginConfig;
import com.jamesdpeters.minecraft.chests.serialize.Config;
import org.bstats.bukkit.Metrics;
@ -17,8 +18,10 @@ public class Stats {
}));
metrics.addCustomChart(new Metrics.SimplePie("update_checker_setting", () -> {
if(Settings.isUpdateCheckEnabled()) return "enabled";
if(PluginConfig.IS_UPDATE_CHECKER_ENABLED.get()) return "enabled";
else return "disabled";
}));
metrics.addCustomChart(new Metrics.SimplePie("language-file", PluginConfig.LANG_FILE::get));
}
}

View File

@ -5,6 +5,7 @@ import com.jamesdpeters.minecraft.chests.api.ApiSpecific;
import com.jamesdpeters.minecraft.chests.filters.Filter;
import com.jamesdpeters.minecraft.chests.filters.HopperFilter;
import com.jamesdpeters.minecraft.chests.interfaces.VirtualInventoryHolder;
import com.jamesdpeters.minecraft.chests.serialize.PluginConfig;
import com.jamesdpeters.minecraft.chests.serialize.LocationInfo;
import com.jamesdpeters.minecraft.chests.storage.chestlink.ChestLinkStorage;
import org.bukkit.Bukkit;
@ -22,6 +23,7 @@ import org.bukkit.block.Hopper;
import org.bukkit.entity.Entity;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
@ -29,11 +31,16 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.util.Vector;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -42,7 +49,7 @@ public class Utils {
public static void openChestInventory(Player player, ChestLinkStorage storage, LocationInfo openedChestLocation){
player.openInventory(storage.getInventory());
//Check if all chests should perform open animation.
if(Settings.isShouldAnimateAllChests()) {
if(PluginConfig.SHOULD_ANIMATE_ALL_CHESTS.get()) {
storage.getLocations().forEach(locationInfo -> {
Location location = locationInfo.getLocation();
if (location != null) {
@ -107,7 +114,7 @@ public class Utils {
return null;
}
public static boolean moveToOtherInventory(Inventory from, int amount, Inventory to, List<Filter> filters){
public static boolean hopperMove(Inventory from, int amount, Inventory to, List<Filter> filters){
ItemStack removed = removeStackFromInventory(from,amount,filters);
if(removed != null) {
HashMap<Integer, ItemStack> leftOvers = to.addItem(removed);
@ -120,8 +127,8 @@ public class Utils {
return false;
}
public static boolean moveToOtherInventory(Inventory from, int amount, Inventory to) {
return moveToOtherInventory(from,amount,to,null);
public static boolean hopperMove(Inventory from, int amount, Inventory to) {
return hopperMove(from,amount,to,null);
}
public static ItemStack getNamedItem(ItemStack item, String name){
@ -245,7 +252,7 @@ public class Utils {
entityStream.filter(entity ->
(entity instanceof ItemFrame
&& entity.getLocation().getBlock().getRelative(((ItemFrame) entity).getAttachedFace()).getState() instanceof Hopper))
.forEach(entity -> ApiSpecific.getNmsProvider().setItemFrameVisible((ItemFrame) entity, !Settings.isFilterItemFrameInvisible()));
.forEach(entity -> ApiSpecific.getNmsProvider().setItemFrameVisible((ItemFrame) entity, !PluginConfig.INVISIBLE_FILTER_ITEM_FRAMES.get()));
}
public static List<String> filterList(List<String> list, String phrase){
@ -284,7 +291,26 @@ public class Utils {
return (delta.getX() <= Bukkit.getViewDistance()*16) && (delta.getZ() <= Bukkit.getViewDistance()*16);
}
public static boolean isBlacklistedWorld(World world){
return PluginConfig.WORLD_BLACKLIST.get().contains(world.getName());
}
public static void copyFromResources(File jarFile, String directory){
try (JarFile jar = new JarFile(jarFile)) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if (!name.startsWith(directory + "/") || entry.isDirectory()) {
continue;
}
ChestsPlusPlus.PLUGIN.saveResource(name, true);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -2,14 +2,14 @@ package com.jamesdpeters.minecraft.chests.runnables;
import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
import com.jamesdpeters.minecraft.chests.filters.HopperFilter;
import com.jamesdpeters.minecraft.chests.misc.Settings;
import com.jamesdpeters.minecraft.chests.serialize.PluginConfig;
import com.jamesdpeters.minecraft.chests.misc.Utils;
import com.jamesdpeters.minecraft.chests.serialize.LocationInfo;
import com.jamesdpeters.minecraft.chests.serialize.SpigotConfig;
import com.jamesdpeters.minecraft.chests.storage.chestlink.ChestLinkStorage;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Hopper;
import org.bukkit.inventory.Inventory;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
@ -35,15 +35,14 @@ public class VirtualChestToHopper extends BukkitRunnable {
for(LocationInfo location : storage.getLocations()) {
if(location != null) {
if (location.getLocation() != null) {
if(!Settings.isRunHoppersInUnloadedChunks() && !Utils.isLocationChunkLoaded(location.getLocation())) continue;
if(!PluginConfig.SHOULD_RUN_HOPPERS_UNLOADED_CHUNKS.get() && !Utils.isLocationChunkLoaded(location.getLocation())) continue;
Location below = location.getLocation().clone().subtract(0, 1, 0);
if (below.getBlock().getState() instanceof Hopper) {
Hopper hopper = (Hopper) below.getBlock().getState();
if (below.getBlock().isBlockIndirectlyPowered() || below.getBlock().isBlockPowered()) {
continue;
}
int hopperAmount = SpigotConfig.getWorldSettings(location.getLocation().getWorld()).getHopperAmount();
if(Utils.moveToOtherInventory(storage.getInventory(), hopperAmount, hopper.getInventory(), HopperFilter.getHopperFilters(below.getBlock()))){
if (move(hopper.getLocation(), hopper.getInventory(), storage.getInventory())){
storage.updateDisplayItem();
}
if(storage.getInventory().getViewers().size() > 0) storage.sort();
@ -52,4 +51,9 @@ public class VirtualChestToHopper extends BukkitRunnable {
}
}
}
public static boolean move(Location targetLocation, Inventory source, Inventory target){
int hopperAmount = SpigotConfig.getWorldSettings(targetLocation.getWorld()).getHopperAmount();
return Utils.hopperMove(source, hopperAmount, target, HopperFilter.getHopperFilters(targetLocation.getBlock()));
}
}

View File

@ -0,0 +1,181 @@
package com.jamesdpeters.minecraft.chests.serialize;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
/**
* This class serves as a basis for creating a simple and easy to use Config using the Spigot/Bukkit api.
* Before any values are accessed the config for a @{@link Plugin} must be loaded using the {@link #load(Plugin)} method.
* To add values to the config they can either be add in this config in the same way the default values are.
* Or added in a separate class, any initialisation of a @{@link Value} or @{@link ListValue} automatically gets added
* to the config.
*
* @Author James Peters - JamesPeters98
*/
public class PluginConfig {
private static FileConfiguration configuration;
public static HashMap<String, List<AbstractValue<?>>> values = new HashMap<>();
/* ***************
* Config Values
* ***************/
public final static Value<Boolean> IS_UPDATE_CHECKER_ENABLED = new Value<>("update-checker", Boolean.class, true);
public final static Value<Integer> UPDATE_CHECKER_PERIOD = new Value<>("update-checker-period", Integer.class, 60*60);
public final static Value<Boolean> SHOULD_LIMIT_CHESTS = new Value<>("limit-chests", Boolean.class, false);
public final static Value<Integer> LIMIT_CHESTS_AMOUNT = new Value<>("limit-chestlinks-amount", Integer.class, 0);
public final static Value<Boolean> SHOULD_ANIMATE_ALL_CHESTS = new Value<>("should-animate-all-chests", Boolean.class, true);
public final static Value<Boolean> SHOULD_RUN_HOPPERS_UNLOADED_CHUNKS = new Value<>("run-hoppers-unloaded-chunks", Boolean.class, false);
public final static Value<Boolean> DISPLAY_CHESTLINK_ARMOUR_STAND = new Value<>("display_chestlink_armour_stand", Boolean.class, true);
public final static Value<Boolean> DISPLAY_AUTOCRAFT_ARMOUR_STAND = new Value<>("display_autocraft_armour_stands", Boolean.class, true);
public final static Value<Boolean> INVISIBLE_FILTER_ITEM_FRAMES = new Value<>("set-filter-itemframe-invisible", Boolean.class, false);
public final static ListValue<String> WORLD_BLACKLIST = new ListValue<>("world-blacklist", String.class, Collections.singletonList(""));
public final static Value<String> LANG_FILE = new Value<>("language-file", String.class, "default");
/**
* Loads this @{@link Plugin}'s Config and adds default values if they don't exist.
* @param plugin - the plugin of the @{@link org.bukkit.configuration.Configuration} to be loaded.
*/
public static void load(Plugin plugin){
//Read in config
configuration = plugin.getConfig();
//Add default values
values.forEach((configSection, values) -> {
if(configSection != null){
ConfigurationSection section = Optional.ofNullable(configuration.getConfigurationSection(configSection)).orElse(configuration.createSection(configSection));
values.forEach(value -> section.addDefault(value.getPath(), value.getDefaultValue()));
} else {
values.forEach((value) -> configuration.addDefault(value.getPath(), value.getDefaultValue()));
}
});
configuration.options().copyDefaults(true);
//Save config and default values
plugin.saveConfig();
//Read config in and set values
configuration = plugin.getConfig();
values.forEach((configSection, values) -> {
if(configSection != null){
ConfigurationSection section = Optional.ofNullable(configuration.getConfigurationSection(configSection)).orElse(configuration.createSection(configSection));
values.forEach(value -> value.setValue(section.get(value.getPath())));
} else {
values.forEach(value -> value.setValue(configuration.get(value.getPath())));
}
});
}
private abstract static class AbstractValue<T> {
protected final String path, configurationSection;
protected final T defaultValue;
protected T value;
AbstractValue(String configurationSection, String path, T defaultValue){
this.path = path;
this.value = defaultValue;
this.defaultValue = defaultValue;
this.configurationSection = configurationSection;
List<AbstractValue<?>> valueList = values.computeIfAbsent(configurationSection, k -> new ArrayList<>());
if(valueList.stream().anyMatch(value -> value.getPath().equals(path))){
throw new IllegalArgumentException("Two values with the same path have been added to the Config: '"+path+"'");
}
valueList.add(this);
}
public T get(){
return value != null ? value : defaultValue;
}
public void set(T value){
this.value = value;
}
public T getDefaultValue() {
return defaultValue;
}
public String getPath() {
return path;
}
public abstract void setValue(Object object);
}
/**
* A class that represents a singular value in a @{@link org.bukkit.configuration.Configuration}
* @param <T> the type of the variable to be stored/loaded.
*/
public static class Value<T> extends AbstractValue<T> {
protected final Class<T> type;
/**
* @param configSection the @{@link ConfigurationSection} this value belongs to.
* @param path the key that represents this value.
* @param type the @{@link Class} that represents this value
* @param defaultValue the default value to be inserted into the @{@link org.bukkit.configuration.Configuration}
*/
public Value(String configSection, String path, Class<T> type, T defaultValue){
super(configSection, path, defaultValue);
this.type = type;
}
public Value(String path, Class<T> type, T defaultValue){
this(null, path, type, defaultValue);
}
public void setValue(Object value){
this.value = type.isInstance(value) ? type.cast(value) : defaultValue;
}
}
/**
* A class that represents a list of values in a @{@link org.bukkit.configuration.Configuration}
* @param <T> the type of the variable to be stored/loaded.
*/
public static class ListValue<T> extends AbstractValue<List<T>> {
protected final Class<T> type;
/**
* @param configSection the @{@link ConfigurationSection} this value belongs to.
* @param path the key that represents this value.
* @param type the @{@link Class} that represents a value in the list
* @param defaultValue the default value to be inserted into the @{@link org.bukkit.configuration.Configuration}
*/
public ListValue(String configSection, String path, Class<T> type, List<T> defaultValue){
super(configSection, path, defaultValue);
this.type = type;
}
/**
* @param path the key that represents this value.
* @param type the @{@link Class} that represents a value in the list
* @param defaultValue the default value to be inserted into the @{@link org.bukkit.configuration.Configuration}
*/
public ListValue(String path, Class<T> type, List<T> defaultValue){
this(null, path, type, defaultValue);
}
public void setValue(Object value){
List<?> list = (value instanceof List) ? (List<?>) value : defaultValue;
this.value = new ArrayList<T>();
for (Object o : list) {
if(type.isInstance(o)){
this.value.add(type.cast(o));
}
}
}
}
}

View File

@ -3,14 +3,13 @@ package com.jamesdpeters.minecraft.chests.storage.abstracts;
import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
import com.jamesdpeters.minecraft.chests.misc.Messages;
import com.jamesdpeters.minecraft.chests.misc.Settings;
import com.jamesdpeters.minecraft.chests.serialize.PluginConfig;
import com.jamesdpeters.minecraft.chests.misc.Utils;
import com.jamesdpeters.minecraft.chests.misc.Values;
import com.jamesdpeters.minecraft.chests.serialize.Config;
import com.jamesdpeters.minecraft.chests.serialize.ConfigStorage;
import com.jamesdpeters.minecraft.chests.serialize.LocationInfo;
import com.jamesdpeters.minecraft.chests.storage.StorageUtils;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.GameMode;
import org.bukkit.Location;
@ -207,8 +206,8 @@ public abstract class StorageType<T extends AbstractStorage> {
}
public boolean isAtLimit(OfflinePlayer player) {
if (Settings.isLimitChests()) {
return getStorageMap(player.getUniqueId()).size() >= Settings.getLimitChestsAmount();
if (PluginConfig.SHOULD_LIMIT_CHESTS.get()) {
return getStorageMap(player.getUniqueId()).size() >= PluginConfig.LIMIT_CHESTS_AMOUNT.get();
}
return false;
}

View File

@ -1,7 +1,7 @@
package com.jamesdpeters.minecraft.chests.storage.autocraft;
import com.jamesdpeters.minecraft.chests.interfaces.VirtualCraftingHolder;
import com.jamesdpeters.minecraft.chests.misc.Settings;
import com.jamesdpeters.minecraft.chests.serialize.PluginConfig;
import com.jamesdpeters.minecraft.chests.serialize.Config;
import com.jamesdpeters.minecraft.chests.serialize.RecipeSerializable;
import com.jamesdpeters.minecraft.chests.storage.abstracts.AbstractStorage;
@ -12,7 +12,6 @@ import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.SerializableAs;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe;
@ -74,7 +73,7 @@ public class AutoCraftingStorage extends AbstractStorage implements Configuratio
@Override
public boolean shouldDisplayArmourStands() {
return Settings.isShouldDisplayAutoCraftStand();
return PluginConfig.DISPLAY_AUTOCRAFT_ARMOUR_STAND.get();
}
@Override

View File

@ -3,7 +3,6 @@ package com.jamesdpeters.minecraft.chests.storage.autocraft;
import com.jamesdpeters.minecraft.chests.lang.Message;
import com.jamesdpeters.minecraft.chests.misc.Messages;
import com.jamesdpeters.minecraft.chests.misc.Permissions;
import com.jamesdpeters.minecraft.chests.misc.Settings;
import com.jamesdpeters.minecraft.chests.misc.Utils;
import com.jamesdpeters.minecraft.chests.misc.Values;
import com.jamesdpeters.minecraft.chests.serialize.Config;
@ -59,7 +58,7 @@ public class AutoCraftingStorageType extends StorageType<AutoCraftingStorage> {
@Override
public boolean hasPermissionToAdd(Player player) {
return player.hasPermission(Permissions.AUTOCRAFT_ADD) && !Settings.isBlacklistedWorld(player.getWorld());
return player.hasPermission(Permissions.AUTOCRAFT_ADD) && !Utils.isBlacklistedWorld(player.getWorld());
}
@Override

View File

@ -4,7 +4,7 @@ import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
import com.jamesdpeters.minecraft.chests.interfaces.VirtualInventoryHolder;
import com.jamesdpeters.minecraft.chests.inventories.ChestLinkMenu;
import com.jamesdpeters.minecraft.chests.misc.Messages;
import com.jamesdpeters.minecraft.chests.misc.Settings;
import com.jamesdpeters.minecraft.chests.serialize.PluginConfig;
import com.jamesdpeters.minecraft.chests.misc.Utils;
import com.jamesdpeters.minecraft.chests.runnables.VirtualChestToHopper;
import com.jamesdpeters.minecraft.chests.serialize.Config;
@ -17,14 +17,12 @@ import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Barrel;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.block.Container;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.SerializableAs;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
@ -193,7 +191,7 @@ public class ChestLinkStorage extends AbstractStorage implements ConfigurationSe
@Override
public boolean shouldDisplayArmourStands() {
return Settings.isShouldDisplayChestLinkStand();
return PluginConfig.DISPLAY_CHESTLINK_ARMOUR_STAND.get();
}
@Override

View File

@ -2,7 +2,7 @@ package com.jamesdpeters.minecraft.chests.storage.chestlink;
import com.jamesdpeters.minecraft.chests.lang.Message;
import com.jamesdpeters.minecraft.chests.misc.Permissions;
import com.jamesdpeters.minecraft.chests.misc.Settings;
import com.jamesdpeters.minecraft.chests.misc.Utils;
import com.jamesdpeters.minecraft.chests.misc.Values;
import com.jamesdpeters.minecraft.chests.runnables.ChestLinkVerifier;
import com.jamesdpeters.minecraft.chests.serialize.Config;
@ -54,7 +54,7 @@ public class ChestLinkStorageType extends StorageType<ChestLinkStorage> {
@Override
public boolean hasPermissionToAdd(Player player) {
return player.hasPermission(Permissions.ADD) && !Settings.isBlacklistedWorld(player.getWorld());
return player.hasPermission(Permissions.ADD) && !Utils.isBlacklistedWorld(player.getWorld());
}
@Override