Updated events to choose the best platform for certain tasks.

This commit is contained in:
sk89q
2014-06-27 13:14:44 -07:00
parent e52ca6661f
commit 9bb70ad335
14 changed files with 282 additions and 40 deletions

View File

@ -31,13 +31,4 @@ import com.sk89q.worldedit.extension.platform.Actor;
@Deprecated
public abstract class LocalPlayer extends AbstractPlayerActor {
/**
* Construct the object.
*
* @param server A reference to the server this player is on
*/
protected LocalPlayer(ServerInterface server) {
super(server);
}
}

View File

@ -40,19 +40,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/
public abstract class AbstractPlayerActor implements Actor, Player {
private final Platform platform;
/**
* Create a new instance.
*
* @param platform the platform
*/
protected AbstractPlayerActor(Platform platform) {
checkNotNull(platform);
this.platform = platform;
}
/**
* Returns direction according to rotation. May return null.
*

View File

@ -24,8 +24,10 @@ import com.sk89q.minecraft.util.commands.CommandsManager;
import com.sk89q.worldedit.BiomeTypes;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalPlayer;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.world.World;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
@ -78,6 +80,26 @@ public interface Platform {
List<? extends World> getWorlds();
/**
* Create a duplicate of the given player.
* </p>
* The given player may have been provided by a different platform.
*
* @param player the player to match
* @return a matched player, otherwise null
*/
@Nullable Player matchPlayer(Player player);
/**
* Create a duplicate of the given world.
* </p>
* The given world may have been provided by a different platform.
*
* @param world the world to match
* @return a matched world, otherwise null
*/
@Nullable World matchWorld(World world);
@Deprecated
void onCommandRegistration(List<Command> commands);

View File

@ -31,6 +31,7 @@ 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 com.sk89q.worldedit.world.World;
import javax.annotation.Nullable;
import java.util.*;
@ -207,6 +208,44 @@ public class PlatformManager {
return new ArrayList<Platform>(platforms);
}
/**
* Given a world, possibly return the same world but using a different
* platform preferred for world editing operations.
*
* @param base the world to match
* @return the preferred world, if one was found, otherwise the given world
*/
public World getWorldForEditing(World base) {
checkNotNull(base);
World match = queryCapability(Capability.WORLD_EDITING).matchWorld(base);
return match != null ? match : base;
}
/**
* Given an actor, return a new one that may use a different platform
* for permissions and world editing.
*
* @param base the base actor to match
* @return a new delegate actor
*/
@SuppressWarnings("unchecked")
public <T extends Actor> T createProxyActor(T base) {
checkNotNull(base);
if (base instanceof Player) {
Player player = (Player) base;
Player permActor = queryCapability(Capability.PERMISSIONS).matchPlayer(player);
if (permActor == null) {
permActor = player;
}
return (T) new PlayerProxy(player, permActor, getWorldForEditing(base.getWorld()));
} else {
return base;
}
}
/**
* Get the command manager.
*
@ -247,7 +286,10 @@ public class PlatformManager {
@SuppressWarnings("deprecation")
@Subscribe
public void handleBlockInteract(BlockInteractEvent event) {
Actor actor = event.getCause();
// Create a proxy actor with a potentially different world for
// making changes to the world
Actor actor = createProxyActor(event.getCause());
Location location = event.getLocation();
Vector vector = location.toVector();
@ -335,7 +377,9 @@ public class PlatformManager {
@SuppressWarnings("deprecation")
@Subscribe
public void handlePlayerInput(PlayerInputEvent event) {
Player player = event.getPlayer();
// Create a proxy actor with a potentially different world for
// making changes to the world
Player player = createProxyActor(event.getPlayer());
switch (event.getInputType()) {
case PRIMARY: {

View File

@ -0,0 +1,125 @@
/*
* 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.worldedit.Vector;
import com.sk89q.worldedit.WorldVector;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.World;
import static com.google.common.base.Preconditions.checkNotNull;
class PlayerProxy extends AbstractPlayerActor {
private final Player basePlayer;
private final Actor permActor;
private final World world;
PlayerProxy(Player basePlayer, Actor permActor, World world) {
checkNotNull(basePlayer);
checkNotNull(permActor);
checkNotNull(world);
this.basePlayer = basePlayer;
this.permActor = permActor;
this.world = world;
}
@Override
public int getItemInHand() {
return basePlayer.getItemInHand();
}
@Override
public void giveItem(int type, int amount) {
basePlayer.giveItem(type, amount);
}
@Override
public BlockBag getInventoryBlockBag() {
return basePlayer.getInventoryBlockBag();
}
@Override
public String getName() {
return basePlayer.getName();
}
@Override
public Location getLocation() {
return basePlayer.getLocation();
}
@Override
public WorldVector getPosition() {
return basePlayer.getPosition();
}
@Override
public double getPitch() {
return basePlayer.getPitch();
}
@Override
public double getYaw() {
return basePlayer.getYaw();
}
@Override
public void setPosition(Vector pos, float pitch, float yaw) {
basePlayer.setPosition(pos, pitch, yaw);
}
@Override
public World getWorld() {
return world;
}
@Override
public void printRaw(String msg) {
basePlayer.printRaw(msg);
}
@Override
public void printDebug(String msg) {
basePlayer.printDebug(msg);
}
@Override
public void print(String msg) {
basePlayer.print(msg);
}
@Override
public void printError(String msg) {
basePlayer.printError(msg);
}
@Override
public String[] getGroups() {
return permActor.getGroups();
}
@Override
public boolean hasPermission(String perm) {
return permActor.hasPermission(perm);
}
}

View File

@ -25,11 +25,13 @@ import com.sk89q.worldedit.BiomeTypes;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalPlayer;
import com.sk89q.worldedit.ServerInterface;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.extension.platform.Preference;
import com.sk89q.worldedit.world.World;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
@ -82,6 +84,18 @@ public class ServerInterfaceAdapter extends ServerInterface {
return platform.getWorlds();
}
@Nullable
@Override
public Player matchPlayer(Player player) {
return platform.matchPlayer(player);
}
@Nullable
@Override
public World matchWorld(World world) {
return platform.matchWorld(world);
}
@Override
@Deprecated
public void onCommandRegistration(List<Command> commands) {