From 8e5e33eec28b9b5f9ba04f21a21163736e56b771 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Tue, 7 Sep 2021 14:02:48 +0100 Subject: [PATCH] Fix #3127 --- .../plotsquared/core/command/Download.java | 2 +- .../core/util/ReflectionUtils.java | 13 --------- .../com/plotsquared/core/util/WorldUtil.java | 28 +++++++++++++------ Core/src/main/resources/lang/messages_en.json | 1 + 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/command/Download.java b/Core/src/main/java/com/plotsquared/core/command/Download.java index 8ba03f312..f81fd244b 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Download.java +++ b/Core/src/main/java/com/plotsquared/core/command/Download.java @@ -139,7 +139,7 @@ public class Download extends SubCommand { ); return; } - player.sendMessage(TranslatableCaption.of("web.generation_link_success"), Template.of("url", url.toString())); + player.sendMessage(TranslatableCaption.of("web.generation_link_success_legacy_world"), Template.of("url", url.toString())); } }); } else { diff --git a/Core/src/main/java/com/plotsquared/core/util/ReflectionUtils.java b/Core/src/main/java/com/plotsquared/core/util/ReflectionUtils.java index 7eb24d413..b4868c83f 100644 --- a/Core/src/main/java/com/plotsquared/core/util/ReflectionUtils.java +++ b/Core/src/main/java/com/plotsquared/core/util/ReflectionUtils.java @@ -28,7 +28,6 @@ package com.plotsquared.core.util; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.util.Map; /** * @author DPOH-VAR @@ -52,18 +51,6 @@ public class ReflectionUtils { } } - public static Map getMap(Map map) { - try { - Class clazz = map.getClass(); - Field m = clazz.getDeclaredField("m"); - m.setAccessible(true); - return (Map) m.get(map); - } catch (Throwable e) { - e.printStackTrace(); - return map; - } - } - public static Class getClass(String name) { try { return Class.forName(name); diff --git a/Core/src/main/java/com/plotsquared/core/util/WorldUtil.java b/Core/src/main/java/com/plotsquared/core/util/WorldUtil.java index e06aaff89..d0c3d1652 100644 --- a/Core/src/main/java/com/plotsquared/core/util/WorldUtil.java +++ b/Core/src/main/java/com/plotsquared/core/util/WorldUtil.java @@ -55,6 +55,7 @@ import java.io.IOException; import java.io.OutputStream; import java.net.URL; import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -245,7 +246,7 @@ public abstract class WorldUtil { final @Nullable String file, final @NonNull RunnableVal whenDone ) { - plot.getHome(home -> SchematicHandler.upload(uuid, file, "zip", new RunnableVal() { + plot.getHome(home -> SchematicHandler.upload(uuid, file, "zip", new RunnableVal<>() { @Override public void run(OutputStream output) { try (final ZipOutputStream zos = new ZipOutputStream(output)) { @@ -255,16 +256,23 @@ public abstract class WorldUtil { ZipEntry ze = new ZipEntry("world" + File.separator + dat.getName()); zos.putNextEntry(ze); try (NBTInputStream nis = new NBTInputStream(new GZIPInputStream(new FileInputStream(dat)))) { - CompoundTag tag = (CompoundTag) nis.readNamedTag().getTag(); - CompoundTag data = (CompoundTag) tag.getValue().get("Data"); - Map map = ReflectionUtils.getMap(data.getValue()); - map.put("SpawnX", new IntTag(home.getX())); - map.put("SpawnY", new IntTag(home.getY())); - map.put("SpawnZ", new IntTag(home.getZ())); + Map tag = ((CompoundTag) nis.readNamedTag().getTag()).getValue(); + Map newMap = new HashMap<>(); + for (Map.Entry entry : tag.entrySet()) { + if (!entry.getKey().equals("Data")) { + newMap.put(entry.getKey(), entry.getValue()); + continue; + } + Map data = new HashMap<>(((CompoundTag) entry.getValue()).getValue()); + data.put("SpawnX", new IntTag(home.getX())); + data.put("SpawnY", new IntTag(home.getY())); + data.put("SpawnZ", new IntTag(home.getZ())); + newMap.put("Data", new CompoundTag(data)); + } try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { try (NBTOutputStream out = new NBTOutputStream(new GZIPOutputStream(baos, true))) { //TODO Find what this should be called - out.writeNamedTag("Schematic????", tag); + out.writeNamedTag("Schematic????", new CompoundTag(newMap)); } zos.write(baos.toByteArray()); } @@ -272,6 +280,7 @@ public abstract class WorldUtil { } setSpawn(spawn); byte[] buffer = new byte[1024]; + Set added = new HashSet<>(); for (Plot current : plot.getConnectedPlots()) { Location bot = current.getBottomAbs(); Location top = current.getTopAbs(); @@ -281,13 +290,14 @@ public abstract class WorldUtil { int trz = top.getZ() >> 9; Set files = getChunkChunks(bot.getWorldName()); for (BlockVector2 mca : files) { - if (mca.getX() >= brx && mca.getX() <= trx && mca.getZ() >= brz && mca.getZ() <= trz) { + if (mca.getX() >= brx && mca.getX() <= trx && mca.getZ() >= brz && mca.getZ() <= trz && !added.contains(mca)) { final File file = getMcr(plot.getWorldName(), mca.getX(), mca.getZ()); if (file != null) { //final String name = "r." + (x - cx) + "." + (z - cz) + ".mca"; String name = file.getName(); final ZipEntry ze = new ZipEntry("world" + File.separator + "region" + File.separator + name); zos.putNextEntry(ze); + added.add(mca); try (FileInputStream in = new FileInputStream(file)) { int len; while ((len = in.read(buffer)) > 0) { diff --git a/Core/src/main/resources/lang/messages_en.json b/Core/src/main/resources/lang/messages_en.json index 8b806d43a..83be145cd 100644 --- a/Core/src/main/resources/lang/messages_en.json +++ b/Core/src/main/resources/lang/messages_en.json @@ -22,6 +22,7 @@ "web.plot_merged": "This plot is merged and therefore cannot be downloaded", "web.generating_link_failed": "Failed to generate download link for plot !", "web.generation_link_success": "Download: > \n Deletion: >\nAttention: Opening the deletion link will delete the file immediately.", + "web.generation_link_success_legacy_world": "Download: >", "web.save_failed": "Failed to save.", "web.load_null": "Please use to get a list of schematics.", "web.load_failed": "Failed to load schematic.",