From bed5fa8fdc7c9d533453a3bd1e31e7e1c528fa90 Mon Sep 17 00:00:00 2001 From: sk89q Date: Fri, 4 Apr 2014 17:54:14 -0700 Subject: [PATCH] Added Actor, Player, Entity, BaseEntity. --- .../java/com/sk89q/worldedit/LocalPlayer.java | 307 +++--------------- .../java/com/sk89q/worldedit/WorldEdit.java | 6 +- .../sk89q/worldedit/entity/BaseEntity.java | 26 ++ .../com/sk89q/worldedit/entity/Entity.java | 218 +++++++++++++ .../com/sk89q/worldedit/entity/Player.java | 83 +++++ .../event/extent/EditSessionEvent.java | 20 +- .../extension/input/ParserContext.java | 33 +- .../worldedit/extension/platform/Actor.java | 136 ++++++++ .../registry/DefaultBlockParser.java | 28 +- .../worldedit/session/SessionManager.java | 78 ++--- 10 files changed, 587 insertions(+), 348 deletions(-) create mode 100644 src/main/java/com/sk89q/worldedit/entity/BaseEntity.java create mode 100644 src/main/java/com/sk89q/worldedit/entity/Entity.java create mode 100644 src/main/java/com/sk89q/worldedit/entity/Player.java create mode 100644 src/main/java/com/sk89q/worldedit/extension/platform/Actor.java diff --git a/src/main/java/com/sk89q/worldedit/LocalPlayer.java b/src/main/java/com/sk89q/worldedit/LocalPlayer.java index 8bf40eaaf..57a779f3b 100644 --- a/src/main/java/com/sk89q/worldedit/LocalPlayer.java +++ b/src/main/java/com/sk89q/worldedit/LocalPlayer.java @@ -19,24 +19,23 @@ package com.sk89q.worldedit; -import java.io.File; - -import com.sk89q.worldedit.extent.inventory.BlockBag; import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BlockID; import com.sk89q.worldedit.blocks.BlockType; import com.sk89q.worldedit.blocks.ItemID; +import com.sk89q.worldedit.entity.Entity; +import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.internal.cui.CUIEvent; import com.sk89q.worldedit.util.TargetBlock; +import java.io.File; + /** - * - * @author sk89q + * Represents a player that uses WorldEdit. In the future, this type + * will be completely replaced by {@link Actor}. */ -public abstract class LocalPlayer { - /** - * Server. - */ +public abstract class LocalPlayer implements Actor, Entity { + protected ServerInterface server; /** @@ -48,11 +47,7 @@ public abstract class LocalPlayer { this.server = server; } - /** - * Returns true if the player is holding a pick axe. - * - * @return whether a pick axe is held - */ + @Override public boolean isHoldingPickAxe() { int item = getItemInHand(); return item == ItemID.IRON_PICK @@ -62,14 +57,7 @@ public abstract class LocalPlayer { || item == ItemID.GOLD_PICKAXE; } - /** - * Find a position for the player to stand that is not inside a block. - * Blocks above the player will be iteratively tested until there is - * a series of two free blocks. The player will be teleported to - * that free position. - * - * @param searchPos search position - */ + @Override public void findFreePosition(WorldVector searchPos) { LocalWorld world = searchPos.getWorld(); int x = searchPos.getBlockX(); @@ -101,11 +89,7 @@ public abstract class LocalPlayer { } } - /** - * Set the player on the ground. - * - * @param searchPos The location to start searching from - */ + @Override public void setOnGround(WorldVector searchPos) { LocalWorld world = searchPos.getWorld(); int x = searchPos.getBlockX(); @@ -125,21 +109,12 @@ public abstract class LocalPlayer { } } - /** - * Find a position for the player to stand that is not inside a block. - * Blocks above the player will be iteratively tested until there is - * a series of two free blocks. The player will be teleported to - * that free position. - */ + @Override public void findFreePosition() { findFreePosition(getBlockIn()); } - /** - * Go up one level to the next free space above. - * - * @return true if a spot was found - */ + @Override public boolean ascendLevel() { final WorldVector pos = getBlockIn(); final int x = pos.getBlockX(); @@ -180,11 +155,7 @@ public abstract class LocalPlayer { return false; } - /** - * Go up one level to the next free space above. - * - * @return true if a spot was found - */ + @Override public boolean descendLevel() { final WorldVector pos = getBlockIn(); final int x = pos.getBlockX(); @@ -229,23 +200,12 @@ public abstract class LocalPlayer { return false; } - /** - * Ascend to the ceiling above. - * - * @param clearance How many blocks to leave above the player's head - * @return whether the player was moved - */ + @Override public boolean ascendToCeiling(int clearance) { return ascendToCeiling(clearance, true); } - /** - * Ascend to the ceiling above. - * - * @param clearance How many blocks to leave above the player's head - * @param alwaysGlass Always put glass under the player - * @return whether the player was moved - */ + @Override public boolean ascendToCeiling(int clearance, boolean alwaysGlass) { Vector pos = getBlockIn(); int x = pos.getBlockX(); @@ -273,23 +233,12 @@ public abstract class LocalPlayer { return false; } - /** - * Just go up. - * - * @param distance How far up to teleport - * @return whether the player was moved - */ + @Override public boolean ascendUpwards(int distance) { return ascendUpwards(distance, true); } - /** - * Just go up. - * - * @param distance How far up to teleport - * @param alwaysGlass Always put glass under the player - * @return whether the player was moved - */ + @Override public boolean ascendUpwards(int distance, boolean alwaysGlass) { final Vector pos = getBlockIn(); final int x = pos.getBlockX(); @@ -315,94 +264,55 @@ public abstract class LocalPlayer { return false; } - /** - * Make the player float in the given blocks. - * - * @param x The X coordinate of the block to float in - * @param y The Y coordinate of the block to float in - * @param z The Z coordinate of the block to float in - */ + @Override public void floatAt(int x, int y, int z, boolean alwaysGlass) { getPosition().getWorld().setBlockType(new Vector(x, y - 1, z), BlockID.GLASS); setPosition(new Vector(x + 0.5, y, z + 0.5)); } - /** - * Get the point of the block that is being stood in. - * - * @return point - */ + @Override public WorldVector getBlockIn() { WorldVector pos = getPosition(); return WorldVector.toBlockPoint(pos.getWorld(), pos.getX(), pos.getY(), pos.getZ()); } - /** - * Get the point of the block that is being stood upon. - * - * @return point - */ + @Override public WorldVector getBlockOn() { WorldVector pos = getPosition(); return WorldVector.toBlockPoint(pos.getWorld(), pos.getX(), pos.getY() - 1, pos.getZ()); } - /** - * Get the point of the block being looked at. May return null. - * Will return the farthest away air block if useLastBlock is true and no other block is found. - * - * @param range How far to checks for blocks - * @param useLastBlock Try to return the last valid air block found. - * @return point - */ + @Override public WorldVector getBlockTrace(int range, boolean useLastBlock) { TargetBlock tb = new TargetBlock(this, range, 0.2); return (useLastBlock ? tb.getAnyTargetBlock() : tb.getTargetBlock()); } + @Override public WorldVectorFace getBlockTraceFace(int range, boolean useLastBlock) { TargetBlock tb = new TargetBlock(this, range, 0.2); return (useLastBlock ? tb.getAnyTargetBlockFace() : tb.getTargetBlockFace()); } - /** - * Get the point of the block being looked at. May return null. - * - * @param range How far to checks for blocks - * @return point - */ + @Override public WorldVector getBlockTrace(int range) { return getBlockTrace(range, false); } - /** - * Get the point of the block being looked at. May return null. - * - * @param range How far to checks for blocks - * @return point - */ + @Override public WorldVector getSolidBlockTrace(int range) { TargetBlock tb = new TargetBlock(this, range, 0.2); return tb.getSolidTargetBlock(); } - /** - * Get the player's cardinal direction (N, W, NW, etc.). May return null. - * - * @return the direction - */ + @Override public PlayerDirection getCardinalDirection() { return getCardinalDirection(0); } - /** - * Get the player's cardinal direction (N, W, NW, etc.) with an offset. May return null. - * @param yawOffset offset that is added to the player's yaw before determining the cardinal direction - * - * @return the direction - */ + @Override public PlayerDirection getCardinalDirection(int yawOffset) { if (getPitch() > 67.5) { return PlayerDirection.DOWN; @@ -449,18 +359,7 @@ public abstract class LocalPlayer { } } - /** - * Get the ID of the item that the player is holding. - * - * @return the item id of the item the player is holding - */ - public abstract int getItemInHand(); - - /** - * Get the Block that the player is holding. - * - * @return the item id of the item the player is holding - */ + @Override public BaseBlock getBlockInHand() throws WorldEditException { final int typeId = getItemInHand(); if (!getWorld().isValidBlockType(typeId)) { @@ -469,65 +368,19 @@ public abstract class LocalPlayer { return new BaseBlock(typeId); } - /** - * Get the name of the player. - * - * @return String - */ - public abstract String getName(); - - /** - * Get the player's position. - * - * @return point - */ - public abstract WorldVector getPosition(); - - /** - * Get the player's world. - * - * @return point - */ - public abstract LocalWorld getWorld(); - /** * Get the player's view pitch. * * @return pitch */ - /** - * Get the player's view pitch. - * - * @return pitch - */ - public abstract double getPitch(); /** * Get the player's view yaw. * * @return yaw */ - /** - * Get the player's view yaw. - * - * @return yaw - */ - public abstract double getYaw(); - /** - * Gives the player an item. - * - * @param type The item id of the item to be given to the player - * @param amount How many items in the stack - */ - public abstract void giveItem(int type, int amount); - - /** - * Pass through the wall that you are looking at. - * - * @param range How far to checks for blocks - * @return whether the player was pass through - */ + @Override public boolean passThroughForwardWall(int range) { int searchDist = 0; TargetBlock hitBlox = new TargetBlock(this, range, 0.2); @@ -571,121 +424,32 @@ public abstract class LocalPlayer { return false; } - /** - * Print a message. - * - * @param msg The message text - */ - public abstract void printRaw(String msg); - - /** - * Print a WorldEdit message. - * - * @param msg The message text - */ - public abstract void printDebug(String msg); - - /** - * Print a WorldEdit message. - * - * @param msg The message text - */ - public abstract void print(String msg); - - /** - * Print a WorldEdit error. - * - * @param msg The error message text - */ - public abstract void printError(String msg); - - /** - * Move the player. - * - * @param pos Where to move them - * @param pitch The pitch (up/down) of the player's view - * @param yaw The yaw (left/right) of the player's view - */ - public abstract void setPosition(Vector pos, float pitch, float yaw); - - /** - * Move the player. - * - * @param pos Where to move them - */ + @Override public void setPosition(Vector pos) { setPosition(pos, (float) getPitch(), (float) getYaw()); } - /** - * Get a player's list of groups. - * - * @return an array containing a group name per entry - */ - public abstract String[] getGroups(); - - /** - * Get this player's block bag. - * - * @return the player's block bag - */ - public abstract BlockBag getInventoryBlockBag(); - - /** - * Checks if a player has permission. - * - * @param perm The permission to check - * @return true if the player has that permission - */ - public abstract boolean hasPermission(String perm); - - /** - * Open a file open dialog. - * - * @param extensions null to allow all - * @return the selected file or null if something went wrong - */ + @Override public File openFileOpenDialog(String[] extensions) { printError("File dialogs are not supported in your environment."); return null; } - /** - * Open a file save dialog. - * - * @param extensions null to allow all - * @return the selected file or null if something went wrong - */ + @Override public File openFileSaveDialog(String[] extensions) { printError("File dialogs are not supported in your environment."); return null; } - /** - * Returns true if the player can destroy bedrock. - * - * @return - */ + @Override public boolean canDestroyBedrock() { return hasPermission("worldedit.override.bedrock"); } - /** - * Send a CUI event. - * - * @param event - */ + @Override public void dispatchCUIEvent(CUIEvent event) { } - /** - * Send the CUI handshake. - * @deprecated Not used anymore; The CUI begins the handshake - */ - @Deprecated - public void dispatchCUIHandshake() { - } - @Override public boolean equals(Object other) { if (!(other instanceof LocalPlayer)) { @@ -700,16 +464,19 @@ public abstract class LocalPlayer { return getName().hashCode(); } + @Override public void checkPermission(String permission) throws WorldEditPermissionException { if (!hasPermission(permission)) { throw new WorldEditPermissionException(); } } + @Override public boolean isPlayer() { return true; } + @Override public boolean hasCreativeMode() { return false; } diff --git a/src/main/java/com/sk89q/worldedit/WorldEdit.java b/src/main/java/com/sk89q/worldedit/WorldEdit.java index f3fbc5dfc..0240ad6a5 100644 --- a/src/main/java/com/sk89q/worldedit/WorldEdit.java +++ b/src/main/java/com/sk89q/worldedit/WorldEdit.java @@ -314,7 +314,7 @@ public class WorldEdit { @Deprecated public BaseBlock getBlock(LocalPlayer player, String arg, boolean allAllowed, boolean allowNoData) throws WorldEditException { ParserContext context = new ParserContext(); - context.setPlayer(player); + context.setActor(player); context.setWorld(player.getWorld()); context.setSession(getSession(player)); context.setRestricted(!allAllowed); @@ -383,7 +383,7 @@ public class WorldEdit { @SuppressWarnings("deprecation") public Pattern getBlockPattern(LocalPlayer player, String input) throws WorldEditException { ParserContext context = new ParserContext(); - context.setPlayer(player); + context.setActor(player); context.setWorld(player.getWorld()); context.setSession(getSession(player)); return Patterns.wrap(getPatternRegistry().parseFromInput(input, context)); @@ -396,7 +396,7 @@ public class WorldEdit { @SuppressWarnings("deprecation") public Mask getBlockMask(LocalPlayer player, LocalSession session, String input) throws WorldEditException { ParserContext context = new ParserContext(); - context.setPlayer(player); + context.setActor(player); context.setWorld(player.getWorld()); context.setSession(session); return Masks.wrap(getMaskRegistry().parseFromInput(input, context)); diff --git a/src/main/java/com/sk89q/worldedit/entity/BaseEntity.java b/src/main/java/com/sk89q/worldedit/entity/BaseEntity.java new file mode 100644 index 000000000..f547929b3 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/entity/BaseEntity.java @@ -0,0 +1,26 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * 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 . + */ + +package com.sk89q.worldedit.entity; + +/** + * A snapshot of an entity that can be reused and passed around. + */ +public class BaseEntity { +} diff --git a/src/main/java/com/sk89q/worldedit/entity/Entity.java b/src/main/java/com/sk89q/worldedit/entity/Entity.java new file mode 100644 index 000000000..960c8923e --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/entity/Entity.java @@ -0,0 +1,218 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * 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 . + */ + +package com.sk89q.worldedit.entity; + +import com.sk89q.worldedit.PlayerDirection; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.WorldVector; +import com.sk89q.worldedit.WorldVectorFace; +import com.sk89q.worldedit.extent.Extent; + +/** + * A reference to an instance of an entity that exists in an {@link Extent} + * and thus would have position and similar details. + *

+ * This object cannot be directly cloned because it represents a particular + * instance of an entity, but a {@link BaseEntity} can be created from + * this entity (or at least, it will be possible in the future), which + * can then be used to spawn new instances of that particular entity + * description. + */ +public interface Entity extends Player { + + /** + * Find a position for the actor to stand that is not inside a block. + * Blocks above the player will be iteratively tested until there is + * a series of two free blocks. The actor will be teleported to + * that free position. + * + * @param searchPos search position + */ + void findFreePosition(WorldVector searchPos); + + /** + * Set the actor on the ground. + * + * @param searchPos The location to start searching from + */ + void setOnGround(WorldVector searchPos); + + /** + * Find a position for the player to stand that is not inside a block. + * Blocks above the player will be iteratively tested until there is + * a series of two free blocks. The player will be teleported to + * that free position. + */ + void findFreePosition(); + + /** + * Go up one level to the next free space above. + * + * @return true if a spot was found + */ + boolean ascendLevel(); + + /** + * Go up one level to the next free space above. + * + * @return true if a spot was found + */ + boolean descendLevel(); + + /** + * Ascend to the ceiling above. + * + * @param clearance How many blocks to leave above the player's head + * @return whether the player was moved + */ + boolean ascendToCeiling(int clearance); + + /** + * Ascend to the ceiling above. + * + * @param clearance How many blocks to leave above the player's head + * @param alwaysGlass Always put glass under the player + * @return whether the player was moved + */ + boolean ascendToCeiling(int clearance, boolean alwaysGlass); + + /** + * Just go up. + * + * @param distance How far up to teleport + * @return whether the player was moved + */ + boolean ascendUpwards(int distance); + + /** + * Just go up. + * + * @param distance How far up to teleport + * @param alwaysGlass Always put glass under the player + * @return whether the player was moved + */ + boolean ascendUpwards(int distance, boolean alwaysGlass); + + /** + * Make the player float in the given blocks. + * + * @param x The X coordinate of the block to float in + * @param y The Y coordinate of the block to float in + * @param z The Z coordinate of the block to float in + */ + void floatAt(int x, int y, int z, boolean alwaysGlass); + + /** + * Get the point of the block that is being stood in. + * + * @return point + */ + WorldVector getBlockIn(); + + /** + * Get the point of the block that is being stood upon. + * + * @return point + */ + WorldVector getBlockOn(); + + /** + * Get the point of the block being looked at. May return null. + * Will return the farthest away air block if useLastBlock is true and no other block is found. + * + * @param range How far to checks for blocks + * @param useLastBlock Try to return the last valid air block found. + * @return point + */ + WorldVector getBlockTrace(int range, boolean useLastBlock); + + WorldVectorFace getBlockTraceFace(int range, boolean useLastBlock); + + /** + * Get the point of the block being looked at. May return null. + * + * @param range How far to checks for blocks + * @return point + */ + WorldVector getBlockTrace(int range); + + /** + * Get the point of the block being looked at. May return null. + * + * @param range How far to checks for blocks + * @return point + */ + WorldVector getSolidBlockTrace(int range); + + /** + * Get the player's cardinal direction (N, W, NW, etc.). May return null. + * + * @return the direction + */ + PlayerDirection getCardinalDirection(); + + /** + * Get the actor's position. + *

+ * If the actor has no permission, then return a dummy location. + * + * @return the actor's position + */ + WorldVector getPosition(); + + /** + * Get the player's view pitch. + * + * @return pitch + */ + double getPitch(); + + /** + * Get the player's view yaw. + * + * @return yaw + */ + double getYaw(); + + /** + * Pass through the wall that you are looking at. + * + * @param range How far to checks for blocks + * @return whether the player was pass through + */ + boolean passThroughForwardWall(int range); + + /** + * Move the player. + * + * @param pos Where to move them + * @param pitch The pitch (up/down) of the player's view + * @param yaw The yaw (left/right) of the player's view + */ + void setPosition(Vector pos, float pitch, float yaw); + + /** + * Move the player. + * + * @param pos Where to move them + */ + void setPosition(Vector pos); + +} diff --git a/src/main/java/com/sk89q/worldedit/entity/Player.java b/src/main/java/com/sk89q/worldedit/entity/Player.java new file mode 100644 index 000000000..cad0865a8 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/entity/Player.java @@ -0,0 +1,83 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * 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 . + */ + +package com.sk89q.worldedit.entity; + +import com.sk89q.worldedit.PlayerDirection; +import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.blocks.BaseBlock; +import com.sk89q.worldedit.extent.inventory.BlockBag; + +/** + * A player. + */ +public interface Player { + + /** + * Returns true if the entity is holding a pick axe. + * + * @return whether a pick axe is held + */ + boolean isHoldingPickAxe(); + + /** + * Get the player's cardinal direction (N, W, NW, etc.) with an offset. May return null. + * @param yawOffset offset that is added to the player's yaw before determining the cardinal direction + * + * @return the direction + */ + PlayerDirection getCardinalDirection(int yawOffset); + + /** + * Get the ID of the item that the player is holding. + * + * @return the item id of the item the player is holding + */ + int getItemInHand(); + + /** + * Get the Block that the player is holding. + * + * @return the item id of the item the player is holding + */ + BaseBlock getBlockInHand() throws WorldEditException; + + /** + * Gives the player an item. + * + * @param type The item id of the item to be given to the player + * @param amount How many items in the stack + */ + void giveItem(int type, int amount); + + /** + * Get this actor's block bag. + * + * @return the actor's block bag + */ + BlockBag getInventoryBlockBag(); + + /** + * Return whether this actor has creative mode. + * + * @return true if creative mode is enabled + */ + boolean hasCreativeMode(); + +} diff --git a/src/main/java/com/sk89q/worldedit/event/extent/EditSessionEvent.java b/src/main/java/com/sk89q/worldedit/event/extent/EditSessionEvent.java index 5a4c611b8..4b4ca0d26 100644 --- a/src/main/java/com/sk89q/worldedit/event/extent/EditSessionEvent.java +++ b/src/main/java/com/sk89q/worldedit/event/extent/EditSessionEvent.java @@ -20,11 +20,11 @@ package com.sk89q.worldedit.event.extent; import com.sk89q.worldedit.EditSession; -import com.sk89q.worldedit.LocalPlayer; import com.sk89q.worldedit.LocalWorld; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.event.Event; +import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extent.Extent; import javax.annotation.Nullable; @@ -60,7 +60,7 @@ import static com.sk89q.worldedit.EditSession.Stage; public class EditSessionEvent extends Event { private final LocalWorld world; - private final LocalPlayer player; + private final Actor actor; private final int maxBlocks; private final Stage stage; private Extent extent; @@ -69,25 +69,25 @@ public class EditSessionEvent extends Event { * Create a new event. * * @param world the world - * @param player the player, or null if not available + * @param actor the actor, or null if there is no actor specified * @param maxBlocks the maximum number of block changes * @param stage the stage */ - public EditSessionEvent(LocalWorld world, LocalPlayer player, int maxBlocks, Stage stage) { + public EditSessionEvent(LocalWorld world, Actor actor, int maxBlocks, Stage stage) { checkNotNull(world); this.world = world; - this.player = player; + this.actor = actor; this.maxBlocks = maxBlocks; this.stage = stage; } /** - * Get the player for this event. + * Get the actor for this event. * - * @return the player, which may be null if unavailable + * @return the actor, which may be null if unavailable */ - public @Nullable LocalPlayer getPlayer() { - return player; + public @Nullable Actor getActor() { + return actor; } /** @@ -144,7 +144,7 @@ public class EditSessionEvent extends Event { * @return a new event */ public EditSessionEvent clone(Stage stage) { - return new EditSessionEvent(world, player, maxBlocks, stage); + return new EditSessionEvent(world, actor, maxBlocks, stage); } } diff --git a/src/main/java/com/sk89q/worldedit/extension/input/ParserContext.java b/src/main/java/com/sk89q/worldedit/extension/input/ParserContext.java index 8b833ac67..cc79d96e1 100644 --- a/src/main/java/com/sk89q/worldedit/extension/input/ParserContext.java +++ b/src/main/java/com/sk89q/worldedit/extension/input/ParserContext.java @@ -22,6 +22,7 @@ package com.sk89q.worldedit.extension.input; import com.sk89q.worldedit.LocalPlayer; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.LocalWorld; +import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.registry.MaskRegistry; import com.sk89q.worldedit.extent.Extent; @@ -38,7 +39,7 @@ public class ParserContext { private @Nullable Extent extent; private @Nullable LocalSession session; private @Nullable LocalWorld world; - private @Nullable LocalPlayer player; + private @Nullable Actor actor; private boolean restricted = true; private boolean preferringWildcard; @@ -97,21 +98,21 @@ public class ParserContext { } /** - * Get the {@link LocalPlayer} set on this context. + * Get the {@link Actor} set on this context. * - * @return a player + * @return an actor, or null */ - public @Nullable LocalPlayer getPlayer() { - return player; + public @Nullable Actor getActor() { + return actor; } /** - * Set the player. + * Set the actor. * - * @param player a player, or null if none is available + * @param actor an actor, or null if none is available */ - public void setPlayer(@Nullable LocalPlayer player) { - this.player = player; + public void setActor(@Nullable Actor actor) { + this.actor = actor; } /** @@ -157,17 +158,17 @@ public class ParserContext { } /** - * Get the {@link LocalPlayer} set on this context. + * Get the {@link Actor} set on this context. * - * @return a player + * @return an actor * @throws InputParseException thrown if no {@link LocalPlayer} is set */ - public LocalPlayer requirePlayer() throws InputParseException { - LocalPlayer player = getPlayer(); - if (player == null) { - throw new InputParseException("No player is known"); + public Actor requireActor() throws InputParseException { + Actor actor = getActor(); + if (actor == null) { + throw new InputParseException("No actor is known"); } - return player; + return actor; } /** diff --git a/src/main/java/com/sk89q/worldedit/extension/platform/Actor.java b/src/main/java/com/sk89q/worldedit/extension/platform/Actor.java new file mode 100644 index 000000000..f31094962 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/extension/platform/Actor.java @@ -0,0 +1,136 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * 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 . + */ + +package com.sk89q.worldedit.extension.platform; + +import com.sk89q.worldedit.LocalWorld; +import com.sk89q.worldedit.WorldEditPermissionException; +import com.sk89q.worldedit.internal.cui.CUIEvent; + +import java.io.File; + +/** + * An object that can perform actions in WorldEdit. + */ +public interface Actor { + + /** + * Get the name of the actor. + * + * @return String + */ + String getName(); + + /** + * Get the actor's world. + * + * @return the world + */ + LocalWorld getWorld(); + + /** + * Print a message. + * + * @param msg The message text + */ + void printRaw(String msg); + + /** + * Print a WorldEdit message. + * + * @param msg The message text + */ + void printDebug(String msg); + + /** + * Print a WorldEdit message. + * + * @param msg The message text + */ + void print(String msg); + + /** + * Print a WorldEdit error. + * + * @param msg The error message text + */ + void printError(String msg); + + /** + * Returns true if the actor can destroy bedrock. + * + * @return true if bedrock can be broken by the actor + */ + boolean canDestroyBedrock(); + + /** + * Get a actor's list of groups. + * + * @return an array containing a group name per entry + */ + String[] getGroups(); + + /** + * Checks if a player has permission. + * + * @param perm The permission to check + * @return true if the player has that permission + */ + boolean hasPermission(String perm); + + /** + * Check whether this actor has the given permission, and throw an + * exception if not. + * + * @param permission the permission + * @throws WorldEditPermissionException thrown if permission is not availabe + */ + void checkPermission(String permission) throws WorldEditPermissionException; + + /** + * Return whether this actor is a player. + * + * @return true if a player + */ + boolean isPlayer(); + + /** + * Open a file open dialog. + * + * @param extensions null to allow all + * @return the selected file or null if something went wrong + */ + File openFileOpenDialog(String[] extensions); + + /** + * Open a file save dialog. + * + * @param extensions null to allow all + * @return the selected file or null if something went wrong + */ + File openFileSaveDialog(String[] extensions); + + /** + * Send a CUI event. + * + * @param event the event + */ + void dispatchCUIEvent(CUIEvent event); + +} diff --git a/src/main/java/com/sk89q/worldedit/extension/registry/DefaultBlockParser.java b/src/main/java/com/sk89q/worldedit/extension/registry/DefaultBlockParser.java index 841d0a752..c26489a02 100644 --- a/src/main/java/com/sk89q/worldedit/extension/registry/DefaultBlockParser.java +++ b/src/main/java/com/sk89q/worldedit/extension/registry/DefaultBlockParser.java @@ -21,10 +21,12 @@ package com.sk89q.worldedit.extension.registry; import com.sk89q.worldedit.*; import com.sk89q.worldedit.blocks.*; +import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.extension.input.DisallowedUsageException; -import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.NoMatchException; +import com.sk89q.worldedit.extension.input.ParserContext; +import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.internal.registry.InputParser; /** @@ -36,13 +38,17 @@ class DefaultBlockParser extends InputParser { super(worldEdit); } - private static BaseBlock getBlockInHand(LocalPlayer player) throws InputParseException { - try { - return player.getBlockInHand(); - } catch (NotABlockException e) { - throw new InputParseException("You're not holding a block!"); - } catch (WorldEditException e) { - throw new InputParseException("Unknown error occurred: " + e.getMessage(), e); + private static BaseBlock getBlockInHand(Actor actor) throws InputParseException { + if (actor instanceof Player) { + try { + return ((Player) actor).getBlockInHand(); + } catch (NotABlockException e) { + throw new InputParseException("You're not holding a block!"); + } catch (WorldEditException e) { + throw new InputParseException("Unknown error occurred: " + e.getMessage(), e); + } + } else { + throw new InputParseException("The user is not a player!"); } } @@ -63,7 +69,7 @@ class DefaultBlockParser extends InputParser { if ("hand".equalsIgnoreCase(testID)) { // Get the block type from the item in the user's hand. - final BaseBlock blockInHand = getBlockInHand(context.requirePlayer()); + final BaseBlock blockInHand = getBlockInHand(context.requireActor()); if (blockInHand.getClass() != BaseBlock.class) { return blockInHand; } @@ -211,8 +217,8 @@ class DefaultBlockParser extends InputParser { } // Check if the item is allowed - LocalPlayer player = context.requirePlayer(); - if (context.isRestricted() && player != null && !player.hasPermission("worldedit.anyblock") + Actor actor = context.requireActor(); + if (context.isRestricted() && actor != null && !actor.hasPermission("worldedit.anyblock") && worldEdit.getConfiguration().disallowedBlocks.contains(blockId)) { throw new DisallowedUsageException("You are not allowed to use '" + input + "'"); } diff --git a/src/main/java/com/sk89q/worldedit/session/SessionManager.java b/src/main/java/com/sk89q/worldedit/session/SessionManager.java index 50895c97b..562bdc757 100644 --- a/src/main/java/com/sk89q/worldedit/session/SessionManager.java +++ b/src/main/java/com/sk89q/worldedit/session/SessionManager.java @@ -20,6 +20,8 @@ package com.sk89q.worldedit.session; import com.sk89q.worldedit.*; +import com.sk89q.worldedit.entity.Player; +import com.sk89q.worldedit.extension.platform.Actor; import javax.annotation.Nullable; import java.util.HashMap; @@ -50,34 +52,34 @@ public class SessionManager { } /** - * Get whether a session exists for the given player. + * Get whether a session exists for the given actor. * - * @param player the player + * @param actor the actor * @return true if a session exists */ - public synchronized boolean contains(LocalPlayer player) { - checkNotNull(player); - return sessions.containsKey(getKey(player)); + public synchronized boolean contains(Actor actor) { + checkNotNull(actor); + return sessions.containsKey(getKey(actor)); } /** - * Gets the session for a player and return it if it exists, otherwise + * Gets the session for an actor and return it if it exists, otherwise * return null. * - * @param player the player - * @return the session for the player, if it exists + * @param actor the actor + * @return the session for the actor, if it exists */ - public synchronized @Nullable LocalSession find(LocalPlayer player) { - checkNotNull(player); - return sessions.get(getKey(player)); + public synchronized @Nullable LocalSession find(Actor actor) { + checkNotNull(actor); + return sessions.get(getKey(actor)); } /** * Gets the session for someone named by the given name and return it if * it exists, otherwise return null. * - * @param name the player's name - * @return the session for the player, if it exists + * @param name the actor's name + * @return the session for the actor, if it exists */ public synchronized @Nullable LocalSession findByName(String name) { checkNotNull(name); @@ -85,32 +87,32 @@ public class SessionManager { } /** - * Get the session for a player and create one if one doesn't exist. + * Get the session for an actor and create one if one doesn't exist. * - * @param player the player + * @param actor the actor * @return a session */ - public synchronized LocalSession get(LocalPlayer player) { - checkNotNull(player); + public synchronized LocalSession get(Actor actor) { + checkNotNull(actor); LocalSession session; LocalConfiguration config = worldEdit.getConfiguration(); - if (sessions.containsKey(player.getName())) { - session = sessions.get(player.getName()); + if (sessions.containsKey(actor.getName())) { + session = sessions.get(actor.getName()); } else { session = new LocalSession(config); session.setBlockChangeLimit(config.defaultChangeLimit); // Remember the session - sessions.put(player.getName(), session); + sessions.put(actor.getName(), session); } // Set the limit on the number of blocks that an operation can - // change at once, or don't if the player has an override or there + // change at once, or don't if the actor has an override or there // is no limit. There is also a default limit int currentChangeLimit = session.getBlockChangeLimit(); - if (!player.hasPermission("worldedit.limit.unrestricted") + if (!actor.hasPermission("worldedit.limit.unrestricted") && config.maxChangeLimit > -1) { // If the default limit is infinite but there is a maximum @@ -128,47 +130,47 @@ public class SessionManager { } } - // Have the session use inventory if it's enabled and the player + // Have the session use inventory if it's enabled and the actor // doesn't have an override session.setUseInventory(config.useInventory && !(config.useInventoryOverride - && (player.hasPermission("worldedit.inventory.unrestricted") - || (config.useInventoryCreativeOverride && player.hasCreativeMode())))); + && (actor.hasPermission("worldedit.inventory.unrestricted") + || (config.useInventoryCreativeOverride && (!(actor instanceof Player) || ((Player) actor).hasCreativeMode()))))); return session; } /** - * Get the key to use in the map for a player. + * Get the key to use in the map for an actor. * - * @param player the player + * @param actor the actor * @return the key object */ - protected String getKey(LocalPlayer player) { - return player.getName(); + protected String getKey(Actor actor) { + return actor.getName(); } /** * Mark for expiration. * - * @param player the player + * @param actor the actor */ - public synchronized void markforExpiration(LocalPlayer player) { - checkNotNull(player); - LocalSession session = find(player); + public synchronized void markforExpiration(Actor actor) { + checkNotNull(actor); + LocalSession session = find(actor); if (session != null) { session.update(); } } /** - * Remove the session for the given player if one exists. + * Remove the session for the given actor if one exists. * - * @param player the player + * @param actor the actor */ - public synchronized void remove(LocalPlayer player) { - checkNotNull(player); - sessions.remove(player.getName()); + public synchronized void remove(Actor actor) { + checkNotNull(actor); + sessions.remove(actor.getName()); } /**