From 39acae08aa2ce6fb0946e9e37abf353feae51de8 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Tue, 2 Aug 2016 17:00:01 +1000 Subject: [PATCH] Minor API changes --- .../com/boydti/fawe/object/FawePlayer.java | 58 ++++++++++++++ .../java/com/sk89q/worldedit/EditSession.java | 77 +++++++++++++++++-- 2 files changed, 127 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/com/boydti/fawe/object/FawePlayer.java b/core/src/main/java/com/boydti/fawe/object/FawePlayer.java index aecec376..eda6af18 100644 --- a/core/src/main/java/com/boydti/fawe/object/FawePlayer.java +++ b/core/src/main/java/com/boydti/fawe/object/FawePlayer.java @@ -9,6 +9,7 @@ import com.boydti.fawe.object.changeset.FaweStreamChangeSet; import com.boydti.fawe.object.clipboard.DiskOptimizedClipboard; import com.boydti.fawe.object.exception.FaweException; import com.boydti.fawe.util.MainUtil; +import com.boydti.fawe.util.SetQueue; import com.boydti.fawe.util.TaskManager; import com.boydti.fawe.util.WEManager; import com.boydti.fawe.wrappers.FakePlayer; @@ -32,6 +33,8 @@ import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @@ -101,6 +104,7 @@ public abstract class FawePlayer { return Fawe.imp().wrap(obj); } + @Deprecated public FawePlayer(final T parent) { this.parent = parent; Fawe.get().register(this); @@ -126,6 +130,10 @@ public abstract class FawePlayer { } } + /** + * Loads any history items from disk: + * - Should already be called if history on disk is enabled + */ public void loadClipboardFromDisk() { File file = new File(Fawe.imp().getDirectory(), "clipboard" + File.separator + getUUID() + ".bd"); try { @@ -214,6 +222,9 @@ public abstract class FawePlayer { */ public abstract void sendTitle(String head, String sub); + /** + * Remove the title + */ public abstract void resetTitle(); /** @@ -312,6 +323,10 @@ public abstract class FawePlayer { this.getSession().setRegionSelector(player.getWorld(), selector); } + /** + * Set the player's WorldEdit selection + * @param selector + */ public void setSelection(final RegionSelector selector) { this.getSession().setRegionSelector(getPlayer().getWorld(), selector); } @@ -416,6 +431,11 @@ public abstract class FawePlayer { return WorldEdit.getInstance().getEditSessionFactory().getEditSession(getWorld(), -1, getPlayer()); } + /** + * Run a task if the player has no currently running action + * @param run + * @return If the task was run + */ public boolean runIfFree(Runnable run) { if (getMeta("fawe_action") != null) { return false; @@ -436,6 +456,11 @@ public abstract class FawePlayer { return true; } + /** + * Run an async task if the player has no currently running action + * @param run + * @return If the task was run + */ public boolean runAsyncIfFree(final Runnable run) { if (getMeta("fawe_action") != null) { return false; @@ -460,4 +485,37 @@ public abstract class FawePlayer { }); return true; } + + /** + * Get the tracked EditSession(s) for this player
+ * - Queued or autoqueued EditSessions are considered tracked + * @param requiredStage + * @return + */ + public Map getTrackedSessions(SetQueue.QueueStage requiredStage) { + Map map = new ConcurrentHashMap<>(); + if (requiredStage == null || requiredStage == SetQueue.QueueStage.ACTIVE) { + for (FaweQueue queue : SetQueue.IMP.getActiveQueues()) { + Set sessions = queue.getEditSessions(); + for (EditSession session : sessions) { + FawePlayer currentPlayer = session.getPlayer(); + if (currentPlayer == this) { + map.put(session, SetQueue.QueueStage.ACTIVE); + } + } + } + } + if (requiredStage == null || requiredStage == SetQueue.QueueStage.INACTIVE) { + for (FaweQueue queue : SetQueue.IMP.getInactiveQueues()) { + Set sessions = queue.getEditSessions(); + for (EditSession session : sessions) { + FawePlayer currentPlayer = session.getPlayer(); + if (currentPlayer == this) { + map.put(session, SetQueue.QueueStage.INACTIVE); + } + } + } + } + return map; + } } \ No newline at end of file diff --git a/core/src/main/java/com/sk89q/worldedit/EditSession.java b/core/src/main/java/com/sk89q/worldedit/EditSession.java index ef5d8b73..6f53aee4 100644 --- a/core/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/core/src/main/java/com/sk89q/worldedit/EditSession.java @@ -160,6 +160,7 @@ public class EditSession implements Extent { private HistoryExtent history; private Extent bypassHistory; private Extent bypassAll; + private FaweLimit originalLimit; private FaweLimit limit; private FawePlayer player; private FaweChangeSet changeTask; @@ -204,7 +205,7 @@ public class EditSession implements Extent { } if (limit == null) { if (player == null) { - limit = FaweLimit.MAX.copy(); + limit = FaweLimit.MAX; } else { limit = player.getLimit(); } @@ -245,7 +246,8 @@ public class EditSession implements Extent { throw new FaweException(BBC.WORLDEDIT_CANCEL_REASON_NO_REGION); } this.blockBag = blockBag; - this.limit = limit; + this.originalLimit = limit; + this.limit = limit.copy(); this.queue = SetQueue.IMP.getNewQueue(Fawe.imp().getWorldName(world), fastmode, autoQueue); queue.addEditSession(this); this.bypassAll = wrapExtent(new FastWorldEditExtent(world, queue), bus, event, Stage.BEFORE_CHANGE); @@ -305,13 +307,49 @@ public class EditSession implements Extent { this(world, null, null, null, null, true, null, null, null, blockBag, eventBus, event); } + /** + * The limit for this specific edit (blocks etc) + * @return + */ + public FaweLimit getLimit() { + return originalLimit; + } + + /** + * Returns a new limit representing how much of this edit's limit has been used so far + * @return + */ + public FaweLimit getLimitUsed() { + FaweLimit newLimit = new FaweLimit(); + newLimit.MAX_CHANGES = originalLimit.MAX_CHANGES - limit.MAX_CHANGES; + newLimit.MAX_FAILS = originalLimit.MAX_FAILS - limit.MAX_FAILS; + newLimit.MAX_CHECKS = originalLimit.MAX_CHECKS - limit.MAX_CHECKS; + newLimit.MAX_ITERATIONS = originalLimit.MAX_ITERATIONS - limit.MAX_ITERATIONS; + newLimit.MAX_BLOCKSTATES = originalLimit.MAX_BLOCKSTATES - limit.MAX_BLOCKSTATES; + newLimit.MAX_ENTITIES = originalLimit.MAX_ENTITIES - limit.MAX_ENTITIES; + newLimit.MAX_HISTORY = limit.MAX_HISTORY; + return newLimit; + } + + /** + * Returns the remaining limits + * @return + */ + public FaweLimit getLimitLeft() { + return limit; + } + + /** + * The region extent restricts block placements to allowed regions + * @return FaweRegionExtent (may be null) + */ public FaweRegionExtent getRegionExtent() { ExtentTraverser traverser = new ExtentTraverser(this.extent).find(FaweRegionExtent.class); return traverser == null ? null : traverser.get(); } /** - * Get the actor + * Get the FawePlayer or null * @return */ @Nullable @@ -337,26 +375,45 @@ public class EditSession implements Extent { return true; } + /** + * Remove this EditSession from the queue
+ * - This doesn't necessarily stop it from being queued again + */ public void dequeue() { if (queue != null) { SetQueue.IMP.dequeue(queue); } } + /** + * Add a task to run when this EditSession is done dispatching + * @param whenDone + */ public void addNotifyTask(Runnable whenDone) { if (queue != null) { queue.addNotifyTask(whenDone); } } + /** + * Send a debug message to the Actor responsible for this EditSession (or Console) + * @param message + * @param args + */ public void debug(BBC message, Object... args) { message.send(player, args); } + /** + * Get the FaweQueue this EditSession uses to queue the changes
+ * - Note: All implementation queues for FAWE are instances of NMSMappedFaweQueue + * @return + */ public FaweQueue getQueue() { return queue; } + @Deprecated private Extent wrapExtent(final Extent extent, final EventBus eventBus, EditSessionEvent event, final Stage stage) { event = event.clone(stage); event.setExtent(extent); @@ -396,7 +453,12 @@ public class EditSession implements Extent { return history != null ? history.getChangeSet() : changeTask; } - public void setChangeSet(FaweChangeSet set) { + /** + * Change the ChangeSet being used for this EditSession + * - If history is disabled, no changeset can be set + * @param set (null = remove the changeset) + */ + public void setChangeSet(@Nullable FaweChangeSet set) { if (set == null) { disableHistory(true); } else { @@ -411,13 +473,12 @@ public class EditSession implements Extent { } /** - * Get the maximum number of blocks that can be changed. -1 will be returned - * if it the limit disabled. - * + * @see #getLimit() * @return the limit (>= 0) or -1 for no limit */ + @Deprecated public int getBlockChangeLimit() { - return -1; + return originalLimit.MAX_CHANGES; } /**