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;
|
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-06-17 07:50:04 +00:00
|
|
|
import com.sk89q.worldedit.blocks.BaseItemStack;
|
2018-12-23 16:19:33 +00:00
|
|
|
import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter;
|
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;
|
2019-04-05 18:51:18 +00:00
|
|
|
import com.sk89q.worldedit.extent.Extent;
|
2014-04-03 03:03:52 +00:00
|
|
|
import com.sk89q.worldedit.extent.inventory.BlockBag;
|
|
|
|
import com.sk89q.worldedit.internal.cui.CUIEvent;
|
2018-12-23 16:19:33 +00:00
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
|
|
|
import com.sk89q.worldedit.math.Vector3;
|
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;
|
2019-03-26 17:41:09 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BaseBlock;
|
2018-12-23 16:19:33 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
|
|
|
import com.sk89q.worldedit.world.block.BlockTypes;
|
2018-07-19 12:41:26 +00:00
|
|
|
import com.sk89q.worldedit.world.gamemode.GameMode;
|
|
|
|
import com.sk89q.worldedit.world.gamemode.GameModes;
|
2018-12-23 16:19:33 +00:00
|
|
|
|
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
|
|
|
|
2019-03-26 17:41:09 +00:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2018-06-16 05:29:48 +00:00
|
|
|
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;
|
|
|
|
|
2019-03-30 21:32:10 +00:00
|
|
|
public BukkitPlayer(Player player) {
|
|
|
|
this(WorldEditPlugin.getInstance(), player);
|
|
|
|
}
|
|
|
|
|
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-10 10:29:06 +00:00
|
|
|
public BaseBlock 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-10 10:29:06 +00:00
|
|
|
return BukkitAdapter.asBlockState(itemStack).toBaseBlock();
|
2014-04-03 03:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return player.getName();
|
|
|
|
}
|
|
|
|
|
2018-12-18 07:36:53 +00:00
|
|
|
@Override
|
|
|
|
public String getDisplayName() {
|
|
|
|
return player.getDisplayName();
|
|
|
|
}
|
|
|
|
|
2014-04-03 03:03:52 +00:00
|
|
|
@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);
|
2019-01-31 15:08:58 +00:00
|
|
|
if (itemStack.getType().getId().equalsIgnoreCase(WorldEdit.getInstance().getConfiguration().wandItem)) {
|
2018-08-15 14:04:40 +00:00
|
|
|
inv.remove(newItem);
|
|
|
|
}
|
|
|
|
final ItemStack item = player.getItemInHand();
|
|
|
|
player.setItemInHand(newItem);
|
2019-03-26 17:41:09 +00:00
|
|
|
HashMap<Integer, ItemStack> overflow = inv.addItem(item);
|
|
|
|
if (!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();
|
2018-08-15 14:04:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 17:41:09 +00:00
|
|
|
}
|
|
|
|
});
|
2018-08-15 14:04:40 +00:00
|
|
|
}
|
|
|
|
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
|
2018-12-23 16:19:33 +00:00
|
|
|
public void setPosition(Vector3 pos, float pitch, float yaw) {
|
2019-04-05 18:51:18 +00:00
|
|
|
if (pos instanceof com.sk89q.worldedit.util.Location) {
|
|
|
|
com.sk89q.worldedit.util.Location loc = (com.sk89q.worldedit.util.Location) pos;
|
|
|
|
Extent extent = loc.getExtent();
|
|
|
|
if (extent instanceof World) {
|
|
|
|
org.bukkit.World world = Bukkit.getWorld(((World) extent).getName());
|
2019-04-06 23:38:25 +00:00
|
|
|
// System.out.println("Teleport to world " + world);
|
2019-04-05 18:51:18 +00:00
|
|
|
player.teleport(new Location(world, pos.getX(), pos.getY(),
|
|
|
|
pos.getZ(), yaw, pitch));
|
|
|
|
}
|
|
|
|
}
|
2014-04-03 03:03:52 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:13:32 +00:00
|
|
|
setPosition(Vector3.at(x + 0.5, y, z + 0.5));
|
2014-04-03 03:03:52 +00:00
|
|
|
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-12-23 16:19:33 +00:00
|
|
|
Vector3 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
|
|
|
|
2018-12-21 06:56:10 +00:00
|
|
|
@Override
|
|
|
|
public boolean setLocation(com.sk89q.worldedit.util.Location location) {
|
|
|
|
return player.teleport(BukkitAdapter.adapt(location));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-27 00:39:10 +00:00
|
|
|
@Override
|
2019-04-02 20:26:51 +00:00
|
|
|
public void sendFakeBlock(BlockVector3 pos, BlockStateHolder block) {
|
2018-12-27 00:39:10 +00:00
|
|
|
Location loc = new Location(player.getWorld(), pos.getX(), pos.getY(), pos.getZ());
|
|
|
|
if (block == null) {
|
|
|
|
player.sendBlockChange(loc, player.getWorld().getBlockAt(loc).getBlockData());
|
|
|
|
} else {
|
|
|
|
player.sendBlockChange(loc, BukkitAdapter.adapt(block));
|
|
|
|
if (block instanceof BaseBlock && ((BaseBlock) block).hasNbtData()) {
|
|
|
|
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
|
|
|
|
if (adapter != null) {
|
|
|
|
adapter.sendFakeNBT(player, pos, ((BaseBlock) block).getNbtData());
|
|
|
|
if (block.getBlockType() == BlockTypes.STRUCTURE_BLOCK) {
|
|
|
|
adapter.sendFakeOP(player);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-03 03:03:52 +00:00
|
|
|
}
|