[Bleeding] Fix the openInventory methods for custom inventories. Fixes BUKKIT-1248

Details:
- The attributes of custom inventory views are no longer ignored
- Enchanting or crafting inventories no longer ignore the passed inventory and open a new one
- Inventories associated with tile entities no longer raise a class cast exception if there was no associated tile entity
- InventoryOpenEvent and InventoryCloseEvent (if they already had some other inventory open) now fire in all cases
- If for any reason the inventory failed to open, the method now returns null instead of returned the previous inventory they had open (or the default inventory, if none)
This commit is contained in:
Celtic Minstrel
2012-03-17 13:06:21 -04:00
committed by EvilSeph
parent 43001ca2a8
commit 784aa3b602
2 changed files with 100 additions and 33 deletions

View File

@@ -1,7 +1,9 @@
package org.bukkit.craftbukkit.inventory;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import net.minecraft.server.Container;
@@ -28,6 +30,30 @@ public class CraftContainer extends Container {
setupSlots(top, bottom);
}
public CraftContainer(final Inventory inventory, final HumanEntity player, int id) {
this(new InventoryView() {
@Override
public Inventory getTopInventory() {
return inventory;
}
@Override
public Inventory getBottomInventory() {
return player.getInventory();
}
@Override
public HumanEntity getPlayer() {
return player;
}
@Override
public InventoryType getType() {
return inventory.getType();
}
}, id);
}
@Override
public InventoryView getBukkitView() {
return view;
@@ -50,27 +76,7 @@ public class CraftContainer extends Container {
cachedTitle = view.getTitle();
if (view.getPlayer() instanceof CraftPlayer) {
CraftPlayer player = (CraftPlayer) view.getPlayer();
int type;
switch(cachedType) {
case WORKBENCH:
type = 1;
break;
case FURNACE:
type = 2;
break;
case DISPENSER:
type = 3;
break;
case ENCHANTING:
type = 4;
break;
case BREWING:
type = 5;
break;
default:
type = 0;
break;
}
int type = getNotchInventoryType(cachedType);
IInventory top = ((CraftInventory)view.getTopInventory()).getInventory();
IInventory bottom = ((CraftInventory)view.getBottomInventory()).getInventory();
this.d.clear();
@@ -85,6 +91,31 @@ public class CraftContainer extends Container {
return true;
}
public static int getNotchInventoryType(InventoryType type) {
int typeID;
switch(type) {
case WORKBENCH:
typeID = 1;
break;
case FURNACE:
typeID = 2;
break;
case DISPENSER:
typeID = 3;
break;
case ENCHANTING:
typeID = 4;
break;
case BREWING:
typeID = 5;
break;
default:
typeID = 0;
break;
}
return typeID;
}
private void setupSlots(IInventory top, IInventory bottom) {
switch(cachedType) {
case CREATIVE: