From d97ff9446505c3f69f964cddace16661c76c3f42 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Wed, 20 Sep 2017 00:33:29 +1000 Subject: [PATCH] Various minor Fix download world spawnpoint Fix regenallroads on augmented world Ensure all chunks have unloaded before unloading a world --- .../com/plotsquared/bukkit/BukkitMain.java | 2 +- .../plot/commands/RegenAllRoads.java | 4 +-- .../plot/util/ReflectionUtils.java | 13 +++++++++ .../plot/util/WorldUtil.java | 27 ++++++++++++++----- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java index f6ee29c01..db343dd4d 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -218,7 +218,7 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain List players = plot.getPlayersInPlot(); if (players.isEmpty() && PlotPlayer.wrap(plot.owner) == null) { for (Chunk chunk : world.getLoadedChunks()) { - chunk.unload(true, false); + if (!chunk.unload(true, false)) return; if (System.currentTimeMillis() - start > 20) { return; } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/RegenAllRoads.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/RegenAllRoads.java index 56df1618b..9f4ef1f61 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/RegenAllRoads.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/RegenAllRoads.java @@ -10,9 +10,7 @@ import com.intellectualcrafters.plot.object.PlotManager; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.ChunkManager; import com.intellectualcrafters.plot.util.MainUtil; -import com.intellectualcrafters.plot.util.WorldUtil; import com.plotsquared.general.commands.CommandDeclaration; - import java.util.Set; @CommandDeclaration( @@ -41,7 +39,7 @@ public class RegenAllRoads extends SubCommand { return false; } PlotArea area = PS.get().getPlotAreaByString(args[0]); - if (area == null || !WorldUtil.IMP.isWorld(area.worldname)) { + if (area == null) { C.NOT_VALID_PLOT_WORLD.send(player, args[0]); return false; } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/util/ReflectionUtils.java b/Core/src/main/java/com/intellectualcrafters/plot/util/ReflectionUtils.java index 48803e57b..cac3218a5 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/util/ReflectionUtils.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/util/ReflectionUtils.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; /** * @author DPOH-VAR @@ -32,6 +33,18 @@ 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 Method findMethod(Class clazz, boolean isStatic, Class returnType, Class... types) { loop: for (Method method : clazz.getMethods()) { diff --git a/Core/src/main/java/com/intellectualcrafters/plot/util/WorldUtil.java b/Core/src/main/java/com/intellectualcrafters/plot/util/WorldUtil.java index 1652c562c..738ee6baf 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/util/WorldUtil.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/util/WorldUtil.java @@ -1,5 +1,10 @@ package com.intellectualcrafters.plot.util; +import com.intellectualcrafters.jnbt.CompoundTag; +import com.intellectualcrafters.jnbt.IntTag; +import com.intellectualcrafters.jnbt.NBTInputStream; +import com.intellectualcrafters.jnbt.NBTOutputStream; +import com.intellectualcrafters.jnbt.Tag; import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.object.ChunkLoc; import com.intellectualcrafters.plot.object.Location; @@ -9,14 +14,17 @@ import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.RegionWrapper; import com.intellectualcrafters.plot.object.RunnableVal; import com.intellectualcrafters.plot.object.schematic.PlotItem; - +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URL; +import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -70,15 +78,22 @@ public abstract class WorldUtil { try (final ZipOutputStream zos = new ZipOutputStream(output)) { File dat = getDat(plot.getWorldName()); Location spawn = getSpawn(plot.getWorldName()); - setSpawn(home); byte[] buffer = new byte[1024]; if (dat != null) { ZipEntry ze = new ZipEntry("world" + File.separator + dat.getName()); zos.putNextEntry(ze); - FileInputStream in = new FileInputStream(dat); - int len; - while ((len = in.read(buffer)) > 0) { - zos.write(buffer, 0, len); + try (NBTInputStream nis = new NBTInputStream(new GZIPInputStream(new FileInputStream(dat)))) { + CompoundTag tag = (CompoundTag) nis.readTag(); + CompoundTag data = (CompoundTag) tag.getValue().get("Data"); + Map map = ReflectionUtils.getMap(data.getValue()); + map.put("SpawnX", new IntTag("SpawnX", home.getX())); + map.put("SpawnY", new IntTag("SpawnY", home.getY())); + map.put("SpawnZ", new IntTag("SpawnZ", home.getZ())); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try (NBTOutputStream out = new NBTOutputStream(new GZIPOutputStream(baos, true))) { + out.writeTag(tag); + } + zos.write(baos.toByteArray()); } } setSpawn(spawn);