Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
at develop 129 lines 3.6 kB view raw
1package org.bukkit.craftbukkit.inventory; 2 3import org.bukkit.Material; 4import org.bukkit.inventory.ItemStack; 5 6import uk.betacraft.uberbukkit.Uberbukkit; 7 8public class CraftItemStack extends ItemStack { 9 protected net.minecraft.server.ItemStack item; 10 11 public CraftItemStack(net.minecraft.server.ItemStack item) { 12 super(item != null ? item.id : 0, item != null ? item.count : 0, (short) (item != null ? item.damage : 0)); 13 this.item = item; 14 } 15 16 /* 'Overwritten' constructors from ItemStack, yay for Java sucking */ 17 public CraftItemStack(final int type) { 18 this(type, 0); 19 } 20 21 public CraftItemStack(final Material type) { 22 this(type, 0); 23 } 24 25 public CraftItemStack(final int type, final int amount) { 26 this(type, amount, (byte) 0); 27 } 28 29 public CraftItemStack(final Material type, final int amount) { 30 this(type.getId(), amount); 31 } 32 33 public CraftItemStack(final int type, final int amount, final short damage) { 34 this(type, amount, damage, null); 35 } 36 37 public CraftItemStack(final Material type, final int amount, final short damage) { 38 this(type.getId(), amount, damage); 39 } 40 41 public CraftItemStack(final Material type, final int amount, final short damage, final Byte data) { 42 this(type.getId(), amount, damage, data); 43 } 44 45 public CraftItemStack(int type, int amount, short damage, Byte data) { 46 this(new net.minecraft.server.ItemStack(type, amount, data != null ? data : damage)); 47 } 48 49 /* 50 * Unsure if we have to sync before each of these calls the values in 'item' 51 * are all public. 52 */ 53 54 @Override 55 public Material getType() { 56 super.setTypeId(item != null ? item.id : 0); // sync, needed? 57 return super.getType(); 58 } 59 60 @Override 61 public int getTypeId() { 62 super.setTypeId(item != null ? item.id : 0); // sync, needed? 63 return item != null ? item.id : 0; 64 } 65 66 @Override 67 public void setTypeId(int type) { 68 // uberbukkit 69 if (!Uberbukkit.getProtocolHandler().canReceiveBlockItem(type)) { 70 return; 71 } 72 73 if (type == 0) { 74 super.setTypeId(0); 75 super.setAmount(0); 76 item = null; 77 } else { 78 if (item == null) { 79 item = new net.minecraft.server.ItemStack(type, 1, 0); 80 super.setAmount(1); 81 } else { 82 item.id = type; 83 super.setTypeId(item.id); 84 } 85 } 86 } 87 88 @Override 89 public int getAmount() { 90 super.setAmount(item != null ? item.count : 0); // sync, needed? 91 return (item != null ? item.count : 0); 92 } 93 94 @Override 95 public void setAmount(int amount) { 96 if (amount == 0) { 97 super.setTypeId(0); 98 super.setAmount(0); 99 item = null; 100 } else { 101 super.setAmount(amount); 102 item.count = amount; 103 } 104 } 105 106 @Override 107 public void setDurability(final short durability) { 108 // Ignore damage if item is null 109 if (item != null) { 110 super.setDurability(durability); 111 item.damage = durability; 112 } 113 } 114 115 @Override 116 public short getDurability() { 117 if (item != null) { 118 super.setDurability((short) item.damage); // sync, needed? 119 return (short) item.damage; 120 } else { 121 return -1; 122 } 123 } 124 125 @Override 126 public int getMaxStackSize() { 127 return item.getItem().getMaxStackSize(); 128 } 129}