SPIGOT-1080: Added support for opening all inventories implementing Bukkit's Inventory class.

This commit is contained in:
Riccardo Paolo Bestetti 2015-07-30 18:34:01 +10:00 committed by md_5
parent 9935adc4ed
commit 5e85956281
2 changed files with 235 additions and 61 deletions

View file

@ -175,67 +175,68 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
EntityPlayer player = (EntityPlayer) getHandle(); EntityPlayer player = (EntityPlayer) getHandle();
InventoryType type = inventory.getType(); InventoryType type = inventory.getType();
Container formerContainer = getHandle().activeContainer; Container formerContainer = getHandle().activeContainer;
// TODO: Should we check that it really IS a CraftInventory first?
CraftInventory craftinv = (CraftInventory) inventory; IInventory iinventory = (inventory instanceof CraftInventory) ? ((CraftInventory) inventory).getInventory() : new org.bukkit.craftbukkit.inventory.InventoryWrapper(inventory);
switch(type) {
case PLAYER: switch (type) {
case CHEST: case PLAYER:
case ENDER_CHEST: case CHEST:
getHandle().openContainer(craftinv.getInventory()); case ENDER_CHEST:
break; getHandle().openContainer(iinventory);
case DISPENSER: break;
if (craftinv.getInventory() instanceof TileEntityDispenser) { case DISPENSER:
getHandle().openContainer((TileEntityDispenser) craftinv.getInventory()); if (iinventory instanceof TileEntityDispenser) {
} else { getHandle().openContainer((TileEntityDispenser) iinventory);
openCustomInventory(inventory, player, "minecraft:dispenser"); } else {
} openCustomInventory(inventory, player, "minecraft:dispenser");
break; }
case FURNACE: break;
if (craftinv.getInventory() instanceof TileEntityFurnace) { case FURNACE:
getHandle().openContainer((TileEntityFurnace) craftinv.getInventory()); if (iinventory instanceof TileEntityFurnace) {
} else { getHandle().openContainer((TileEntityFurnace) iinventory);
openCustomInventory(inventory, player, "minecraft:furnace"); } else {
} openCustomInventory(inventory, player, "minecraft:furnace");
break; }
case WORKBENCH: break;
openCustomInventory(inventory, player, "minecraft:crafting_table"); case WORKBENCH:
break; openCustomInventory(inventory, player, "minecraft:crafting_table");
case BREWING: break;
if (craftinv.getInventory() instanceof TileEntityBrewingStand) { case BREWING:
getHandle().openContainer((TileEntityBrewingStand) craftinv.getInventory()); if (iinventory instanceof TileEntityBrewingStand) {
} else { getHandle().openContainer((TileEntityBrewingStand) iinventory);
openCustomInventory(inventory, player, "minecraft:brewing_stand"); } else {
} openCustomInventory(inventory, player, "minecraft:brewing_stand");
break; }
case ENCHANTING: break;
openCustomInventory(inventory, player, "minecraft:enchanting_table"); case ENCHANTING:
break; openCustomInventory(inventory, player, "minecraft:enchanting_table");
case HOPPER: break;
if (craftinv.getInventory() instanceof TileEntityHopper) { case HOPPER:
getHandle().openContainer((TileEntityHopper) craftinv.getInventory()); if (iinventory instanceof TileEntityHopper) {
} else if (craftinv.getInventory() instanceof EntityMinecartHopper) { getHandle().openContainer((TileEntityHopper) iinventory);
getHandle().openContainer((EntityMinecartHopper) craftinv.getInventory()); } else if (iinventory instanceof EntityMinecartHopper) {
} else { getHandle().openContainer((EntityMinecartHopper) iinventory);
openCustomInventory(inventory, player, "minecraft:hopper"); } else {
} openCustomInventory(inventory, player, "minecraft:hopper");
break; }
case BEACON: break;
if (craftinv.getInventory() instanceof TileEntityBeacon) { case BEACON:
getHandle().openContainer((TileEntityBeacon) craftinv.getInventory()); if (iinventory instanceof TileEntityBeacon) {
} else { getHandle().openContainer((TileEntityBeacon) iinventory);
openCustomInventory(inventory, player, "minecraft:beacon"); } else {
} openCustomInventory(inventory, player, "minecraft:beacon");
break; }
case ANVIL: break;
if (craftinv.getInventory() instanceof BlockAnvil.TileEntityContainerAnvil) { case ANVIL:
getHandle().openTileEntity((BlockAnvil.TileEntityContainerAnvil) craftinv.getInventory()); if (iinventory instanceof BlockAnvil.TileEntityContainerAnvil) {
} else { getHandle().openTileEntity((BlockAnvil.TileEntityContainerAnvil) iinventory);
openCustomInventory(inventory, player, "minecraft:anvil"); } else {
} openCustomInventory(inventory, player, "minecraft:anvil");
break; }
case CREATIVE: break;
case CRAFTING: case CREATIVE:
throw new IllegalArgumentException("Can't open a " + type + " inventory!"); case CRAFTING:
throw new IllegalArgumentException("Can't open a " + type + " inventory!");
} }
if (getHandle().activeContainer == formerContainer) { if (getHandle().activeContainer == formerContainer) {
return null; return null;

View file

@ -0,0 +1,173 @@
package org.bukkit.craftbukkit.inventory;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.server.EntityHuman;
import net.minecraft.server.IChatBaseComponent;
import net.minecraft.server.IInventory;
import net.minecraft.server.ItemStack;
import org.bukkit.craftbukkit.entity.CraftHumanEntity;
import org.bukkit.craftbukkit.util.CraftChatMessage;
import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
public class InventoryWrapper implements IInventory {
private final Inventory inventory;
private final List<HumanEntity> viewers = new ArrayList<HumanEntity>();
public InventoryWrapper(Inventory inventory) {
this.inventory = inventory;
}
@Override
public int getSize() {
return inventory.getSize();
}
@Override
public ItemStack getItem(int i) {
return CraftItemStack.asNMSCopy(inventory.getItem(i));
}
@Override
public ItemStack splitStack(int i, int j) {
// Copied from CraftItemStack
ItemStack stack = getItem(i);
ItemStack result;
if (stack == null) {
return null;
}
if (stack.count <= j) {
this.setItem(i, null);
result = stack;
} else {
result = CraftItemStack.copyNMSStack(stack, j);
stack.count -= j;
}
this.update();
return result;
}
@Override
public ItemStack splitWithoutUpdate(int i) {
// Copied from CraftItemStack
ItemStack stack = getItem(i);
ItemStack result;
if (stack == null) {
return null;
}
if (stack.count <= 1) {
this.setItem(i, null);
result = stack;
} else {
result = CraftItemStack.copyNMSStack(stack, 1);
stack.count -= 1;
}
return result;
}
@Override
public void setItem(int i, ItemStack itemstack) {
inventory.setItem(i, CraftItemStack.asBukkitCopy(itemstack));
}
@Override
public int getMaxStackSize() {
return inventory.getMaxStackSize();
}
@Override
public void update() {
}
@Override
public boolean a(EntityHuman entityhuman) {
return true;
}
@Override
public void startOpen(EntityHuman entityhuman) {
}
@Override
public void closeContainer(EntityHuman entityhuman) {
}
@Override
public boolean b(int i, ItemStack itemstack) {
return true;
}
@Override
public int getProperty(int i) {
return 0;
}
@Override
public void b(int i, int j) {
}
@Override
public int g() {
return 0;
}
@Override
public void l() {
inventory.clear();
}
@Override
public ItemStack[] getContents() {
int size = getSize();
ItemStack[] items = new ItemStack[size];
for (int i = 0; i < size; i++) {
items[i] = getItem(i);
}
return items;
}
@Override
public void onOpen(CraftHumanEntity who) {
viewers.add(who);
}
@Override
public void onClose(CraftHumanEntity who) {
viewers.remove(who);
}
@Override
public List<HumanEntity> getViewers() {
return viewers;
}
@Override
public InventoryHolder getOwner() {
return inventory.getHolder();
}
@Override
public void setMaxStackSize(int size) {
inventory.setMaxStackSize(size);
}
@Override
public String getName() {
return inventory.getName();
}
@Override
public boolean hasCustomName() {
return getName() != null;
}
@Override
public IChatBaseComponent getScoreboardDisplayName() {
return CraftChatMessage.fromString(getName())[0];
}
}