From 8a40320a64ff100cc06bd8d55aba07162c276b7f Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Thu, 29 Dec 2016 19:43:27 +1100 Subject: [PATCH] Fix changeset NPE --- .../rollback/RollbackOptimizedHistory.java | 10 ++++++++++ .../com/boydti/fawe/object/NullChangeSet.java | 9 +++++++++ .../object/changeset/DiskStorageHistory.java | 16 +++++++++++++++- .../fawe/object/changeset/FaweChangeSet.java | 16 +++++++++++++++- .../object/changeset/FaweStreamChangeSet.java | 14 ++++++++++++++ .../changeset/MemoryOptimizedHistory.java | 4 ++++ .../boydti/fawe/util/EditSessionBuilder.java | 19 +++++++++++++++++-- 7 files changed, 84 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/com/boydti/fawe/logging/rollback/RollbackOptimizedHistory.java b/core/src/main/java/com/boydti/fawe/logging/rollback/RollbackOptimizedHistory.java index d7f3fbe6..d3e3c649 100644 --- a/core/src/main/java/com/boydti/fawe/logging/rollback/RollbackOptimizedHistory.java +++ b/core/src/main/java/com/boydti/fawe/logging/rollback/RollbackOptimizedHistory.java @@ -29,6 +29,16 @@ public class RollbackOptimizedHistory extends DiskStorageHistory { this.time = System.currentTimeMillis(); } + public RollbackOptimizedHistory(String world, UUID uuid, int index) { + super(world, uuid, index); + this.time = System.currentTimeMillis(); + } + + public RollbackOptimizedHistory(String world, UUID uuid) { + super(world, uuid); + this.time = System.currentTimeMillis(); + } + public long getTime() { return time; } diff --git a/core/src/main/java/com/boydti/fawe/object/NullChangeSet.java b/core/src/main/java/com/boydti/fawe/object/NullChangeSet.java index f1ea0b8b..df0dcf6d 100644 --- a/core/src/main/java/com/boydti/fawe/object/NullChangeSet.java +++ b/core/src/main/java/com/boydti/fawe/object/NullChangeSet.java @@ -13,6 +13,10 @@ public class NullChangeSet extends FaweChangeSet { super(world); } + public NullChangeSet(String world) { + super(world); + } + @Override public final boolean flush() { return false; @@ -43,6 +47,11 @@ public class NullChangeSet extends FaweChangeSet { } + @Override + public void addChangeTask(FaweQueue queue) { + + } + @Override public Iterator getIterator(BlockBag blockBag, int mode, boolean redo) { return getIterator(redo); diff --git a/core/src/main/java/com/boydti/fawe/object/changeset/DiskStorageHistory.java b/core/src/main/java/com/boydti/fawe/object/changeset/DiskStorageHistory.java index 5e2e3a32..7eaac963 100644 --- a/core/src/main/java/com/boydti/fawe/object/changeset/DiskStorageHistory.java +++ b/core/src/main/java/com/boydti/fawe/object/changeset/DiskStorageHistory.java @@ -55,7 +55,16 @@ public class DiskStorageHistory extends FaweStreamChangeSet { public DiskStorageHistory(World world, UUID uuid) { super(world); - File folder = MainUtil.getFile(Fawe.imp().getDirectory(), Settings.PATHS.HISTORY + File.separator + Fawe.imp().getWorldName(world) + File.separator + uuid); + init(uuid, Fawe.imp().getWorldName(world)); + } + + public DiskStorageHistory(String world, UUID uuid) { + super(world); + init(uuid, world); + } + + private void init(UUID uuid, String worldName) { + File folder = MainUtil.getFile(Fawe.imp().getDirectory(), Settings.PATHS.HISTORY + File.separator + worldName + File.separator + uuid); int max = 0; if (folder.exists()) { for (File file : folder.listFiles()) { @@ -71,6 +80,11 @@ public class DiskStorageHistory extends FaweStreamChangeSet { init(uuid, ++max); } + public DiskStorageHistory(String world, UUID uuid, int index) { + super(world); + init(uuid, index); + } + public DiskStorageHistory(World world, UUID uuid, int index) { super(world); init(uuid, index); diff --git a/core/src/main/java/com/boydti/fawe/object/changeset/FaweChangeSet.java b/core/src/main/java/com/boydti/fawe/object/changeset/FaweChangeSet.java index 1d2334fc..358f631a 100644 --- a/core/src/main/java/com/boydti/fawe/object/changeset/FaweChangeSet.java +++ b/core/src/main/java/com/boydti/fawe/object/changeset/FaweChangeSet.java @@ -1,6 +1,7 @@ package com.boydti.fawe.object.changeset; import com.boydti.fawe.Fawe; +import com.boydti.fawe.FaweAPI; import com.boydti.fawe.FaweCache; import com.boydti.fawe.config.Settings; import com.boydti.fawe.object.FaweChunk; @@ -31,7 +32,8 @@ import java.util.concurrent.atomic.AtomicInteger; public abstract class FaweChangeSet implements ChangeSet { - private final World world; + private World world; + private final String worldName; private final boolean mainThread; private final int layers; private AtomicInteger waitingCombined = new AtomicInteger(0); @@ -47,13 +49,25 @@ public abstract class FaweChangeSet implements ChangeSet { } } + public FaweChangeSet(String world) { + this.worldName = world; + this.mainThread = Fawe.get().isMainThread(); + this.layers = FaweChunk.HEIGHT >> 4; + } + public FaweChangeSet(World world) { this.world = world; + this.worldName = Fawe.imp().getWorldName(world); this.mainThread = Fawe.get().isMainThread(); this.layers = (this.world.getMaxY() + 1) >> 4; } + public String getWorldName() { + return worldName; + } + public World getWorld() { + if (world == null && worldName != null) world = FaweAPI.getWorld(worldName); return world; } diff --git a/core/src/main/java/com/boydti/fawe/object/changeset/FaweStreamChangeSet.java b/core/src/main/java/com/boydti/fawe/object/changeset/FaweStreamChangeSet.java index 2bbc4123..623858bf 100644 --- a/core/src/main/java/com/boydti/fawe/object/changeset/FaweStreamChangeSet.java +++ b/core/src/main/java/com/boydti/fawe/object/changeset/FaweStreamChangeSet.java @@ -35,9 +35,23 @@ public abstract class FaweStreamChangeSet extends FaweChangeSet { this(world, Settings.HISTORY.COMPRESSION_LEVEL, Settings.HISTORY.STORE_REDO, Settings.HISTORY.SMALL_EDITS); } + public FaweStreamChangeSet(String world) { + this(world, Settings.HISTORY.COMPRESSION_LEVEL, Settings.HISTORY.STORE_REDO, Settings.HISTORY.SMALL_EDITS); + } + + public FaweStreamChangeSet(String world, int compression, boolean storeRedo, boolean smallLoc) { + super(world); + this.compression = compression; + init(storeRedo, smallLoc); + } + public FaweStreamChangeSet(World world, int compression, boolean storeRedo, boolean smallLoc) { super(world); this.compression = compression; + init(storeRedo, smallLoc); + } + + private void init(boolean storeRedo, boolean smallLoc) { if (storeRedo) { if (smallLoc) { mode = 4; diff --git a/core/src/main/java/com/boydti/fawe/object/changeset/MemoryOptimizedHistory.java b/core/src/main/java/com/boydti/fawe/object/changeset/MemoryOptimizedHistory.java index 69fd62c1..b8595d84 100644 --- a/core/src/main/java/com/boydti/fawe/object/changeset/MemoryOptimizedHistory.java +++ b/core/src/main/java/com/boydti/fawe/object/changeset/MemoryOptimizedHistory.java @@ -46,6 +46,10 @@ public class MemoryOptimizedHistory extends FaweStreamChangeSet { super(world); } + public MemoryOptimizedHistory(String world) { + super(world); + } + @Override public boolean flush() { super.flush(); diff --git a/core/src/main/java/com/boydti/fawe/util/EditSessionBuilder.java b/core/src/main/java/com/boydti/fawe/util/EditSessionBuilder.java index 1facab29..2b6dc747 100644 --- a/core/src/main/java/com/boydti/fawe/util/EditSessionBuilder.java +++ b/core/src/main/java/com/boydti/fawe/util/EditSessionBuilder.java @@ -1,5 +1,7 @@ package com.boydti.fawe.util; +import com.boydti.fawe.Fawe; +import com.boydti.fawe.FaweAPI; import com.boydti.fawe.config.Settings; import com.boydti.fawe.logging.rollback.RollbackOptimizedHistory; import com.boydti.fawe.object.FaweLimit; @@ -56,11 +58,13 @@ public class EditSessionBuilder { public EditSessionBuilder(@Nonnull World world){ checkNotNull(world); this.world = world; + this.worldName = Fawe.imp().getWorldName(world); } public EditSessionBuilder(@Nonnull String worldName) { checkNotNull(worldName); this.worldName = worldName; + this.world = FaweAPI.getWorld(worldName); } public EditSessionBuilder player(@Nullable FawePlayer player) { @@ -91,12 +95,13 @@ public class EditSessionBuilder { } public EditSessionBuilder changeSetNull() { - return changeSet(new NullChangeSet(world)); + return changeSet(world == null ? new NullChangeSet(worldName) : new NullChangeSet(world)); } public EditSessionBuilder world(@Nonnull World world) { checkNotNull(world); this.world = world; + this.worldName = Fawe.imp().getWorldName(world); return this; } @@ -107,7 +112,17 @@ public class EditSessionBuilder { * @return */ public EditSessionBuilder changeSet(boolean disk, @Nullable UUID uuid, int compression) { - if (disk) { + if (world == null) { + if (disk) { + if (Settings.HISTORY.USE_DATABASE) { + this.changeSet = new RollbackOptimizedHistory(worldName, uuid); + } else { + this.changeSet = new DiskStorageHistory(worldName, uuid); + } + } else { + this.changeSet = new MemoryOptimizedHistory(worldName); + } + } else if (disk) { if (Settings.HISTORY.USE_DATABASE) { this.changeSet = new RollbackOptimizedHistory(world, uuid); } else {