From 02937cc543572ab9f598ff9aaaa545565436d809 Mon Sep 17 00:00:00 2001 From: Sauilitired Date: Sun, 23 Dec 2018 19:43:36 +0100 Subject: [PATCH] Add configuration conversion and fix BukkitLegacyMappings --- .../plotsquared/bukkit/BukkitMain.java | 4 ++ .../bukkit/util/BukkitLegacyMappings.java | 21 +++----- .../bukkit/util/block/BukkitLocalQueue.java | 8 ++- .../plotsquared/plot/IPlotMain.java | 5 ++ .../plotsquared/plot/PlotSquared.java | 53 +++++++++++++++---- .../plotsquared/plot/commands/Setup.java | 2 +- .../plotsquared/plot/config/C.java | 18 ++++++- .../plot/util/LegacyConverter.java | 24 ++++++++- .../plotsquared/plot/util/LegacyMappings.java | 2 - 9 files changed, 103 insertions(+), 34 deletions(-) diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/BukkitMain.java index f0190b125..ed5884990 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/BukkitMain.java @@ -254,6 +254,10 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain } } + @Override public void shutdown() { + this.getServer().getPluginManager().disablePlugin(this); + } + @Override public void disable() { onDisable(); } diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitLegacyMappings.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitLegacyMappings.java index 3c644cc3f..ae58998d8 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitLegacyMappings.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitLegacyMappings.java @@ -663,7 +663,7 @@ public final class BukkitLegacyMappings extends LegacyMappings { new LegacyBlock(2266, "record_11", "music_disc_11"), new LegacyBlock(2267, "record_12", "music_disc_wait")}; - private static final Map LEGACY_ID_TO_STRING_PLOT_BLOCK = new HashMap<>(); + // private static final Map LEGACY_ID_TO_STRING_PLOT_BLOCK = new HashMap<>(); private static final Map LEGACY_ID_AND_DATA_TO_STRING_PLOT_BLOCK = new HashMap<>(); private static final Map NEW_STRING_TO_LEGACY_PLOT_BLOCK = new HashMap<>(); @@ -685,13 +685,15 @@ public final class BukkitLegacyMappings extends LegacyMappings { private void addAll(@NonNull final Collection blocks) { for (final LegacyBlock legacyBlock : blocks) { - LEGACY_ID_TO_STRING_PLOT_BLOCK - .put(legacyBlock.getNumericalId(), legacyBlock.toStringPlotBlock()); - if (legacyBlock.getDataValue() != 0) { + // LEGACY_ID_TO_STRING_PLOT_BLOCK + // .put(legacyBlock.getNumericalId(), legacyBlock.toStringPlotBlock()); + /*if (legacyBlock.getDataValue() != 0) { LEGACY_ID_AND_DATA_TO_STRING_PLOT_BLOCK .put(new IdDataPair(legacyBlock.getNumericalId(), legacyBlock.getDataValue()), legacyBlock.toStringPlotBlock()); - } + } */ + LEGACY_ID_AND_DATA_TO_STRING_PLOT_BLOCK.put(new IdDataPair(legacyBlock.getNumericalId(), + legacyBlock.getDataValue()), legacyBlock.toStringPlotBlock()); NEW_STRING_TO_LEGACY_PLOT_BLOCK .put(legacyBlock.getLegacyName(), legacyBlock.toStringPlotBlock()); OLD_STRING_TO_STRING_PLOT_BLOCK @@ -758,7 +760,7 @@ public final class BukkitLegacyMappings extends LegacyMappings { final int data = Integer.parseInt(parts[1]); return fromLegacyToString(id, data); } else { - return fromLegacyToString(Integer.parseInt(workingString)); + return fromLegacyToString(Integer.parseInt(workingString), 0); } } catch (final Throwable exception) { return null; @@ -766,14 +768,7 @@ public final class BukkitLegacyMappings extends LegacyMappings { } } - public PlotBlock fromLegacyToString(final int id) { - return LEGACY_ID_TO_STRING_PLOT_BLOCK.get(id); - } - public PlotBlock fromLegacyToString(final int id, final int data) { - if (data == 0) { - return fromLegacyToString(id); - } return LEGACY_ID_AND_DATA_TO_STRING_PLOT_BLOCK.get(new IdDataPair(id, data)); } diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/block/BukkitLocalQueue.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/block/BukkitLocalQueue.java index 4022ed188..fe7db6474 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/block/BukkitLocalQueue.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/block/BukkitLocalQueue.java @@ -125,12 +125,10 @@ public class BukkitLocalQueue extends BasicLocalBlockQueue { } else { final LegacyPlotBlock legacyPlotBlock = (LegacyPlotBlock) plotBlock; material = PlotSquared.get().IMP.getLegacyMappings() - .fromLegacyToString(legacyPlotBlock.getId()).to(Material.class); + .fromLegacyToString(legacyPlotBlock.getId(), legacyPlotBlock.getData()).to(Material.class); if (material == null) { throw new IllegalStateException(String - .format("Could not find material that matches %s", - PlotSquared.get().IMP.getLegacyMappings() - .fromLegacyToString(legacyPlotBlock.getId()))); + .format("Could not find material that matches %s", legacyPlotBlock.toString())); } } block.setType(material, false); @@ -142,7 +140,7 @@ public class BukkitLocalQueue extends BasicLocalBlockQueue { } final LegacyPlotBlock legacyPlotBlock = (LegacyPlotBlock) plotBlock; return Material.getMaterial(PlotSquared.get().IMP.getLegacyMappings() - .fromLegacyToString(((LegacyPlotBlock) plotBlock).id).toString()) == block.getType() + .fromLegacyToString(((LegacyPlotBlock) plotBlock).id, ((LegacyPlotBlock) plotBlock).data).toString()) == block.getType() && (legacyPlotBlock.id == 0 || legacyPlotBlock.data == block.getData()); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/IPlotMain.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/IPlotMain.java index a1ea8e725..aeac6d560 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/IPlotMain.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/IPlotMain.java @@ -52,6 +52,11 @@ public interface IPlotMain extends ILogger { */ void disable(); + /** + * Completely shut down the plugin + */ + void shutdown(); + /** * Get the version of the PlotSquared being used. * diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java index ab83748bf..56237b88c 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java @@ -119,7 +119,13 @@ import java.util.zip.ZipInputStream; } } TaskManager.IMP = this.IMP.getTaskManager(); - setupConfigs(); + + // World Util. Has to be done before config files are loaded + WorldUtil.IMP = this.IMP.initWorldUtil(); + + if (!setupConfigs()) { + return; + } this.translationFile = MainUtil.getFile(this.IMP.getDirectory(), Settings.Paths.TRANSLATIONS + File.separator + IMP.getPluginName() + ".use_THIS.yml"); @@ -176,8 +182,6 @@ import java.util.zip.ZipInputStream; InventoryUtil.manager = this.IMP.initInventoryUtil(); // create setup util class SetupUtils.manager = this.IMP.initSetupUtils(); - // World Util - WorldUtil.IMP = this.IMP.initWorldUtil(); // Set block GlobalBlockQueue.IMP = new GlobalBlockQueue(IMP.initBlockQueue(), 1); GlobalBlockQueue.IMP.runTask(); @@ -1543,11 +1547,13 @@ import java.util.zip.ZipInputStream; try { // Validate that all data in the db is correct final HashSet plots = new HashSet<>(); - foreachPlotRaw(new RunnableVal() { - @Override public void run(Plot value) { - plots.add(value); - } - }); + try { + foreachPlotRaw(new RunnableVal() { + @Override public void run(Plot value) { + plots.add(value); + } + }); + } catch (final Exception ignored) {} DBFunc.validatePlots(plots); // Close the connection @@ -1661,7 +1667,7 @@ import java.util.zip.ZipInputStream; * - Storage: storage.yml
* - Translation: PlotSquared.use_THIS.yml, style.yml
*/ - public void setupConfigs() { + public boolean setupConfigs() { File folder = new File(this.IMP.getDirectory(), "config"); if (!folder.exists() && !folder.mkdirs()) { PlotSquared.log(C.PREFIX @@ -1674,6 +1680,34 @@ import java.util.zip.ZipInputStream; "Could not create the worlds file, please create \"worlds.yml\" manually."); } this.worlds = YamlConfiguration.loadConfiguration(this.worldsFile); + + if (this.worlds.contains("worlds")) { + if (!this.worlds.contains("configuration_version") || !this.worlds + .getString("configuration_version").equalsIgnoreCase(LegacyConverter.CONFIGURATION_VERSION)) { + // Conversion needed + log(C.LEGACY_CONFIG_FOUND.s()); + try { + com.google.common.io.Files + .copy(this.worldsFile, new File(folder, "worlds.yml.old")); + log(C.LEGACY_CONFIG_BACKUP.s()); + final ConfigurationSection worlds = this.worlds.getConfigurationSection("worlds"); + final LegacyConverter converter = new LegacyConverter(worlds); + converter.convert(); + this.worlds.set("configuration_version", LegacyConverter.CONFIGURATION_VERSION); + this.worlds.set("worlds", worlds); // Redundant, but hey... ¯\_(ツ)_/¯ + this.worlds.save(this.worldsFile); + log(C.LEGACY_CONFIG_DONE.s()); + } catch (final Exception e) { + log(C.LEGACY_CONFIG_CONVERSION_FAILED.s()); + e.printStackTrace(); + } + // Disable plugin + this.IMP.shutdown(); + return false; + } + } else { + this.worlds.set("configuration_version", LegacyConverter.CONFIGURATION_VERSION); + } } catch (IOException ignored) { PlotSquared.log("Failed to save settings.yml"); } @@ -1734,6 +1768,7 @@ import java.util.zip.ZipInputStream; PlotSquared.log("Configuration file saving failed"); e.printStackTrace(); } + return true; } /** diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Setup.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Setup.java index e866a0bc4..cfbab8ed7 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Setup.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Setup.java @@ -247,7 +247,7 @@ import java.util.Map.Entry; boolean valid = false; try { - step.isValid(args[0]); + valid = step.isValid(args[0]); } catch (final Configuration.UnsafeBlockException e) { C.NOT_ALLOWED_BLOCK.send(player, e.getUnsafeBlock().toString()); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/C.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/C.java index bada78ad2..8ef0dcee4 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/C.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/C.java @@ -851,6 +851,21 @@ public enum C { * Custom */ + /** + * Legacy Configuration Conversion + */ + LEGACY_CONFIG_FOUND("A legacy configuration file was detected. Conversion will be attempted.", + "LegacyConfig"), LEGACY_CONFIG_BACKUP( + "A copy of worlds.yml $1have been saved in the file worlds.yml.old$1.", + "LegacyConfig"), LEGACY_CONFIG_REPLACED("> %s has been replaced with %s", + "LegacyConfig"), LEGACY_CONFIG_DONE( + "The conversion has finished. PlotSquared will now be disabled and the new configuration file will" + + " be used at next startup. Please review the new worlds.yml file. " + + "Please note that schematics will not be converted, as we are now using WorldEdit to handle schematics. " + + "You need to re-generate the schematics.", + "LegacyConfig"), LEGACY_CONFIG_CONVERSION_FAILED( + "Failed to convert the legacy configuration file. See stack trace for information.", "LegacyConfig"), + CUSTOM_STRING("-", "-"); public static final HashMap replacements = new HashMap<>(); @@ -1048,5 +1063,4 @@ public enum C { } else { caller.sendMessage(msg); } - } -} + }} diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/LegacyConverter.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/LegacyConverter.java index b11e668f0..cad110b12 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/LegacyConverter.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/LegacyConverter.java @@ -1,11 +1,16 @@ package com.github.intellectualsites.plotsquared.plot.util; import com.github.intellectualsites.plotsquared.configuration.ConfigurationSection; +import com.github.intellectualsites.plotsquared.plot.PlotSquared; +import com.github.intellectualsites.plotsquared.plot.config.C; import com.github.intellectualsites.plotsquared.plot.object.BlockBucket; import com.github.intellectualsites.plotsquared.plot.object.PlotBlock; import lombok.NonNull; -import java.util.*; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * Converts legacy configurations into the new (BlockBucket) format @@ -13,6 +18,8 @@ import java.util.*; @SuppressWarnings("unused") public final class LegacyConverter { + public static final String CONFIGURATION_VERSION = "post_flattening"; + private enum ConfigurationType { BLOCK, BLOCK_LIST } @@ -40,7 +47,7 @@ public final class LegacyConverter { } private void setString(@NonNull final ConfigurationSection section, @NonNull final String string, @NonNull final BlockBucket blocks) { - if (!this.configuration.contains(string)) { + if (!section.contains(string)) { throw new IllegalArgumentException(String.format("No such key: %s", string)); } section.set(string, blocks.toString()); @@ -85,6 +92,7 @@ public final class LegacyConverter { @NonNull final String block) { final BlockBucket bucket = this.blockToBucket(block); this.setString(section, key, bucket); + PlotSquared.log(C.LEGACY_CONFIG_REPLACED.f(block, bucket.toString())); } private void convertBlockList(@NonNull final ConfigurationSection section, @NonNull final String key, @@ -92,6 +100,18 @@ public final class LegacyConverter { final PlotBlock[] blocks = this.splitBlockList(blockList); final BlockBucket bucket = this.blockListToBucket(blocks); this.setString(section, key, bucket); + PlotSquared.log(C.LEGACY_CONFIG_REPLACED.f(plotBlockArrayString(blocks), bucket.toString())); + } + + private String plotBlockArrayString(@NonNull final PlotBlock[] blocks) { + final StringBuilder builder = new StringBuilder(); + for (int i = 0; i < blocks.length; i++) { + builder.append(blocks[i].toString()); + if ((i + 1) < blocks.length) { + builder.append(","); + } + } + return builder.toString(); } public void convert() { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/LegacyMappings.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/LegacyMappings.java index f3ba8db34..d79ad6653 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/LegacyMappings.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/LegacyMappings.java @@ -6,8 +6,6 @@ public abstract class LegacyMappings { public abstract PlotBlock fromAny(final String string); - public abstract PlotBlock fromLegacyToString(final int id); - public abstract PlotBlock fromLegacyToString(final int id, final int data); public abstract PlotBlock fromLegacyToString(final String id);