From b6b934d65ca520dc1225346d6f350e978adc4685 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichocki?= Date: Wed, 8 Sep 2021 14:46:57 +0200 Subject: [PATCH] Update to v1.4.1 --- pom.xml | 38 +++++- .../plytki/virtualrealty/VirtualRealty.java | 49 ++++---- .../commands/VirtualRealtyCommand.java | 24 ++-- .../configs/PluginConfiguration.java | 103 ++++++++++++++++ .../configs/SizesConfiguration.java | 89 ++++++++++++++ .../IncompatibleVersionException.java | 9 -- .../exceptions/MaterialMatchException.java | 9 ++ .../listeners/PlotProtectionListener.java | 24 +++- .../loaders/PluginConfiguration.java | 105 ---------------- .../loaders/SizesConfiguration.java | 114 ------------------ .../java/me/plytki/virtualrealty/sql/SQL.java | 2 +- .../virtualrealty/utils/ConfigUtil.java | 51 -------- .../utils/ConfigurationFactory.java | 37 ++++++ .../utils/multiversion/Chat.java | 9 +- .../utils/multiversion/VMaterial.java | 19 ++- 15 files changed, 362 insertions(+), 320 deletions(-) create mode 100644 src/main/java/me/plytki/virtualrealty/configs/PluginConfiguration.java create mode 100644 src/main/java/me/plytki/virtualrealty/configs/SizesConfiguration.java delete mode 100644 src/main/java/me/plytki/virtualrealty/exceptions/IncompatibleVersionException.java create mode 100644 src/main/java/me/plytki/virtualrealty/exceptions/MaterialMatchException.java delete mode 100644 src/main/java/me/plytki/virtualrealty/loaders/PluginConfiguration.java delete mode 100644 src/main/java/me/plytki/virtualrealty/loaders/SizesConfiguration.java delete mode 100644 src/main/java/me/plytki/virtualrealty/utils/ConfigUtil.java create mode 100644 src/main/java/me/plytki/virtualrealty/utils/ConfigurationFactory.java diff --git a/pom.xml b/pom.xml index ac3fb78..656689c 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.plytki VirtualRealty - 1.4.0 + 1.4.1 jar VirtualRealty @@ -53,6 +53,9 @@ org.bstats me.plytki.virtualrealty.utils + + org.apache.commons.io + me.plytki.virtualrealty.utils org.h2 @@ -101,6 +104,10 @@ panda-repository https://repo.panda-lang.org/ + + okaeri-repo + https://storehouse.okaeri.eu/repository/maven-public/ + @@ -135,9 +142,32 @@ compile - org.diorite - diorite-config-minimal - 1.2 + eu.okaeri + okaeri-configs-yaml-bukkit + 3.1.0 + + + eu.okaeri + okaeri-configs-serdes-commons + 3.1.0 + + + eu.okaeri + okaeri-configs-validator-okaeri + 3.1.0 + + + commons-io + commons-io + 2.11.0 + compile + + + org.apache.commons + commons-lang3 + 3.12.0 + compile + diff --git a/src/main/java/me/plytki/virtualrealty/VirtualRealty.java b/src/main/java/me/plytki/virtualrealty/VirtualRealty.java index abee090..9683095 100644 --- a/src/main/java/me/plytki/virtualrealty/VirtualRealty.java +++ b/src/main/java/me/plytki/virtualrealty/VirtualRealty.java @@ -2,17 +2,18 @@ import me.plytki.virtualrealty.commands.PlotCommand; import me.plytki.virtualrealty.commands.VirtualRealtyCommand; +import me.plytki.virtualrealty.configs.PluginConfiguration; +import me.plytki.virtualrealty.configs.SizesConfiguration; import me.plytki.virtualrealty.enums.PlotSize; +import me.plytki.virtualrealty.exceptions.MaterialMatchException; import me.plytki.virtualrealty.listeners.PlotListener; import me.plytki.virtualrealty.listeners.PlotProtectionListener; import me.plytki.virtualrealty.listeners.WorldListener; -import me.plytki.virtualrealty.loaders.PluginConfiguration; -import me.plytki.virtualrealty.loaders.SizesConfiguration; import me.plytki.virtualrealty.managers.PlotManager; import me.plytki.virtualrealty.objects.Plot; import me.plytki.virtualrealty.sql.SQL; import me.plytki.virtualrealty.tasks.PlotExpireTask; -import me.plytki.virtualrealty.utils.ConfigUtil; +import me.plytki.virtualrealty.utils.ConfigurationFactory; import me.plytki.virtualrealty.utils.UpdateChecker; import me.plytki.virtualrealty.utils.multiversion.VMaterial; import org.apache.commons.io.FileUtils; @@ -38,8 +39,8 @@ public final class VirtualRealty extends JavaPlugin { public static File plotsFolder; public static File plotsSchemaFolder; - private PluginConfiguration pluginConfiguration; - private SizesConfiguration sizesConfiguration; + public static PluginConfiguration pluginConfiguration; + public static SizesConfiguration sizesConfiguration; private final File pluginConfigurationFile = new File(this.getDataFolder(), "config.yml"); private final File sizesConfigurationFile = new File(this.getDataFolder(), "sizes.yml"); @@ -78,14 +79,16 @@ public void onEnable() { plotsSchemaFolder = new File(plotsFolder.getAbsolutePath(), "primary-terrain"); plotsSchemaFolder.mkdirs(); try { - this.pluginConfiguration = ConfigUtil.loadConfig(this.pluginConfigurationFile, PluginConfiguration.class); - this.sizesConfiguration = ConfigUtil.loadConfig(this.sizesConfigurationFile, SizesConfiguration.class); - } catch (Exception exception) { + ConfigurationFactory configFactory = new ConfigurationFactory(); + pluginConfiguration = configFactory.createPluginConfiguration(pluginConfigurationFile); + sizesConfiguration = configFactory.createSizesConfiguration(sizesConfigurationFile); + } + catch (Exception exception) { exception.printStackTrace(); - configError = true; - this.getServer().getPluginManager().disablePlugin(this); + //shutdown("Critical error has been encountered!"); return; } + //createSizesConfig(); loadSizesConfiguration(); connectToDatabase(); @@ -170,36 +173,36 @@ private void connectToDatabase() { public static void loadSizesConfiguration() { for (PlotSize plotSize : PlotSize.values()) { if (plotSize == PlotSize.CUSTOM) return; - SizesConfiguration.Size classSize = null; + SizesConfiguration.PlotSizes.Size classSize = null; switch (plotSize) { case SMALL: { - classSize = getInstance().sizesConfiguration.plotSizes.SMALL; + classSize = sizesConfiguration.plotSizes.SMALL; break; } case MEDIUM: { - classSize = getInstance().sizesConfiguration.plotSizes.MEDIUM; + classSize = sizesConfiguration.plotSizes.MEDIUM; break; } case LARGE: { - classSize = getInstance().sizesConfiguration.plotSizes.LARGE; + classSize = sizesConfiguration.plotSizes.LARGE; break; } } Material floorMaterial; try { - floorMaterial = VMaterial.getMaterial(classSize.floorMaterial.toUpperCase()); - floorMaterial.name(); - } catch (Exception e) { + floorMaterial = VMaterial.catchMaterial(classSize.floorMaterial.toUpperCase()); + } catch (MaterialMatchException e) { floorMaterial = VirtualRealty.isLegacy ? Material.GRASS : Material.GRASS_BLOCK; - VirtualRealty.getInstance().getLogger().warning("Couldn't parse floor-material from sizes.yml | Using default: " + (VirtualRealty.isLegacy ? Material.GRASS : Material.GRASS_BLOCK)); + e.printStackTrace(); + //throw new MaterialMatchException("Couldn't parse floor-material from sizes.yml | Using default: " + (VirtualRealty.isLegacy ? Material.GRASS : Material.GRASS_BLOCK)); } Material borderMaterial; try { - borderMaterial = VMaterial.getMaterial(classSize.borderMaterial.toUpperCase()); - borderMaterial.name(); - } catch (Exception e) { + borderMaterial = VMaterial.catchMaterial(classSize.borderMaterial.toUpperCase()); + } catch (MaterialMatchException e) { borderMaterial = VirtualRealty.isLegacy ? Material.getMaterial("STEP") : Material.STONE_BRICK_SLAB; - VirtualRealty.getInstance().getLogger().warning("Couldn't parse border-material from sizes.yml | Using default: " + (VirtualRealty.isLegacy ? Material.getMaterial("STEP") : Material.STONE_BRICK_SLAB)); + e.printStackTrace(); + //throw new MaterialMatchException("Couldn't parse border-material from sizes.yml | Using default: " + (VirtualRealty.isLegacy ? Material.getMaterial("STEP") : Material.STONE_BRICK_SLAB)); } plotSize.setFloorMaterial(floorMaterial); plotSize.setFloorData(classSize.floorData); @@ -216,7 +219,7 @@ public static VirtualRealty getInstance() { } public static PluginConfiguration getPluginConfiguration() { - return getInstance().pluginConfiguration; + return pluginConfiguration; } public boolean checkLegacyVersions() { diff --git a/src/main/java/me/plytki/virtualrealty/commands/VirtualRealtyCommand.java b/src/main/java/me/plytki/virtualrealty/commands/VirtualRealtyCommand.java index 5a6dd68..ca5e7f1 100644 --- a/src/main/java/me/plytki/virtualrealty/commands/VirtualRealtyCommand.java +++ b/src/main/java/me/plytki/virtualrealty/commands/VirtualRealtyCommand.java @@ -3,6 +3,7 @@ import me.plytki.virtualrealty.VirtualRealty; import me.plytki.virtualrealty.enums.Direction; import me.plytki.virtualrealty.enums.PlotSize; +import me.plytki.virtualrealty.exceptions.MaterialMatchException; import me.plytki.virtualrealty.managers.PlotManager; import me.plytki.virtualrealty.objects.Plot; import me.plytki.virtualrealty.utils.Permissions; @@ -169,7 +170,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command case "CREATE": { if (!Permissions.hasPermission(sender, commandPermission, "create")) return false; if (sender instanceof Player) { - if (args.length == 2 || args.length == 3) { + if ((args.length == 2 || args.length == 3) && !args[1].equalsIgnoreCase("custom")) { PlotSize plotSize = null; try { plotSize = PlotSize.valueOf(args[1].toUpperCase()); @@ -193,16 +194,20 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command } sender.sendMessage(VirtualRealty.PREFIX + "§aNot colliding. Creating plot.."); long timeStart = System.currentTimeMillis(); - PlotManager.createPlot(location, plotSize, material); + Plot plot = PlotManager.createPlot(location, plotSize, material); long timeEnd = System.currentTimeMillis(); - sender.sendMessage(VirtualRealty.PREFIX + "§aPlot created! §8(§7" + (timeEnd - timeStart) + " ms§8)"); + BaseComponent textComponent = new TextComponent(VirtualRealty.PREFIX + "§aPlot "); + BaseComponent textComponent2 = new TextComponent("§8#§7" + plot.getID()); + BaseComponent textComponent3 = new TextComponent(" §acreated! §8(§7" + (timeEnd - timeStart) + " ms§8)"); + textComponent2.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponent[]{new TextComponent("§a§oClick to show detailed information about the plot! §8(§7ID: §f" + plot.getID() + "§8)")})); + textComponent2.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/vrplot info " + plot.getID())); + new Chat(textComponent, textComponent2, textComponent3).sendTo(p); } } else { sender.sendMessage(VirtualRealty.PREFIX + "§cSize not recognized!"); return false; } } else { - PlotSize plotSize = PlotSize.CUSTOM; int length; int width; int height; @@ -222,7 +227,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command sender.sendMessage(VirtualRealty.PREFIX + "§cYou cant create new plot on the existing plot!"); return false; } else { - Material material = Material.GRASS; + Material material = Material.matchMaterial(VirtualRealty.isLegacy ? "GRASS" : "GRASS_BLOCK"); if (args.length >= 5) { try { //String[] materialText = Arrays.copyOfRange(args, 5, args.length); @@ -234,9 +239,14 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command } sender.sendMessage(VirtualRealty.PREFIX + "§aNot colliding. Creating plot.."); long timeStart = System.currentTimeMillis(); - PlotManager.createPlot(location, length, width, height, material); + Plot plot = PlotManager.createPlot(location, length, width, height, material); long timeEnd = System.currentTimeMillis(); - sender.sendMessage(VirtualRealty.PREFIX + "§aPlot created! §8(§7" + (timeEnd - timeStart) + " ms§8)"); + BaseComponent textComponent = new TextComponent(VirtualRealty.PREFIX + "§aPlot "); + BaseComponent textComponent2 = new TextComponent("§8#§7" + plot.getID()); + BaseComponent textComponent3 = new TextComponent(" §acreated! §8(§7" + (timeEnd - timeStart) + " ms§8)"); + textComponent2.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponent[]{new TextComponent("§a§oClick to show detailed information about the plot! §8(§7ID: §f" + plot.getID() + "§8)")})); + textComponent2.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/vrplot info " + plot.getID())); + new Chat(textComponent, textComponent2, textComponent3).sendTo(p); } } } diff --git a/src/main/java/me/plytki/virtualrealty/configs/PluginConfiguration.java b/src/main/java/me/plytki/virtualrealty/configs/PluginConfiguration.java new file mode 100644 index 0000000..9f5a0c7 --- /dev/null +++ b/src/main/java/me/plytki/virtualrealty/configs/PluginConfiguration.java @@ -0,0 +1,103 @@ +package me.plytki.virtualrealty.configs; + +import eu.okaeri.configs.OkaeriConfig; +import eu.okaeri.configs.annotation.*; +import me.plytki.virtualrealty.VirtualRealty; +import org.bukkit.GameMode; + + +@Header("################################################################") +@Header("# #") +@Header("# Virtual Realty #") +@Header("# #") +@Header("################################################################") +@Names(strategy = NameStrategy.IDENTITY, modifier = NameModifier.TO_LOWER_CASE) +public class PluginConfiguration extends OkaeriConfig { + + @Comment(" ") + @Comment("-------------------------") + @Comment("Don't change this value!") + @CustomKey("config-version") + public String configVersion = VirtualRealty.getInstance().getDescription().getVersion(); + + @Comment("-------------------------") + @Comment("Set player gamemode to change when they enter their plot") + @CustomKey("enable-plot-gamemode") + public boolean enablePlotGameMode = false; + + @Comment("Set which gamemode players change to when they enter their plot") + @CustomKey("default-plot-gamemode") + public String plotGameMode = "SURVIVAL"; + + @Comment("Set forced change to plot gamemode when players enter their plot") + @CustomKey("force-plot-gamemode") + public boolean forcePlotGameMode = false; + + public GameMode getGameMode() { + try { + return GameMode.valueOf(plotGameMode); + } catch (Exception e) { + VirtualRealty.getInstance().getLogger().warning("Couldn't parse plot-gamemode from config.yml\nUsing default: SURVIVAL"); + return GameMode.SURVIVAL; + } + } + + @Comment("Allow players to build outside of their plots") + @CustomKey("allow-outplot-build") + public boolean allowOutPlotBuild = true; + + @Comment("Type of data recording") + @Comment("H2 - Local database (Automatically started with our plugin)") + @Comment("MYSQL - External database") + @CustomKey("data-model") + public DataModel dataModel = DataModel.H2; + + @Comment("Data required to connect to the database") + @Comment("The plotsTableName section is the name of the VR data table in the database") + @Comment("It is best to change these names only if you really need to (e.g. there is a conflict with another plugin)") + @Comment("To rename tables when you already have some VR data in the database:") + @Comment("1. Turn off the server") + @Comment("2. Change data in VR config") + @Comment("3. Rename database tables using phpMyAdmin for example") + @CustomKey("mysql") + public MySQL mysql = new MySQL("localhost", 3306, "db", "root", "passwd", true, "vr_plots"); + + + public enum DataModel { + H2, + MYSQL + } + + @Names(strategy = NameStrategy.IDENTITY) + public static class MySQL extends OkaeriConfig { + + @Variable("VR_MYSQL_HOSTNAME") + public String hostname; + @Variable("VR_MYSQL_PORT") + public int port; + @Variable("VR_MYSQL_DATABASE") + public String database; + @Variable("VR_MYSQL_USER") + public String user; + @Variable("VR_MYSQL_PASSWORD") + public String password; + @Variable("VR_MYSQL_USE_SSL") + public boolean useSSL; + @Variable("VR_MYSQL_USERS_TABLE_NAME") + public String plotsTableName; + + public MySQL() { + } + + public MySQL(String hostname, int port, String database, String user, String password, boolean useSSL, String plotsTableName) { + this.hostname = hostname; + this.port = port; + this.database = database; + this.user = user; + this.password = password; + this.useSSL = useSSL; + this.plotsTableName = plotsTableName; + } + } + +} diff --git a/src/main/java/me/plytki/virtualrealty/configs/SizesConfiguration.java b/src/main/java/me/plytki/virtualrealty/configs/SizesConfiguration.java new file mode 100644 index 0000000..1751108 --- /dev/null +++ b/src/main/java/me/plytki/virtualrealty/configs/SizesConfiguration.java @@ -0,0 +1,89 @@ +package me.plytki.virtualrealty.configs; + +import eu.okaeri.configs.OkaeriConfig; +import eu.okaeri.configs.annotation.*; +import me.plytki.virtualrealty.VirtualRealty; +import me.plytki.virtualrealty.enums.PlotSize; +import org.bukkit.Material; + +import java.time.Instant; + +@Header("################################################################") +@Header("# #") +@Header("# Plot Sizes #") +@Header("# #") +@Header("################################################################") +public class SizesConfiguration extends OkaeriConfig { + + @Comment(" ") + @Comment("-------------------------") + @Comment("Don't change this value!") + @CustomKey("config-version") + public final String configVersion = VirtualRealty.getInstance().getDescription().getVersion(); + @Comment("-------------------------") + + @Comment(" ") + @Comment("(<1.13) Legacy Materials: https://helpch.at/docs/1.8/org/bukkit/Material.html") + @Comment("(>1.12) Post-Legacy Materials: https://helpch.at/docs/1.13/org/bukkit/Material.html") + @Comment(" ") + @Comment("floor-data and border-data are only for legacy versions * <1.13 *") + @CustomKey("plot-sizes") + public PlotSizes plotSizes = new PlotSizes(); + + @Names(strategy = NameStrategy.IDENTITY) + public static class PlotSizes extends OkaeriConfig { + + @Variable("VR_SIZE_SMALL") + public Size SMALL = new Size(PlotSize.SMALL); + @Variable("VR_SIZE_MEDIUM") + public Size MEDIUM = new Size(PlotSize.MEDIUM); + @Variable("VR_SIZE_LARGE") + public Size LARGE = new Size(PlotSize.LARGE); + + public PlotSizes() { + } + + public PlotSizes(Size small, Size medium, Size large) { + this.SMALL = small; + this.MEDIUM = medium; + this.LARGE = large; + } + + public static class Size extends OkaeriConfig { + + @Variable("VR_SIZE_FLOORMATERIAL") + @CustomKey("floor-material") + public String floorMaterial; + @Variable("VR_SIZE_FLOORDATA") + @CustomKey("floor-data") + public byte floorData; + @Variable("VR_SIZE_BORDERMATERIAL") + @CustomKey("border-material") + public String borderMaterial; + @Variable("VR_SIZE_BORDERDATA") + @CustomKey("border-data") + public byte borderData; + @Variable("VR_SIZE_LENGTH") + public int length; + @Variable("VR_SIZE_WIDTH") + public int width; + @Variable("VR_SIZE_HEIGHT") + public int height; + + public Size() { + } + + public Size(PlotSize plotSize) { + this.floorMaterial = plotSize.getFloorMaterial().name(); + this.floorData = plotSize.getFloorData(); + this.borderMaterial = plotSize.getBorderMaterial().name(); + this.borderData = plotSize.getBorderData(); + this.length = plotSize.getLength(); + this.width = plotSize.getWidth(); + this.height = plotSize.getHeight(); + } + + } + } + +} diff --git a/src/main/java/me/plytki/virtualrealty/exceptions/IncompatibleVersionException.java b/src/main/java/me/plytki/virtualrealty/exceptions/IncompatibleVersionException.java deleted file mode 100644 index 3ff825f..0000000 --- a/src/main/java/me/plytki/virtualrealty/exceptions/IncompatibleVersionException.java +++ /dev/null @@ -1,9 +0,0 @@ -package me.plytki.virtualrealty.exceptions; - -public class IncompatibleVersionException extends Exception { - - public IncompatibleVersionException(String exception) { - super(exception); - } - -} diff --git a/src/main/java/me/plytki/virtualrealty/exceptions/MaterialMatchException.java b/src/main/java/me/plytki/virtualrealty/exceptions/MaterialMatchException.java new file mode 100644 index 0000000..bfe84ec --- /dev/null +++ b/src/main/java/me/plytki/virtualrealty/exceptions/MaterialMatchException.java @@ -0,0 +1,9 @@ +package me.plytki.virtualrealty.exceptions; + +public class MaterialMatchException extends Exception { + + public MaterialMatchException(String exception) { + super(exception); + } + +} diff --git a/src/main/java/me/plytki/virtualrealty/listeners/PlotProtectionListener.java b/src/main/java/me/plytki/virtualrealty/listeners/PlotProtectionListener.java index 9f82923..ab596d0 100644 --- a/src/main/java/me/plytki/virtualrealty/listeners/PlotProtectionListener.java +++ b/src/main/java/me/plytki/virtualrealty/listeners/PlotProtectionListener.java @@ -32,7 +32,11 @@ public void onInteract(PlayerInteractEvent e) { Plot plot = PlotManager.getPlot(e.getClickedBlock().getLocation()); if (plot != null) { if (plot.getOwnedBy() != null) { - if (!plot.getOwnedBy().equals(player.getUniqueId())) { + boolean hasAccess = player.hasPermission("virtualrealty.plot.build"); + if (!hasAccess) { + hasAccess = plot.getOwnedBy().equals(player.getUniqueId()); + } + if (!hasAccess) { e.setCancelled(true); player.sendMessage(VirtualRealty.PREFIX + "§cYou can't interact here!"); } else { @@ -75,7 +79,11 @@ public void onBlockBreakOutside(BlockBreakEvent e) { Plot plot = PlotManager.getBorderedPlot(player.getLocation()); if (plot != null) { if (plot.getOwnedBy() != null) { - if (plot.getOwnedBy().equals(player.getUniqueId())) { + boolean hasAccess = player.hasPermission("virtualrealty.plot.build"); + if (!hasAccess) { + hasAccess = plot.getOwnedBy().equals(player.getUniqueId()); + } + if (!hasAccess) { Plot locationPlot = PlotManager.getBorderedPlot(player.getLocation()); if (locationPlot != null && !PlotManager.isLocationInPlot(e.getBlock().getLocation(), plot)) { e.setCancelled(true); @@ -92,7 +100,11 @@ public void onBlockPlace(BlockPlaceEvent e) { Plot plot = PlotManager.getPlot(e.getBlockPlaced().getLocation()); if (plot != null) { if (plot.getOwnedBy() != null) { - if (!plot.getOwnedBy().equals(player.getUniqueId())) { + boolean hasAccess = player.hasPermission("virtualrealty.plot.build"); + if (!hasAccess) { + hasAccess = plot.getOwnedBy().equals(player.getUniqueId()); + } + if (!hasAccess) { e.setCancelled(true); player.sendMessage(VirtualRealty.PREFIX + "§cYou can't build here!"); } else { @@ -111,7 +123,11 @@ public void onBlockBreak(BlockBreakEvent e) { Plot plot = PlotManager.getPlot(e.getBlock().getLocation()); if (plot != null) { if (plot.getOwnedBy() != null) { - if (!plot.getOwnedBy().equals(player.getUniqueId())) { + boolean hasAccess = player.hasPermission("virtualrealty.plot.build"); + if (!hasAccess) { + hasAccess = plot.getOwnedBy().equals(player.getUniqueId()); + } + if (!hasAccess) { e.setCancelled(true); player.sendMessage(VirtualRealty.PREFIX + "§cYou can't build here!"); } else { diff --git a/src/main/java/me/plytki/virtualrealty/loaders/PluginConfiguration.java b/src/main/java/me/plytki/virtualrealty/loaders/PluginConfiguration.java deleted file mode 100644 index c2d33a2..0000000 --- a/src/main/java/me/plytki/virtualrealty/loaders/PluginConfiguration.java +++ /dev/null @@ -1,105 +0,0 @@ -package me.plytki.virtualrealty.loaders; - -import me.plytki.virtualrealty.VirtualRealty;; -import org.bukkit.GameMode; -import org.diorite.cfg.annotations.*; -import org.diorite.cfg.annotations.CfgStringStyle.StringStyle; -import org.diorite.cfg.annotations.defaults.CfgDelegateDefault; - -@CfgClass(name = "PluginConfiguration") -@CfgDelegateDefault("{new}") -@CfgComment("~-~-~-~-~-~-~-~-~-~-~-~~-~-~-~~ #") -@CfgComment(" #") -@CfgComment(" Virtual Realty #") -@CfgComment(" #") -@CfgComment("~-~-~-~-~-~-~-~-~-~-~-~~-~-~-~~ #") -public class PluginConfiguration { - - @CfgComment(" ") - @CfgComment("-------------------------") - @CfgComment("Don't change this value!") - @CfgName("config-version") - public final String configVersion = VirtualRealty.getInstance().getDescription().getVersion(); - - @CfgComment("-------------------------") - @CfgComment("Set player gamemode to change when they enter their plot") - @CfgName("enable-plot-gamemode") - public boolean enablePlotGameMode = false; - - @CfgComment("Set which gamemode players change to when they enter their plot") - @CfgName("default-plot-gamemode") - public String plotGameMode = "SURVIVAL"; - - @CfgComment("Set forced change to plot gamemode when players enter their plot") - @CfgName("force-plot-gamemode") - public boolean forcePlotGameMode = false; - - public GameMode getGameMode() { - try { - return GameMode.valueOf(plotGameMode); - } catch (Exception e) { - VirtualRealty.getInstance().getLogger().warning("Couldn't parse plot-gamemode from config.yml\nUsing default: SURVIVAL"); - return GameMode.SURVIVAL; - } - } - - @CfgComment("Allow players to build outside of their plots") - @CfgName("allow-outplot-build") - public boolean allowOutPlotBuild = true; - - @CfgComment("Type of data recording") - @CfgComment("H2 - Local database (Automatically started with our plugin)") - @CfgComment("MYSQL - External database") - @CfgName("data-model") - public DataModel dataModel = DataModel.H2; - - @CfgComment("Data required to connect to the database") - @CfgComment("The plotsTableName section is the name of the VR data table in the database") - @CfgComment("It is best to change these names only if you really need to (e.g. there is a conflict with another plugin)") - @CfgComment("To rename tables when you already have some VR data in the database:") - @CfgComment("1. Turn off the server") - @CfgComment("2. Change data in VR config") - @CfgComment("3. Rename database tables using phpMyAdmin for example") - @CfgName("mysql") - public MySQL mysql = new MySQL("localhost", 3306, "db", "root", "passwd", true, "vr_plots"); - - public enum DataModel { - H2, - MYSQL - } - - public static class MySQL { - @CfgStringStyle(StringStyle.ALWAYS_QUOTED) - public String hostname; - - public int port; - - @CfgStringStyle(StringStyle.ALWAYS_QUOTED) - public String database; - - @CfgStringStyle(StringStyle.ALWAYS_QUOTED) - public String user; - - @CfgStringStyle(StringStyle.ALWAYS_QUOTED) - public String password; - - public boolean useSSL; - - @CfgStringStyle(StringStyle.ALWAYS_QUOTED) - public String plotsTableName; - - public MySQL(String hostname, int port, String database, String user, String password, boolean useSSL, String plotsTableName) { - this.hostname = hostname; - this.port = port; - this.database = database; - this.user = user; - this.password = password; - this.useSSL = useSSL; - this.plotsTableName = plotsTableName; - } - - private MySQL() {} - - } - -} diff --git a/src/main/java/me/plytki/virtualrealty/loaders/SizesConfiguration.java b/src/main/java/me/plytki/virtualrealty/loaders/SizesConfiguration.java deleted file mode 100644 index 62d12f3..0000000 --- a/src/main/java/me/plytki/virtualrealty/loaders/SizesConfiguration.java +++ /dev/null @@ -1,114 +0,0 @@ -package me.plytki.virtualrealty.loaders; - -import me.plytki.virtualrealty.VirtualRealty; -import me.plytki.virtualrealty.enums.PlotSize; -import org.bukkit.Material; -import org.diorite.cfg.annotations.CfgClass; -import org.diorite.cfg.annotations.CfgComment; -import org.diorite.cfg.annotations.CfgName; -import org.diorite.cfg.annotations.CfgStringStyle; -import org.diorite.cfg.annotations.CfgStringStyle.StringStyle; -import org.diorite.cfg.annotations.defaults.CfgDelegateDefault; - -@CfgClass(name = "SizesConfiguration") -@CfgDelegateDefault("{new}") -@CfgComment("~-~-~-~-~-~-~-~-~-~-~-~~-~-~-~~ #") -@CfgComment(" #") -@CfgComment(" Plot Sizes #") -@CfgComment(" #") -@CfgComment("~-~-~-~-~-~-~-~-~-~-~-~~-~-~-~~ #") -public class SizesConfiguration { - - @CfgComment(" ") - @CfgComment("-------------------------") - @CfgComment("Don't change this value!") - @CfgName("config-version") - public final String configVersion = VirtualRealty.getInstance().getDescription().getVersion(); - @CfgComment("-------------------------") - - @CfgComment(" ") - @CfgComment("(<1.13) Legacy Materials: https://helpch.at/docs/1.8/org/bukkit/Material.html") - @CfgComment("(>1.12) Post-Legacy Materials: https://helpch.at/docs/1.13/org/bukkit/Material.html") - @CfgComment(" ") - @CfgComment("floor-data and border-data are only for legacy versions * <1.13 *") - @CfgName("plot-sizes") - public PlotSizes plotSizes = new PlotSizes(new Small(PlotSize.SMALL), new Medium(PlotSize.MEDIUM), new Large(PlotSize.LARGE)); - - public static class PlotSizes { - - public Small SMALL; - - public Medium MEDIUM; - - public Large LARGE; - - public PlotSizes(Small small, Medium medium, Large large) { - this.SMALL = small; - this.MEDIUM = medium; - this.LARGE = large; - } - - private PlotSizes() {} - - } - - public static class Small extends Size { - - public Small(PlotSize plotSize) { - super(plotSize.getFloorMaterial(), plotSize.getFloorData(), plotSize.getBorderMaterial(), plotSize.getBorderData(), plotSize.getLength(), plotSize.getWidth(), plotSize.getHeight()); - } - - private Small() {} - - } - - public static class Medium extends Size { - - public Medium(PlotSize plotSize) { - super(plotSize.getFloorMaterial(), plotSize.getFloorData(), plotSize.getBorderMaterial(), plotSize.getBorderData(), plotSize.getLength(), plotSize.getWidth(), plotSize.getHeight()); - - } - - private Medium() {} - - } - - public static class Large extends Size { - - public Large(PlotSize plotSize) { - super(plotSize.getFloorMaterial(), plotSize.getFloorData(), plotSize.getBorderMaterial(), plotSize.getBorderData(), plotSize.getLength(), plotSize.getWidth(), plotSize.getHeight()); - } - - private Large() {} - - } - - public abstract static class Size { - - @CfgName("floor-material") - public String floorMaterial; - @CfgName("floor-data") - public byte floorData; - @CfgName("border-material") - public String borderMaterial; - @CfgName("border-data") - public byte borderData; - public int length; - public int width; - public int height; - - public Size(Material floorMaterial, byte floorData, Material borderMaterial, byte borderData, int length, int width, int height) { - this.floorMaterial = floorMaterial.name(); - this.floorData = floorData; - this.borderMaterial = borderMaterial.name(); - this.borderData = borderData; - this.length = length; - this.width = width; - this.height = height; - } - - private Size() {} - - } - -} diff --git a/src/main/java/me/plytki/virtualrealty/sql/SQL.java b/src/main/java/me/plytki/virtualrealty/sql/SQL.java index 43f2655..c0dab7b 100644 --- a/src/main/java/me/plytki/virtualrealty/sql/SQL.java +++ b/src/main/java/me/plytki/virtualrealty/sql/SQL.java @@ -1,7 +1,7 @@ package me.plytki.virtualrealty.sql; import me.plytki.virtualrealty.VirtualRealty; -import me.plytki.virtualrealty.loaders.PluginConfiguration; +import me.plytki.virtualrealty.configs.PluginConfiguration; import java.sql.Connection; import java.sql.DriverManager; diff --git a/src/main/java/me/plytki/virtualrealty/utils/ConfigUtil.java b/src/main/java/me/plytki/virtualrealty/utils/ConfigUtil.java deleted file mode 100644 index c9e41bb..0000000 --- a/src/main/java/me/plytki/virtualrealty/utils/ConfigUtil.java +++ /dev/null @@ -1,51 +0,0 @@ -package me.plytki.virtualrealty.utils; - -import org.apache.commons.lang.Validate; -import org.diorite.cfg.system.Template; -import org.diorite.cfg.system.TemplateCreator; - -import java.io.File; -import java.io.IOException; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; - -public class ConfigUtil { - - public static T loadConfig(File file, Class implementationFile) { - Template template = TemplateCreator.getTemplate(implementationFile); - Constructor implementationFileConstructor = (Constructor) Reflections.getConstructor(implementationFile); - T config; - if (!file.exists()) { - try { - try { - config = template.fillDefaults(implementationFileConstructor.newInstance()); - } catch (final InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - throw new RuntimeException("Couldn't get access to " + implementationFile.getName() + " constructor", e); - } - Validate.isTrue(file.createNewFile(), "Couldn't create " + file.getAbsolutePath() + " config file"); - } catch (final IOException e) { - throw new RuntimeException("IO exception when creating config file: " + file.getAbsolutePath(), e); - } - } else { - try { - try { - config = template.load(file); - if (config == null) { - config = template.fillDefaults(implementationFileConstructor.newInstance()); - } - } catch (final IOException | IllegalArgumentException | InvocationTargetException e) { - throw new RuntimeException("IO exception when loading config file: " + file.getAbsolutePath(), e); - } - } catch (final InstantiationException | IllegalAccessException e) { - throw new RuntimeException("Couldn't get access to " + implementationFile.getName() + " constructor", e); - } - } - try { - template.dump(file, config, false); - } catch (final IOException e) { - throw new RuntimeException("Can't dump configuration file!", e); - } - return config; - } - -} diff --git a/src/main/java/me/plytki/virtualrealty/utils/ConfigurationFactory.java b/src/main/java/me/plytki/virtualrealty/utils/ConfigurationFactory.java new file mode 100644 index 0000000..3239cd9 --- /dev/null +++ b/src/main/java/me/plytki/virtualrealty/utils/ConfigurationFactory.java @@ -0,0 +1,37 @@ +package me.plytki.virtualrealty.utils; + +import eu.okaeri.configs.ConfigManager; +import eu.okaeri.configs.serdes.commons.SerdesCommons; +import eu.okaeri.configs.validator.okaeri.OkaeriValidator; +import eu.okaeri.configs.yaml.bukkit.YamlBukkitConfigurer; +import me.plytki.virtualrealty.configs.PluginConfiguration; +import me.plytki.virtualrealty.configs.SizesConfiguration; + +import java.io.File; + +public class ConfigurationFactory { + + public ConfigurationFactory() { + + } + + public PluginConfiguration createPluginConfiguration(File pluginConfigurationFile) { + return ConfigManager.create(PluginConfiguration.class, (it) -> { + it.withConfigurer(new OkaeriValidator(new YamlBukkitConfigurer(), true), new SerdesCommons()); + it.withBindFile(pluginConfigurationFile); + it.saveDefaults(); + it.load(true); + }); + } + + public SizesConfiguration createSizesConfiguration(File sizesConfigurationFile) { + return ConfigManager.create(SizesConfiguration.class, (it) -> { + it.withConfigurer(new OkaeriValidator(new YamlBukkitConfigurer(), true), new SerdesCommons()); + it.withBindFile(sizesConfigurationFile); + it.saveDefaults(); + it.load(true); + }); + } + + +} diff --git a/src/main/java/me/plytki/virtualrealty/utils/multiversion/Chat.java b/src/main/java/me/plytki/virtualrealty/utils/multiversion/Chat.java index d685c98..c8ecdc2 100644 --- a/src/main/java/me/plytki/virtualrealty/utils/multiversion/Chat.java +++ b/src/main/java/me/plytki/virtualrealty/utils/multiversion/Chat.java @@ -10,12 +10,19 @@ public class Chat { - private final BaseComponent text; + private BaseComponent text; public Chat(BaseComponent text) { this.text = text; } + public Chat(BaseComponent... text) { + this.text = new TextComponent(); + for (BaseComponent baseComponent : text) { + this.text.addExtra(baseComponent); + } + } + public Chat(String text) { this.text = new TextComponent(text); } diff --git a/src/main/java/me/plytki/virtualrealty/utils/multiversion/VMaterial.java b/src/main/java/me/plytki/virtualrealty/utils/multiversion/VMaterial.java index b5eace8..3e651a4 100644 --- a/src/main/java/me/plytki/virtualrealty/utils/multiversion/VMaterial.java +++ b/src/main/java/me/plytki/virtualrealty/utils/multiversion/VMaterial.java @@ -1,6 +1,7 @@ package me.plytki.virtualrealty.utils.multiversion; import me.plytki.virtualrealty.VirtualRealty; +import me.plytki.virtualrealty.exceptions.MaterialMatchException; import org.bukkit.Material; import java.lang.reflect.InvocationTargetException; @@ -8,7 +9,7 @@ public class VMaterial { - public static Material getMaterial(int materialID) { + public static Material getMaterial(int materialID) throws MaterialMatchException { if (VirtualRealty.isLegacy) { try { Method m = Material.class.getDeclaredMethod("getMaterial", int.class); @@ -26,6 +27,21 @@ public static Material getMaterial(int materialID) { counter++; } } + throw new MaterialMatchException("Couldn't parse material: " + materialID); + } + + public static Material catchMaterial(String material) throws MaterialMatchException { + try { + Method m = Material.class.getDeclaredMethod("getMaterial", String.class); + m.setAccessible(true); + Material mat = (Material) m.invoke(Material.class, material); + if (mat == null) { + throw new MaterialMatchException("Couldn't parse material: \"" + material + "\""); + } + return mat; + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + e.printStackTrace(); + } return null; } @@ -40,4 +56,5 @@ public static Material getMaterial(String material) { return null; } + }