Finished bigger size to smaller, other has bugs

This commit is contained in:
Norbi Peti 2017-08-17 01:46:20 +02:00
parent ca6ab8e63d
commit 55743a9127

View file

@ -1,28 +1,24 @@
package buttondevteam.townyplotresizer; package buttondevteam.townyplotresizer;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Locale;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration; 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 { public class PlotResizer {
@SuppressWarnings("unchecked") private static Stream<String> conflines;
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
System.out.println("Towny plot resizer - https://github.com/TBMCPlugins/TownyPlotResizer"); System.out.println("Towny plot resizer - https://github.com/TBMCPlugins/TownyPlotResizer");
System.out.println("Make sure you have a backup of Towny data"); System.out.println("Make sure you have a backup of Towny data");
@ -62,62 +58,53 @@ public class PlotResizer {
float ratio = oldsize / newsize; float ratio = oldsize / newsize;
float ratio2D = ratio == 1 ? ratio : ratio > 1 ? ratio * 2 : ratio / 2; float ratio2D = ratio == 1 ? ratio : ratio > 1 ? ratio * 2 : ratio / 2;
float priceratio = 1 / ratio2D; float priceratio = 1 / ratio2D;
List<String> conflines = Files.readAllLines(file.toPath()); conflines = Files.readAllLines(file.toPath()).stream();
conflines.forEach( conflines = conflines
s -> s = s.replace("town_block_size: '" + oldsize + "'", "town_block_size: '" + newsize + "'")); .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 = PlotResizer::setValue; - Works with generics
SetValueFunction func = (path, parser, isfloat, isprice) -> { SetValueFunction func = (path, parser, isfloat, isprice) -> {
double oldval = parser == null ? isfloat ? config.getDouble(path) : config.getInt(path) 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[] spl = path.split("\\.");
String pathend = spl[spl.length - 1]; 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 ? "'" : "") pathend + ": " + (parser != null ? "'" : "")
+ String.format(isfloat ? "%f" : "%d", oldval * (isprice ? priceratio : ratio2D)) + String.format(Locale.ROOT, isfloat ? "%s" : "%.0f", oldval) + (parser != null ? "'" : ""),
+ (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); func.setValue("town.town_block_ratio", Integer::parseInt, false, false);
System.out.println("Set block ratio to " + town.getString("town_block_ratio")); final String path = "townBlockLimit";
config.set("levels.town_level", Arrays.asList(config.getMapList("levels.town_level").stream().map(map -> { config.getMapList("levels.town_level").stream()
((Map<String, Object>) map).put("townBlockLimit", ((int) map.get("townBlockLimit")) * ratio2D); .sorted((m1, m2) -> Integer.compare((int) m2.get(path), (int) m1.get(path))).forEach(map -> {
return map; // TODO: Store values in array, replace in same order int oldval = (int) map.get(path);
}).toArray(Map[]::new))); conflines = conflines.map(s -> s.replace(path + ": " + oldval,
System.out.println("Set town levels"); path + ": " + String.format("%.0f", oldval * ratio2D)));
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")); System.out.println("Set town level block limits");
town.set("min_plot_distance_from_town_plot", func.setValue("town.max_plots_per_resident", Integer::parseInt, false, false);
Integer.parseInt(town.getString("min_plot_distance_from_town_plot")) * ratio2D); func.setValue("town.min_plot_distance_from_town_plot", Integer::parseInt, false, false);
System.out.println("Set min plot distance to " + town.getString("min_plot_distance_from_town_plot")); func.setValue("town.min_distance_from_town_homeblock", Integer::parseInt, false, false);
town.set("min_distance_from_town_homeblock", func.setValue("town.max_distance_between_homeblocks", Integer::parseInt, false, false);
Integer.parseInt(town.getString("min_distance_from_town_homeblock")) * ratio2D); func.setValue("war.event.eco.wartime_town_block_loss_price", Float::parseFloat, true, true);
System.out.println("Set min distance from homeblock to " + town.getString("min_distance_from_town_homeblock")); func.setValue("economy.new_expand.price_claim_townblock", Float::parseFloat, true, true);
town.set("max_distance_between_homeblocks", func.setValue("economy.new_expand.price_purchased_bonus_townblock", Float::parseFloat, true, true);
Integer.parseInt(town.getString("max_distance_between_homeblocks")) * ratio2D); func.setValue("economy.new_expand.price_purchased_bonus_townblock_increase", Float::parseFloat, true, true);
System.out.println("Set max distance from homeblock to " + town.getString("max_distance_between_homeblocks")); func.setValue("war.economy.townblock_won", Integer::parseInt, false, true);
config.set("war.event.eco.wartime_town_block_loss_price", // Files.write(file.toPath().resolveSibling("config_test.yml"), conflines.collect(Collectors.toList()), StandardOpenOption.CREATE);
Float.parseFloat(config.getString("war.event.eco.wartime_town_block_loss_price")) * priceratio); Files.write(file.toPath(), conflines.collect(Collectors.toList()), StandardOpenOption.CREATE);
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
System.out.println("Saved Towny configuration"); System.out.println("Saved Towny configuration");
System.out.println(); System.out.println(); // TODO: TownblocklimitBONUS
System.out.println("Setting town plots..."); System.out.println("Setting town plots...");
file = new File(file.getParentFile().getParentFile(), "data/townblocks"); file = new File(file.getParentFile().getParentFile(), "data/townblocks");
List<String> townblocks = new ArrayList<>();
for (File f : file.listFiles()) { for (File f : file.listFiles()) {
if (!f.isDirectory()) if (!f.isDirectory())
continue; continue;
@ -130,18 +117,40 @@ public class PlotResizer {
int x = Integer.parseInt(coords[0]), y = Integer.parseInt(coords[1]), int x = Integer.parseInt(coords[0]), y = Integer.parseInt(coords[1]),
size = Integer.parseInt(coords[2].split("\\.")[0]); size = Integer.parseInt(coords[2].split("\\.")[0]);
if (size == oldsize) { if (size == oldsize) {
for (int i = 0; i < ratio; i++) for (int i = 0; (float) i < ratio; i++) {
for (int j = 0; j < ratio; j++) for (int j = 0; (float) j < ratio; j++) {
Files.copy( Files.copy(
ff.toPath(), ff.toPath().resolveSibling((x * (int) ratio + i) + "_" ff.toPath(), ff.toPath().resolveSibling((x * (int) ratio + i) + "_"
+ (y * (int) ratio + j) + "_" + (int) newsize + ".data"), + (y * (int) ratio + j) + "_" + (int) newsize + ".data"),
StandardCopyOption.REPLACE_EXISTING); StandardCopyOption.REPLACE_EXISTING);
townblocks.add(f.getName() + "," + (x * (int) ratio + i) + "," + (y * (int) ratio + j));
}
}
ff.delete(); ff.delete();
C++; C++;
} }
} }
System.out.println("Finished with " + f.getName() + ", renaming " + C + " blocks"); 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<String> 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 public static boolean isInteger(String s, int radix) { // https://stackoverflow.com/a/5439547/2703239