Added events for the other WorldEdit.handle*() methods.

This commit is contained in:
sk89q 2014-06-26 20:07:04 -07:00
parent 7827dfea9e
commit d9cea950b0
7 changed files with 227 additions and 80 deletions

View File

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

View File

@ -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.

View File

@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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;
}

View File

@ -0,0 +1,37 @@
/*
* 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.event.platform;
/**
* The type of input sent.
*/
public enum InputType {
/**
* Left click.
*/
PRIMARY,
/**
* Right click.
*/
SECONDARY
}

View File

@ -17,20 +17,21 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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
}

View File

@ -0,0 +1,78 @@
/*
* 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.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;
}
}

View File

@ -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.
*/