2014-04-03 03:03:52 +00:00
|
|
|
/*
|
2014-04-04 22:03:18 +00:00
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
2014-04-03 03:03:52 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* 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
|
2014-04-03 03:03:52 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* 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.
|
2014-04-03 03:03:52 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-04-03 03:03:52 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-04-04 22:03:18 +00:00
|
|
|
*/
|
2014-04-03 03:03:52 +00:00
|
|
|
|
|
|
|
package com.sk89q.worldedit.bukkit;
|
|
|
|
|
2018-08-15 14:04:40 +00:00
|
|
|
import com.boydti.fawe.object.RunnableVal;
|
|
|
|
import com.boydti.fawe.util.TaskManager;
|
2014-04-03 03:03:52 +00:00
|
|
|
import com.sk89q.util.StringUtil;
|
|
|
|
import com.sk89q.worldedit.Vector;
|
2018-08-15 14:04:40 +00:00
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
2014-07-10 22:49:34 +00:00
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.blocks.BaseBlock;
|
|
|
|
import com.sk89q.worldedit.world.block.BlockState;
|
2018-06-17 07:50:04 +00:00
|
|
|
import com.sk89q.worldedit.blocks.BaseItemStack;
|
2014-07-10 22:49:34 +00:00
|
|
|
import com.sk89q.worldedit.entity.BaseEntity;
|
2018-06-16 05:29:48 +00:00
|
|
|
import com.sk89q.worldedit.extension.platform.AbstractPlayerActor;
|
2014-04-03 03:03:52 +00:00
|
|
|
import com.sk89q.worldedit.extent.inventory.BlockBag;
|
|
|
|
import com.sk89q.worldedit.internal.cui.CUIEvent;
|
2014-07-28 03:44:05 +00:00
|
|
|
import com.sk89q.worldedit.session.SessionKey;
|
2018-06-17 07:50:04 +00:00
|
|
|
import com.sk89q.worldedit.util.HandSide;
|
2018-06-16 05:29:48 +00:00
|
|
|
import com.sk89q.worldedit.world.World;
|
2018-07-19 12:41:26 +00:00
|
|
|
import com.sk89q.worldedit.world.gamemode.GameMode;
|
|
|
|
import com.sk89q.worldedit.world.gamemode.GameModes;
|
2018-08-12 16:36:39 +00:00
|
|
|
import jdk.nashorn.internal.ir.Block;
|
2014-07-28 03:44:05 +00:00
|
|
|
import org.bukkit.Bukkit;
|
2014-07-10 22:49:34 +00:00
|
|
|
import org.bukkit.Location;
|
2018-08-15 14:04:40 +00:00
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.entity.Item;
|
2014-07-10 22:49:34 +00:00
|
|
|
import org.bukkit.entity.Player;
|
2018-08-15 14:04:40 +00:00
|
|
|
import org.bukkit.event.player.PlayerDropItemEvent;
|
2014-07-10 22:49:34 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2018-08-15 14:04:40 +00:00
|
|
|
import org.bukkit.inventory.PlayerInventory;
|
2014-04-03 03:03:52 +00:00
|
|
|
|
2018-08-15 14:04:40 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2014-07-28 03:44:05 +00:00
|
|
|
import java.util.UUID;
|
2014-07-16 02:47:47 +00:00
|
|
|
|
2018-06-16 05:29:48 +00:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
|
|
public class BukkitPlayer extends AbstractPlayerActor {
|
2014-07-28 03:44:05 +00:00
|
|
|
|
2014-04-03 03:03:52 +00:00
|
|
|
private Player player;
|
|
|
|
private WorldEditPlugin plugin;
|
|
|
|
|
2018-06-16 05:29:48 +00:00
|
|
|
public BukkitPlayer(WorldEditPlugin plugin, Player player) {
|
2014-04-03 03:03:52 +00:00
|
|
|
this.plugin = plugin;
|
|
|
|
this.player = player;
|
|
|
|
}
|
|
|
|
|
2014-07-28 03:44:05 +00:00
|
|
|
@Override
|
|
|
|
public UUID getUniqueId() {
|
|
|
|
return player.getUniqueId();
|
|
|
|
}
|
|
|
|
|
2014-04-03 03:03:52 +00:00
|
|
|
@Override
|
2018-06-17 07:50:04 +00:00
|
|
|
public BaseItemStack getItemInHand(HandSide handSide) {
|
|
|
|
ItemStack itemStack = handSide == HandSide.MAIN_HAND
|
|
|
|
? player.getInventory().getItemInMainHand()
|
|
|
|
: player.getInventory().getItemInOffHand();
|
2018-07-19 02:55:02 +00:00
|
|
|
return BukkitAdapter.adapt(itemStack);
|
2014-04-03 03:03:52 +00:00
|
|
|
}
|
|
|
|
|
2014-06-27 20:14:44 +00:00
|
|
|
@Override
|
2018-08-12 16:36:39 +00:00
|
|
|
public BlockState getBlockInHand(HandSide handSide) throws WorldEditException {
|
2018-06-17 07:50:04 +00:00
|
|
|
ItemStack itemStack = handSide == HandSide.MAIN_HAND
|
|
|
|
? player.getInventory().getItemInMainHand()
|
|
|
|
: player.getInventory().getItemInOffHand();
|
2018-08-12 16:36:39 +00:00
|
|
|
return BukkitAdapter.asBlockState(itemStack);
|
2014-04-03 03:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return player.getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-18 07:53:33 +00:00
|
|
|
public void giveItem(BaseItemStack itemStack) {
|
2018-08-15 14:04:40 +00:00
|
|
|
final PlayerInventory inv = player.getInventory();
|
|
|
|
ItemStack newItem = BukkitAdapter.adapt(itemStack);
|
|
|
|
if (itemStack.getType() == WorldEdit.getInstance().getConfiguration().wandItem) {
|
|
|
|
inv.remove(newItem);
|
|
|
|
}
|
|
|
|
final ItemStack item = player.getItemInHand();
|
|
|
|
player.setItemInHand(newItem);
|
|
|
|
if (item != null) {
|
|
|
|
HashMap<Integer, ItemStack> overflow = inv.addItem(item);
|
|
|
|
if (overflow != null && !overflow.isEmpty()) {
|
|
|
|
TaskManager.IMP.sync(new RunnableVal<Object>() {
|
|
|
|
@Override
|
|
|
|
public void run(Object value) {
|
|
|
|
for (Map.Entry<Integer, ItemStack> entry : overflow.entrySet()) {
|
|
|
|
ItemStack stack = entry.getValue();
|
|
|
|
if (stack.getType() != Material.AIR && stack.getAmount() > 0) {
|
|
|
|
Item dropped = player.getWorld().dropItem(player.getLocation(), stack);
|
|
|
|
PlayerDropItemEvent event = new PlayerDropItemEvent(player, dropped);
|
|
|
|
if (event.isCancelled()) {
|
|
|
|
dropped.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
player.updateInventory();
|
2014-04-03 03:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void printRaw(String msg) {
|
|
|
|
for (String part : msg.split("\n")) {
|
|
|
|
player.sendMessage(part);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void print(String msg) {
|
|
|
|
for (String part : msg.split("\n")) {
|
|
|
|
player.sendMessage("\u00A7d" + part);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void printDebug(String msg) {
|
|
|
|
for (String part : msg.split("\n")) {
|
|
|
|
player.sendMessage("\u00A77" + part);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void printError(String msg) {
|
|
|
|
for (String part : msg.split("\n")) {
|
|
|
|
player.sendMessage("\u00A7c" + part);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setPosition(Vector pos, float pitch, float yaw) {
|
|
|
|
player.teleport(new Location(player.getWorld(), pos.getX(), pos.getY(),
|
|
|
|
pos.getZ(), yaw, pitch));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String[] getGroups() {
|
|
|
|
return plugin.getPermissionsResolver().getGroups(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BlockBag getInventoryBlockBag() {
|
|
|
|
return new BukkitPlayerBlockBag(player);
|
|
|
|
}
|
|
|
|
|
2018-07-19 12:41:26 +00:00
|
|
|
@Override
|
|
|
|
public GameMode getGameMode() {
|
|
|
|
return GameModes.get(player.getGameMode().name().toLowerCase());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setGameMode(GameMode gameMode) {
|
|
|
|
player.setGameMode(org.bukkit.GameMode.valueOf(gameMode.getId().toUpperCase()));
|
|
|
|
}
|
|
|
|
|
2014-04-03 03:03:52 +00:00
|
|
|
@Override
|
|
|
|
public boolean hasPermission(String perm) {
|
|
|
|
return (!plugin.getLocalConfiguration().noOpPermissions && player.isOp())
|
|
|
|
|| plugin.getPermissionsResolver().hasPermission(
|
|
|
|
player.getWorld().getName(), player, perm);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-16 05:29:48 +00:00
|
|
|
public World getWorld() {
|
2018-07-19 02:55:02 +00:00
|
|
|
return BukkitAdapter.adapt(player.getWorld());
|
2014-04-03 03:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void dispatchCUIEvent(CUIEvent event) {
|
|
|
|
String[] params = event.getParameters();
|
|
|
|
String send = event.getTypeId();
|
|
|
|
if (params.length > 0) {
|
|
|
|
send = send + "|" + StringUtil.joinString(params, "|");
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
player.sendPluginMessage(plugin, WorldEditPlugin.getCuiPluginChannel(), send.getBytes(CUIChannelListener.UTF_8_CHARSET));
|
2014-04-03 03:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Player getPlayer() {
|
|
|
|
return player;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void floatAt(int x, int y, int z, boolean alwaysGlass) {
|
|
|
|
if (alwaysGlass || !player.getAllowFlight()) {
|
|
|
|
super.floatAt(x, y, z, alwaysGlass);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setPosition(new Vector(x + 0.5, y, z + 0.5));
|
|
|
|
player.setFlying(true);
|
|
|
|
}
|
2014-04-27 02:48:21 +00:00
|
|
|
|
2014-04-28 06:34:39 +00:00
|
|
|
@Override
|
|
|
|
public BaseEntity getState() {
|
|
|
|
throw new UnsupportedOperationException("Cannot create a state from this object");
|
|
|
|
}
|
|
|
|
|
2014-04-27 02:48:21 +00:00
|
|
|
@Override
|
|
|
|
public com.sk89q.worldedit.util.Location getLocation() {
|
|
|
|
Location nativeLocation = player.getLocation();
|
2018-07-19 02:55:02 +00:00
|
|
|
Vector position = BukkitAdapter.asVector(nativeLocation);
|
2014-07-10 22:49:34 +00:00
|
|
|
return new com.sk89q.worldedit.util.Location(
|
|
|
|
getWorld(),
|
|
|
|
position,
|
2014-07-16 03:10:14 +00:00
|
|
|
nativeLocation.getYaw(),
|
|
|
|
nativeLocation.getPitch());
|
2014-04-27 02:48:21 +00:00
|
|
|
}
|
2014-07-16 02:47:47 +00:00
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public <T> T getFacet(Class<? extends T> cls) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-07-28 03:44:05 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public SessionKey getSessionKey() {
|
|
|
|
return new SessionKeyImpl(this.player.getUniqueId(), player.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class SessionKeyImpl implements SessionKey {
|
|
|
|
// If not static, this will leak a reference
|
|
|
|
|
|
|
|
private final UUID uuid;
|
|
|
|
private final String name;
|
|
|
|
|
|
|
|
private SessionKeyImpl(UUID uuid, String name) {
|
|
|
|
this.uuid = uuid;
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public UUID getUniqueId() {
|
|
|
|
return uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isActive() {
|
|
|
|
// This is a thread safe call on CraftBukkit because it uses a
|
|
|
|
// CopyOnWrite list for the list of players, but the Bukkit
|
|
|
|
// specification doesn't require thread safety (though the
|
|
|
|
// spec is extremely incomplete)
|
2018-07-12 12:23:03 +00:00
|
|
|
return Bukkit.getServer().getPlayer(uuid) != null;
|
2014-07-28 03:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isPersistent() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-03 03:03:52 +00:00
|
|
|
}
|