Converted settings.

This commit is contained in:
Brianna 2019-09-11 11:05:58 -04:00
parent 3df31ef786
commit 1b8a1b5acf

View File

@ -1,212 +1,85 @@
package com.songoda.epicfarming.settings;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.configuration.Config;
import com.songoda.core.configuration.ConfigSetting;
import com.songoda.core.hooks.EconomyManager;
import com.songoda.epicfarming.EpicFarming;
import com.songoda.epicfarming.utils.Methods;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* Created by songo on 6/4/2017.
*/
public class Settings implements Listener {
public class Settings {
private String pluginName = "EpicFarming";
static final Config config = EpicFarming.getInstance().getConfig().getCoreConfig();
private static final Pattern SETTINGS_PATTERN = Pattern.compile("(.{1,28}(?:\\s|$))|(.{0,28})", Pattern.DOTALL);
public static final ConfigSetting UPGRADE_WITH_ECONOMY = new ConfigSetting(config, "Main.Upgrade With Economy", true,
"Should you be able to upgrade farmses with economy?");
private Map<Player, String> cat = new HashMap<>();
public static final ConfigSetting UPGRADE_WITH_XP = new ConfigSetting(config, "Main.Upgrade With XP", true,
"Should you be able to upgrade farms with experience?");
private final EpicFarming instance;
public static final ConfigSetting PARTICLE_TYPE = new ConfigSetting(config, "Main.Upgrade Particle Type", "SPELL_WITCH",
"The type of particle shown when a furnace is upgraded.");
public Settings(EpicFarming plugin) {
this.instance = plugin;
Bukkit.getPluginManager().registerEvents(this, plugin);
}
public static final ConfigSetting FARM_TICK_SPEED = new ConfigSetting(config, "Main.Farm Tick Speed", 70,
"The delay in ticks between each farm growth event.");
private Map<Player, String> current = new HashMap<>();
public static final ConfigSetting ENTITY_TICK_SPEED = new ConfigSetting(config, "Main.Entity Tick Speed", 100,
"The delay in ticks between each farm entity event.");
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
ItemStack clickedItem = event.getCurrentItem();
public static final ConfigSetting GROWTH_TICK_SPEED = new ConfigSetting(config, "Main.Growth Tick Speed", 20,
"The delay in ticks between each farm entity event.");
if (event.getInventory() != event.getWhoClicked().getOpenInventory().getTopInventory()
|| clickedItem == null || !clickedItem.hasItemMeta()
|| !clickedItem.getItemMeta().hasDisplayName()) {
return;
}
if (event.getView().getTitle().equals(pluginName + " Settings Manager")) {
event.setCancelled(true);
if (clickedItem.getType().name().contains("STAINED_GLASS")) return;
String type = ChatColor.stripColor(clickedItem.getItemMeta().getDisplayName());
this.cat.put((Player) event.getWhoClicked(), type);
this.openEditor((Player) event.getWhoClicked());
} else if (event.getView().getTitle().equals(pluginName + " Settings Editor")) {
event.setCancelled(true);
if (clickedItem.getType().name().contains("STAINED_GLASS")) return;
Player player = (Player) event.getWhoClicked();
String key = cat.get(player) + "." + ChatColor.stripColor(clickedItem.getItemMeta().getDisplayName());
if (instance.getConfig().get(key).getClass().getName().equals("java.lang.Boolean")) {
this.instance.getConfig().set(key, !instance.getConfig().getBoolean(key));
this.finishEditing(player);
} else {
this.editObject(player, key);
}
}
}
@EventHandler
public void onChat(AsyncPlayerChatEvent event) {
Player player = event.getPlayer();
if (!current.containsKey(player)) return;
String value = current.get(player);
FileConfiguration config = instance.getConfig();
if (config.isInt(value)) {
config.set(value, Integer.parseInt(event.getMessage()));
} else if (config.isDouble(value)) {
config.set(value, Double.parseDouble(event.getMessage()));
} else if (config.isString(value)) {
config.set(value, event.getMessage());
}
this.finishEditing(player);
event.setCancelled(true);
}
public void finishEditing(Player player) {
this.current.remove(player);
this.instance.saveConfig();
this.openEditor(player);
}
public void editObject(Player player, String current) {
this.current.put(player, ChatColor.stripColor(current));
player.closeInventory();
player.sendMessage("");
player.sendMessage(Methods.formatText("&7Please enter a value for &6" + current + "&7."));
if (instance.getConfig().isInt(current) || instance.getConfig().isDouble(current)) {
player.sendMessage(Methods.formatText("&cUse only numbers."));
}
player.sendMessage("");
}
public void openSettingsManager(Player player) {
Inventory inventory = Bukkit.createInventory(null, 27, pluginName + " Settings Manager");
ItemStack glass = Methods.getGlass();
for (int i = 0; i < inventory.getSize(); i++) {
inventory.setItem(i, glass);
}
int slot = 10;
for (String key : instance.getConfig().getDefaultSection().getKeys(false)) {
ItemStack item = new ItemStack(Material.WHITE_WOOL, 1, (byte) (slot - 9)); //ToDo: Make this function as it was meant to.
ItemMeta meta = item.getItemMeta();
meta.setLore(Collections.singletonList(Methods.formatText("&6Click To Edit This Category.")));
meta.setDisplayName(Methods.formatText("&f&l" + key));
item.setItemMeta(meta);
inventory.setItem(slot, item);
slot++;
}
player.openInventory(inventory);
}
public void openEditor(Player player) {
Inventory inventory = Bukkit.createInventory(null, 54, pluginName + " Settings Editor");
FileConfiguration config = instance.getConfig();
int slot = 0;
for (String key : config.getConfigurationSection(cat.get(player)).getKeys(true)) {
String fKey = cat.get(player) + "." + key;
ItemStack item = new ItemStack(Material.DIAMOND_HELMET);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(Methods.formatText("&6" + key));
List<String> lore = new ArrayList<>();
if (config.isBoolean(fKey)) {
item.setType(Material.LEVER);
lore.add(Methods.formatText(config.getBoolean(fKey) ? "&atrue" : "&cfalse"));
} else if (config.isString(fKey)) {
item.setType(Material.PAPER);
lore.add(Methods.formatText("&9" + config.getString(fKey)));
} else if (config.isInt(fKey)) {
item.setType(Material.CLOCK);
lore.add(Methods.formatText("&5" + config.getInt(fKey)));
}
meta.setLore(lore);
item.setItemMeta(meta);
inventory.setItem(slot, item);
slot++;
}
player.openInventory(inventory);
}
public void updateSettings() {
for (settings s : settings.values()) {
instance.getConfig().addDefault(s.setting, s.option);
}
}
public enum settings {
o1("Main.Upgrade With Economy", true),
o2("Main.Upgrade With XP", true),
o3("Main.Upgrade Particle Type", "SPELL_WITCH"),
o4("Main.Sounds Enabled", true),
o5("Main.Farm Tick Speed", 70),
o6("Main.Entity Tick Speed", 100),
o7("Main.Growth Tick Speed", 20),
o8("Main.Farm Block Material", "END_ROD"),
o9("Main.Allow Non Command Issued Farm Items", false),
o10("Main.Auto Breeding Cap", 15),
o11("Main.Animate", true),
o123("Main.Disable Auto Til Land", false),
d1("Database.Activate Mysql Support",false),
d6("Database.IP","127.0.0.1"),
d7("Database.Port",3306),
d2("Database.Username","root"),
d3("Database.Password","password"),
d4("Database.Database Name","EpicFarming"),
d5("Database.Prefix","EFA-"),
o12("Interfaces.Economy Icon", "SUNFLOWER"),
o13("Interfaces.XP Icon", "EXPERIENCE_BOTTLE"),
o14("Interfaces.Glass Type 1", 7),
o15("Interfaces.Glass Type 2", 11),
o16("Interfaces.Glass Type 3", 3),
LANGUGE_MODE("System.Language Mode", "en_US"),
o17("System.Debugger Enabled", false);
private String setting;
private Object option;
settings(String setting, Object option) {
this.setting = setting;
this.option = option;
public static final ConfigSetting FARM_BLOCK_MATERIAL = new ConfigSetting(config, "Main.Farm Block Material", "END_ROD",
"What material should be used as a farm item?");
public static final ConfigSetting NON_COMMAND_FARMS = new ConfigSetting(config, "Main.Allow Non Command Issued Farm Items", false,
"Should farm item materials found in the wild work as farms?");
public static final ConfigSetting AUTO_BREEDING_CAP = new ConfigSetting(config, "Main.Auto Breeding Cap", 15,
"How many entities should auto breeding stop at?");
public static final ConfigSetting ANIMATE = new ConfigSetting(config, "Main.Animate", true,
"Should the processed farm item be animated above the farm item?");
public static final ConfigSetting AUTO_TIL_LAND = new ConfigSetting(config, "Main.Disable Auto Til Land", false,
"Should farms auto til land around them?");
public static final ConfigSetting ECONOMY_PLUGIN = new ConfigSetting(config, "Main.Economy", EconomyManager.getEconomy() == null ? "Vault" : EconomyManager.getEconomy().getName(),
"Which economy plugin should be used?",
"Supported plugins you have installed: \"" + EconomyManager.getManager().getRegisteredPlugins().stream().collect(Collectors.joining("\", \"")) + "\".");
public static final ConfigSetting ECO_ICON = new ConfigSetting(config, "Interfaces.Economy Icon", "SUNFLOWER");
public static final ConfigSetting XP_ICON = new ConfigSetting(config, "Interfaces.XP Icon", "EXPERIENCE_BOTTLE");
public static final ConfigSetting GLASS_TYPE_1 = new ConfigSetting(config, "Interfaces.Glass Type 1", "GRAY_STAINED_GLASS_PANE");
public static final ConfigSetting GLASS_TYPE_2 = new ConfigSetting(config, "Interfaces.Glass Type 2", "BLUE_STAINED_GLASS_PANE");
public static final ConfigSetting GLASS_TYPE_3 = new ConfigSetting(config, "Interfaces.Glass Type 3", "LIGHT_BLUE_STAINED_GLASS_PANE");
public static final ConfigSetting LANGUGE_MODE = new ConfigSetting(config, "System.Language Mode", "en_US",
"The enabled language file.",
"More language files (if available) can be found in the plugins data folder.");
/**
* In order to set dynamic economy comment correctly, this needs to be
* called after EconomyManager load
*/
public static void setupConfig() {
config.load();
config.setAutoremove(true).setAutosave(true);
// convert glass pane settings
int color;
if ((color = GLASS_TYPE_1.getInt(-1)) != -1) {
config.set(GLASS_TYPE_1.getKey(), CompatibleMaterial.getGlassPaneColor(color).name());
}
if ((color = GLASS_TYPE_2.getInt(-1)) != -1) {
config.set(GLASS_TYPE_2.getKey(), CompatibleMaterial.getGlassPaneColor(color).name());
}
if ((color = GLASS_TYPE_3.getInt(-1)) != -1) {
config.set(GLASS_TYPE_3.getKey(), CompatibleMaterial.getGlassPaneColor(color).name());
}
config.saveChanges();
}
}