From 55743a91277004aaaede76fcef04eb3a7c3838a9 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 17 Aug 2017 01:46:20 +0200 Subject: [PATCH] Finished bigger size to smaller, other has bugs --- .../townyplotresizer/PlotResizer.java | 127 ++++++++++-------- 1 file changed, 68 insertions(+), 59 deletions(-) diff --git a/src/main/java/buttondevteam/townyplotresizer/PlotResizer.java b/src/main/java/buttondevteam/townyplotresizer/PlotResizer.java index 0367c98..285cd85 100644 --- a/src/main/java/buttondevteam/townyplotresizer/PlotResizer.java +++ b/src/main/java/buttondevteam/townyplotresizer/PlotResizer.java @@ -1,28 +1,24 @@ package buttondevteam.townyplotresizer; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStreamReader; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.StandardCopyOption; +import java.nio.file.StandardOpenOption; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Map; -import java.util.function.BiConsumer; -import java.util.function.Function; +import java.util.Locale; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.util.FileUtil; -import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.reader.StreamReader; - -import com.google.common.collect.Lists; public class PlotResizer { - @SuppressWarnings("unchecked") + private static Stream conflines; + public static void main(String[] args) throws IOException { System.out.println("Towny plot resizer - https://github.com/TBMCPlugins/TownyPlotResizer"); System.out.println("Make sure you have a backup of Towny data"); @@ -62,62 +58,53 @@ public class PlotResizer { float ratio = oldsize / newsize; float ratio2D = ratio == 1 ? ratio : ratio > 1 ? ratio * 2 : ratio / 2; float priceratio = 1 / ratio2D; - List conflines = Files.readAllLines(file.toPath()); - conflines.forEach( - s -> s = s.replace("town_block_size: '" + oldsize + "'", "town_block_size: '" + newsize + "'")); + conflines = Files.readAllLines(file.toPath()).stream(); + conflines = conflines + .map(s -> s.replace("town_block_size: '" + oldsize + "'", "town_block_size: '" + newsize + "'")); + System.out.println("Set block size to " + newsize); // SetValueFunction func = PlotResizer::setValue; - Works with generics SetValueFunction func = (path, parser, isfloat, isprice) -> { double oldval = parser == null ? isfloat ? config.getDouble(path) : config.getInt(path) - : (float) parser.apply(config.getString(path)); + : parser.apply(config.getString(path)).floatValue(); String[] spl = path.split("\\."); String pathend = spl[spl.length - 1]; - conflines.forEach(s -> s = s.replace(pathend + ": " + (parser != null ? "'" + oldval + "'" : oldval), + System.out.println("Replace \"" + pathend + ": " + (parser != null ? "'" : "") + + String.format(Locale.ROOT, isfloat ? "%s" : "%.0f", oldval) + (parser != null ? "'" : "") + "\""); + System.out.println("With \"" + pathend + ": " + (parser != null ? "'" : "") + + String.format(Locale.ROOT, isfloat ? "%s" : "%.0f", oldval * (isprice ? priceratio : ratio2D)) + + (parser != null ? "'" : "") + "\""); + conflines = conflines.map(s -> s.replace( pathend + ": " + (parser != null ? "'" : "") - + String.format(isfloat ? "%f" : "%d", oldval * (isprice ? priceratio : ratio2D)) - + (parser != null ? "'" : ""))); + + String.format(Locale.ROOT, isfloat ? "%s" : "%.0f", oldval) + (parser != null ? "'" : ""), + pathend + ": " + (parser != null ? "'" : "") + String.format(Locale.ROOT, isfloat ? "%s" : "%.0f", + oldval * (isprice ? priceratio : ratio2D)) + (parser != null ? "'" : ""))); + System.out.println("Set " + path + " to " + oldval * (isprice ? priceratio : ratio2D)); }; - System.out.println("Set block size to " + newsize); func.setValue("town.town_block_ratio", Integer::parseInt, false, false); - System.out.println("Set block ratio to " + town.getString("town_block_ratio")); - config.set("levels.town_level", Arrays.asList(config.getMapList("levels.town_level").stream().map(map -> { - ((Map) map).put("townBlockLimit", ((int) map.get("townBlockLimit")) * ratio2D); - return map; // TODO: Store values in array, replace in same order - }).toArray(Map[]::new))); - System.out.println("Set town levels"); - town.set("max_plots_per_resident", Integer.parseInt(town.getString("max_plots_per_resident")) * ratio2D); // TODO - System.out.println("Set max plots per resident to " + town.getString("max_plots_per_resident")); - town.set("min_plot_distance_from_town_plot", - Integer.parseInt(town.getString("min_plot_distance_from_town_plot")) * ratio2D); - System.out.println("Set min plot distance to " + town.getString("min_plot_distance_from_town_plot")); - town.set("min_distance_from_town_homeblock", - Integer.parseInt(town.getString("min_distance_from_town_homeblock")) * ratio2D); - System.out.println("Set min distance from homeblock to " + town.getString("min_distance_from_town_homeblock")); - town.set("max_distance_between_homeblocks", - Integer.parseInt(town.getString("max_distance_between_homeblocks")) * ratio2D); - System.out.println("Set max distance from homeblock to " + town.getString("max_distance_between_homeblocks")); - config.set("war.event.eco.wartime_town_block_loss_price", - Float.parseFloat(config.getString("war.event.eco.wartime_town_block_loss_price")) * priceratio); - System.out.println("Set wartime town block loss price to " - + config.getString("war.event.eco.wartime_town_block_loss_price")); - ConfigurationSection new_expand = config.getConfigurationSection("economy.new_expand"); - new_expand.set("price_claim_townblock", - Float.parseFloat(new_expand.getString("price_claim_townblock")) * priceratio); - System.out.println("Set claim price to " + new_expand.getString("price_claim_townblock")); - new_expand.set("price_purchased_bonus_townblock", - Float.parseFloat(new_expand.getString("price_purchased_bonus_townblock")) * priceratio); - System.out.println("Set bonus claim price to " + new_expand.getString("price_purchased_bonus_townblock")); - new_expand.set("price_purchased_bonus_townblock_increase", - Float.parseFloat(new_expand.getString("price_purchased_bonus_townblock_increase")) * priceratio); - System.out.println("Set bonus claim price increase to " - + new_expand.getString("price_purchased_bonus_townblock_increase")); - config.set("war.economy.townblock_won", - Integer.parseInt(config.getString("war.economy.townblock_won")) * priceratio); - System.out.println("Set town block won price to " + config.getString("war.economy.townblock_won")); - // TODO: Comments get ERASED - Read then replace + final String path = "townBlockLimit"; + config.getMapList("levels.town_level").stream() + .sorted((m1, m2) -> Integer.compare((int) m2.get(path), (int) m1.get(path))).forEach(map -> { + int oldval = (int) map.get(path); + conflines = conflines.map(s -> s.replace(path + ": " + oldval, + path + ": " + String.format("%.0f", oldval * ratio2D))); + }); + System.out.println("Set town level block limits"); + func.setValue("town.max_plots_per_resident", Integer::parseInt, false, false); + func.setValue("town.min_plot_distance_from_town_plot", Integer::parseInt, false, false); + func.setValue("town.min_distance_from_town_homeblock", Integer::parseInt, false, false); + func.setValue("town.max_distance_between_homeblocks", Integer::parseInt, false, false); + func.setValue("war.event.eco.wartime_town_block_loss_price", Float::parseFloat, true, true); + func.setValue("economy.new_expand.price_claim_townblock", Float::parseFloat, true, true); + func.setValue("economy.new_expand.price_purchased_bonus_townblock", Float::parseFloat, true, true); + func.setValue("economy.new_expand.price_purchased_bonus_townblock_increase", Float::parseFloat, true, true); + func.setValue("war.economy.townblock_won", Integer::parseInt, false, true); + // Files.write(file.toPath().resolveSibling("config_test.yml"), conflines.collect(Collectors.toList()), StandardOpenOption.CREATE); + Files.write(file.toPath(), conflines.collect(Collectors.toList()), StandardOpenOption.CREATE); System.out.println("Saved Towny configuration"); - System.out.println(); + System.out.println(); // TODO: TownblocklimitBONUS System.out.println("Setting town plots..."); file = new File(file.getParentFile().getParentFile(), "data/townblocks"); + List townblocks = new ArrayList<>(); for (File f : file.listFiles()) { if (!f.isDirectory()) continue; @@ -130,18 +117,40 @@ public class PlotResizer { int x = Integer.parseInt(coords[0]), y = Integer.parseInt(coords[1]), size = Integer.parseInt(coords[2].split("\\.")[0]); if (size == oldsize) { - for (int i = 0; i < ratio; i++) - for (int j = 0; j < ratio; j++) + for (int i = 0; (float) i < ratio; i++) { + for (int j = 0; (float) j < ratio; j++) { Files.copy( ff.toPath(), ff.toPath().resolveSibling((x * (int) ratio + i) + "_" + (y * (int) ratio + j) + "_" + (int) newsize + ".data"), StandardCopyOption.REPLACE_EXISTING); + townblocks.add(f.getName() + "," + (x * (int) ratio + i) + "," + (y * (int) ratio + j)); + } + } ff.delete(); C++; } } System.out.println("Finished with " + f.getName() + ", renaming " + C + " blocks"); - } // TODO: Homeblock + } // TODO: THIS DOESN'T WORK FOR SMALLER TO BIGGER CHANGE + Files.write(file.toPath().resolveSibling("townblocks.txt"), townblocks, StandardOpenOption.TRUNCATE_EXISTING); + for (File townfile : file.toPath().resolveSibling("towns").toFile().listFiles()) { + if (townfile.isDirectory()) + continue; + Path townpath = townfile.toPath(); + List lines = Files.readAllLines(townpath); + lines.replaceAll(line -> { + if (!line.startsWith("homeBlock=")) + return line; + String[] kv = line.split("="); + if (kv.length > 1 && kv[0].equals("homeBlock")) { + String[] loc = kv[1].split(","); + return "homeBlock=" + loc[0] + "," + (int) (Integer.parseInt(loc[1]) * ratio) + "," + + (int) (Integer.parseInt(loc[2]) * ratio); + } + return line; + }); + Files.write(townpath, lines); + } } public static boolean isInteger(String s, int radix) { // https://stackoverflow.com/a/5439547/2703239