diff --git a/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/Configuration/Config.java b/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/Configuration/Config.java index f1cb18e7c..3d507939a 100644 --- a/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/Configuration/Config.java +++ b/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/Configuration/Config.java @@ -2,22 +2,10 @@ package me.mrCookieSlime.CSCoreLibPlugin.Configuration; import java.io.File; import java.io.IOException; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; import java.util.Set; -import java.util.UUID; -import org.bukkit.Bukkit; -import org.bukkit.Chunk; -import org.bukkit.Location; -import org.bukkit.Sound; -import org.bukkit.World; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; -import org.bukkit.plugin.Plugin; /** * An old remnant of CS-CoreLib. @@ -30,18 +18,6 @@ public class Config { private final File file; private FileConfiguration config; - /** - * Creates a new Config Object for the config.yml File of - * the specified Plugin - * - * @param plugin - * The Instance of the Plugin, the config.yml is referring to - */ - public Config(Plugin plugin) { - this.file = new File("plugins/" + plugin.getDescription().getName().replace(" ", "_") + "/config.yml"); - this.config = YamlConfiguration.loadConfiguration(this.file); - } - /** * Creates a new Config Object for the specified File * @@ -49,8 +25,7 @@ public class Config { * The File for which the Config object is created for */ public Config(File file) { - this.file = file; - this.config = YamlConfiguration.loadConfiguration(this.file); + this(file, YamlConfiguration.loadConfiguration(file)); } /** @@ -96,10 +71,6 @@ public class Config { return this.config; } - protected void store(String path, Object value) { - this.config.set(path, value); - } - /** * Sets the Value for the specified Path * @@ -109,38 +80,7 @@ public class Config { * The Value for that Path */ public void setValue(String path, Object value) { - if (value == null) { - this.store(path, value); - this.store(path + "_extra", null); - } else if (value instanceof Inventory) { - for (int i = 0; i < ((Inventory) value).getSize(); i++) { - setValue(path + "." + i, ((Inventory) value).getItem(i)); - } - } else if (value instanceof Date) { - this.store(path, String.valueOf(((Date) value).getTime())); - } else if (value instanceof Long) { - this.store(path, String.valueOf(value)); - } else if (value instanceof UUID) { - this.store(path, value.toString()); - } else if (value instanceof Sound) { - this.store(path, String.valueOf(value)); - } else if (value instanceof ItemStack) { - this.store(path, new ItemStack((ItemStack) value)); - } else if (value instanceof Location) { - setValue(path + ".x", ((Location) value).getX()); - setValue(path + ".y", ((Location) value).getY()); - setValue(path + ".z", ((Location) value).getZ()); - setValue(path + ".pitch", ((Location) value).getPitch()); - setValue(path + ".yaw", ((Location) value).getYaw()); - setValue(path + ".world", ((Location) value).getWorld().getName()); - } else if (value instanceof Chunk) { - setValue(path + ".x", ((Chunk) value).getX()); - setValue(path + ".z", ((Chunk) value).getZ()); - setValue(path + ".world", ((Chunk) value).getWorld().getName()); - } else if (value instanceof World) { - this.store(path, ((World) value).getName()); - } else - this.store(path, value); + this.config.set(path, value); } /** @@ -174,8 +114,9 @@ public class Config { * The Value for that Path */ public void setDefaultValue(String path, Object value) { - if (!contains(path)) + if (!contains(path)) { setValue(path, value); + } } /** @@ -200,17 +141,6 @@ public class Config { return config.get(path); } - /** - * Returns the ItemStack at the specified Path - * - * @param path - * The path in the Config File - * @return The ItemStack at that Path - */ - public ItemStack getItem(String path) { - return config.getItemStack(path); - } - /** * Returns the String at the specified Path * @@ -222,66 +152,6 @@ public class Config { return config.getString(path); } - /** - * Returns the Integer at the specified Path - * - * @param path - * The path in the Config File - * @return The Integer at that Path - */ - public int getInt(String path) { - return config.getInt(path); - } - - /** - * Returns the Boolean at the specified Path - * - * @param path - * The path in the Config File - * @return The Boolean at that Path - */ - public boolean getBoolean(String path) { - return config.getBoolean(path); - } - - /** - * Returns the StringList at the specified Path - * - * @param path - * The path in the Config File - * @return The StringList at that Path - */ - public List getStringList(String path) { - return config.getStringList(path); - } - - /** - * Returns the ItemList at the specified Path - * - * @param path - * The path in the Config File - * @return The ItemList at that Path - */ - public List getItemList(String path) { - List list = new ArrayList(); - for (String key : getKeys(path)) { - if (!key.endsWith("_extra")) - list.add(getItem(path + "." + key)); - } - return list; - } - - /** - * Returns the IntegerList at the specified Path - * - * @param path - * The path in the Config File - * @return The IntegerList at that Path - */ - public List getIntList(String path) { - return config.getIntegerList(path); - } - /** * Recreates the File of this Config */ @@ -291,143 +161,6 @@ public class Config { } catch (IOException e) {} } - /** - * Returns the Float at the specified Path - * - * @param path - * The path in the Config File - * @return The Float at that Path - */ - public Float getFloat(String path) { - return Float.valueOf(String.valueOf(getValue(path))); - } - - /** - * Returns the Long at the specified Path - * - * @param path - * The path in the Config File - * @return The Long at that Path - */ - public Long getLong(String path) { - return Long.valueOf(String.valueOf(getValue(path))); - } - - /** - * Returns the Sound at the specified Path - * - * @param path - * The path in the Config File - * @return The Sound at that Path - */ - public Sound getSound(String path) { - return Sound.valueOf(getString(path)); - } - - /** - * Returns the Date at the specified Path - * - * @param path - * The path in the Config File - * @return The Date at that Path - */ - public Date getDate(String path) { - return new Date(getLong(path)); - } - - /** - * Returns the Chunk at the specified Path - * - * @param path - * The path in the Config File - * @return The Chunk at that Path - */ - public Chunk getChunk(String path) { - return Bukkit.getWorld(getString(path + ".world")).getChunkAt(getInt(path + ".x"), getInt(path + ".z")); - } - - /** - * Returns the UUID at the specified Path - * - * @param path - * The path in the Config File - * @return The UUID at that Path - */ - public UUID getUUID(String path) { - return UUID.fromString(getString(path)); - } - - /** - * Returns the World at the specified Path - * - * @param path - * The path in the Config File - * @return The World at that Path - */ - public World getWorld(String path) { - return Bukkit.getWorld(getString(path)); - } - - /** - * Returns the Double at the specified Path - * - * @param path - * The path in the Config File - * @return The Double at that Path - */ - public Double getDouble(String path) { - return config.getDouble(path); - } - - /** - * Returns the Location at the specified Path - * - * @param path - * The path in the Config File - * @return The Location at that Path - */ - public Location getLocation(String path) { - if (this.contains(path + ".pitch")) { - return new Location(Bukkit.getWorld(getString(path + ".world")), getDouble(path + ".x"), getDouble(path + ".y"), getDouble(path + ".z"), getFloat(path + ".yaw"), getFloat(path + ".pitch")); - } else { - return new Location(Bukkit.getWorld(this.getString(path + ".world")), this.getDouble(path + ".x"), this.getDouble(path + ".y"), this.getDouble(path + ".z")); - } - } - - @Deprecated - public void setLocation(String path, Location location) { - setValue(path + ".x", location.getX()); - setValue(path + ".y", location.getY()); - setValue(path + ".z", location.getZ()); - setValue(path + ".world", location.getWorld().getName()); - } - - @Deprecated - public void setInventory(String path, Inventory inventory) { - for (int i = 0; i < inventory.getSize(); i++) { - setValue(path + "." + i, inventory.getItem(i)); - } - } - - /** - * Gets the Contents of an Inventory at the specified Path - * - * @param path - * The path in the Config File - * @param size - * The Size of the Inventory - * @param title - * The Title of the Inventory - * @return The generated Inventory - */ - public Inventory getInventory(String path, int size, String title) { - Inventory inventory = Bukkit.createInventory(null, size, title); - for (int i = 0; i < size; i++) { - inventory.setItem(i, getItem(path + "." + i)); - } - return inventory; - } - /** * Returns all Paths in this Config * diff --git a/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/general/Inventory/MenuListener.java b/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/general/Inventory/MenuListener.java index 64aba2285..3483e5a6e 100644 --- a/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/general/Inventory/MenuListener.java +++ b/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/general/Inventory/MenuListener.java @@ -33,11 +33,10 @@ public class MenuListener implements Listener { @EventHandler public void onClose(InventoryCloseEvent e) { - ChestMenu menu = menus.get(e.getPlayer().getUniqueId()); + ChestMenu menu = menus.remove(e.getPlayer().getUniqueId()); if (menu != null) { menu.getMenuCloseHandler().onClose((Player) e.getPlayer()); - menus.remove(e.getPlayer().getUniqueId()); } } diff --git a/src/main/java/me/mrCookieSlime/Slimefun/api/BlockInfoConfig.java b/src/main/java/me/mrCookieSlime/Slimefun/api/BlockInfoConfig.java index e34351800..c2f5a821e 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/api/BlockInfoConfig.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/api/BlockInfoConfig.java @@ -2,10 +2,11 @@ package me.mrCookieSlime.Slimefun.api; import java.io.File; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.Set; +import javax.annotation.Nonnull; + import org.bukkit.Location; import org.bukkit.configuration.file.FileConfiguration; @@ -39,12 +40,13 @@ public class BlockInfoConfig extends Config { this.data = data; } + @Nonnull public Map getMap() { return data; } @Override - protected void store(String path, Object value) { + public void setValue(String path, Object value) { if (value != null && !(value instanceof String)) { throw new UnsupportedOperationException("Can't set \"" + path + "\" to \"" + value + "\" (type: " + value.getClass().getSimpleName() + ") because BlockInfoConfig only supports Strings"); } @@ -76,70 +78,42 @@ public class BlockInfoConfig extends Config { return data.keySet(); } - @Override - public int getInt(String path) { - throw invalidType(path); - } - - @Override - public boolean getBoolean(String path) { - throw invalidType(path); - } - - @Override - public List getStringList(String path) { - throw invalidType(path); - } - - @Override - public List getIntList(String path) { - throw invalidType(path); - } - - @Override - public Double getDouble(String path) { - throw invalidType(path); - } - @Override public Set getKeys(String path) { - throw invalidType(path); - } - - private UnsupportedOperationException invalidType(String path) { - return new UnsupportedOperationException("Can't get \"" + path + "\" because BlockInfoConfig only supports String values"); + throw new UnsupportedOperationException("Cannot get keys for BlockInfoConfig"); } @Override public File getFile() { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException("BlockInfoConfigs do not have a File"); } @Override public FileConfiguration getConfiguration() { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException("BlockInfoConfigs do not have a FileConfiguration"); } @Override public void save() { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException("BlockInfoConfigs cannot be saved to a File"); } @Override public void save(File file) { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException("BlockInfoConfigs cannot be saved to a File"); } @Override public void createFile() { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException("BlockInfoConfigs cannot be created from a File"); } @Override public void reload() { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException("BlockInfoConfigs cannot be reloaded"); } + @Nonnull public String toJSON() { return new GsonBuilder().create().toJson(data); } diff --git a/src/main/java/me/mrCookieSlime/Slimefun/api/EmptyBlockData.java b/src/main/java/me/mrCookieSlime/Slimefun/api/EmptyBlockData.java index c1cae795b..9e95c9d6e 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/api/EmptyBlockData.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/api/EmptyBlockData.java @@ -17,7 +17,7 @@ class EmptyBlockData extends BlockInfoConfig { } @Override - protected void store(String path, Object value) { + public void setValue(String path, Object value) { throw new UnsupportedOperationException("Cannot store values (" + path + ':' + value + " on a read-only data object!"); }