mirror of
https://github.com/songoda/UltimateModeration.git
synced 2024-11-22 18:26:10 +01:00
Overhauled SettingsManager
This commit is contained in:
parent
d4c5f9f2be
commit
dfefe12a07
@ -4,7 +4,7 @@ stages:
|
||||
variables:
|
||||
name: "UltimateModeration"
|
||||
path: "/builds/$CI_PROJECT_PATH"
|
||||
version: "1.1.3"
|
||||
version: "1.1.4"
|
||||
|
||||
build:
|
||||
stage: build
|
||||
|
@ -22,8 +22,9 @@ import com.songoda.ultimatemoderation.tickets.TicketStatus;
|
||||
import com.songoda.ultimatemoderation.utils.Methods;
|
||||
import com.songoda.ultimatemoderation.utils.Metrics;
|
||||
import com.songoda.ultimatemoderation.utils.ServerVersion;
|
||||
import com.songoda.ultimatemoderation.utils.SettingsManager;
|
||||
import com.songoda.ultimatemoderation.utils.gui.AbstractGUI;
|
||||
import com.songoda.ultimatemoderation.utils.settings.Setting;
|
||||
import com.songoda.ultimatemoderation.utils.settings.SettingsManager;
|
||||
import com.songoda.ultimatemoderation.utils.updateModules.LocaleModule;
|
||||
import com.songoda.update.Plugin;
|
||||
import com.songoda.update.SongodaUpdate;
|
||||
@ -64,10 +65,10 @@ public class UltimateModeration extends JavaPlugin {
|
||||
console.sendMessage(Methods.formatText("&7Action: &aEnabling&7..."));
|
||||
|
||||
this.settingsManager = new SettingsManager(this);
|
||||
this.setupConfig();
|
||||
this.settingsManager.setupConfig();
|
||||
|
||||
// Setup language
|
||||
String langMode = SettingsManager.Setting.LANGUGE_MODE.getString();
|
||||
String langMode = Setting.LANGUGE_MODE.getString();
|
||||
Locale.init(this);
|
||||
Locale.saveDefaultLocale("en_US");
|
||||
this.locale = Locale.getLocale(getConfig().getString("System.Language Mode", langMode));
|
||||
@ -106,7 +107,7 @@ public class UltimateModeration extends JavaPlugin {
|
||||
// Starting Metrics
|
||||
new Metrics(this);
|
||||
|
||||
int timeout = SettingsManager.Setting.AUTOSAVE.getInt() * 60 * 20;
|
||||
int timeout = Setting.AUTOSAVE.getInt() * 60 * 20;
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> storage.doSave(), timeout, timeout);
|
||||
console.sendMessage(Methods.formatText("&a============================="));
|
||||
}
|
||||
@ -216,17 +217,10 @@ public class UltimateModeration extends JavaPlugin {
|
||||
return serverVersion.ordinal() >= version.ordinal();
|
||||
}
|
||||
|
||||
private void setupConfig() {
|
||||
settingsManager.updateSettings();
|
||||
this.getConfig().options().copyDefaults(true);
|
||||
this.saveConfig();
|
||||
}
|
||||
|
||||
public void reload() {
|
||||
locale.reloadMessages();
|
||||
references = new References();
|
||||
this.setupConfig();
|
||||
saveConfig();
|
||||
this.settingsManager.reloadConfig();
|
||||
}
|
||||
|
||||
public CommandManager getCommandManager() {
|
||||
|
@ -4,7 +4,7 @@ import com.songoda.ultimatemoderation.UltimateModeration;
|
||||
import com.songoda.ultimatemoderation.command.AbstractCommand;
|
||||
import com.songoda.ultimatemoderation.utils.Methods;
|
||||
import com.songoda.ultimatemoderation.utils.ServerVersion;
|
||||
import com.songoda.ultimatemoderation.utils.SettingsManager;
|
||||
import com.songoda.ultimatemoderation.utils.settings.Setting;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Particle;
|
||||
import org.bukkit.Sound;
|
||||
@ -51,10 +51,10 @@ public class CommandVanish extends AbstractCommand {
|
||||
player.setInvulnerable(true);
|
||||
player.sendMessage(Methods.formatText(instance.getReferences().getPrefix() + instance.getLocale().getMessage("command.vanish.toggledOn")));
|
||||
}
|
||||
if (SettingsManager.Setting.VANISH_EFFECTS.getBoolean()) {
|
||||
player.getWorld().playSound(player.getLocation(), Sound.valueOf(SettingsManager.Setting.VANISH_SOUND.getString()), 1L, 1L);
|
||||
if (Setting.VANISH_EFFECTS.getBoolean()) {
|
||||
player.getWorld().playSound(player.getLocation(), Sound.valueOf(Setting.VANISH_SOUND.getString()), 1L, 1L);
|
||||
|
||||
if (SettingsManager.Setting.VANISH_BATS.getBoolean()) {
|
||||
if (Setting.VANISH_BATS.getBoolean()) {
|
||||
List<Entity> entities = new ArrayList<>();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
entities.add(player.getWorld().spawnEntity(player.getLocation().add(0, 1, 0), EntityType.BAT));
|
||||
@ -70,7 +70,7 @@ public class CommandVanish extends AbstractCommand {
|
||||
float yy = (float) (0 + (Math.random() * 2));
|
||||
float zz = (float) (0 + (Math.random() * 1));
|
||||
if (instance.isServerVersionAtLeast(ServerVersion.V1_12))
|
||||
player.getWorld().spawnParticle(Particle.valueOf(SettingsManager.Setting.VANISH_PARTICLE.getString()), player.getLocation().add(0, 1, 0), 35, xx, yy, zz, 0);
|
||||
player.getWorld().spawnParticle(Particle.valueOf(Setting.VANISH_PARTICLE.getString()), player.getLocation().add(0, 1, 0), 35, xx, yy, zz, 0);
|
||||
}
|
||||
|
||||
for (Player p : Bukkit.getOnlinePlayers()) {
|
||||
|
@ -3,20 +3,14 @@ package com.songoda.ultimatemoderation.gui;
|
||||
import com.songoda.ultimatemoderation.UltimateModeration;
|
||||
import com.songoda.ultimatemoderation.tickets.Ticket;
|
||||
import com.songoda.ultimatemoderation.tickets.TicketResponse;
|
||||
import com.songoda.ultimatemoderation.tickets.TicketStatus;
|
||||
import com.songoda.ultimatemoderation.utils.AbstractChatConfirm;
|
||||
import com.songoda.ultimatemoderation.utils.SettingsManager;
|
||||
import com.songoda.ultimatemoderation.utils.gui.AbstractGUI;
|
||||
import org.bukkit.Bukkit;
|
||||
import com.songoda.ultimatemoderation.utils.settings.Setting;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class GUITicketType extends AbstractGUI {
|
||||
|
||||
@ -40,7 +34,7 @@ public class GUITicketType extends AbstractGUI {
|
||||
resetClickables();
|
||||
registerClickables();
|
||||
|
||||
List<String> types = SettingsManager.Setting.TICKET_TYPES.getStringList();
|
||||
List<String> types = Setting.TICKET_TYPES.getStringList();
|
||||
|
||||
for (int i = 0; i < types.size(); i ++) {
|
||||
createButton(i, Material.PAPER, types.get(i));
|
||||
|
@ -5,7 +5,7 @@ import com.songoda.ultimatemoderation.punish.AppliedPunishment;
|
||||
import com.songoda.ultimatemoderation.punish.PunishmentType;
|
||||
import com.songoda.ultimatemoderation.staffchat.StaffChannel;
|
||||
import com.songoda.ultimatemoderation.utils.Methods;
|
||||
import com.songoda.ultimatemoderation.utils.SettingsManager;
|
||||
import com.songoda.ultimatemoderation.utils.settings.Setting;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
@ -39,7 +39,7 @@ public class ChatListener implements Listener {
|
||||
public void onChat(AsyncPlayerChatEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
|
||||
long slowmode = slowModeOverride == 0 ? Methods.parseTime(SettingsManager.Setting.SLOW_MODE.getString()) : slowModeOverride;
|
||||
long slowmode = slowModeOverride == 0 ? Methods.parseTime(Setting.SLOW_MODE.getString()) : slowModeOverride;
|
||||
|
||||
if (!player.hasPermission("um.slowmode.bypass") && slowmode != 0) {
|
||||
List<Log> chats = chatLog.stream().filter(log -> log.player == player.getUniqueId()).collect(Collectors.toList());
|
||||
|
@ -4,7 +4,7 @@ import com.songoda.ultimatemoderation.UltimateModeration;
|
||||
import com.songoda.ultimatemoderation.command.commands.CommandCommandSpy;
|
||||
import com.songoda.ultimatemoderation.punish.AppliedPunishment;
|
||||
import com.songoda.ultimatemoderation.punish.PunishmentType;
|
||||
import com.songoda.ultimatemoderation.utils.SettingsManager;
|
||||
import com.songoda.ultimatemoderation.utils.settings.Setting;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@ -12,7 +12,6 @@ import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommandListener implements Listener {
|
||||
|
||||
@ -30,13 +29,13 @@ public class CommandListener implements Listener {
|
||||
|
||||
List<AppliedPunishment> appliedPunishments = instance.getPunishmentManager().getPlayer(player).getActivePunishments(PunishmentType.MUTE);
|
||||
if (!appliedPunishments.isEmpty()) {
|
||||
if (SettingsManager.Setting.MUTE_DISABLED_COMMANDS.getStringList().stream()
|
||||
if (Setting.MUTE_DISABLED_COMMANDS.getStringList().stream()
|
||||
.anyMatch(s -> command.toUpperCase().startsWith("/" + s.toUpperCase())))
|
||||
event.setCancelled(true);
|
||||
|
||||
}
|
||||
|
||||
List<String> blockedCommands = SettingsManager.Setting.BLOCKED_COMMANDS.getStringList();
|
||||
List<String> blockedCommands = Setting.BLOCKED_COMMANDS.getStringList();
|
||||
|
||||
for (String cmd : blockedCommands) {
|
||||
if (command.toUpperCase().startsWith("/" + cmd.toUpperCase())
|
||||
|
@ -2,20 +2,18 @@ package com.songoda.ultimatemoderation.staffchat;
|
||||
|
||||
import com.songoda.ultimatemoderation.UltimateModeration;
|
||||
import com.songoda.ultimatemoderation.utils.Methods;
|
||||
import com.songoda.ultimatemoderation.utils.SettingsManager;
|
||||
import com.songoda.ultimatemoderation.utils.settings.Setting;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class StaffChannel {
|
||||
|
||||
private final String channelName;
|
||||
private char chatChar = SettingsManager.Setting.STAFFCHAT_COLOR_CODE.getChar();
|
||||
private char chatChar = Setting.STAFFCHAT_COLOR_CODE.getChar();
|
||||
private final List<UUID> members = new ArrayList<>();
|
||||
private final List<String> chatLog = new ArrayList<>();
|
||||
|
||||
|
@ -4,7 +4,7 @@ import com.songoda.ultimatemoderation.UltimateModeration;
|
||||
import com.songoda.ultimatemoderation.listeners.ChatListener;
|
||||
import com.songoda.ultimatemoderation.utils.Methods;
|
||||
import com.songoda.ultimatemoderation.utils.ServerVersion;
|
||||
import com.songoda.ultimatemoderation.utils.SettingsManager;
|
||||
import com.songoda.ultimatemoderation.utils.settings.Setting;
|
||||
import net.md_5.bungee.api.ChatMessageType;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -34,7 +34,7 @@ public class SlowModeTask extends BukkitRunnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long slowmode = ChatListener.getSlowModeOverride() == 0 ? Methods.parseTime(SettingsManager.Setting.SLOW_MODE.getString()) : ChatListener.getSlowModeOverride();
|
||||
long slowmode = ChatListener.getSlowModeOverride() == 0 ? Methods.parseTime(Setting.SLOW_MODE.getString()) : ChatListener.getSlowModeOverride();
|
||||
|
||||
if (slowmode == 0) return;
|
||||
|
||||
|
@ -73,23 +73,36 @@ public class Methods {
|
||||
public static String makeReadable(Long time) {
|
||||
if (time == null)
|
||||
return "";
|
||||
return String.format("%dd %dh %dm",
|
||||
TimeUnit.MILLISECONDS.toDays(time),
|
||||
TimeUnit.MILLISECONDS.toHours(time) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(time)),
|
||||
TimeUnit.MILLISECONDS.toMinutes(time) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(time)));
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
long days = TimeUnit.MILLISECONDS.toDays(time);
|
||||
long hours = TimeUnit.MILLISECONDS.toHours(time) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(time));
|
||||
long minutes = TimeUnit.MILLISECONDS.toMinutes(time) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(time));
|
||||
long seconds = TimeUnit.MILLISECONDS.toSeconds(time) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(time));
|
||||
|
||||
if (days != 0L)
|
||||
sb.append(" ").append(days).append("d");
|
||||
if (hours != 0L)
|
||||
sb.append(" ").append(hours).append("h");
|
||||
if (minutes != 0L)
|
||||
sb.append(" ").append(minutes).append("m");
|
||||
if (seconds != 0L)
|
||||
sb.append(" ").append(seconds).append("s");
|
||||
return sb.toString().trim();
|
||||
}
|
||||
|
||||
|
||||
public static long parseTime(String input) {
|
||||
long result = 0;
|
||||
String number = "";
|
||||
StringBuilder number = new StringBuilder();
|
||||
for (int i = 0; i < input.length(); i++) {
|
||||
char c = input.charAt(i);
|
||||
if (Character.isDigit(c)) {
|
||||
number += c;
|
||||
} else if (Character.isLetter(c) && !number.isEmpty()) {
|
||||
result += convert(Integer.parseInt(number), c);
|
||||
number = "";
|
||||
number.append(c);
|
||||
} else if (Character.isLetter(c) && (number.length() > 0)) {
|
||||
result += convert(Integer.parseInt(number.toString()), c);
|
||||
number = new StringBuilder();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
@ -1,229 +0,0 @@
|
||||
package com.songoda.ultimatemoderation.utils;
|
||||
|
||||
import com.songoda.ultimatemoderation.UltimateModeration;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Created by songo on 6/4/2017.
|
||||
*/
|
||||
public class SettingsManager implements Listener {
|
||||
|
||||
private static final Pattern SETTINGS_PATTERN = Pattern.compile("(.{1,28}(?:\\s|$))|(.{0,28})", Pattern.DOTALL);
|
||||
private final UltimateModeration instance;
|
||||
private String pluginName = "UltimateModeration";
|
||||
private Map<Player, String> cat = new HashMap<>();
|
||||
private Map<Player, String> current = new HashMap<>();
|
||||
|
||||
public SettingsManager(UltimateModeration plugin) {
|
||||
this.instance = plugin;
|
||||
Bukkit.getPluginManager().registerEvents(this, plugin);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInventoryClick(InventoryClickEvent event) {
|
||||
ItemStack clickedItem = event.getCurrentItem();
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(UltimateModeration.getInstance(), () ->
|
||||
this.finishEditing(player), 0L);
|
||||
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
private void finishEditing(Player player) {
|
||||
this.current.remove(player);
|
||||
this.instance.saveConfig();
|
||||
this.openEditor(player);
|
||||
}
|
||||
|
||||
private 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(instance.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.WHITE_WOOL : Material.valueOf("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);
|
||||
}
|
||||
|
||||
private 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(instance.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.CLOCK : Material.valueOf("WATCH"));
|
||||
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() {
|
||||
FileConfiguration config = instance.getConfig();
|
||||
|
||||
for (Setting setting : Setting.values()) {
|
||||
config.addDefault(setting.setting, setting.option);
|
||||
}
|
||||
}
|
||||
|
||||
public enum Setting {
|
||||
|
||||
VANISH_EFFECTS("Main.Enable Vanish Effects", true),
|
||||
VANISH_SOUND("Main.Vanish Sound", "ENTITY_GENERIC_EXPLODE"),
|
||||
VANISH_BATS("Main.Release Bats On Vanish", true),
|
||||
VANISH_PARTICLE("Main.Vanish Particle", "EXPLOSION_NORMAL"),
|
||||
SLOW_MODE("Main.SLOW_MODE", "0s"),
|
||||
|
||||
BLOCKED_COMMANDS("Main.Blocked Commands", Arrays.asList("Fly", "Op", "Plugins", "Pl")),
|
||||
|
||||
AUTOSAVE("Main.Auto Save Interval In Seconds", 15),
|
||||
|
||||
STAFFCHAT_COLOR_CODE("Main.Staff Chat Color Code", 'b'),
|
||||
TICKET_TYPES("Main.Ticket Types", Arrays.asList("Grief", "Player Report", "Bug Report", "Suggestion", "Other")),
|
||||
|
||||
MUTE_DISABLED_COMMANDS("Main.Mute Disabled Commands", Arrays.asList("minecraft:me", "minecraft:tell")),
|
||||
|
||||
GLASS_TYPE_1("Interfaces.Glass Type 1", 7),
|
||||
GLASS_TYPE_2("Interfaces.Glass Type 2", 11),
|
||||
GLASS_TYPE_3("Interfaces.Glass Type 3", 3),
|
||||
|
||||
DATABASE_SUPPORT("Database.Activate Mysql Support", false),
|
||||
DATABASE_IP("Database.IP", "127.0.0.1"),
|
||||
DATABASE_PORT("Database.Port", 3306),
|
||||
DATABASE_NAME("Database.Database Name", "UltimateModeration"),
|
||||
DATABASE_PREFIX("Database.Prefix", "UM-"),
|
||||
DATABASE_USERNAME("Database.Username", "PUT_USERNAME_HERE"),
|
||||
DATABASE_PASSWORD("Database.Password", "PUT_PASSWORD_HERE"),
|
||||
|
||||
LANGUGE_MODE("System.Language Mode", "en_US");
|
||||
|
||||
private String setting;
|
||||
private Object option;
|
||||
|
||||
Setting(String setting, Object option) {
|
||||
this.setting = setting;
|
||||
this.option = option;
|
||||
}
|
||||
|
||||
public List<String> getStringList() {
|
||||
return UltimateModeration.getInstance().getConfig().getStringList(setting);
|
||||
}
|
||||
|
||||
public boolean getBoolean() {
|
||||
return UltimateModeration.getInstance().getConfig().getBoolean(setting);
|
||||
}
|
||||
|
||||
public int getInt() {
|
||||
return UltimateModeration.getInstance().getConfig().getInt(setting);
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return UltimateModeration.getInstance().getConfig().getString(setting);
|
||||
}
|
||||
|
||||
public char getChar() { return UltimateModeration.getInstance().getConfig().getString(setting).charAt(0); }
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.songoda.ultimatemoderation.utils.settings;
|
||||
|
||||
public enum Category {
|
||||
|
||||
MAIN("General settings and options."),
|
||||
INTERFACES("These settings allow you to alter the way interfaces look.",
|
||||
"They are used in GUI's to make paterns, change them up then open up a",
|
||||
"GUI to see how it works."),
|
||||
DATABASE("Settings regarding the Database."),
|
||||
SYSTEM("System related settings.");
|
||||
|
||||
private String[] comments;
|
||||
|
||||
|
||||
Category(String... comments) {
|
||||
this.comments = comments;
|
||||
}
|
||||
|
||||
public String[] getComments() {
|
||||
return comments;
|
||||
}
|
||||
}
|
@ -0,0 +1,134 @@
|
||||
package com.songoda.ultimatemoderation.utils.settings;
|
||||
|
||||
import com.songoda.ultimatemoderation.UltimateModeration;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public enum Setting {
|
||||
|
||||
VANISH_EFFECTS("Main.Enable Vanish Effects", true,
|
||||
"Show particles and play sound when going in and out of vanish."),
|
||||
|
||||
VANISH_SOUND("Main.Vanish Sound", "ENTITY_GENERIC_EXPLODE",
|
||||
"Sound to be played when going into vanish."),
|
||||
|
||||
VANISH_BATS("Main.Release Bats On Vanish", true,
|
||||
"Shows bats when entering vanish."),
|
||||
|
||||
VANISH_PARTICLE("Main.Vanish Particle", "EXPLOSION_NORMAL",
|
||||
"Show particles when entering vanish."),
|
||||
|
||||
SLOW_MODE("Main.SLOW_MODE", "0s",
|
||||
"Limits how often a player can send a chat message by the corresponding amount."),
|
||||
|
||||
BLOCKED_COMMANDS("Main.Blocked Commands", Arrays.asList("Fly", "Op", "Plugins", "Pl"),
|
||||
"Prevents players from running the specified commands."),
|
||||
|
||||
AUTOSAVE("Main.Auto Save Interval In Seconds", 15,
|
||||
"The amount of time in between saving to file.",
|
||||
"This is purely a safety function to prevent against unplanned crashes or",
|
||||
"restarts. With that said it is advised to keep this enabled.",
|
||||
"If however you enjoy living on the edge, feel free to turn it off."),
|
||||
|
||||
STAFFCHAT_COLOR_CODE("Main.Staff Chat Color Code", 'b',
|
||||
"Color of messages sent in staff chat."),
|
||||
|
||||
TICKET_TYPES("Main.Ticket Types", Arrays.asList("Grief", "Player Report", "Bug Report", "Suggestion", "Other"),
|
||||
"Types of tickets players can open."),
|
||||
|
||||
MUTE_DISABLED_COMMANDS("Main.Mute Disabled Commands", Arrays.asList("minecraft:me", "minecraft:tell"),
|
||||
"Commands disabled when a player is muted."),
|
||||
|
||||
GLASS_TYPE_1("Interfaces.Glass Type 1", 7),
|
||||
GLASS_TYPE_2("Interfaces.Glass Type 2", 11),
|
||||
GLASS_TYPE_3("Interfaces.Glass Type 3", 3),
|
||||
|
||||
DATABASE_SUPPORT("Database.Activate Mysql Support", false,
|
||||
"Should MySQL be used for data storage?"),
|
||||
|
||||
DATABASE_IP("Database.IP", "127.0.0.1",
|
||||
"MySQL IP"),
|
||||
|
||||
DATABASE_PORT("Database.Port", 3306,
|
||||
"MySQL Port"),
|
||||
|
||||
DATABASE_NAME("Database.Database Name", "UltimateModeration",
|
||||
"The database you are inserting data into."),
|
||||
|
||||
DATABASE_PREFIX("Database.Prefix", "US-",
|
||||
"The prefix for tables inserted into the database."),
|
||||
|
||||
DATABASE_USERNAME("Database.Username", "PUT_USERNAME_HERE",
|
||||
"MySQL Username"),
|
||||
|
||||
DATABASE_PASSWORD("Database.Password", "PUT_PASSWORD_HERE",
|
||||
"MySQL Password"),
|
||||
|
||||
LANGUGE_MODE("System.Language Mode", "en_US",
|
||||
"The enabled language file.",
|
||||
"More language files (if available) can be found in the plugins data folder.");
|
||||
|
||||
private String setting;
|
||||
private Object option;
|
||||
private String[] comments;
|
||||
|
||||
Setting(String setting, Object option, String... comments) {
|
||||
this.setting = setting;
|
||||
this.option = option;
|
||||
this.comments = comments;
|
||||
}
|
||||
|
||||
Setting(String setting, Object option) {
|
||||
this.setting = setting;
|
||||
this.option = option;
|
||||
this.comments = null;
|
||||
}
|
||||
|
||||
public static Setting getSetting(String setting) {
|
||||
List<Setting> settings = Arrays.stream(values()).filter(setting1 -> setting1.setting.equals(setting)).collect(Collectors.toList());
|
||||
if (settings.isEmpty()) return null;
|
||||
return settings.get(0);
|
||||
}
|
||||
|
||||
public String getSetting() {
|
||||
return setting;
|
||||
}
|
||||
|
||||
public Object getOption() {
|
||||
return option;
|
||||
}
|
||||
|
||||
public String[] getComments() {
|
||||
return comments;
|
||||
}
|
||||
|
||||
public List<String> getStringList() {
|
||||
return UltimateModeration.getInstance().getConfig().getStringList(setting);
|
||||
}
|
||||
|
||||
public boolean getBoolean() {
|
||||
return UltimateModeration.getInstance().getConfig().getBoolean(setting);
|
||||
}
|
||||
|
||||
public int getInt() {
|
||||
return UltimateModeration.getInstance().getConfig().getInt(setting);
|
||||
}
|
||||
|
||||
public long getLong() {
|
||||
return UltimateModeration.getInstance().getConfig().getLong(setting);
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return UltimateModeration.getInstance().getConfig().getString(setting);
|
||||
}
|
||||
|
||||
public char getChar() {
|
||||
return UltimateModeration.getInstance().getConfig().getString(setting).charAt(0);
|
||||
}
|
||||
|
||||
public double getDouble() {
|
||||
return UltimateModeration.getInstance().getConfig().getDouble(setting);
|
||||
}
|
||||
}
|
@ -0,0 +1,327 @@
|
||||
package com.songoda.ultimatemoderation.utils.settings;
|
||||
|
||||
import com.songoda.ultimatemoderation.UltimateModeration;
|
||||
import com.songoda.ultimatemoderation.utils.Methods;
|
||||
import com.songoda.ultimatemoderation.utils.ServerVersion;
|
||||
import com.songoda.ultimatemoderation.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.io.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created by songoda on 6/4/2017.
|
||||
*/
|
||||
public class SettingsManager implements Listener {
|
||||
|
||||
private final UltimateModeration plugin;
|
||||
private Map<Player, String> cat = new HashMap<>();
|
||||
private Map<Player, String> current = new HashMap<>();
|
||||
|
||||
public SettingsManager(UltimateModeration plugin) {
|
||||
this.plugin = plugin;
|
||||
Bukkit.getPluginManager().registerEvents(this, plugin);
|
||||
}
|
||||
|
||||
public static <K, V> void add(LinkedHashMap<K, V> map, int index, K key, V value) {
|
||||
assert (map != null);
|
||||
assert !map.containsKey(key);
|
||||
assert (index >= 0) && (index < map.size());
|
||||
|
||||
int i = 0;
|
||||
List<Map.Entry<K, V>> rest = new ArrayList<>();
|
||||
for (Map.Entry<K, V> entry : map.entrySet()) {
|
||||
if (i++ >= index) {
|
||||
rest.add(entry);
|
||||
}
|
||||
}
|
||||
map.put(key, value);
|
||||
for (Map.Entry<K, V> entry : rest) {
|
||||
map.remove(entry.getKey());
|
||||
map.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInventoryClick(InventoryClickEvent event) {
|
||||
ItemStack clickedItem = event.getCurrentItem();
|
||||
|
||||
if (event.getInventory() != event.getWhoClicked().getOpenInventory().getTopInventory()
|
||||
|| clickedItem == null || !clickedItem.hasItemMeta()
|
||||
|| !clickedItem.getItemMeta().hasDisplayName()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getView().getTitle().equals(plugin.getName() + " 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(plugin.getName() + " 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 (plugin.getConfig().get(key).getClass().getName().equals("java.lang.Boolean")) {
|
||||
this.plugin.getConfig().set(key, !plugin.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 = plugin.getConfig();
|
||||
if (config.isLong(value)) {
|
||||
config.set(value, Long.parseLong(event.getMessage()));
|
||||
} else 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());
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(UltimateModeration.getInstance(), () ->
|
||||
this.finishEditing(player), 0L);
|
||||
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
private void finishEditing(Player player) {
|
||||
this.current.remove(player);
|
||||
this.saveConfig();
|
||||
this.openEditor(player);
|
||||
}
|
||||
|
||||
private 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 (plugin.getConfig().isInt(current) || plugin.getConfig().isDouble(current)) {
|
||||
player.sendMessage(Methods.formatText("&cUse only numbers."));
|
||||
}
|
||||
player.sendMessage("");
|
||||
}
|
||||
|
||||
public void openSettingsManager(Player player) {
|
||||
Inventory inventory = Bukkit.createInventory(null, 27, plugin.getName() + " Settings Manager");
|
||||
ItemStack glass = Methods.getGlass();
|
||||
for (int i = 0; i < inventory.getSize(); i++) {
|
||||
inventory.setItem(i, glass);
|
||||
}
|
||||
|
||||
int slot = 10;
|
||||
for (String key : plugin.getConfig().getDefaultSection().getKeys(false)) {
|
||||
ItemStack item = new ItemStack(plugin.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.LEGACY_WOOL : Material.valueOf("WOOL"), 1, (byte) (slot - 9));
|
||||
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);
|
||||
}
|
||||
|
||||
private void openEditor(Player player) {
|
||||
Inventory inventory = Bukkit.createInventory(null, 54, plugin.getName() + " Settings Editor");
|
||||
FileConfiguration config = plugin.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("&7" + config.getString(fKey)));
|
||||
} else if (config.isInt(fKey)) {
|
||||
item.setType(plugin.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.CLOCK : Material.valueOf("WATCH"));
|
||||
lore.add(Methods.formatText("&7" + config.getInt(fKey)));
|
||||
} else if (config.isLong(fKey)) {
|
||||
item.setType(plugin.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.CLOCK : Material.valueOf("WATCH"));
|
||||
lore.add(Methods.formatText("&7" + config.getLong(fKey)));
|
||||
} else if (config.isDouble(fKey)) {
|
||||
item.setType(plugin.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.CLOCK : Material.valueOf("WATCH"));
|
||||
lore.add(Methods.formatText("&7" + config.getDouble(fKey)));
|
||||
}
|
||||
|
||||
Setting setting = Setting.getSetting(fKey);
|
||||
|
||||
if (setting != null && setting.getComments() != null) {
|
||||
lore.add("");
|
||||
|
||||
String comment = String.join(" ", setting.getComments());
|
||||
|
||||
int lastIndex = 0;
|
||||
for (int n = 0; n < comment.length(); n++) {
|
||||
if (n - lastIndex < 30)
|
||||
continue;
|
||||
|
||||
if (comment.charAt(n) == ' ') {
|
||||
lore.add(Methods.formatText("&8" + comment.substring(lastIndex, n).trim()));
|
||||
lastIndex = n;
|
||||
}
|
||||
}
|
||||
|
||||
if (lastIndex - comment.length() < 30)
|
||||
lore.add(Methods.formatText("&8" + comment.substring(lastIndex).trim()));
|
||||
|
||||
}
|
||||
|
||||
meta.setLore(lore);
|
||||
item.setItemMeta(meta);
|
||||
|
||||
inventory.setItem(slot, item);
|
||||
slot++;
|
||||
}
|
||||
|
||||
player.openInventory(inventory);
|
||||
}
|
||||
|
||||
public void reloadConfig() {
|
||||
plugin.reloadConfig();
|
||||
this.setupConfig();
|
||||
}
|
||||
|
||||
public void setupConfig() {
|
||||
FileConfiguration config = plugin.getConfig();
|
||||
|
||||
for (Setting setting : Setting.values()) {
|
||||
config.addDefault(setting.getSetting(), setting.getOption());
|
||||
}
|
||||
plugin.getConfig().options().copyDefaults(true);
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
private void saveConfig() {
|
||||
String dump = plugin.getConfig().saveToString();
|
||||
|
||||
StringBuilder config = new StringBuilder();
|
||||
|
||||
BufferedReader bufReader = new BufferedReader(new StringReader(dump));
|
||||
|
||||
try {
|
||||
boolean first = true;
|
||||
|
||||
String line;
|
||||
int currentTab = 0;
|
||||
String category = "";
|
||||
|
||||
while ((line = bufReader.readLine()) != null) {
|
||||
if (line.trim().startsWith("#")) continue;
|
||||
|
||||
int tabChange = line.length() - line.trim().length();
|
||||
if (currentTab != tabChange) {
|
||||
category = category.contains(".") && tabChange != 0 ? category.substring(0, category.indexOf(".")) : "";
|
||||
currentTab = tabChange;
|
||||
}
|
||||
|
||||
if (line.endsWith(":")) {
|
||||
bufReader.mark(1000);
|
||||
String found = bufReader.readLine();
|
||||
bufReader.reset();
|
||||
|
||||
if (!found.trim().startsWith("-")) {
|
||||
|
||||
String newCategory = line.substring(0, line.length() - 1).trim();
|
||||
|
||||
if (category.equals(""))
|
||||
category = newCategory;
|
||||
else
|
||||
category += "." + newCategory;
|
||||
|
||||
currentTab = tabChange + 2;
|
||||
|
||||
if (!first) {
|
||||
config.append("\n\n");
|
||||
} else {
|
||||
first = false;
|
||||
}
|
||||
|
||||
if (!category.contains("."))
|
||||
config.append("#").append("\n");
|
||||
try {
|
||||
Category categoryObj = Category.valueOf(category.toUpperCase()
|
||||
.replace(" ", "_")
|
||||
.replace(".", "_"));
|
||||
|
||||
config.append(new String(new char[tabChange]).replace('\0', ' '));
|
||||
for (String l : categoryObj.getComments())
|
||||
config.append("# ").append(l).append("\n");
|
||||
} catch (IllegalArgumentException e) {
|
||||
config.append("# ").append(category).append("\n");
|
||||
}
|
||||
if (!category.contains("."))
|
||||
config.append("#").append("\n");
|
||||
|
||||
config.append(line).append("\n");
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (line.trim().startsWith("-")) {
|
||||
config.append(line).append("\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
String key = category + "." + (line.split(":")[0].trim());
|
||||
for (Setting setting : Setting.values()) {
|
||||
if (!setting.getSetting().equals(key) || setting.getComments() == null) continue;
|
||||
config.append(" ").append("\n");
|
||||
for (String l : setting.getComments()) {
|
||||
config.append(new String(new char[currentTab]).replace('\0', ' '));
|
||||
config.append("# ").append(l).append("\n");
|
||||
}
|
||||
}
|
||||
config.append(line).append("\n");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
BufferedWriter writer =
|
||||
new BufferedWriter(new FileWriter(new File(plugin.getDataFolder() + "\\config.yml")));
|
||||
writer.write(config.toString());
|
||||
writer.flush();
|
||||
writer.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user