Skip to content
Permalink
Browse files

Add a world override concept to LocalSession, and allow a lot more co…

…mmands to be performed by actors.
  • Loading branch information...
me4502 committed Jul 15, 2019
1 parent 62c7490 commit 31a75941da05fa912361c45b7e33599b56d29b55
@@ -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;

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

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.extension.platform.Actor;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.util.eventbus.EventBus;
import com.sk89q.worldedit.world.World;
@@ -65,10 +65,10 @@ public EditSession getEditSession(World world, int maxBlocks) {
*
* @param world the world
* @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
*/
public EditSession getEditSession(World world, int maxBlocks, Player player) {
public EditSession getEditSession(World world, int maxBlocks, Actor actor) {

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

@@ -114,10 +114,10 @@ public EditSession getEditSession(World world, int maxBlocks, BlockBag blockBag)
* @param world the world
* @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 player the player that the {@link EditSession} is for
* @param actor the actor that the {@link EditSession} is for
* @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 ============

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

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

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

@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) {
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));
}

}
@@ -100,6 +100,7 @@
private transient BlockVector3 cuiTemporaryBlock;
private transient EditSession.ReorderMode reorderMode = EditSession.ReorderMode.MULTI_STAGE;
private transient List<Countable<BlockState>> lastDistribution;
private transient World worldOverride;

// Saved properties
private String lastScript;
@@ -226,21 +227,21 @@ public void remember(EditSession editSession) {
* Performs an undo.
*
* @param newBlockBag a new block bag
* @param player the player
* @param actor the actor
* @return whether anything was undone
*/
public EditSession undo(@Nullable BlockBag newBlockBag, Player player) {
checkNotNull(player);
public EditSession undo(@Nullable BlockBag newBlockBag, Actor actor) {
checkNotNull(actor);
--historyPointer;
if (historyPointer >= 0) {
EditSession editSession = history.get(historyPointer);
try (EditSession newEditSession = WorldEdit.getInstance().getEditSessionFactory()
.getEditSession(editSession.getWorld(), -1, newBlockBag, player)) {
.getEditSession(editSession.getWorld(), -1, newBlockBag, actor)) {
newEditSession.enableStandardMode();
newEditSession.setReorderMode(reorderMode);
newEditSession.setFastMode(fastMode);
if (newEditSession.getSurvivalExtent() != null) {
newEditSession.getSurvivalExtent().setStripNbt(!player.hasPermission("worldedit.setnbt"));
newEditSession.getSurvivalExtent().setStripNbt(!actor.hasPermission("worldedit.setnbt"));
}
editSession.undo(newEditSession);
}
@@ -255,20 +256,20 @@ public EditSession undo(@Nullable BlockBag newBlockBag, Player player) {
* Performs a redo
*
* @param newBlockBag a new block bag
* @param player the player
* @param actor the actor
* @return whether anything was redone
*/
public EditSession redo(@Nullable BlockBag newBlockBag, Player player) {
checkNotNull(player);
public EditSession redo(@Nullable BlockBag newBlockBag, Actor actor) {
checkNotNull(actor);
if (historyPointer < history.size()) {
EditSession editSession = history.get(historyPointer);
try (EditSession newEditSession = WorldEdit.getInstance().getEditSessionFactory()
.getEditSession(editSession.getWorld(), -1, newBlockBag, player)) {
.getEditSession(editSession.getWorld(), -1, newBlockBag, actor)) {
newEditSession.enableStandardMode();
newEditSession.setReorderMode(reorderMode);
newEditSession.setFastMode(fastMode);
if (newEditSession.getSurvivalExtent() != null) {
newEditSession.getSurvivalExtent().setStripNbt(!player.hasPermission("worldedit.setnbt"));
newEditSession.getSurvivalExtent().setStripNbt(!actor.hasPermission("worldedit.setnbt"));
}
editSession.redo(newEditSession);
}
@@ -279,6 +280,19 @@ public EditSession redo(@Nullable BlockBag newBlockBag, Player player) {
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.
*
@@ -483,14 +497,18 @@ public boolean toggleSuperPickAxe() {
* Get the position use for commands that take a center point
* (i.e. //forestgen, etc.).
*
* @param player the player
* @param actor the actor
* @return the position to use
* @throws IncompleteRegionException thrown if a region is not fully selected
*/
public BlockVector3 getPlacementPosition(Player player) throws IncompleteRegionException {
checkNotNull(player);
public BlockVector3 getPlacementPosition(Actor actor) throws IncompleteRegionException {
checkNotNull(actor);
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();
@@ -653,9 +671,9 @@ public void setLastScript(@Nullable String lastScript) {
/**
* 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() {
@@ -894,9 +912,10 @@ public EditSession createEditSession(Player player) {
BlockBag blockBag = getBlockBag(player);

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

editSession.setFastMode(fastMode);
@@ -909,6 +928,31 @@ public EditSession createEditSession(Player player) {
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.
*
@@ -229,16 +229,16 @@ public SessionManager getSessionManager() {
* traversal exploits by checking the root directory and the file directory.
* 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 filename filename (user-submitted)
* @param defaultExt append an extension if missing one, null to not use
* @param extensions list of extensions, null for any
* @return a file
* @throws FilenameException thrown if the filename is invalid
*/
public File getSafeSaveFile(Player player, File dir, String filename, String defaultExt, String... extensions) throws FilenameException {
return getSafeFile(player, dir, filename, defaultExt, extensions, true);
public File getSafeSaveFile(Actor actor, File dir, String filename, String defaultExt, String... extensions) throws FilenameException {
return getSafeFile(actor, dir, filename, defaultExt, extensions, true);
}

/**
@@ -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.
* 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 filename filename (user-submitted)
* @param defaultExt append an extension if missing one, null to not use
* @param extensions list of extensions, null for any
* @return a file
* @throws FilenameException thrown if the filename is invalid
*/
public File getSafeOpenFile(Player player, File dir, String filename, String defaultExt, String... extensions) throws FilenameException {
return getSafeFile(player, dir, filename, defaultExt, extensions, false);
public File getSafeOpenFile(Actor actor, File dir, String filename, String defaultExt, String... extensions) throws FilenameException {
return getSafeFile(actor, dir, filename, defaultExt, extensions, false);
}

/**
* Get a safe path to a file.
*
* @param player the player
* @param actor the actor
* @param dir sub-directory to look in
* @param filename filename (user-submitted)
* @param defaultExt append an extension if missing one, null to not use
@@ -271,16 +271,16 @@ public File getSafeOpenFile(Player player, File dir, String filename, String def
* @return a file
* @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;

File f;

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

if (f == null) {

0 comments on commit 31a7594

Please sign in to comment.
You can’t perform that action at this time.