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;
|
|
|
|
import com.sk89q.worldedit.internal.cui.CUIEvent;
|
2018-10-14 10:40:53 +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-07-30 13:26:06 +00:00
|
|
|
import com.sk89q.worldedit.world.item.ItemTypes;
|
2018-10-14 10:40:53 +00:00
|
|
|
|
2016-01-12 10:15:33 +00:00
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
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;
|
2018-08-18 09:35:53 +00:00
|
|
|
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
|
2016-03-19 16:53:52 +00:00
|
|
|
import net.minecraft.util.EnumHand;
|
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;
|
2018-06-17 07:50:04 +00:00
|
|
|
import net.minecraftforge.fml.common.registry.ForgeRegistries;
|
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-10-14 10:40:53 +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-07-05 08:48:37 +00:00
|
|
|
return new BaseItemStack(ItemTypes.get(ForgeRegistries.ITEMS.getKey(is.getItem()).toString()));
|
2016-01-12 10:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return this.player.getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
@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) {
|
|
|
|
this.player.inventory.addItemStackToInventory(
|
|
|
|
new ItemStack(Item.getByNameOrId(itemStack.getType().getId()), itemStack.getAmount(), 0));
|
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)));
|
2016-03-19 16:53:52 +00:00
|
|
|
SPacketCustomPayload packet = new SPacketCustomPayload(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-10-14 10:40:53 +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;
|
|
|
|
}
|
2018-08-18 09:35:53 +00:00
|
|
|
|
|
|
|
@Override
|
2018-10-14 10:40:53 +00:00
|
|
|
public void sendFakeBlock(BlockVector3 pos, BlockStateHolder block) {
|
|
|
|
BlockPos loc = ForgeAdapter.toBlockPos(pos);
|
2018-08-18 09:35:53 +00:00
|
|
|
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()))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-12 10:15:33 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public SessionKey getSessionKey() {
|
|
|
|
return new SessionKeyImpl(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() {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|