From d9cea950b0b8e9ee3a401c744d48bd48e43353a3 Mon Sep 17 00:00:00 2001 From: sk89q Date: Thu, 26 Jun 2014 20:07:04 -0700 Subject: [PATCH] Added events for the other WorldEdit.handle*() methods. --- .../java/com/sk89q/worldedit/WorldEdit.java | 77 +++------------ .../com/sk89q/worldedit/entity/Player.java | 3 +- .../BlockInteractEvent.java | 8 +- .../worldedit/event/platform/InputType.java | 37 ++++++++ .../Interaction.java} | 9 +- .../event/platform/PlayerInputEvent.java | 78 +++++++++++++++ .../extension/platform/PlatformManager.java | 95 +++++++++++++++++-- 7 files changed, 227 insertions(+), 80 deletions(-) rename src/main/java/com/sk89q/worldedit/event/{actor => platform}/BlockInteractEvent.java (94%) create mode 100644 src/main/java/com/sk89q/worldedit/event/platform/InputType.java rename src/main/java/com/sk89q/worldedit/event/{actor/InteractionType.java => platform/Interaction.java} (90%) create mode 100644 src/main/java/com/sk89q/worldedit/event/platform/PlayerInputEvent.java diff --git a/src/main/java/com/sk89q/worldedit/WorldEdit.java b/src/main/java/com/sk89q/worldedit/WorldEdit.java index e20a6efa9..6dca280b5 100644 --- a/src/main/java/com/sk89q/worldedit/WorldEdit.java +++ b/src/main/java/com/sk89q/worldedit/WorldEdit.java @@ -23,12 +23,11 @@ import com.sk89q.minecraft.util.commands.CommandsManager; import com.sk89q.worldedit.CuboidClipboard.FlipDirection; import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BlockType; -import com.sk89q.worldedit.command.tool.DoubleActionTraceTool; -import com.sk89q.worldedit.command.tool.Tool; -import com.sk89q.worldedit.command.tool.TraceTool; -import com.sk89q.worldedit.event.actor.BlockInteractEvent; +import com.sk89q.worldedit.event.platform.BlockInteractEvent; import com.sk89q.worldedit.event.extent.EditSessionEvent; import com.sk89q.worldedit.event.platform.CommandEvent; +import com.sk89q.worldedit.event.platform.InputType; +import com.sk89q.worldedit.event.platform.PlayerInputEvent; import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.extension.platform.Platform; import com.sk89q.worldedit.extension.platform.PlatformManager; @@ -57,8 +56,8 @@ import java.util.Set; import java.util.logging.Logger; import static com.google.common.base.Preconditions.checkNotNull; -import static com.sk89q.worldedit.event.actor.InteractionType.PRIMARY_INPUT; -import static com.sk89q.worldedit.event.actor.InteractionType.SECONDARY_INPUT; +import static com.sk89q.worldedit.event.platform.Interaction.HIT; +import static com.sk89q.worldedit.event.platform.Interaction.OPEN; /** * The entry point and container for a working implementation of WorldEdit. @@ -722,35 +721,9 @@ public class WorldEdit { * @return true if the swing was handled */ public boolean handleArmSwing(LocalPlayer player) { - if (player.getItemInHand() == getConfiguration().navigationWand) { - if (getConfiguration().navigationWandMaxDistance <= 0) { - return false; - } - - if (!player.hasPermission("worldedit.navigation.jumpto.tool")) { - return false; - } - - WorldVector pos = player.getSolidBlockTrace(getConfiguration().navigationWandMaxDistance); - if (pos != null) { - player.findFreePosition(pos); - } else { - player.printError("No block in sight (or too far)!"); - } - return true; - } - - LocalSession session = getSession(player); - - Tool tool = session.getTool(player.getItemInHand()); - if (tool != null && tool instanceof DoubleActionTraceTool) { - if (tool.canUse(player)) { - ((DoubleActionTraceTool) tool).actSecondary(getServer(), getConfiguration(), player, session); - return true; - } - } - - return false; + PlayerInputEvent event = new PlayerInputEvent(player, InputType.PRIMARY); + getEventBus().post(event); + return event.isCancelled(); } /** @@ -760,33 +733,9 @@ public class WorldEdit { * @return true if the right click was handled */ public boolean handleRightClick(LocalPlayer player) { - if (player.getItemInHand() == getConfiguration().navigationWand) { - if (getConfiguration().navigationWandMaxDistance <= 0) { - return false; - } - - if (!player.hasPermission("worldedit.navigation.thru.tool")) { - return false; - } - - if (!player.passThroughForwardWall(40)) { - player.printError("Nothing to pass through!"); - } - - return true; - } - - LocalSession session = getSession(player); - - Tool tool = session.getTool(player.getItemInHand()); - if (tool != null && tool instanceof TraceTool) { - if (tool.canUse(player)) { - ((TraceTool) tool).actPrimary(getServer(), getConfiguration(), player, session); - return true; - } - } - - return false; + PlayerInputEvent event = new PlayerInputEvent(player, InputType.SECONDARY); + getEventBus().post(event); + return event.isCancelled(); } /** @@ -797,7 +746,7 @@ public class WorldEdit { * @return false if you want the action to go through */ public boolean handleBlockRightClick(LocalPlayer player, WorldVector clicked) { - BlockInteractEvent event = new BlockInteractEvent(player, clicked.toLocation(), SECONDARY_INPUT); + BlockInteractEvent event = new BlockInteractEvent(player, clicked.toLocation(), OPEN); getEventBus().post(event); return event.isCancelled(); } @@ -810,7 +759,7 @@ public class WorldEdit { * @return false if you want the action to go through */ public boolean handleBlockLeftClick(LocalPlayer player, WorldVector clicked) { - BlockInteractEvent event = new BlockInteractEvent(player, clicked.toLocation(), PRIMARY_INPUT); + BlockInteractEvent event = new BlockInteractEvent(player, clicked.toLocation(), HIT); getEventBus().post(event); return event.isCancelled(); } diff --git a/src/main/java/com/sk89q/worldedit/entity/Player.java b/src/main/java/com/sk89q/worldedit/entity/Player.java index 4d5a5b601..0098dc2f4 100644 --- a/src/main/java/com/sk89q/worldedit/entity/Player.java +++ b/src/main/java/com/sk89q/worldedit/entity/Player.java @@ -22,12 +22,13 @@ 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.extension.platform.Actor; import com.sk89q.worldedit.extent.inventory.BlockBag; /** * A player. */ -public interface Player extends Entity { +public interface Player extends Entity, Actor { /** * Returns true if the entity is holding a pick axe. diff --git a/src/main/java/com/sk89q/worldedit/event/actor/BlockInteractEvent.java b/src/main/java/com/sk89q/worldedit/event/platform/BlockInteractEvent.java similarity index 94% rename from src/main/java/com/sk89q/worldedit/event/actor/BlockInteractEvent.java rename to src/main/java/com/sk89q/worldedit/event/platform/BlockInteractEvent.java index cec761cdd..2fdb4e443 100644 --- a/src/main/java/com/sk89q/worldedit/event/actor/BlockInteractEvent.java +++ b/src/main/java/com/sk89q/worldedit/event/platform/BlockInteractEvent.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package com.sk89q.worldedit.event.actor; +package com.sk89q.worldedit.event.platform; import com.sk89q.worldedit.event.Cancellable; import com.sk89q.worldedit.event.Event; @@ -33,7 +33,7 @@ public class BlockInteractEvent extends Event implements Cancellable { private final Actor cause; private final Location location; - private final InteractionType type; + private final Interaction type; private boolean cancelled; /** @@ -43,7 +43,7 @@ public class BlockInteractEvent extends Event implements Cancellable { * @param location the location of the block * @param type the type of interaction */ - public BlockInteractEvent(Actor cause, Location location, InteractionType type) { + public BlockInteractEvent(Actor cause, Location location, Interaction type) { checkNotNull(cause); checkNotNull(location); checkNotNull(type); @@ -75,7 +75,7 @@ public class BlockInteractEvent extends Event implements Cancellable { * * @return the type of interaction */ - public InteractionType getType() { + public Interaction getType() { return type; } diff --git a/src/main/java/com/sk89q/worldedit/event/platform/InputType.java b/src/main/java/com/sk89q/worldedit/event/platform/InputType.java new file mode 100644 index 000000000..4f614d719 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/event/platform/InputType.java @@ -0,0 +1,37 @@ +/* + * 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.event.platform; + +/** + * The type of input sent. + */ +public enum InputType { + + /** + * Left click. + */ + PRIMARY, + + /** + * Right click. + */ + SECONDARY + +} diff --git a/src/main/java/com/sk89q/worldedit/event/actor/InteractionType.java b/src/main/java/com/sk89q/worldedit/event/platform/Interaction.java similarity index 90% rename from src/main/java/com/sk89q/worldedit/event/actor/InteractionType.java rename to src/main/java/com/sk89q/worldedit/event/platform/Interaction.java index f965de7c1..91331fa56 100644 --- a/src/main/java/com/sk89q/worldedit/event/actor/InteractionType.java +++ b/src/main/java/com/sk89q/worldedit/event/platform/Interaction.java @@ -17,20 +17,21 @@ * along with this program. If not, see . */ -package com.sk89q.worldedit.event.actor; +package com.sk89q.worldedit.event.platform; /** * The type of interaction. */ -public enum InteractionType { +public enum Interaction { /** * Refers to primary input usage (left click). */ - PRIMARY_INPUT, + HIT, /** * Refers to secondary input usage (right click). */ - SECONDARY_INPUT + OPEN + } diff --git a/src/main/java/com/sk89q/worldedit/event/platform/PlayerInputEvent.java b/src/main/java/com/sk89q/worldedit/event/platform/PlayerInputEvent.java new file mode 100644 index 000000000..2bc39ee97 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/event/platform/PlayerInputEvent.java @@ -0,0 +1,78 @@ +/* + * 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.event.platform; + +import com.sk89q.worldedit.entity.Player; +import com.sk89q.worldedit.event.Cancellable; +import com.sk89q.worldedit.event.Event; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Raised whenever a player sends input. + */ +public class PlayerInputEvent extends Event implements Cancellable { + + private final Player player; + private final InputType inputType; + private boolean cancelled; + + /** + * Create a new event. + * + * @param player the player + * @param inputType the input type + */ + public PlayerInputEvent(Player player, InputType inputType) { + checkNotNull(player); + checkNotNull(inputType); + this.player = player; + this.inputType = inputType; + } + + /** + * Get the player that sent the input. + * + * @return the player + */ + public Player getPlayer() { + return player; + } + + /** + * Get the type of input sent. + * + * @return the input sent + */ + public InputType getInputType() { + return inputType; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + +} diff --git a/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java b/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java index 7ddc5e09c..4a93df9e4 100644 --- a/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java +++ b/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java @@ -20,12 +20,11 @@ package com.sk89q.worldedit.extension.platform; import com.sk89q.worldedit.*; -import com.sk89q.worldedit.command.tool.BlockTool; -import com.sk89q.worldedit.command.tool.DoubleActionBlockTool; -import com.sk89q.worldedit.command.tool.Tool; +import com.sk89q.worldedit.command.tool.*; import com.sk89q.worldedit.entity.Player; -import com.sk89q.worldedit.event.actor.BlockInteractEvent; -import com.sk89q.worldedit.event.actor.InteractionType; +import com.sk89q.worldedit.event.platform.BlockInteractEvent; +import com.sk89q.worldedit.event.platform.Interaction; +import com.sk89q.worldedit.event.platform.PlayerInputEvent; import com.sk89q.worldedit.internal.ServerInterfaceAdapter; import com.sk89q.worldedit.regions.RegionSelector; import com.sk89q.worldedit.util.Location; @@ -213,7 +212,7 @@ public class PlatformManager { Player player = (Player) actor; LocalSession session = worldEdit.getSessionManager().get(actor); - if (event.getType() == InteractionType.PRIMARY_INPUT) { + if (event.getType() == Interaction.HIT) { if (player.getItemInHand() == getConfiguration().wandItem) { if (!session.isToolControlEnabled()) { return; @@ -254,7 +253,7 @@ public class PlatformManager { } } - } else if (event.getType() == InteractionType.SECONDARY_INPUT) { + } else if (event.getType() == Interaction.OPEN) { if (player.getItemInHand() == getConfiguration().wandItem) { if (!session.isToolControlEnabled()) { return; @@ -289,6 +288,88 @@ public class PlatformManager { } } + @Subscribe + public void handlePlayerInput(PlayerInputEvent event) { + Player player = event.getPlayer(); + + switch (event.getInputType()) { + case PRIMARY: { + if (player.getItemInHand() == getConfiguration().navigationWand) { + if (getConfiguration().navigationWandMaxDistance <= 0) { + return; + } + + if (!player.hasPermission("worldedit.navigation.jumpto.tool")) { + return; + } + + WorldVector pos = player.getSolidBlockTrace(getConfiguration().navigationWandMaxDistance); + if (pos != null) { + player.findFreePosition(pos); + } else { + player.printError("No block in sight (or too far)!"); + } + + event.setCancelled(true); + return; + } + + LocalSession session = worldEdit.getSessionManager().get(player); + + if (player instanceof LocalPlayer) { // Temporary workaround + LocalPlayer localPlayer = (LocalPlayer) player; + + Tool tool = session.getTool(player.getItemInHand()); + if (tool != null && tool instanceof DoubleActionTraceTool) { + if (tool.canUse(localPlayer)) { + ((DoubleActionTraceTool) tool).actSecondary(getServerInterface(), getConfiguration(), localPlayer, session); + event.setCancelled(true); + return; + } + } + } + + break; + } + + case SECONDARY: { + if (player.getItemInHand() == getConfiguration().navigationWand) { + if (getConfiguration().navigationWandMaxDistance <= 0) { + return; + } + + if (!player.hasPermission("worldedit.navigation.thru.tool")) { + return; + } + + if (!player.passThroughForwardWall(40)) { + player.printError("Nothing to pass through!"); + } + + event.setCancelled(true); + return; + } + + LocalSession session = worldEdit.getSessionManager().get(player); + + if (player instanceof LocalPlayer) { // Temporary workaround + LocalPlayer localPlayer = (LocalPlayer) player; + + Tool tool = session.getTool(player.getItemInHand()); + if (tool != null && tool instanceof TraceTool) { + if (tool.canUse(localPlayer)) { + ((TraceTool) tool).actPrimary(getServerInterface(), getConfiguration(), localPlayer, session); + event.setCancelled(true); + return; + } + } + } + + break; + } + } + } + /** * A default configuration for when none is set. */