From 1e8ce84769e5901b54c9a44f4794726c7921ef2b Mon Sep 17 00:00:00 2001 From: boy0001 Date: Sun, 15 Feb 2015 21:52:51 +1100 Subject: [PATCH] reduce memory usage --- PlotSquared/pom.xml | 2 +- .../intellectualcrafters/plot/PlotMain.java | 8 +- .../plot/commands/Condense.java | 79 +++++++++++++++++++ .../plot/commands/Move.java | 23 ++++-- .../plot/generator/AugmentedPopulator.java | 10 ++- .../plot/util/ChunkManager.java | 37 +++++++-- .../plot/util/ClusterManager.java | 6 +- 7 files changed, 139 insertions(+), 26 deletions(-) create mode 100644 PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Condense.java diff --git a/PlotSquared/pom.xml b/PlotSquared/pom.xml index 4bd363698..8e5115cfc 100644 --- a/PlotSquared/pom.xml +++ b/PlotSquared/pom.xml @@ -8,7 +8,7 @@ UTF-8 PlotSquared - 2.7.2 + 2.7.3 PlotSquared jar diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java index 0f8c2b8f4..998516baa 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java @@ -1021,7 +1021,7 @@ public class PlotMain extends JavaPlugin implements Listener { } } catch (Exception e) { - System.out.print("ERRRRRRRRRRRRRR 6"); + PlotMain.sendConsoleSenderMessage("&d=== Oh no! Please set the generator for the " + world + " ==="); e.printStackTrace(); LOADING_WORLD = false; removePlotWorld(world); @@ -1594,7 +1594,7 @@ public class PlotMain extends JavaPlugin implements Listener { try { AbstractSetBlock.setBlockManager = new SetBlockFast_1_8(); } - catch (Exception e) { + catch (Throwable e) { e.printStackTrace(); AbstractSetBlock.setBlockManager = new SetBlockSlow(); } @@ -1602,7 +1602,7 @@ public class PlotMain extends JavaPlugin implements Listener { else { try { AbstractSetBlock.setBlockManager = new SetBlockFast(); - } catch (NoSuchMethodException e) { + } catch (Throwable e) { AbstractSetBlock.setBlockManager = new SetBlockSlow(); } } @@ -1619,7 +1619,7 @@ public class PlotMain extends JavaPlugin implements Listener { } // Handle UUIDS { - boolean checkVersion = checkVersion(1, 7, 5); + boolean checkVersion = checkVersion(1, 7, 6); if (!checkVersion) { sendConsoleSenderMessage(C.PREFIX.s()+" &c[WARN] Titles are disabled - please update your version of Bukkit to support this feature."); Settings.TITLES = false; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Condense.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Condense.java new file mode 100644 index 000000000..bc9378024 --- /dev/null +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Condense.java @@ -0,0 +1,79 @@ +//////////////////////////////////////////////////////////////////////////////////////////////////// +// PlotSquared - A plot manager and world generator for the Bukkit API / +// Copyright (c) 2014 IntellectualSites/IntellectualCrafters / +// / +// This program is free software; you can redistribute it and/or modify / +// it under the terms of the GNU General Public License as published by / +// the Free Software Foundation; either version 3 of the License, or / +// (at your option) any later version. / +// / +// This program is distributed in the hope that it will be useful, / +// but WITHOUT ANY WARRANTY; without even the implied warranty of / +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the / +// GNU General Public License for more details. / +// / +// You should have received a copy of the GNU General Public License / +// along with this program; if not, write to the Free Software Foundation, / +// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA / +// / +// You can contact us via: support@intellectualsites.com / +//////////////////////////////////////////////////////////////////////////////////////////////////// + +package com.intellectualcrafters.plot.commands; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Player; + +import com.intellectualcrafters.plot.PlotMain; +import com.intellectualcrafters.plot.config.C; +import com.intellectualcrafters.plot.generator.HybridPlotManager; +import com.intellectualcrafters.plot.generator.HybridPlotWorld; +import com.intellectualcrafters.plot.object.ChunkLoc; +import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotId; +import com.intellectualcrafters.plot.util.ChunkManager; +import com.intellectualcrafters.plot.util.ExpireManager; +import com.intellectualcrafters.plot.util.PlayerFunctions; +import com.intellectualcrafters.plot.util.PlotHelper; +import com.intellectualcrafters.plot.util.TaskManager; + +public class Condense extends SubCommand { + + public static boolean TASK = false; + private static int TASK_ID = 0; + + public Condense() { + super("condense", "plots.admin", "Condense a plotworld", "condense", "", CommandCategory.DEBUG, false); + } + + public PlotId getId(String id) { + try { + String[] split = id.split(";"); + return new PlotId(Integer.parseInt(split[0]), Integer.parseInt(split[1])); + } + catch (Exception e) { + return null; + } + } + + @Override + public boolean execute(final Player plr, final String... args) { + return true; + } + + public static void sendMessage(final String message) { + PlotMain.sendConsoleSenderMessage("&3PlotSquared -> Plot condense&8: &7" + message); + } + +} diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Move.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Move.java index 03510b1fb..a2cc119bc 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Move.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Move.java @@ -45,14 +45,23 @@ import com.intellectualcrafters.plot.util.TaskManager; public class Move extends SubCommand { public Move() { - super("move", "plots.admin", "plot moving debug test", "move", "condense", CommandCategory.DEBUG, false); + super("move", "plots.admin", "plot moving debug test", "move", "condense", CommandCategory.DEBUG, true); } @Override public boolean execute(final Player plr, final String... args) { + if (plr == null) { + PlayerFunctions.sendMessage(plr, "MUST BE EXECUTED BY PLAYER"); + } World world = plr.getWorld(); PlotId plot1 = PlotHelper.parseId(args[0]); PlotId plot2 = PlotHelper.parseId(args[1]); + if (plot1 == null || plot2 == null) { + PlayerFunctions.sendMessage(plr, "INVALID PLOT ID\n/plot move "); + } + if (plot1 == plot2) { + PlayerFunctions.sendMessage(plr, "DUPLICATE ID"); + } if (move(world, plot1, plot2, null)) { PlayerFunctions.sendMessage(plr, "MOVE SUCCESS"); } @@ -63,9 +72,9 @@ public class Move extends SubCommand { } public boolean move(final World world, final PlotId current, PlotId newPlot, final Runnable whenDone) { - Location bot1 = PlotHelper.getPlotBottomLoc(world, current); + final Location bot1 = PlotHelper.getPlotBottomLoc(world, current); Location bot2 = PlotHelper.getPlotBottomLoc(world, newPlot); - Location top = PlotHelper.getPlotTopLoc(world, current); + final Location top = PlotHelper.getPlotTopLoc(world, current); final Plot currentPlot = PlotHelper.getPlot(world, current); if (currentPlot.owner == null) { return false; @@ -79,7 +88,7 @@ public class Move extends SubCommand { int offset_x = newPlot.x - current.x; int offset_y = newPlot.y - current.y; - ArrayList selection = PlayerFunctions.getPlotSelectionIds(pos1.id, pos2.id); + final ArrayList selection = PlayerFunctions.getPlotSelectionIds(pos1.id, pos2.id); String worldname = world.getName(); for (PlotId id : selection) { DBFunc.movePlot(world.getName(), new PlotId(id.x, id.y), new PlotId(id.x + offset_x, id.y + offset_y)); @@ -92,10 +101,8 @@ public class Move extends SubCommand { ChunkManager.copyRegion(bot1, top, bot2, new Runnable() { @Override public void run() { - PlotHelper.clear(null, world, PlotHelper.getPlot(world, current), true); - if (whenDone != null) { - TaskManager.runTaskLater(whenDone, 1); - } + ChunkManager.regenerateRegion(bot1, top, null); + TaskManager.runTaskLater(whenDone, 1); } }); return true; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/AugmentedPopulator.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/AugmentedPopulator.java index 91a10ea48..993bfac95 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/AugmentedPopulator.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/AugmentedPopulator.java @@ -1,5 +1,6 @@ package com.intellectualcrafters.plot.generator; +import java.util.Arrays; import java.util.Random; import org.bukkit.Bukkit; @@ -16,6 +17,7 @@ import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotManager; import com.intellectualcrafters.plot.object.PlotWorld; import com.intellectualcrafters.plot.object.RegionWrapper; +import com.intellectualcrafters.plot.util.AbstractSetBlock; import com.intellectualcrafters.plot.util.ChunkManager; import com.intellectualcrafters.plot.util.PlotHelper; import com.intellectualcrafters.plot.util.TaskManager; @@ -129,8 +131,8 @@ public class AugmentedPopulator extends BlockPopulator { @Override public void run() { populateBiome(world, x, z); - chunk.unload(); - chunk.load(); + chunk.unload(true, false); + AbstractSetBlock.setBlockManager.update(Arrays.asList( new Chunk[] {chunk})); } }, 20); } @@ -146,8 +148,8 @@ public class AugmentedPopulator extends BlockPopulator { public void run() { chunk.load(true); populateBlocks(world, rand, X, Z, x, z, check); - chunk.unload(); - chunk.load(); + chunk.unload(true, false); + AbstractSetBlock.setBlockManager.update(Arrays.asList( new Chunk[] {chunk})); } }, 40 + rand.nextInt(40)); } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ChunkManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ChunkManager.java index 7b270f364..81c3c419c 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ChunkManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ChunkManager.java @@ -2,6 +2,7 @@ package com.intellectualcrafters.plot.util; import java.io.File; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; @@ -129,6 +130,9 @@ public class ChunkManager { private static HashSet entities; + /** + * Copy a region to a new location (in the same world) + */ public static boolean copyRegion(final Location pos1, final Location pos2, final Location newPos, final Runnable whenDone) { int relX = newPos.getBlockX() - pos1.getBlockX(); int relZ = newPos.getBlockZ() - pos1.getBlockZ(); @@ -138,6 +142,9 @@ public class ChunkManager { Chunk c1 = world.getChunkAt(pos1); Chunk c2 = world.getChunkAt(pos2); + Chunk c3 = world.getChunkAt(pos1.getBlockX() + relX, pos1.getBlockZ() + relZ); + Chunk c4 = world.getChunkAt(pos2.getBlockX() + relX, pos2.getBlockZ() + relZ); + final int sx = pos1.getBlockX(); final int sz = pos1.getBlockZ(); final int ex = pos2.getBlockX(); @@ -148,17 +155,33 @@ public class ChunkManager { final int c2x = c2.getX(); final int c2z = c2.getZ(); + final int c3x = c3.getX(); + final int c3z = c3.getZ(); + final int c4x = c4.getX(); + final int c4z = c4.getZ(); + // Copy entities + ArrayList chunks = new ArrayList<>(); initMaps(); - for (int x = c1x; x <= c2x; x ++) { - for (int z = c1z; z <= c2z; z ++) { + for (int x = c3x; x <= c4x; x ++) { + for (int z = c3z; z <= c4z; z ++) { Chunk chunk = world.getChunkAt(x, z); + chunks.add(chunk); chunk.load(false); saveEntitiesIn(chunk, region); restoreEntities(world, relX, relZ); } } + // Load chunks + for (int x = c1x; x <= c2x; x ++) { + for (int z = c1z; z <= c2z; z ++) { + Chunk chunk = world.getChunkAt(x, z); + chunk.load(true); + chunks.add(chunk); + } + } + // Copy blocks int maxY = world.getMaxHeight(); for (int x = sx; x <= ex; x++) { @@ -173,9 +196,11 @@ public class ChunkManager { } } restoreBlocks(world, relX, relZ); - TaskManager.runTaskLater(whenDone, 1); - + AbstractSetBlock.setBlockManager.update(chunks); + for (Chunk chunk : chunks) { + chunk.unload(true, false); + } return true; } @@ -264,8 +289,8 @@ public class ChunkManager { restoreBlocks(world, 0, 0); restoreEntities(world, 0, 0); } - chunk.unload(); - chunk.load(); + chunk.unload(true, false); + AbstractSetBlock.setBlockManager.update(Arrays.asList( new Chunk[] {chunk})); } CURRENT_PLOT_CLEAR = null; } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ClusterManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ClusterManager.java index 8b703d2f6..96a1aaa31 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ClusterManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ClusterManager.java @@ -1,6 +1,7 @@ package com.intellectualcrafters.plot.util; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -287,16 +288,15 @@ public class ClusterManager { @Override public void run() { if (populator == null || plotworld.TYPE == 0) { + AbstractSetBlock.setBlockManager.update(Arrays.asList( new Chunk[] {chunk})); world.regenerateChunk(chunk.getX(), chunk.getZ()); - chunk.unload(); - chunk.load(); + chunk.unload(true, false); } else { populator.populate(world, rand, chunk); } } }, i); - } } } \ No newline at end of file