Fixes #355 (forge inventory)

This commit is contained in:
Jesse Boyd 2016-11-30 12:51:05 +11:00
parent 768230c916
commit 3bf576d5d5
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
15 changed files with 2130 additions and 0 deletions

View File

@ -30,6 +30,8 @@ public class ForgeMain {
MinecraftForge.EVENT_BUS.register(this);
FMLCommonHandler.instance().bus().register(this);
this.IMP = new FaweForge(this, event.getModLog(), event.getModMetadata(), directory);
com.sk89q.worldedit.forge.ForgePlayer.inject();
}
@Mod.EventHandler

View File

@ -0,0 +1,199 @@
package com.boydti.fawe.forge;
import com.sk89q.worldedit.WorldVector;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.blocks.ItemType;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.extent.inventory.BlockBagException;
import com.sk89q.worldedit.extent.inventory.OutOfBlocksException;
import com.sk89q.worldedit.extent.inventory.OutOfSpaceException;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
public class ForgePlayerBlockBag extends BlockBag {
private EntityPlayerMP player;
private ItemStack[] items;
private boolean changed;
/**
* Construct the object.
*
* @param player the player
*/
public ForgePlayerBlockBag(EntityPlayerMP player) {
this.player = player;
}
/**
* Loads inventory on first use.
*/
private void loadInventory() {
if (items == null) {
items = new ItemStack[player.inventory.getSizeInventory()];
for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
items[i] = player.inventory.getStackInSlot(i);
}
}
}
/**
* Get the player.
*
* @return the player
*/
public EntityPlayerMP getPlayer() {
return player;
}
@Override
public void fetchItem(BaseItem item) throws BlockBagException {
final int id = item.getType();
final int damage = item.getData();
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
assert(amount == 1);
boolean usesDamageValue = ItemType.usesDamageValue(id);
if (id == BlockID.AIR) {
throw new IllegalArgumentException("Can't fetch air block");
}
loadInventory();
boolean found = false;
for (int slot = 0; slot < items.length; ++slot) {
ItemStack forgeItem = items[slot];
if (forgeItem == null) {
continue;
}
int itemId = Item.getIdFromItem(forgeItem.getItem());
if (itemId != id) {
// Type id doesn't fit
continue;
}
if (usesDamageValue && forgeItem.getItemDamage() != damage) {
// Damage value doesn't fit.
continue;
}
int currentAmount = forgeItem.stackSize;
if (currentAmount < 0) {
// Unlimited
return;
}
changed = true;
if (currentAmount > 1) {
forgeItem.stackSize--;
found = true;
} else {
items[slot] = null;
found = true;
}
break;
}
if (!found) {
throw new OutOfBlocksException();
}
}
@Override
public void storeItem(BaseItem item) throws BlockBagException {
final int id = item.getType();
final int damage = item.getData();
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
assert(amount <= 64);
boolean usesDamageValue = ItemType.usesDamageValue(id);
if (id == BlockID.AIR) {
throw new IllegalArgumentException("Can't store air block");
}
loadInventory();
int freeSlot = -1;
for (int slot = 0; slot < items.length; ++slot) {
ItemStack forgeItem = items[slot];
if (forgeItem == null) {
// Delay using up a free slot until we know there are no stacks
// of this item to merge into
if (freeSlot == -1) {
freeSlot = slot;
}
continue;
}
int itemId = Item.getIdFromItem(forgeItem.getItem());
if (itemId != id) {
// Type id doesn't fit
continue;
}
if (usesDamageValue && forgeItem.getItemDamage() != damage) {
// Damage value doesn't fit.
continue;
}
int currentAmount = forgeItem.stackSize;
if (currentAmount < 0) {
// Unlimited
return;
}
if (currentAmount >= 64) {
// Full stack
continue;
}
changed = true;
int spaceLeft = 64 - currentAmount;
if (spaceLeft >= amount) {
forgeItem.stackSize += amount;
return;
}
forgeItem.stackSize = (64);
amount -= spaceLeft;
}
if (freeSlot > -1) {
changed = true;
items[freeSlot] = new ItemStack(Item.getItemById(id), amount);
return;
}
throw new OutOfSpaceException(id);
}
@Override
public void flushChanges() {
if (items != null && changed) {
for (int i = 0; i < items.length; i++) {
player.inventory.setInventorySlotContents(i, items[i]);
}
items = null;
changed = false;
}
}
@Override
public void addSourcePosition(WorldVector pos) {
}
@Override
public void addSingleSourcePosition(WorldVector pos) {
}
}

View File

@ -0,0 +1,227 @@
/*
* 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.boydti.fawe.forge.ForgePlayerBlockBag;
import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldVector;
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.LocalWorldAdapter;
import com.sk89q.worldedit.internal.cui.CUIEvent;
import com.sk89q.worldedit.session.SessionKey;
import com.sk89q.worldedit.util.Location;
import io.netty.buffer.Unpooled;
import java.util.UUID;
import javax.annotation.Nullable;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.server.SPacketCustomPayload;
import net.minecraft.util.EnumHand;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
public class ForgePlayer extends AbstractPlayerActor {
private final ForgePlatform platform;
private final EntityPlayerMP player;
protected ForgePlayer(ForgePlatform platform, EntityPlayerMP player) {
System.out.println("New forge player!!!!");
this.platform = platform;
this.player = player;
ThreadSafeCache.getInstance().getOnlineIds().add(getUniqueId());
}
@Override
public UUID getUniqueId() {
return player.getUniqueID();
}
@Override
public int getItemInHand() {
ItemStack is = this.player.getHeldItem(EnumHand.MAIN_HAND);
return is == null ? 0 : Item.getIdFromItem(is.getItem());
}
@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() {
Vector position = new Vector(this.player.posX, this.player.posY, this.player.posZ);
return new Location(
ForgeWorldEdit.inst.getWorld(this.player.worldObj),
position,
this.player.rotationYaw,
this.player.rotationPitch);
}
@SuppressWarnings("deprecation")
@Override
public WorldVector getPosition() {
return new WorldVector(LocalWorldAdapter.adapt(ForgeWorldEdit.inst.getWorld(this.player.worldObj)), this.player.posX, this.player.posY, this.player.posZ);
}
@Override
public com.sk89q.worldedit.world.World getWorld() {
return ForgeWorldEdit.inst.getWorld(this.player.worldObj);
}
@Override
public double getPitch() {
return this.player.rotationPitch;
}
@Override
public double getYaw() {
return this.player.rotationYaw;
}
@Override
public void giveItem(int type, int amt) {
this.player.inventory.addItemStackToInventory(new ItemStack(Item.getItemById(type), amt, 0));
}
@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)));
SPacketCustomPayload packet = new SPacketCustomPayload(ForgeWorldEdit.CUI_PLUGIN_CHANNEL, buffer);
this.player.connection.sendPacket(packet);
}
@Override
public void printRaw(String msg) {
for (String part : msg.split("\n")) {
this.player.addChatMessage(new TextComponentString(part));
}
}
@Override
public void printDebug(String msg) {
sendColorized(msg, TextFormatting.GRAY);
}
@Override
public void print(String msg) {
sendColorized(msg, TextFormatting.LIGHT_PURPLE);
}
@Override
public void printError(String msg) {
sendColorized(msg, TextFormatting.RED);
}
private void sendColorized(String msg, TextFormatting formatting) {
for (String part : msg.split("\n")) {
TextComponentString component = new TextComponentString(part);
component.getStyle().setColor(formatting);
this.player.addChatMessage(component);
}
}
@Override
public void setPosition(Vector pos, float pitch, float yaw) {
this.player.connection.setPlayerLocation(pos.getX(), pos.getY(), pos.getZ(), yaw, pitch);
}
@Override
public String[] getGroups() {
return new String[]{}; // WorldEditMod.inst.getPermissionsResolver().getGroups(this.player.username);
}
@Override
public BlockBag getInventoryBlockBag() {
return new ForgePlayerBlockBag(player);
}
@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
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;
}
}
public static Class<ForgePlayer> inject() {
return ForgePlayer.class;
}
}

View File

@ -29,6 +29,8 @@ public class ForgeMain {
MinecraftForge.EVENT_BUS.register(this);
FMLCommonHandler.instance().bus().register(this);
this.IMP = new FaweForge(this, event.getModLog(), event.getModMetadata(), directory);
com.sk89q.worldedit.forge.ForgePlayer.inject();
}
@Mod.EventHandler

View File

@ -0,0 +1,199 @@
package com.boydti.fawe.forge;
import com.sk89q.worldedit.WorldVector;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.blocks.ItemType;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.extent.inventory.BlockBagException;
import com.sk89q.worldedit.extent.inventory.OutOfBlocksException;
import com.sk89q.worldedit.extent.inventory.OutOfSpaceException;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
public class ForgePlayerBlockBag extends BlockBag {
private EntityPlayerMP player;
private ItemStack[] items;
private boolean changed;
/**
* Construct the object.
*
* @param player the player
*/
public ForgePlayerBlockBag(EntityPlayerMP player) {
this.player = player;
}
/**
* Loads inventory on first use.
*/
private void loadInventory() {
if (items == null) {
items = new ItemStack[player.inventory.getSizeInventory()];
for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
items[i] = player.inventory.getStackInSlot(i);
}
}
}
/**
* Get the player.
*
* @return the player
*/
public EntityPlayerMP getPlayer() {
return player;
}
@Override
public void fetchItem(BaseItem item) throws BlockBagException {
final int id = item.getType();
final int damage = item.getData();
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
assert(amount == 1);
boolean usesDamageValue = ItemType.usesDamageValue(id);
if (id == BlockID.AIR) {
throw new IllegalArgumentException("Can't fetch air block");
}
loadInventory();
boolean found = false;
for (int slot = 0; slot < items.length; ++slot) {
ItemStack forgeItem = items[slot];
if (forgeItem == null) {
continue;
}
int itemId = Item.getIdFromItem(forgeItem.getItem());
if (itemId != id) {
// Type id doesn't fit
continue;
}
if (usesDamageValue && forgeItem.getItemDamage() != damage) {
// Damage value doesn't fit.
continue;
}
int currentAmount = forgeItem.getCount();
if (currentAmount < 0) {
// Unlimited
return;
}
changed = true;
if (currentAmount > 1) {
forgeItem.setCount(forgeItem.getCount() - 1);;
found = true;
} else {
items[slot] = null;
found = true;
}
break;
}
if (!found) {
throw new OutOfBlocksException();
}
}
@Override
public void storeItem(BaseItem item) throws BlockBagException {
final int id = item.getType();
final int damage = item.getData();
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
assert(amount <= 64);
boolean usesDamageValue = ItemType.usesDamageValue(id);
if (id == BlockID.AIR) {
throw new IllegalArgumentException("Can't store air block");
}
loadInventory();
int freeSlot = -1;
for (int slot = 0; slot < items.length; ++slot) {
ItemStack forgeItem = items[slot];
if (forgeItem == null) {
// Delay using up a free slot until we know there are no stacks
// of this item to merge into
if (freeSlot == -1) {
freeSlot = slot;
}
continue;
}
int itemId = Item.getIdFromItem(forgeItem.getItem());
if (itemId != id) {
// Type id doesn't fit
continue;
}
if (usesDamageValue && forgeItem.getItemDamage() != damage) {
// Damage value doesn't fit.
continue;
}
int currentAmount = forgeItem.getCount();
if (currentAmount < 0) {
// Unlimited
return;
}
if (currentAmount >= 64) {
// Full stack
continue;
}
changed = true;
int spaceLeft = 64 - currentAmount;
if (spaceLeft >= amount) {
forgeItem.setCount(forgeItem.getCount() + amount);
return;
}
forgeItem.setCount(64);
amount -= spaceLeft;
}
if (freeSlot > -1) {
changed = true;
items[freeSlot] = new ItemStack(Item.getItemById(id), amount);
return;
}
throw new OutOfSpaceException(id);
}
@Override
public void flushChanges() {
if (items != null && changed) {
for (int i = 0; i < items.length; i++) {
player.inventory.setInventorySlotContents(i, items[i]);
}
items = null;
changed = false;
}
}
@Override
public void addSourcePosition(WorldVector pos) {
}
@Override
public void addSingleSourcePosition(WorldVector pos) {
}
}

View File

@ -0,0 +1,224 @@
/*
* 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.boydti.fawe.forge.ForgePlayerBlockBag;
import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldVector;
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.LocalWorldAdapter;
import com.sk89q.worldedit.internal.cui.CUIEvent;
import com.sk89q.worldedit.session.SessionKey;
import com.sk89q.worldedit.util.Location;
import io.netty.buffer.Unpooled;
import java.util.UUID;
import javax.annotation.Nullable;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.server.SPacketCustomPayload;
import net.minecraft.util.EnumHand;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
public class ForgePlayer extends AbstractPlayerActor {
private final EntityPlayerMP player;
protected ForgePlayer(EntityPlayerMP player) {
this.player = player;
ThreadSafeCache.getInstance().getOnlineIds().add(getUniqueId());
}
@Override
public UUID getUniqueId() {
return player.getUniqueID();
}
@Override
public int getItemInHand() {
ItemStack is = this.player.getHeldItem(EnumHand.MAIN_HAND);
return is == null ? 0 : Item.getIdFromItem(is.getItem());
}
@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() {
Vector position = new Vector(this.player.posX, this.player.posY, this.player.posZ);
return new Location(
ForgeWorldEdit.inst.getWorld(this.player.world),
position,
this.player.rotationYaw,
this.player.rotationPitch);
}
@SuppressWarnings("deprecation")
@Override
public WorldVector getPosition() {
return new WorldVector(LocalWorldAdapter.adapt(ForgeWorldEdit.inst.getWorld(this.player.world)), this.player.posX, this.player.posY, this.player.posZ);
}
@Override
public com.sk89q.worldedit.world.World getWorld() {
return ForgeWorldEdit.inst.getWorld(this.player.world);
}
@Override
public double getPitch() {
return this.player.rotationPitch;
}
@Override
public double getYaw() {
return this.player.rotationYaw;
}
@Override
public void giveItem(int type, int amt) {
this.player.inventory.addItemStackToInventory(new ItemStack(Item.getItemById(type), amt, 0));
}
@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)));
SPacketCustomPayload packet = new SPacketCustomPayload(ForgeWorldEdit.CUI_PLUGIN_CHANNEL, buffer);
this.player.connection.sendPacket(packet);
}
@Override
public void printRaw(String msg) {
for (String part : msg.split("\n")) {
this.player.sendMessage(new TextComponentString(part));
}
}
@Override
public void printDebug(String msg) {
sendColorized(msg, TextFormatting.GRAY);
}
@Override
public void print(String msg) {
sendColorized(msg, TextFormatting.LIGHT_PURPLE);
}
@Override
public void printError(String msg) {
sendColorized(msg, TextFormatting.RED);
}
private void sendColorized(String msg, TextFormatting formatting) {
for (String part : msg.split("\n")) {
TextComponentString component = new TextComponentString(part);
component.getStyle().setColor(formatting);
this.player.sendMessage(component);
}
}
@Override
public void setPosition(Vector pos, float pitch, float yaw) {
this.player.connection.setPlayerLocation(pos.getX(), pos.getY(), pos.getZ(), yaw, pitch);
}
@Override
public String[] getGroups() {
return new String[]{}; // WorldEditMod.inst.getPermissionsResolver().getGroups(this.player.username);
}
@Override
public BlockBag getInventoryBlockBag() {
return new ForgePlayerBlockBag(player);
}
@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
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;
}
}
public static Class<ForgePlayer> inject() {
return ForgePlayer.class;
}
}

View File

@ -31,6 +31,8 @@ public class ForgeMain {
MinecraftForge.EVENT_BUS.register(this);
FMLCommonHandler.instance().bus().register(this);
this.IMP = new FaweForge(this, event.getModLog(), event.getModMetadata(), directory);
com.sk89q.worldedit.forge.ForgePlayer.inject();
}
@Mod.EventHandler

View File

@ -0,0 +1,199 @@
package com.boydti.fawe.forge;
import com.sk89q.worldedit.WorldVector;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.blocks.ItemType;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.extent.inventory.BlockBagException;
import com.sk89q.worldedit.extent.inventory.OutOfBlocksException;
import com.sk89q.worldedit.extent.inventory.OutOfSpaceException;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
public class ForgePlayerBlockBag extends BlockBag {
private EntityPlayerMP player;
private ItemStack[] items;
private boolean changed;
/**
* Construct the object.
*
* @param player the player
*/
public ForgePlayerBlockBag(EntityPlayerMP player) {
this.player = player;
}
/**
* Loads inventory on first use.
*/
private void loadInventory() {
if (items == null) {
items = new ItemStack[player.inventory.getSizeInventory()];
for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
items[i] = player.inventory.getStackInSlot(i);
}
}
}
/**
* Get the player.
*
* @return the player
*/
public EntityPlayerMP getPlayer() {
return player;
}
@Override
public void fetchItem(BaseItem item) throws BlockBagException {
final int id = item.getType();
final int damage = item.getData();
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
assert(amount == 1);
boolean usesDamageValue = ItemType.usesDamageValue(id);
if (id == BlockID.AIR) {
throw new IllegalArgumentException("Can't fetch air block");
}
loadInventory();
boolean found = false;
for (int slot = 0; slot < items.length; ++slot) {
ItemStack forgeItem = items[slot];
if (forgeItem == null) {
continue;
}
int itemId = Item.getIdFromItem(forgeItem.getItem());
if (itemId != id) {
// Type id doesn't fit
continue;
}
if (usesDamageValue && forgeItem.getItemDamage() != damage) {
// Damage value doesn't fit.
continue;
}
int currentAmount = forgeItem.stackSize;
if (currentAmount < 0) {
// Unlimited
return;
}
changed = true;
if (currentAmount > 1) {
forgeItem.stackSize--;
found = true;
} else {
items[slot] = null;
found = true;
}
break;
}
if (!found) {
throw new OutOfBlocksException();
}
}
@Override
public void storeItem(BaseItem item) throws BlockBagException {
final int id = item.getType();
final int damage = item.getData();
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
assert(amount <= 64);
boolean usesDamageValue = ItemType.usesDamageValue(id);
if (id == BlockID.AIR) {
throw new IllegalArgumentException("Can't store air block");
}
loadInventory();
int freeSlot = -1;
for (int slot = 0; slot < items.length; ++slot) {
ItemStack forgeItem = items[slot];
if (forgeItem == null) {
// Delay using up a free slot until we know there are no stacks
// of this item to merge into
if (freeSlot == -1) {
freeSlot = slot;
}
continue;
}
int itemId = Item.getIdFromItem(forgeItem.getItem());
if (itemId != id) {
// Type id doesn't fit
continue;
}
if (usesDamageValue && forgeItem.getItemDamage() != damage) {
// Damage value doesn't fit.
continue;
}
int currentAmount = forgeItem.stackSize;
if (currentAmount < 0) {
// Unlimited
return;
}
if (currentAmount >= 64) {
// Full stack
continue;
}
changed = true;
int spaceLeft = 64 - currentAmount;
if (spaceLeft >= amount) {
forgeItem.stackSize += amount;
return;
}
forgeItem.stackSize = (64);
amount -= spaceLeft;
}
if (freeSlot > -1) {
changed = true;
items[freeSlot] = new ItemStack(Item.getItemById(id), amount);
return;
}
throw new OutOfSpaceException(id);
}
@Override
public void flushChanges() {
if (items != null && changed) {
for (int i = 0; i < items.length; i++) {
player.inventory.setInventorySlotContents(i, items[i]);
}
items = null;
changed = false;
}
}
@Override
public void addSourcePosition(WorldVector pos) {
}
@Override
public void addSingleSourcePosition(WorldVector pos) {
}
}

View File

@ -0,0 +1,220 @@
/*
* 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.boydti.fawe.forge.ForgePlayerBlockBag;
import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldVector;
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.LocalWorldAdapter;
import com.sk89q.worldedit.internal.cui.CUIEvent;
import com.sk89q.worldedit.session.SessionKey;
import com.sk89q.worldedit.util.Location;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.network.play.server.S3FPacketCustomPayload;
import net.minecraft.util.ChatComponentText;
import javax.annotation.Nullable;
import java.util.UUID;
public class ForgePlayer extends AbstractPlayerActor {
private final ForgePlatform platform;
private final EntityPlayerMP player;
protected ForgePlayer(ForgePlatform platform, EntityPlayerMP player) {
this.platform = platform;
this.player = player;
ThreadSafeCache.getInstance().getOnlineIds().add(getUniqueId());
}
@Override
public UUID getUniqueId() {
return player.getUniqueID();
}
@Override
public int getItemInHand() {
ItemStack is = this.player.getCurrentEquippedItem();
return is == null ? 0 : Item.getIdFromItem(is.getItem());
}
@Override
public String getName() {
return this.player.getCommandSenderName();
}
@Override
public BaseEntity getState() {
throw new UnsupportedOperationException("Cannot create a state from this object");
}
@Override
public Location getLocation() {
Vector position = new Vector(this.player.posX, this.player.posY, this.player.posZ);
return new Location(
ForgeWorldEdit.inst.getWorld(this.player.worldObj),
position,
this.player.cameraYaw,
this.player.cameraPitch);
}
@Override
public WorldVector getPosition() {
return new WorldVector(LocalWorldAdapter.adapt(ForgeWorldEdit.inst.getWorld(this.player.worldObj)), this.player.posX, this.player.posY, this.player.posZ);
}
@Override
public com.sk89q.worldedit.world.World getWorld() {
return ForgeWorldEdit.inst.getWorld(this.player.worldObj);
}
@Override
public double getPitch() {
return this.player.rotationPitch;
}
@Override
public double getYaw() {
return this.player.rotationYaw;
}
@Override
public void giveItem(int type, int amt) {
this.player.inventory.addItemStackToInventory(new ItemStack(Item.getItemById(type), amt, 0));
}
@Override
public void dispatchCUIEvent(CUIEvent event) {
String[] params = event.getParameters();
String send = event.getTypeId();
if (params.length > 0) {
send = send + "|" + StringUtil.joinString(params, "|");
}
S3FPacketCustomPayload packet = new S3FPacketCustomPayload(ForgeWorldEdit.CUI_PLUGIN_CHANNEL, send.getBytes(WECUIPacketHandler.UTF_8_CHARSET));
this.player.playerNetServerHandler.sendPacket(packet);
}
@Override
public void printRaw(String msg) {
for (String part : msg.split("\n")) {
this.player.addChatMessage(new ChatComponentText(part));
}
}
@Override
public void printDebug(String msg) {
for (String part : msg.split("\n")) {
this.player.addChatMessage(new ChatComponentText("\u00a77" + part));
}
}
@Override
public void print(String msg) {
for (String part : msg.split("\n")) {
this.player.addChatMessage(new ChatComponentText("\u00a7d" + part));
}
}
@Override
public void printError(String msg) {
for (String part : msg.split("\n")) {
this.player.addChatMessage(new ChatComponentText("\u00a7c" + part));
}
}
@Override
public void setPosition(Vector pos, float pitch, float yaw) {
this.player.playerNetServerHandler.setPlayerLocation(pos.getX(), pos.getY(), pos.getZ(), pitch, yaw);
}
@Override
public String[] getGroups() {
return new String[]{}; // WorldEditMod.inst.getPermissionsResolver().getGroups(this.player.username);
}
@Override
public BlockBag getInventoryBlockBag() {
return new ForgePlayerBlockBag(player);
}
@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
public SessionKey getSessionKey() {
return new SessionKeyImpl(player.getUniqueID(), player.getCommandSenderName());
}
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;
}
}
public static Class<ForgePlayer> inject() {
return ForgePlayer.class;
}
}

View File

@ -31,6 +31,8 @@ public class ForgeMain {
MinecraftForge.EVENT_BUS.register(this);
FMLCommonHandler.instance().bus().register(this);
this.IMP = new FaweForge(this, event.getModLog(), event.getModMetadata(), directory);
com.sk89q.worldedit.forge.ForgePlayer.inject();
}
@Mod.EventHandler

View File

@ -0,0 +1,199 @@
package com.boydti.fawe.forge;
import com.sk89q.worldedit.WorldVector;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.blocks.ItemType;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.extent.inventory.BlockBagException;
import com.sk89q.worldedit.extent.inventory.OutOfBlocksException;
import com.sk89q.worldedit.extent.inventory.OutOfSpaceException;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
public class ForgePlayerBlockBag extends BlockBag {
private EntityPlayerMP player;
private ItemStack[] items;
private boolean changed;
/**
* Construct the object.
*
* @param player the player
*/
public ForgePlayerBlockBag(EntityPlayerMP player) {
this.player = player;
}
/**
* Loads inventory on first use.
*/
private void loadInventory() {
if (items == null) {
items = new ItemStack[player.inventory.getSizeInventory()];
for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
items[i] = player.inventory.getStackInSlot(i);
}
}
}
/**
* Get the player.
*
* @return the player
*/
public EntityPlayerMP getPlayer() {
return player;
}
@Override
public void fetchItem(BaseItem item) throws BlockBagException {
final int id = item.getType();
final int damage = item.getData();
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
assert(amount == 1);
boolean usesDamageValue = ItemType.usesDamageValue(id);
if (id == BlockID.AIR) {
throw new IllegalArgumentException("Can't fetch air block");
}
loadInventory();
boolean found = false;
for (int slot = 0; slot < items.length; ++slot) {
ItemStack forgeItem = items[slot];
if (forgeItem == null) {
continue;
}
int itemId = Item.getIdFromItem(forgeItem.getItem());
if (itemId != id) {
// Type id doesn't fit
continue;
}
if (usesDamageValue && forgeItem.getItemDamage() != damage) {
// Damage value doesn't fit.
continue;
}
int currentAmount = forgeItem.stackSize;
if (currentAmount < 0) {
// Unlimited
return;
}
changed = true;
if (currentAmount > 1) {
forgeItem.stackSize--;
found = true;
} else {
items[slot] = null;
found = true;
}
break;
}
if (!found) {
throw new OutOfBlocksException();
}
}
@Override
public void storeItem(BaseItem item) throws BlockBagException {
final int id = item.getType();
final int damage = item.getData();
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
assert(amount <= 64);
boolean usesDamageValue = ItemType.usesDamageValue(id);
if (id == BlockID.AIR) {
throw new IllegalArgumentException("Can't store air block");
}
loadInventory();
int freeSlot = -1;
for (int slot = 0; slot < items.length; ++slot) {
ItemStack forgeItem = items[slot];
if (forgeItem == null) {
// Delay using up a free slot until we know there are no stacks
// of this item to merge into
if (freeSlot == -1) {
freeSlot = slot;
}
continue;
}
int itemId = Item.getIdFromItem(forgeItem.getItem());
if (itemId != id) {
// Type id doesn't fit
continue;
}
if (usesDamageValue && forgeItem.getItemDamage() != damage) {
// Damage value doesn't fit.
continue;
}
int currentAmount = forgeItem.stackSize;
if (currentAmount < 0) {
// Unlimited
return;
}
if (currentAmount >= 64) {
// Full stack
continue;
}
changed = true;
int spaceLeft = 64 - currentAmount;
if (spaceLeft >= amount) {
forgeItem.stackSize += amount;
return;
}
forgeItem.stackSize = (64);
amount -= spaceLeft;
}
if (freeSlot > -1) {
changed = true;
items[freeSlot] = new ItemStack(Item.getItemById(id), amount);
return;
}
throw new OutOfSpaceException(id);
}
@Override
public void flushChanges() {
if (items != null && changed) {
for (int i = 0; i < items.length; i++) {
player.inventory.setInventorySlotContents(i, items[i]);
}
items = null;
changed = false;
}
}
@Override
public void addSourcePosition(WorldVector pos) {
}
@Override
public void addSingleSourcePosition(WorldVector pos) {
}
}

View File

@ -0,0 +1,227 @@
/*
* 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.boydti.fawe.forge.ForgePlayerBlockBag;
import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldVector;
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.LocalWorldAdapter;
import com.sk89q.worldedit.internal.cui.CUIEvent;
import com.sk89q.worldedit.session.SessionKey;
import com.sk89q.worldedit.util.Location;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.server.S3FPacketCustomPayload;
import net.minecraft.util.ChatComponentText;
import io.netty.buffer.Unpooled;
import net.minecraft.util.EnumChatFormatting;
import javax.annotation.Nullable;
import java.util.UUID;
public class ForgePlayer extends AbstractPlayerActor {
private final ForgePlatform platform;
private final EntityPlayerMP player;
protected ForgePlayer(ForgePlatform platform, EntityPlayerMP player) {
this.platform = platform;
this.player = player;
ThreadSafeCache.getInstance().getOnlineIds().add(getUniqueId());
}
@Override
public UUID getUniqueId() {
return player.getUniqueID();
}
@Override
public int getItemInHand() {
ItemStack is = this.player.getCurrentEquippedItem();
return is == null ? 0 : Item.getIdFromItem(is.getItem());
}
@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() {
Vector position = new Vector(this.player.posX, this.player.posY, this.player.posZ);
return new Location(
ForgeWorldEdit.inst.getWorld(this.player.worldObj),
position,
this.player.rotationYaw,
this.player.rotationPitch);
}
@Override
public WorldVector getPosition() {
return new WorldVector(LocalWorldAdapter.adapt(ForgeWorldEdit.inst.getWorld(this.player.worldObj)), this.player.posX, this.player.posY, this.player.posZ);
}
@Override
public com.sk89q.worldedit.world.World getWorld() {
return ForgeWorldEdit.inst.getWorld(this.player.worldObj);
}
@Override
public double getPitch() {
return this.player.rotationPitch;
}
@Override
public double getYaw() {
return this.player.rotationYaw;
}
@Override
public void giveItem(int type, int amt) {
this.player.inventory.addItemStackToInventory(new ItemStack(Item.getItemById(type), amt, 0));
}
@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)));
S3FPacketCustomPayload packet = new S3FPacketCustomPayload(ForgeWorldEdit.CUI_PLUGIN_CHANNEL, buffer);
this.player.playerNetServerHandler.sendPacket(packet);
}
@Override
public void printRaw(String msg) {
for (String part : msg.split("\n")) {
this.player.addChatMessage(new ChatComponentText(part));
}
}
@Override
public void printDebug(String msg) {
sendColorized(msg, EnumChatFormatting.GRAY);
}
@Override
public void print(String msg) {
sendColorized(msg, EnumChatFormatting.LIGHT_PURPLE);
}
@Override
public void printError(String msg) {
sendColorized(msg, EnumChatFormatting.RED);
}
private void sendColorized(String msg, EnumChatFormatting formatting) {
for (String part : msg.split("\n")) {
ChatComponentText component = new ChatComponentText(part);
component.getChatStyle().setColor(formatting);
this.player.addChatMessage(component);
}
}
@Override
public void setPosition(Vector pos, float pitch, float yaw) {
this.player.playerNetServerHandler.setPlayerLocation(pos.getX(), pos.getY(), pos.getZ(), yaw, pitch);
}
@Override
public String[] getGroups() {
return new String[]{}; // WorldEditMod.inst.getPermissionsResolver().getGroups(this.player.username);
}
@Override
public BlockBag getInventoryBlockBag() {
return new ForgePlayerBlockBag(player);
}
@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
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;
}
}
public static Class<ForgePlayer> inject() {
return ForgePlayer.class;
}
}

View File

@ -30,6 +30,8 @@ public class ForgeMain {
MinecraftForge.EVENT_BUS.register(this);
FMLCommonHandler.instance().bus().register(this);
this.IMP = new FaweForge(this, event.getModLog(), event.getModMetadata(), directory);
com.sk89q.worldedit.forge.ForgePlayer.inject();
}
@Mod.EventHandler

View File

@ -0,0 +1,199 @@
package com.boydti.fawe.forge;
import com.sk89q.worldedit.WorldVector;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.blocks.ItemType;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.extent.inventory.BlockBagException;
import com.sk89q.worldedit.extent.inventory.OutOfBlocksException;
import com.sk89q.worldedit.extent.inventory.OutOfSpaceException;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
public class ForgePlayerBlockBag extends BlockBag {
private EntityPlayerMP player;
private ItemStack[] items;
private boolean changed;
/**
* Construct the object.
*
* @param player the player
*/
public ForgePlayerBlockBag(EntityPlayerMP player) {
this.player = player;
}
/**
* Loads inventory on first use.
*/
private void loadInventory() {
if (items == null) {
items = new ItemStack[player.inventory.getSizeInventory()];
for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
items[i] = player.inventory.getStackInSlot(i);
}
}
}
/**
* Get the player.
*
* @return the player
*/
public EntityPlayerMP getPlayer() {
return player;
}
@Override
public void fetchItem(BaseItem item) throws BlockBagException {
final int id = item.getType();
final int damage = item.getData();
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
assert(amount == 1);
boolean usesDamageValue = ItemType.usesDamageValue(id);
if (id == BlockID.AIR) {
throw new IllegalArgumentException("Can't fetch air block");
}
loadInventory();
boolean found = false;
for (int slot = 0; slot < items.length; ++slot) {
ItemStack forgeItem = items[slot];
if (forgeItem == null) {
continue;
}
int itemId = Item.getIdFromItem(forgeItem.getItem());
if (itemId != id) {
// Type id doesn't fit
continue;
}
if (usesDamageValue && forgeItem.getItemDamage() != damage) {
// Damage value doesn't fit.
continue;
}
int currentAmount = forgeItem.stackSize;
if (currentAmount < 0) {
// Unlimited
return;
}
changed = true;
if (currentAmount > 1) {
forgeItem.stackSize--;
found = true;
} else {
items[slot] = null;
found = true;
}
break;
}
if (!found) {
throw new OutOfBlocksException();
}
}
@Override
public void storeItem(BaseItem item) throws BlockBagException {
final int id = item.getType();
final int damage = item.getData();
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
assert(amount <= 64);
boolean usesDamageValue = ItemType.usesDamageValue(id);
if (id == BlockID.AIR) {
throw new IllegalArgumentException("Can't store air block");
}
loadInventory();
int freeSlot = -1;
for (int slot = 0; slot < items.length; ++slot) {
ItemStack forgeItem = items[slot];
if (forgeItem == null) {
// Delay using up a free slot until we know there are no stacks
// of this item to merge into
if (freeSlot == -1) {
freeSlot = slot;
}
continue;
}
int itemId = Item.getIdFromItem(forgeItem.getItem());
if (itemId != id) {
// Type id doesn't fit
continue;
}
if (usesDamageValue && forgeItem.getItemDamage() != damage) {
// Damage value doesn't fit.
continue;
}
int currentAmount = forgeItem.stackSize;
if (currentAmount < 0) {
// Unlimited
return;
}
if (currentAmount >= 64) {
// Full stack
continue;
}
changed = true;
int spaceLeft = 64 - currentAmount;
if (spaceLeft >= amount) {
forgeItem.stackSize += amount;
return;
}
forgeItem.stackSize = (64);
amount -= spaceLeft;
}
if (freeSlot > -1) {
changed = true;
items[freeSlot] = new ItemStack(Item.getItemById(id), amount);
return;
}
throw new OutOfSpaceException(id);
}
@Override
public void flushChanges() {
if (items != null && changed) {
for (int i = 0; i < items.length; i++) {
player.inventory.setInventorySlotContents(i, items[i]);
}
items = null;
changed = false;
}
}
@Override
public void addSourcePosition(WorldVector pos) {
}
@Override
public void addSingleSourcePosition(WorldVector pos) {
}
}

View File

@ -0,0 +1,227 @@
/*
* 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.boydti.fawe.forge.ForgePlayerBlockBag;
import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldVector;
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.LocalWorldAdapter;
import com.sk89q.worldedit.internal.cui.CUIEvent;
import com.sk89q.worldedit.session.SessionKey;
import com.sk89q.worldedit.util.Location;
import io.netty.buffer.Unpooled;
import java.util.UUID;
import javax.annotation.Nullable;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.server.SPacketCustomPayload;
import net.minecraft.util.EnumHand;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
public class ForgePlayer extends AbstractPlayerActor {
private final ForgePlatform platform;
private final EntityPlayerMP player;
protected ForgePlayer(ForgePlatform platform, EntityPlayerMP player) {
System.out.println("New forge player!!!!");
this.platform = platform;
this.player = player;
ThreadSafeCache.getInstance().getOnlineIds().add(getUniqueId());
}
@Override
public UUID getUniqueId() {
return player.getUniqueID();
}
@Override
public int getItemInHand() {
ItemStack is = this.player.getHeldItem(EnumHand.MAIN_HAND);
return is == null ? 0 : Item.getIdFromItem(is.getItem());
}
@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() {
Vector position = new Vector(this.player.posX, this.player.posY, this.player.posZ);
return new Location(
ForgeWorldEdit.inst.getWorld(this.player.worldObj),
position,
this.player.rotationYaw,
this.player.rotationPitch);
}
@SuppressWarnings("deprecation")
@Override
public WorldVector getPosition() {
return new WorldVector(LocalWorldAdapter.adapt(ForgeWorldEdit.inst.getWorld(this.player.worldObj)), this.player.posX, this.player.posY, this.player.posZ);
}
@Override
public com.sk89q.worldedit.world.World getWorld() {
return ForgeWorldEdit.inst.getWorld(this.player.worldObj);
}
@Override
public double getPitch() {
return this.player.rotationPitch;
}
@Override
public double getYaw() {
return this.player.rotationYaw;
}
@Override
public void giveItem(int type, int amt) {
this.player.inventory.addItemStackToInventory(new ItemStack(Item.getItemById(type), amt, 0));
}
@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)));
SPacketCustomPayload packet = new SPacketCustomPayload(ForgeWorldEdit.CUI_PLUGIN_CHANNEL, buffer);
this.player.connection.sendPacket(packet);
}
@Override
public void printRaw(String msg) {
for (String part : msg.split("\n")) {
this.player.addChatMessage(new TextComponentString(part));
}
}
@Override
public void printDebug(String msg) {
sendColorized(msg, TextFormatting.GRAY);
}
@Override
public void print(String msg) {
sendColorized(msg, TextFormatting.LIGHT_PURPLE);
}
@Override
public void printError(String msg) {
sendColorized(msg, TextFormatting.RED);
}
private void sendColorized(String msg, TextFormatting formatting) {
for (String part : msg.split("\n")) {
TextComponentString component = new TextComponentString(part);
component.getStyle().setColor(formatting);
this.player.addChatMessage(component);
}
}
@Override
public void setPosition(Vector pos, float pitch, float yaw) {
this.player.connection.setPlayerLocation(pos.getX(), pos.getY(), pos.getZ(), yaw, pitch);
}
@Override
public String[] getGroups() {
return new String[]{}; // WorldEditMod.inst.getPermissionsResolver().getGroups(this.player.username);
}
@Override
public BlockBag getInventoryBlockBag() {
return new ForgePlayerBlockBag(player);
}
@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
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;
}
}
public static Class<ForgePlayer> inject() {
return ForgePlayer.class;
}
}