2016-01-12 10:15:33 +00:00
|
|
|
/*
|
|
|
|
* 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.forge;
|
|
|
|
|
|
|
|
import com.sk89q.util.StringUtil;
|
2018-06-17 07:50:04 +00:00
|
|
|
import com.sk89q.worldedit.blocks.BaseItemStack;
|
2016-01-12 10:15:33 +00:00
|
|
|
import com.sk89q.worldedit.entity.BaseEntity;
|
|
|
|
import com.sk89q.worldedit.extension.platform.AbstractPlayerActor;
|
|
|
|
import com.sk89q.worldedit.extent.inventory.BlockBag;
|
2018-12-29 06:42:02 +00:00
|
|
|
import com.sk89q.worldedit.forge.net.handler.WECUIPacketHandler;
|
2016-01-12 10:15:33 +00:00
|
|
|
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;
|
2016-01-12 10:15:33 +00:00
|
|
|
import com.sk89q.worldedit.session.SessionKey;
|
2018-06-17 07:50:04 +00:00
|
|
|
import com.sk89q.worldedit.util.HandSide;
|
2016-01-12 10:15:33 +00:00
|
|
|
import com.sk89q.worldedit.util.Location;
|
2018-08-18 09:35:53 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BaseBlock;
|
|
|
|
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
2018-12-23 16:19:33 +00:00
|
|
|
|
2016-01-12 10:15:33 +00:00
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.network.PacketBuffer;
|
2016-03-19 16:53:52 +00:00
|
|
|
import net.minecraft.network.play.server.SPacketCustomPayload;
|
|
|
|
import net.minecraft.util.EnumHand;
|
2018-12-26 12:46:18 +00:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
2018-08-18 09:35:53 +00:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-03-19 16:53:52 +00:00
|
|
|
import net.minecraft.util.text.TextComponentString;
|
|
|
|
import net.minecraft.util.text.TextFormatting;
|
2016-01-12 10:15:33 +00:00
|
|
|
|
2018-06-16 05:29:48 +00:00
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2018-12-23 16:19:33 +00:00
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
|
2016-01-12 10:15:33 +00:00
|
|
|
public class ForgePlayer extends AbstractPlayerActor {
|
|
|
|
|
|
|
|
private final EntityPlayerMP player;
|
|
|
|
|
2016-11-18 06:17:04 +00:00
|
|
|
protected ForgePlayer(EntityPlayerMP player) {
|
2016-01-12 10:15:33 +00:00
|
|
|
this.player = player;
|
|
|
|
ThreadSafeCache.getInstance().getOnlineIds().add(getUniqueId());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public UUID getUniqueId() {
|
|
|
|
return player.getUniqueID();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-17 07:50:04 +00:00
|
|
|
public BaseItemStack getItemInHand(HandSide handSide) {
|
|
|
|
ItemStack is = this.player.getHeldItem(handSide == HandSide.MAIN_HAND ? EnumHand.MAIN_HAND : EnumHand.OFF_HAND);
|
2018-12-29 06:04:36 +00:00
|
|
|
return ForgeAdapter.adapt(is);
|
2016-01-12 10:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
2018-12-26 12:46:18 +00:00
|
|
|
return this.player.getName().getFormattedText();
|
2016-01-12 10:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BaseEntity getState() {
|
|
|
|
throw new UnsupportedOperationException("Cannot create a state from this object");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Location getLocation() {
|
2018-10-19 20:13:32 +00:00
|
|
|
Vector3 position = Vector3.at(this.player.posX, this.player.posY, this.player.posZ);
|
2016-01-12 10:15:33 +00:00
|
|
|
return new Location(
|
2016-11-18 06:17:04 +00:00
|
|
|
ForgeWorldEdit.inst.getWorld(this.player.world),
|
2016-01-12 10:15:33 +00:00
|
|
|
position,
|
|
|
|
this.player.rotationYaw,
|
|
|
|
this.player.rotationPitch);
|
|
|
|
}
|
|
|
|
|
2018-12-21 06:56:10 +00:00
|
|
|
@Override
|
|
|
|
public boolean setLocation(Location location) {
|
|
|
|
// TODO
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-12 10:15:33 +00:00
|
|
|
@Override
|
|
|
|
public com.sk89q.worldedit.world.World getWorld() {
|
2016-11-18 06:17:04 +00:00
|
|
|
return ForgeWorldEdit.inst.getWorld(this.player.world);
|
2016-01-12 10:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-18 07:53:33 +00:00
|
|
|
public void giveItem(BaseItemStack itemStack) {
|
2018-12-29 06:04:36 +00:00
|
|
|
this.player.inventory.addItemStackToInventory(ForgeAdapter.adapt(itemStack));
|
2016-01-12 10:15:33 +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, "|");
|
|
|
|
}
|
|
|
|
PacketBuffer buffer = new PacketBuffer(Unpooled.copiedBuffer(send.getBytes(WECUIPacketHandler.UTF_8_CHARSET)));
|
2018-12-26 12:46:18 +00:00
|
|
|
SPacketCustomPayload packet = new SPacketCustomPayload(new ResourceLocation(ForgeWorldEdit.CUI_PLUGIN_CHANNEL), buffer);
|
2016-06-18 07:32:17 +00:00
|
|
|
this.player.connection.sendPacket(packet);
|
2016-01-12 10:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void printRaw(String msg) {
|
|
|
|
for (String part : msg.split("\n")) {
|
2016-11-18 06:17:04 +00:00
|
|
|
this.player.sendMessage(new TextComponentString(part));
|
2016-01-12 10:15:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void printDebug(String msg) {
|
2016-03-19 16:53:52 +00:00
|
|
|
sendColorized(msg, TextFormatting.GRAY);
|
2016-01-12 10:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void print(String msg) {
|
2016-03-19 16:53:52 +00:00
|
|
|
sendColorized(msg, TextFormatting.LIGHT_PURPLE);
|
2016-01-12 10:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void printError(String msg) {
|
2016-03-19 16:53:52 +00:00
|
|
|
sendColorized(msg, TextFormatting.RED);
|
2016-01-12 10:15:33 +00:00
|
|
|
}
|
|
|
|
|
2016-03-19 16:53:52 +00:00
|
|
|
private void sendColorized(String msg, TextFormatting formatting) {
|
2016-01-12 10:15:33 +00:00
|
|
|
for (String part : msg.split("\n")) {
|
2016-03-19 16:53:52 +00:00
|
|
|
TextComponentString component = new TextComponentString(part);
|
|
|
|
component.getStyle().setColor(formatting);
|
2016-11-18 06:17:04 +00:00
|
|
|
this.player.sendMessage(component);
|
2016-01-12 10:15:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-12-23 16:19:33 +00:00
|
|
|
public void setPosition(Vector3 pos, float pitch, float yaw) {
|
2016-06-18 07:32:17 +00:00
|
|
|
this.player.connection.setPlayerLocation(pos.getX(), pos.getY(), pos.getZ(), yaw, pitch);
|
2016-01-12 10:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String[] getGroups() {
|
|
|
|
return new String[]{}; // WorldEditMod.inst.getPermissionsResolver().getGroups(this.player.username);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BlockBag getInventoryBlockBag() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasPermission(String perm) {
|
|
|
|
return ForgeWorldEdit.inst.getPermissionsProvider().hasPermission(player, perm);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public <T> T getFacet(Class<? extends T> cls) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-12-27 00:39:10 +00:00
|
|
|
public <B extends BlockStateHolder<B>> void sendFakeBlock(BlockVector3 pos, B block) {
|
2018-12-23 16:19:33 +00:00
|
|
|
BlockPos loc = ForgeAdapter.toBlockPos(pos);
|
|
|
|
if (block == null) {
|
|
|
|
// TODO
|
|
|
|
// player.sendBlockChange(loc, player.getWorld().getBlockAt(loc).getBlockData());
|
|
|
|
} else {
|
|
|
|
// TODO
|
|
|
|
// player.sendBlockChange(loc, BukkitAdapter.adapt(block));
|
|
|
|
if (block instanceof BaseBlock && ((BaseBlock) block).hasNbtData()) {
|
|
|
|
player.connection.sendPacket(new SPacketUpdateTileEntity(
|
|
|
|
new BlockPos(pos.getBlockX(), pos.getBlockY(), pos.getBlockZ()), 7,
|
|
|
|
NBTConverter.toNative(((BaseBlock) block).getNbtData()))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-12 10:15:33 +00:00
|
|
|
public SessionKey getSessionKey() {
|
2018-12-26 12:46:18 +00:00
|
|
|
return new SessionKeyImpl(player.getUniqueID(), player.getName().getString());
|
2016-01-12 10:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
// We can't directly check if the player is online because
|
|
|
|
// the list of players is not thread safe
|
|
|
|
return ThreadSafeCache.getInstance().getOnlineIds().contains(uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isPersistent() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|