From 3bf576d5d59b6b036e5c45d772b7729b4981c84a Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Wed, 30 Nov 2016 12:51:05 +1100 Subject: [PATCH] Fixes #355 (forge inventory) --- .../java/com/boydti/fawe/forge/ForgeMain.java | 2 + .../fawe/forge/ForgePlayerBlockBag.java | 199 +++++++++++++++ .../sk89q/worldedit/forge/ForgePlayer.java | 227 ++++++++++++++++++ .../java/com/boydti/fawe/forge/ForgeMain.java | 2 + .../fawe/forge/ForgePlayerBlockBag.java | 199 +++++++++++++++ .../sk89q/worldedit/forge/ForgePlayer.java | 224 +++++++++++++++++ .../java/com/boydti/fawe/forge/ForgeMain.java | 2 + .../fawe/forge/ForgePlayerBlockBag.java | 199 +++++++++++++++ .../sk89q/worldedit/forge/ForgePlayer.java | 220 +++++++++++++++++ .../java/com/boydti/fawe/forge/ForgeMain.java | 2 + .../fawe/forge/ForgePlayerBlockBag.java | 199 +++++++++++++++ .../sk89q/worldedit/forge/ForgePlayer.java | 227 ++++++++++++++++++ .../java/com/boydti/fawe/forge/ForgeMain.java | 2 + .../fawe/forge/ForgePlayerBlockBag.java | 199 +++++++++++++++ .../sk89q/worldedit/forge/ForgePlayer.java | 227 ++++++++++++++++++ 15 files changed, 2130 insertions(+) create mode 100644 forge110/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java create mode 100644 forge110/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java create mode 100644 forge111/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java create mode 100644 forge111/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java create mode 100644 forge1710/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java create mode 100644 forge1710/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java create mode 100644 forge189/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java create mode 100644 forge189/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java create mode 100644 forge194/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java create mode 100644 forge194/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java diff --git a/forge110/src/main/java/com/boydti/fawe/forge/ForgeMain.java b/forge110/src/main/java/com/boydti/fawe/forge/ForgeMain.java index 672a75e0..31abd570 100644 --- a/forge110/src/main/java/com/boydti/fawe/forge/ForgeMain.java +++ b/forge110/src/main/java/com/boydti/fawe/forge/ForgeMain.java @@ -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 diff --git a/forge110/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java b/forge110/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java new file mode 100644 index 00000000..e33836c6 --- /dev/null +++ b/forge110/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java @@ -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) { + } + +} \ No newline at end of file diff --git a/forge110/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java b/forge110/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java new file mode 100644 index 00000000..ba94fcb5 --- /dev/null +++ b/forge110/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java @@ -0,0 +1,227 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * 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 . + */ + +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 getFacet(Class 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 inject() { + return ForgePlayer.class; + } +} \ No newline at end of file diff --git a/forge111/src/main/java/com/boydti/fawe/forge/ForgeMain.java b/forge111/src/main/java/com/boydti/fawe/forge/ForgeMain.java index c35b6d8c..de1def5d 100644 --- a/forge111/src/main/java/com/boydti/fawe/forge/ForgeMain.java +++ b/forge111/src/main/java/com/boydti/fawe/forge/ForgeMain.java @@ -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 diff --git a/forge111/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java b/forge111/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java new file mode 100644 index 00000000..183ee439 --- /dev/null +++ b/forge111/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java @@ -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) { + } + +} \ No newline at end of file diff --git a/forge111/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java b/forge111/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java new file mode 100644 index 00000000..2d214382 --- /dev/null +++ b/forge111/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java @@ -0,0 +1,224 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * 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 . + */ + +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 getFacet(Class 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 inject() { + return ForgePlayer.class; + } +} \ No newline at end of file diff --git a/forge1710/src/main/java/com/boydti/fawe/forge/ForgeMain.java b/forge1710/src/main/java/com/boydti/fawe/forge/ForgeMain.java index a7c56424..fe300d95 100644 --- a/forge1710/src/main/java/com/boydti/fawe/forge/ForgeMain.java +++ b/forge1710/src/main/java/com/boydti/fawe/forge/ForgeMain.java @@ -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 diff --git a/forge1710/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java b/forge1710/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java new file mode 100644 index 00000000..e33836c6 --- /dev/null +++ b/forge1710/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java @@ -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) { + } + +} \ No newline at end of file diff --git a/forge1710/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java b/forge1710/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java new file mode 100644 index 00000000..d2ae2890 --- /dev/null +++ b/forge1710/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java @@ -0,0 +1,220 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * 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 . + */ + +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 getFacet(Class 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 inject() { + return ForgePlayer.class; + } +} \ No newline at end of file diff --git a/forge189/src/main/java/com/boydti/fawe/forge/ForgeMain.java b/forge189/src/main/java/com/boydti/fawe/forge/ForgeMain.java index 21cfd85f..1120dd8c 100644 --- a/forge189/src/main/java/com/boydti/fawe/forge/ForgeMain.java +++ b/forge189/src/main/java/com/boydti/fawe/forge/ForgeMain.java @@ -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 diff --git a/forge189/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java b/forge189/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java new file mode 100644 index 00000000..e33836c6 --- /dev/null +++ b/forge189/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java @@ -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) { + } + +} \ No newline at end of file diff --git a/forge189/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java b/forge189/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java new file mode 100644 index 00000000..ee00a4f4 --- /dev/null +++ b/forge189/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java @@ -0,0 +1,227 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * 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 . + */ + +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 getFacet(Class 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 inject() { + return ForgePlayer.class; + } + +} \ No newline at end of file diff --git a/forge194/src/main/java/com/boydti/fawe/forge/ForgeMain.java b/forge194/src/main/java/com/boydti/fawe/forge/ForgeMain.java index 672a75e0..31abd570 100644 --- a/forge194/src/main/java/com/boydti/fawe/forge/ForgeMain.java +++ b/forge194/src/main/java/com/boydti/fawe/forge/ForgeMain.java @@ -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 diff --git a/forge194/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java b/forge194/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java new file mode 100644 index 00000000..e33836c6 --- /dev/null +++ b/forge194/src/main/java/com/boydti/fawe/forge/ForgePlayerBlockBag.java @@ -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) { + } + +} \ No newline at end of file diff --git a/forge194/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java b/forge194/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java new file mode 100644 index 00000000..ba94fcb5 --- /dev/null +++ b/forge194/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java @@ -0,0 +1,227 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * 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 . + */ + +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 getFacet(Class 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 inject() { + return ForgePlayer.class; + } +} \ No newline at end of file