[Bleeding] Implementation of inventory framework. Addresses BUKKIT-856

See the corresponding Bukkit commit for details.

Implementation details:
- Any packets that include an itemstack will send air stacks as null; maybe this will even eliminate the client crash that occurs if the client receives an air stack
- Better handling of null itemstacks in general (ie less converting them to air stacks)
- Inventory.setContents() can now take an array smaller than the inventory without error
- Player.updateInventory() should now correctly update the result slot in a crafting inventory

Some small credit goes to Afforess (initial implementation of openInventory() methods) and Drakia (initial implementation of InventoryOpenEvent and InventoryCloseEvent).

By: Celtic Minstrel <celtic.minstrel.ca@some.place>
This commit is contained in:
CraftBukkit/Spigot
2012-02-29 13:56:35 -05:00
parent a3dccca671
commit 472b179cd1
21 changed files with 1134 additions and 43 deletions

View File

@@ -5,6 +5,7 @@ import java.util.List;
import java.util.Map;
import net.minecraft.server.ChunkCoordinates;
import net.minecraft.server.Container;
import net.minecraft.server.DamageSource;
import net.minecraft.server.Entity;
import net.minecraft.server.EntityArrow;
@@ -15,8 +16,10 @@ import net.minecraft.server.EntityItem;
import net.minecraft.server.EntityLiving;
import net.minecraft.server.EntityPlayer;
import net.minecraft.server.EntityPotion;
import net.minecraft.server.InventoryCrafting;
import net.minecraft.server.Item;
import net.minecraft.server.ItemStack;
import net.minecraft.server.Packet101CloseWindow;
import net.minecraft.server.World;
import net.minecraft.server.WorldServer;
@@ -32,6 +35,7 @@ import org.bukkit.craftbukkit.block.CraftBlock;
import org.bukkit.craftbukkit.block.CraftBlockState;
import org.bukkit.craftbukkit.entity.CraftLivingEntity;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.craftbukkit.inventory.CraftInventoryCrafting;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.entity.AnimalTamer;
import org.bukkit.entity.Arrow;
@@ -47,8 +51,11 @@ import org.bukkit.event.block.*;
import org.bukkit.event.entity.*;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.event.player.*;
import org.bukkit.event.server.ServerListPingEvent;
import org.bukkit.inventory.InventoryView;
public class CraftEventFactory {
// helper methods
@@ -413,4 +420,36 @@ public class CraftEventFactory {
entity.getBukkitEntity().getServer().getPluginManager().callEvent(event);
return event;
}
public static Container callInventoryOpenEvent(EntityPlayer player, Container container) {
if (player.activeContainer != player.defaultContainer) { // fire INVENTORY_CLOSE if one already open
player.netServerHandler.a(new Packet101CloseWindow(player.activeContainer.windowId));
}
CraftServer server = ((WorldServer) player.world).getServer();
CraftPlayer craftPlayer = (CraftPlayer) player.getBukkitEntity();
player.activeContainer.transferTo(container, craftPlayer);
InventoryOpenEvent event = new InventoryOpenEvent(container.getBukkitView());
server.getPluginManager().callEvent(event);
if (event.isCancelled()) {
container.transferTo(player.activeContainer, craftPlayer);
return null;
}
return container;
}
public static ItemStack callPreCraftEvent(InventoryCrafting matrix, ItemStack result, InventoryView lastCraftView, boolean isRepair) {
CraftInventoryCrafting inventory = new CraftInventoryCrafting(matrix, matrix.resultInventory);
inventory.setResult(new CraftItemStack(result));
PrepareItemCraftEvent event = new PrepareItemCraftEvent(inventory, lastCraftView, isRepair);
Bukkit.getPluginManager().callEvent(event);
org.bukkit.inventory.ItemStack bitem = event.getInventory().getResult();
return CraftItemStack.createNMSItemStack(bitem);
}
}