Added BlockInteractEvent to replace handleBlockLeftClick().

Needed quite a shim for tools/brushes for now.
This commit is contained in:
sk89q
2014-06-26 16:56:40 -07:00
parent 0e00f0ac9d
commit 900c9b5258
12 changed files with 248 additions and 53 deletions

View File

@ -19,9 +19,16 @@
package com.sk89q.worldedit.extension.platform;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.ServerInterface;
import com.sk89q.worldedit.WorldEdit;
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.entity.Player;
import com.sk89q.worldedit.event.actor.BlockInteractEvent;
import com.sk89q.worldedit.internal.ServerInterfaceAdapter;
import com.sk89q.worldedit.regions.RegionSelector;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.eventbus.Subscribe;
import javax.annotation.Nullable;
import java.util.ArrayList;
@ -43,6 +50,7 @@ public class PlatformManager {
private final LocalConfiguration defaultConfig = new DefaultConfiguration();
private final List<Platform> platforms = new ArrayList<Platform>();
private final WorldEdit worldEdit;
private final CommandManager commandManager;
private @Nullable Platform primary = null;
@ -53,7 +61,11 @@ public class PlatformManager {
*/
public PlatformManager(WorldEdit worldEdit) {
checkNotNull(worldEdit);
this.worldEdit = worldEdit;
this.commandManager = new CommandManager(worldEdit);
// Register this instance for events
worldEdit.getEventBus().register(this);
}
/**
@ -182,13 +194,66 @@ public class PlatformManager {
if (platform instanceof ServerInterface) {
return (ServerInterface) platform;
} else {
return new ServerInterfaceAdapter(platform);
return ServerInterfaceAdapter.adapt(platform);
}
} else {
throw new IllegalStateException("No platform has been registered");
}
}
@Subscribe
public void handleBlockInteract(BlockInteractEvent event) {
Actor actor = event.getCause();
Location location = event.getLocation();
Vector vector = location.toVector();
// At this time, only handle interaction from players
if (actor instanceof Player) {
Player player = (Player) actor;
LocalSession session = worldEdit.getSessionManager().get(actor);
if (player.getItemInHand() == getConfiguration().wandItem) {
if (!session.isToolControlEnabled()) {
return;
}
if (!actor.hasPermission("worldedit.selection.pos")) {
return;
}
RegionSelector selector = session.getRegionSelector(player.getWorld());
if (selector.selectPrimary(location.toVector())) {
selector.explainPrimarySelection(actor, session, vector);
}
event.setCancelled(true);
return;
}
if (player instanceof LocalPlayer) { // Temporary workaround
LocalPlayer localPlayer = (LocalPlayer) player;
WorldVector worldVector = new WorldVector(location);
if (player.isHoldingPickAxe() && session.hasSuperPickAxe()) {
final BlockTool superPickaxe = session.getSuperPickaxe();
if (superPickaxe != null && superPickaxe.canUse(localPlayer)) {
event.setCancelled(superPickaxe.actPrimary(getServerInterface(), getConfiguration(), localPlayer, session, worldVector));
return;
}
}
Tool tool = session.getTool(player.getItemInHand());
if (tool != null && tool instanceof DoubleActionBlockTool) {
if (tool.canUse(localPlayer)) {
((DoubleActionBlockTool) tool).actSecondary(getServerInterface(), getConfiguration(), localPlayer, session, worldVector);
event.setCancelled(true);
}
}
}
}
}
/**
* A default configuration for when none is set.
*/

View File

@ -1,109 +0,0 @@
/*
* 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.extension.platform;
import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandsManager;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.world.World;
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Adapts {@link Platform}s into the legacy {@link ServerInterface}.
*/
class ServerInterfaceAdapter extends ServerInterface {
private final Platform platform;
/**
* Create a new adapter.
*
* @param platform the platform
*/
ServerInterfaceAdapter(Platform platform) {
checkNotNull(platform);
this.platform = platform;
}
@Override
public int resolveItem(String name) {
return platform.resolveItem(name);
}
@Override
public boolean isValidMobType(String type) {
return platform.isValidMobType(type);
}
@Override
public void reload() {
platform.reload();
}
@Override
public BiomeTypes getBiomes() {
return platform.getBiomes();
}
@Override
public int schedule(long delay, long period, Runnable task) {
return platform.schedule(delay, period, task);
}
@Override
public List<? extends World> getWorlds() {
return platform.getWorlds();
}
@Override
@Deprecated
public void onCommandRegistration(List<Command> commands) {
platform.onCommandRegistration(commands);
}
@Override
public void onCommandRegistration(List<Command> commands, CommandsManager<LocalPlayer> manager) {
platform.onCommandRegistration(commands, manager);
}
@Override
public LocalConfiguration getConfiguration() {
return platform.getConfiguration();
}
@Override
public String getVersion() {
return platform.getVersion();
}
@Override
public String getPlatformName() {
return platform.getPlatformName();
}
@Override
public String getPlatformVersion() {
return platform.getPlatformVersion();
}
}