Add configuration conversion and fix BukkitLegacyMappings

This commit is contained in:
Sauilitired 2018-12-23 19:43:36 +01:00
parent 165cac8b13
commit 02937cc543
No known key found for this signature in database
GPG Key ID: C0207FF7EA146678
9 changed files with 103 additions and 34 deletions

View File

@ -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();
}

View File

@ -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<Integer, PlotBlock> LEGACY_ID_TO_STRING_PLOT_BLOCK = new HashMap<>();
// private static final Map<Integer, PlotBlock> LEGACY_ID_TO_STRING_PLOT_BLOCK = new HashMap<>();
private static final Map<IdDataPair, PlotBlock> LEGACY_ID_AND_DATA_TO_STRING_PLOT_BLOCK =
new HashMap<>();
private static final Map<String, PlotBlock> NEW_STRING_TO_LEGACY_PLOT_BLOCK = new HashMap<>();
@ -685,13 +685,15 @@ public final class BukkitLegacyMappings extends LegacyMappings {
private void addAll(@NonNull final Collection<LegacyBlock> 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));
}

View File

@ -125,12 +125,10 @@ public class BukkitLocalQueue<T> extends BasicLocalBlockQueue<T> {
} 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<T> extends BasicLocalBlockQueue<T> {
}
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());
}

View File

@ -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.
*

View File

@ -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<Plot> plots = new HashSet<>();
foreachPlotRaw(new RunnableVal<Plot>() {
@Override public void run(Plot value) {
plots.add(value);
}
});
try {
foreachPlotRaw(new RunnableVal<Plot>() {
@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<br>
* - Translation: PlotSquared.use_THIS.yml, style.yml<br>
*/
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;
}
/**

View File

@ -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());
}

View File

@ -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<String, String> replacements = new HashMap<>();
@ -1048,5 +1063,4 @@ public enum C {
} else {
caller.sendMessage(msg);
}
}
}
}}

View File

@ -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() {

View File

@ -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);