Skip to content

Commit

Permalink
Add a world override concept to LocalSession, and allow a lot more co…
Browse files Browse the repository at this point in the history
…mmands to be performed by actors.
  • Loading branch information
me4502 committed Aug 12, 2019
1 parent c3cb091 commit 527dd7e
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 133 deletions.
@@ -1,3 +1,22 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldedit.cli.schematic; package com.sk89q.worldedit.cli.schematic;


import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.EditSession;
Expand Down
Expand Up @@ -21,8 +21,8 @@


import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;


import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.event.extent.EditSessionEvent; import com.sk89q.worldedit.event.extent.EditSessionEvent;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extent.inventory.BlockBag; import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.util.eventbus.EventBus; import com.sk89q.worldedit.util.eventbus.EventBus;
import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.World;
Expand Down Expand Up @@ -65,10 +65,10 @@ public EditSession getEditSession(World world, int maxBlocks) {
* *
* @param world the world * @param world the world
* @param maxBlocks the maximum number of blocks that can be changed, or -1 to use no limit * @param maxBlocks the maximum number of blocks that can be changed, or -1 to use no limit
* @param player the player that the {@link EditSession} is for * @param actor the actor that the {@link EditSession} is for
* @return an instance * @return an instance
*/ */
public EditSession getEditSession(World world, int maxBlocks, Player player) { public EditSession getEditSession(World world, int maxBlocks, Actor actor) {


// ============ READ ME ============ // ============ READ ME ============


Expand Down Expand Up @@ -114,10 +114,10 @@ public EditSession getEditSession(World world, int maxBlocks, BlockBag blockBag)
* @param world the world * @param world the world
* @param maxBlocks the maximum number of blocks that can be changed, or -1 to use no limit * @param maxBlocks the maximum number of blocks that can be changed, or -1 to use no limit
* @param blockBag an optional {@link BlockBag} to use, otherwise null * @param blockBag an optional {@link BlockBag} to use, otherwise null
* @param player the player that the {@link EditSession} is for * @param actor the actor that the {@link EditSession} is for
* @return an instance * @return an instance
*/ */
public EditSession getEditSession(World world, int maxBlocks, BlockBag blockBag, Player player) { public EditSession getEditSession(World world, int maxBlocks, BlockBag blockBag, Actor actor) {


// ============ READ ME ============ // ============ READ ME ============


Expand Down Expand Up @@ -156,8 +156,8 @@ public EditSession getEditSession(World world, int maxBlocks) {
} }


@Override @Override
public EditSession getEditSession(World world, int maxBlocks, Player player) { public EditSession getEditSession(World world, int maxBlocks, Actor actor) {
return getEditSession(world, maxBlocks, null, player); return getEditSession(world, maxBlocks, null, actor);
} }


@Override @Override
Expand All @@ -166,11 +166,11 @@ public EditSession getEditSession(World world, int maxBlocks, BlockBag blockBag)
} }


@Override @Override
public EditSession getEditSession(World world, int maxBlocks, BlockBag blockBag, Player player) { public EditSession getEditSession(World world, int maxBlocks, BlockBag blockBag, Actor actor) {
if (WorldEdit.getInstance().getConfiguration().traceUnflushedSessions) { if (WorldEdit.getInstance().getConfiguration().traceUnflushedSessions) {
return new TracedEditSession(eventBus, world, maxBlocks, blockBag, new EditSessionEvent(world, player, maxBlocks, null)); return new TracedEditSession(eventBus, world, maxBlocks, blockBag, new EditSessionEvent(world, actor, maxBlocks, null));
} }
return new EditSession(eventBus, world, maxBlocks, blockBag, new EditSessionEvent(world, player, maxBlocks, null)); return new EditSession(eventBus, world, maxBlocks, blockBag, new EditSessionEvent(world, actor, maxBlocks, null));
} }


} }
Expand Down
82 changes: 63 additions & 19 deletions worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java
Expand Up @@ -100,6 +100,7 @@ public class LocalSession {
private transient BlockVector3 cuiTemporaryBlock; private transient BlockVector3 cuiTemporaryBlock;
private transient EditSession.ReorderMode reorderMode = EditSession.ReorderMode.MULTI_STAGE; private transient EditSession.ReorderMode reorderMode = EditSession.ReorderMode.MULTI_STAGE;
private transient List<Countable<BlockState>> lastDistribution; private transient List<Countable<BlockState>> lastDistribution;
private transient World worldOverride;


// Saved properties // Saved properties
private String lastScript; private String lastScript;
Expand Down Expand Up @@ -226,21 +227,21 @@ public void remember(EditSession editSession) {
* Performs an undo. * Performs an undo.
* *
* @param newBlockBag a new block bag * @param newBlockBag a new block bag
* @param player the player * @param actor the actor
* @return whether anything was undone * @return whether anything was undone
*/ */
public EditSession undo(@Nullable BlockBag newBlockBag, Player player) { public EditSession undo(@Nullable BlockBag newBlockBag, Actor actor) {
checkNotNull(player); checkNotNull(actor);
--historyPointer; --historyPointer;
if (historyPointer >= 0) { if (historyPointer >= 0) {
EditSession editSession = history.get(historyPointer); EditSession editSession = history.get(historyPointer);
try (EditSession newEditSession = WorldEdit.getInstance().getEditSessionFactory() try (EditSession newEditSession = WorldEdit.getInstance().getEditSessionFactory()
.getEditSession(editSession.getWorld(), -1, newBlockBag, player)) { .getEditSession(editSession.getWorld(), -1, newBlockBag, actor)) {
newEditSession.enableStandardMode(); newEditSession.enableStandardMode();
newEditSession.setReorderMode(reorderMode); newEditSession.setReorderMode(reorderMode);
newEditSession.setFastMode(fastMode); newEditSession.setFastMode(fastMode);
if (newEditSession.getSurvivalExtent() != null) { if (newEditSession.getSurvivalExtent() != null) {
newEditSession.getSurvivalExtent().setStripNbt(!player.hasPermission("worldedit.setnbt")); newEditSession.getSurvivalExtent().setStripNbt(!actor.hasPermission("worldedit.setnbt"));
} }
editSession.undo(newEditSession); editSession.undo(newEditSession);
} }
Expand All @@ -255,20 +256,20 @@ public EditSession undo(@Nullable BlockBag newBlockBag, Player player) {
* Performs a redo * Performs a redo
* *
* @param newBlockBag a new block bag * @param newBlockBag a new block bag
* @param player the player * @param actor the actor
* @return whether anything was redone * @return whether anything was redone
*/ */
public EditSession redo(@Nullable BlockBag newBlockBag, Player player) { public EditSession redo(@Nullable BlockBag newBlockBag, Actor actor) {
checkNotNull(player); checkNotNull(actor);
if (historyPointer < history.size()) { if (historyPointer < history.size()) {
EditSession editSession = history.get(historyPointer); EditSession editSession = history.get(historyPointer);
try (EditSession newEditSession = WorldEdit.getInstance().getEditSessionFactory() try (EditSession newEditSession = WorldEdit.getInstance().getEditSessionFactory()
.getEditSession(editSession.getWorld(), -1, newBlockBag, player)) { .getEditSession(editSession.getWorld(), -1, newBlockBag, actor)) {
newEditSession.enableStandardMode(); newEditSession.enableStandardMode();
newEditSession.setReorderMode(reorderMode); newEditSession.setReorderMode(reorderMode);
newEditSession.setFastMode(fastMode); newEditSession.setFastMode(fastMode);
if (newEditSession.getSurvivalExtent() != null) { if (newEditSession.getSurvivalExtent() != null) {
newEditSession.getSurvivalExtent().setStripNbt(!player.hasPermission("worldedit.setnbt")); newEditSession.getSurvivalExtent().setStripNbt(!actor.hasPermission("worldedit.setnbt"));
} }
editSession.redo(newEditSession); editSession.redo(newEditSession);
} }
Expand All @@ -279,6 +280,19 @@ public EditSession redo(@Nullable BlockBag newBlockBag, Player player) {
return null; return null;
} }


public boolean hasWorldOverride() {
return this.worldOverride != null;
}

@Nullable
public World getWorldOverride() {
return this.worldOverride;
}

public void setWorldOverride(@Nullable World worldOverride) {
this.worldOverride = worldOverride;
}

/** /**
* Get the default region selector. * Get the default region selector.
* *
Expand Down Expand Up @@ -483,14 +497,18 @@ public boolean toggleSuperPickAxe() {
* Get the position use for commands that take a center point * Get the position use for commands that take a center point
* (i.e. //forestgen, etc.). * (i.e. //forestgen, etc.).
* *
* @param player the player * @param actor the actor
* @return the position to use * @return the position to use
* @throws IncompleteRegionException thrown if a region is not fully selected * @throws IncompleteRegionException thrown if a region is not fully selected
*/ */
public BlockVector3 getPlacementPosition(Player player) throws IncompleteRegionException { public BlockVector3 getPlacementPosition(Actor actor) throws IncompleteRegionException {
checkNotNull(player); checkNotNull(actor);
if (!placeAtPos1) { if (!placeAtPos1) {
return player.getBlockIn().toVector().toBlockPoint(); if (actor.isPlayer() && actor instanceof Player) {
return ((Player) actor).getBlockIn().toVector().toBlockPoint();
} else {
throw new IncompleteRegionException();
}
} }


return selector.getPrimaryPosition(); return selector.getPrimaryPosition();
Expand Down Expand Up @@ -653,9 +671,9 @@ public void setLastScript(@Nullable String lastScript) {
/** /**
* Tell the player the WorldEdit version. * Tell the player the WorldEdit version.
* *
* @param player the player * @param actor the actor
*/ */
public void tellVersion(Actor player) { public void tellVersion(Actor actor) {
} }


public boolean shouldUseServerCUI() { public boolean shouldUseServerCUI() {
Expand Down Expand Up @@ -894,9 +912,10 @@ public EditSession createEditSession(Player player) {
BlockBag blockBag = getBlockBag(player); BlockBag blockBag = getBlockBag(player);


// Create an edit session // Create an edit session
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory() EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(
.getEditSession(player.isPlayer() ? player.getWorld() : null, hasWorldOverride() ? getWorldOverride() : player.isPlayer() ? player.getWorld() : null,
getBlockChangeLimit(), blockBag, player); getBlockChangeLimit(), blockBag, player
);
Request.request().setEditSession(editSession); Request.request().setEditSession(editSession);


editSession.setFastMode(fastMode); editSession.setFastMode(fastMode);
Expand All @@ -909,6 +928,31 @@ public EditSession createEditSession(Player player) {
return editSession; return editSession;
} }


/**
* Construct a new edit session.
*
* @param actor the actor
* @return an edit session
*/
public EditSession createEditSession(Actor actor) {
checkNotNull(actor);
checkNotNull(getWorldOverride());

// Create an edit session
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory()
.getEditSession(getWorldOverride(), getBlockChangeLimit());
Request.request().setEditSession(editSession);

editSession.setFastMode(fastMode);
editSession.setReorderMode(reorderMode);
editSession.setMask(mask);
if (editSession.getSurvivalExtent() != null) {
editSession.getSurvivalExtent().setStripNbt(!actor.hasPermission("worldedit.setnbt"));
}

return editSession;
}

/** /**
* Checks if the session has fast mode enabled. * Checks if the session has fast mode enabled.
* *
Expand Down
22 changes: 11 additions & 11 deletions worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java
Expand Up @@ -229,16 +229,16 @@ public SessionManager getSessionManager() {
* traversal exploits by checking the root directory and the file directory. * traversal exploits by checking the root directory and the file directory.
* On success, a {@code java.io.File} object will be returned. * On success, a {@code java.io.File} object will be returned.
* *
* @param player the player * @param actor the actor
* @param dir sub-directory to look in * @param dir sub-directory to look in
* @param filename filename (user-submitted) * @param filename filename (user-submitted)
* @param defaultExt append an extension if missing one, null to not use * @param defaultExt append an extension if missing one, null to not use
* @param extensions list of extensions, null for any * @param extensions list of extensions, null for any
* @return a file * @return a file
* @throws FilenameException thrown if the filename is invalid * @throws FilenameException thrown if the filename is invalid
*/ */
public File getSafeSaveFile(Player player, File dir, String filename, String defaultExt, String... extensions) throws FilenameException { public File getSafeSaveFile(Actor actor, File dir, String filename, String defaultExt, String... extensions) throws FilenameException {
return getSafeFile(player, dir, filename, defaultExt, extensions, true); return getSafeFile(actor, dir, filename, defaultExt, extensions, true);
} }


/** /**
Expand All @@ -247,22 +247,22 @@ public File getSafeSaveFile(Player player, File dir, String filename, String def
* traversal exploits by checking the root directory and the file directory. * traversal exploits by checking the root directory and the file directory.
* On success, a {@code java.io.File} object will be returned. * On success, a {@code java.io.File} object will be returned.
* *
* @param player the player * @param actor the actor
* @param dir sub-directory to look in * @param dir sub-directory to look in
* @param filename filename (user-submitted) * @param filename filename (user-submitted)
* @param defaultExt append an extension if missing one, null to not use * @param defaultExt append an extension if missing one, null to not use
* @param extensions list of extensions, null for any * @param extensions list of extensions, null for any
* @return a file * @return a file
* @throws FilenameException thrown if the filename is invalid * @throws FilenameException thrown if the filename is invalid
*/ */
public File getSafeOpenFile(Player player, File dir, String filename, String defaultExt, String... extensions) throws FilenameException { public File getSafeOpenFile(Actor actor, File dir, String filename, String defaultExt, String... extensions) throws FilenameException {
return getSafeFile(player, dir, filename, defaultExt, extensions, false); return getSafeFile(actor, dir, filename, defaultExt, extensions, false);
} }


/** /**
* Get a safe path to a file. * Get a safe path to a file.
* *
* @param player the player * @param actor the actor
* @param dir sub-directory to look in * @param dir sub-directory to look in
* @param filename filename (user-submitted) * @param filename filename (user-submitted)
* @param defaultExt append an extension if missing one, null to not use * @param defaultExt append an extension if missing one, null to not use
Expand All @@ -271,16 +271,16 @@ public File getSafeOpenFile(Player player, File dir, String filename, String def
* @return a file * @return a file
* @throws FilenameException thrown if the filename is invalid * @throws FilenameException thrown if the filename is invalid
*/ */
private File getSafeFile(@Nullable Player player, File dir, String filename, String defaultExt, String[] extensions, boolean isSave) throws FilenameException { private File getSafeFile(@Nullable Actor actor, File dir, String filename, String defaultExt, String[] extensions, boolean isSave) throws FilenameException {
if (extensions != null && (extensions.length == 1 && extensions[0] == null)) extensions = null; if (extensions != null && (extensions.length == 1 && extensions[0] == null)) extensions = null;


File f; File f;


if (filename.equals("#") && player != null) { if (filename.equals("#") && actor != null) {
if (isSave) { if (isSave) {
f = player.openFileSaveDialog(extensions); f = actor.openFileSaveDialog(extensions);
} else { } else {
f = player.openFileOpenDialog(extensions); f = actor.openFileOpenDialog(extensions);
} }


if (f == null) { if (f == null) {
Expand Down

0 comments on commit 527dd7e

Please sign in to comment.