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