public static void handleEditBookEvent(EntityPlayer player, ItemStack newBookItem) { int itemInHandIndex = player.inventory.itemInHandIndex; PlayerEditBookEvent editBookEvent = new PlayerEditBookEvent(player.getBukkitEntity(), player.inventory.itemInHandIndex, (BookMeta) CraftItemStack.getItemMeta(player.inventory.getItemInHand()), (BookMeta) CraftItemStack.getItemMeta(newBookItem), newBookItem.getItem() == Items.WRITTEN_BOOK); player.world.getServer().getPluginManager().callEvent(editBookEvent); ItemStack itemInHand = player.inventory.getItem(itemInHandIndex); // If they've got the same item in their hand, it'll need to be updated. if (itemInHand != null && itemInHand.getItem() == Items.BOOK_AND_QUILL) { if (!editBookEvent.isCancelled()) { CraftItemStack.setItemMeta(itemInHand, editBookEvent.getNewBookMeta()); if (editBookEvent.isSigning()) { itemInHand.setItem(Items.WRITTEN_BOOK); } } // Client will have updated its idea of the book item; we need to overwrite that Slot slot = player.activeContainer.getSlot(player.inventory, itemInHandIndex); player.playerConnection.sendPacket(new PacketPlayOutSetSlot(player.activeContainer.windowId, slot.rawSlotIndex, itemInHand)); } }
public static void handleEditBookEvent(net.minecraft.entity.player.EntityPlayerMP player, net.minecraft.item.ItemStack newBookItem) { int itemInHandIndex = player.inventory.currentItem; PlayerEditBookEvent editBookEvent = new PlayerEditBookEvent(player.getBukkitEntity(), player.inventory.currentItem, (BookMeta) CraftItemStack.getItemMeta(player.inventory.getCurrentItem()), (BookMeta) CraftItemStack.getItemMeta(newBookItem), newBookItem.getItem() == net.minecraft.init.Items.written_book); player.worldObj.getServer().getPluginManager().callEvent(editBookEvent); net.minecraft.item.ItemStack itemInHand = player.inventory.getStackInSlot(itemInHandIndex); // If they've got the same item in their hand, it'll need to be updated. if (itemInHand != null && itemInHand.getItem() == net.minecraft.init.Items.writable_book) { if (!editBookEvent.isCancelled()) { CraftItemStack.setItemMeta(itemInHand, editBookEvent.getNewBookMeta()); if (editBookEvent.isSigning()) { itemInHand.func_150996_a(net.minecraft.init.Items.written_book); } } // Client will have updated its idea of the book item; we need to overwrite that net.minecraft.inventory.Slot slot = player.openContainer.getSlotFromInventory((net.minecraft.inventory.IInventory) player.inventory, itemInHandIndex); player.playerNetServerHandler.sendPacket(new net.minecraft.network.play.server.S2FPacketSetSlot(player.openContainer.windowId, slot.slotNumber, itemInHand)); } }
@Override public ItemStack modifyItemStack(ItemStack stack, String arguments) { net.minecraft.item.ItemStack nmsStack = CraftItemStack.asNMSCopy(stack); try { nmsStack.setTagCompound((net.minecraft.nbt.NBTTagCompound) net.minecraft.nbt.JsonToNBT.func_150315_a(arguments)); } catch (net.minecraft.nbt.NBTException e) { e.printStackTrace(); } stack.setItemMeta(CraftItemStack.getItemMeta(nmsStack)); return stack; }
public static void showFakeItems(Plugin plugin, Player viewer, Location location, org.bukkit.inventory.ItemStack item, int count, Duration duration) { if(count <= 0) return; final EntityPlayer nmsPlayer = ((CraftPlayer) viewer).getHandle(); final int[] entityIds = new int[count]; for(int i = 0; i < count; i++) { final EntityItem entity = new EntityItem(nmsPlayer.getWorld(), location.getX(), location.getY(), location.getZ(), CraftItemStack.asNMSCopy(item)); entity.motX = randomEntityVelocity(); entity.motY = randomEntityVelocity(); entity.motZ = randomEntityVelocity(); sendPacket(viewer, new PacketPlayOutSpawnEntity(entity, ENTITY_TYPE_IDS.get(org.bukkit.entity.Item.class))); sendPacket(viewer, new PacketPlayOutEntityMetadata(entity.getId(), entity.getDataWatcher(), true)); entityIds[i] = entity.getId(); } scheduleEntityDestroy(plugin, viewer.getUniqueId(), duration, entityIds); }
public CraftFirework(CraftServer server, EntityFireworkRocket entity) { super(server, entity); ItemStack item = getHandle().getDataWatcher().getWatchableObjectItemStack(FIREWORK_ITEM_INDEX); if (item == null) { item = new ItemStack(Items.fireworks); getHandle().getDataWatcher().updateObject(FIREWORK_ITEM_INDEX, item); } this.item = CraftItemStack.asCraftMirror(item); // Ensure the item is a firework... if (this.item.getType() != Material.FIREWORK) { this.item.setType(Material.FIREWORK); } }
private static PlayerEvent getPlayerBucketEvent(boolean isFilling, EntityHuman who, int clickedX, int clickedY, int clickedZ, int clickedFace, ItemStack itemstack, net.minecraft.server.Item item) { Player player = (who == null) ? null : (Player) who.getBukkitEntity(); CraftItemStack itemInHand = CraftItemStack.asNewCraftStack(item); Material bucket = CraftMagicNumbers.getMaterial(itemstack.getItem()); CraftWorld craftWorld = (CraftWorld) player.getWorld(); CraftServer craftServer = (CraftServer) player.getServer(); Block blockClicked = craftWorld.getBlockAt(clickedX, clickedY, clickedZ); BlockFace blockFace = CraftBlock.notchToBlockFace(clickedFace); PlayerEvent event = null; if (isFilling) { event = new PlayerBucketFillEvent(player, blockClicked, blockFace, bucket, itemInHand); ((PlayerBucketFillEvent) event).setCancelled(!canBuild(craftWorld, player, clickedX, clickedZ)); } else { event = new PlayerBucketEmptyEvent(player, blockClicked, blockFace, bucket, itemInHand); ((PlayerBucketEmptyEvent) event).setCancelled(!canBuild(craftWorld, player, clickedX, clickedZ)); } craftServer.getPluginManager().callEvent(event); return event; }
@Test public void maxStackSize() { if (INVALIDATED_MATERIALS.contains(material)) return; final ItemStack bukkit = new ItemStack(material); final CraftItemStack craft = CraftItemStack.asCraftCopy(bukkit); if (material == Material.AIR) { final int MAX_AIR_STACK = 0 /* Why can't I hold all of these AIR? */; assertThat(material.getMaxStackSize(), is(MAX_AIR_STACK)); assertThat(bukkit.getMaxStackSize(), is(MAX_AIR_STACK)); assertThat(craft.getMaxStackSize(), is(MAX_AIR_STACK)); } else { assertThat(material.getMaxStackSize(), is(CraftMagicNumbers.getItem(material).getMaxStackSize())); assertThat(bukkit.getMaxStackSize(), is(material.getMaxStackSize())); assertThat(craft.getMaxStackSize(), is(material.getMaxStackSize())); } }
public ItemStack c() { ItemStack itemstack = null; short short1 = this.readShort(); if (short1 >= 0) { byte b0 = this.readByte(); short short2 = this.readShort(); itemstack = new ItemStack(Item.getById(short1), b0, short2); itemstack.tag = this.b(); // CraftBukkit start if (itemstack.tag != null) { CraftItemStack.setItemMeta(itemstack, CraftItemStack.getItemMeta(itemstack)); } // CraftBukkit end } return itemstack; }
public void setItemOnCursor(ItemStack item) { net.minecraft.item.ItemStack stack = CraftItemStack.asNMSCopy(item); getHandle().inventory.setItemStack(stack); if (this instanceof CraftPlayer) { ((net.minecraft.entity.player.EntityPlayerMP) getHandle()).updateHeldItem(); // Send set slot for cursor } }
public void setItem(ItemStack item) { // The ItemStack must not be null. Validate.notNull(item, "ItemStack cannot be null."); // The ItemStack must be a potion. Validate.isTrue(item.getType() == Material.POTION, "ItemStack must be a potion. This item stack was " + item.getType() + "."); getHandle().potionDamage = CraftItemStack.asNMSCopy(item); }
public void setItem(org.bukkit.inventory.ItemStack item) { if (item == null || item.getTypeId() == 0) { getHandle().getDataWatcher().addObjectByDataType(2, 5); getHandle().getDataWatcher().setObjectWatched(2); } else { getHandle().setDisplayedItem(CraftItemStack.asNMSCopy(item)); } }
public Collection<ItemStack> getDrops() { List<ItemStack> drops = new ArrayList<ItemStack>(); net.minecraft.block.Block block = this.getNMSBlock(); if (block != Blocks.air) { byte data = getData(); // based on nms.Block.dropNaturally int count = block.quantityDroppedWithBonus(0, chunk.getHandle().worldObj.rand); for (int i = 0; i < count; ++i) { Item item = block.getItemDropped(data, chunk.getHandle().worldObj.rand, 0); if (item != null) { // Skulls are special, their data is based on the tile entity if (Blocks.skull == block) { net.minecraft.item.ItemStack nmsStack = new net.minecraft.item.ItemStack(item, 1, block.getDamageValue(chunk.getHandle().worldObj, x, y, z)); TileEntitySkull tileentityskull = (TileEntitySkull) chunk.getHandle().worldObj.getTileEntity(x, y, z); if (tileentityskull.func_145904_a() == 3 && tileentityskull.func_152108_a() != null) { nmsStack.setTagCompound(new NBTTagCompound()); NBTTagCompound nbttagcompound = new NBTTagCompound(); NBTUtil.func_152460_a(nbttagcompound, tileentityskull.func_152108_a()); nmsStack.getTagCompound().setTag("SkullOwner", nbttagcompound); } drops.add(CraftItemStack.asBukkitCopy(nmsStack)); // We don't want to drop cocoa blocks, we want to drop cocoa beans. } else if (Blocks.cocoa == block) { int dropAmount = (BlockCocoa.func_149987_c(data) >= 2 ? 3 : 1); for (int j = 0; j < dropAmount; ++j) { drops.add(new ItemStack(Material.INK_SACK, 1, (short) 3)); } } else { drops.add(new ItemStack(org.bukkit.craftbukkit.util.CraftMagicNumbers.getMaterial(item), 1, (short) block.damageDropped(data))); } } } } return drops; }
/** * EntityShootBowEvent */ public static EntityShootBowEvent callEntityShootBowEvent(net.minecraft.entity.EntityLivingBase who, net.minecraft.item.ItemStack itemstack, net.minecraft.entity.projectile.EntityArrow entityArrow, float force) { LivingEntity shooter = (LivingEntity) who.getBukkitEntity(); CraftItemStack itemInHand = CraftItemStack.asCraftMirror(itemstack); Arrow arrow = (Arrow) entityArrow.getBukkitEntity(); if (itemInHand != null && (itemInHand.getType() == Material.AIR || itemInHand.getAmount() == 0)) { itemInHand = null; } EntityShootBowEvent event = new EntityShootBowEvent(shooter, itemInHand, arrow, force); Bukkit.getPluginManager().callEvent(event); return event; }
private static net.minecraft.util.WeightedRandomFishable toNms(WeightedRandomFishable bukkit) { net.minecraft.util.WeightedRandomFishable ret = new net.minecraft.util.WeightedRandomFishable( CraftItemStack.asNMSCopy(bukkit.getItemStack()), bukkit.getWeight()).func_150709_a(bukkit.getDamageFraction()); if (bukkit.hasRandomEnchantments()) { ret.func_150707_a(); } return ret; }
@Override public List<OreDictionaryEntry> getOreEntries(ItemStack itemStack) { int[] ids = OreDictionary.getOreIDs(CraftItemStack.asNMSCopy(itemStack)); ImmutableList.Builder<OreDictionaryEntry> builder = ImmutableList.builder(); for (int id : ids) { builder.add(OreDictionaryEntry.valueOf(id)); } return builder.build(); }
@Override public List<ItemStack> getDefinitions(OreDictionaryEntry entry) { @SuppressWarnings("deprecation") List<net.minecraft.item.ItemStack> items = OreDictionary.getOres(entry.getId()); ImmutableList.Builder<ItemStack> builder = ImmutableList.builder(); for (net.minecraft.item.ItemStack nmsItem : items) { builder.add(CraftItemStack.asCraftMirror(nmsItem)); } return builder.build(); }
private void assertMatches(ItemStack item, ItemStack query) { final ItemMatcher matcher = new ItemMatcher(item); if(!matcher.test(query)) { fail("Item should match: reference=" + item + " query=" + query); } final CraftItemStack nms = CraftItemStack.asCraftMirror(CraftItemStack.asNMSCopy(query)); if(!matcher.test(nms)) { fail("Converted item should match: reference=" + item + " query=" + query); } }
private void refuteMatches(ItemStack item, ItemStack query) { final ItemMatcher matcher = new ItemMatcher(item); if(matcher.test(query)) { fail("Item should not match: reference=" + item + " query=" + query); } final CraftItemStack nms = CraftItemStack.asCraftMirror(CraftItemStack.asNMSCopy(query)); if(matcher.test(nms)) { fail("Converted item should not match: reference=" + item + " query=" + query); } }
static ItemStack asNMS(org.bukkit.inventory.ItemStack bukkit) { if(bukkit instanceof CraftItemStack) { return ((CraftItemStack) bukkit).getHandle(); } else { return CraftItemStack.asNMSCopy(bukkit); } }