Java net.minecraft.item.ItemStack 代码实例

・19 分钟阅读

以下是展示如何使用net.minecraft.item.ItemStack的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


protected void dropInventory(World world, int x, int y, int z)
{
    TileEntity tileEntity = world.getTileEntity(x, y, z);
    if (!(tileEntity instanceof IInventory))
    {
        return;
    }
    IInventory inventory = (IInventory) tileEntity;
    for (int i = 0; i < inventory.getSizeInventory(); i++)
    {
        ItemStack itemStack = inventory.getStackInSlot(i);
        if (itemStack != null && itemStack.stackSize > 0)
        {
            Random rand = new Random();
            float dX = rand.nextFloat() * 0.8F + 0.1F;
            float dY = rand.nextFloat() * 0.8F + 0.1F;
            float dZ = rand.nextFloat() * 0.8F + 0.1F;
            EntityItem entityItem = new EntityItem(world, x + dX, y + dY, z + dZ, itemStack.copy());
            if (itemStack.hasTagCompound())
            {
                entityItem.getEntityItem().setTagCompound((NBTTagCompound) itemStack.getTagCompound().copy());
            }
            float factor = 0.05F;
            entityItem.motionX = rand.nextGaussian() * factor;
            entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
            entityItem.motionZ = rand.nextGaussian() * factor;
            world.spawnEntityInWorld(entityItem);
            itemStack.stackSize = 0;
        }
    }
}
 

实例 2


public static boolean areCompoundsEqual(ItemStack is1, ItemStack is2)
{
        ItemStack is3 = is1.copy();
        ItemStack is4 = is2.copy();
        NBTTagCompound is3Tags = is3.getTagCompound();
        NBTTagCompound is4Tags = is4.getTagCompound();
        if (is3Tags == null)
                return is4Tags == null || is4Tags.hasNoTags();
        if (is4Tags == null)
                return is3Tags.hasNoTags();
        //Removed during porting this code to 1.8 due to there not being any heat infrastructure at the time.
        /*float temp3 = TFC_ItemHeat.getTemp(is1);
        float temp4 = TFC_ItemHeat.getTemp(is2);
        is3Tags.removeTag("temp");
        is4Tags.removeTag("temp");*/
        return is3Tags.equals(is4Tags) /*&&  Math.abs(temp3 - temp4) < 5*/;
}
 

实例 3


@Override
@SideOnly(Side.CLIENT)
public void getSubItems(Item itemIn, CreativeTabs tab, List subItems)
{
        ItemStack is = new ItemStack(itemIn, 1, 0);
        NBTTagCompound nbt = new NBTTagCompound();
        is.setTagCompound(nbt);
        Food.setDecayTimer(is, net.minecraft.client.Minecraft.getMinecraft().theWorld.getWorldTime()+expiration);
        subItems.add(is);
}
 

实例 4


@Override
public void readFromNBT(NBTTagCompound NBTTC) {
        this.Editable = false;
        super.readFromNBT(NBTTC);
        this.theMetadata=NBTTC.getInteger("theMetadata");//for Sub Block or Item
        this.modeNumber = NBTTC.getInteger("modeNumber");
        this.itemID = NBTTC.getInteger("itemID");
        this.itemMetadata = NBTTC.getInteger("itemMetadata");
        this.largeSignTextColor = NBTTC.getInteger("largeSignTextColor");
        this.hasShadow = NBTTC.getBoolean("hasShadow");
        this.largeSignText[0] = NBTTC.getString("largeSignText");
        this.XAdjust = NBTTC.getFloat("XAdjust");
        this.YAdjust = NBTTC.getFloat("YAdjust");
        this.rotate = NBTTC.getFloat("rotate");
        this.scaleAdjust = NBTTC.getFloat("scaleAdjust");
        this.side=NBTTC.getInteger("side");
        if(NBTTC.hasKey("itemStack"))
                this.itemStack=ItemStack.loadItemStackFromNBT(NBTTC.getCompoundTag("itemStack"));
        this.NBTTC = NBTTC;
}
 

实例 5


/**
 * Drop the equipment for this entity.
 */
@Override
protected void dropEquipment(final boolean p_82160_1_, final int p_82160_2_)
{
        for (int var3 = 0; var3 < this.getInventory().length; ++var3)
        {
                final ItemStack var4 = this.getEquipmentInSlot(var3);
                final boolean var5 = this.equipmentDropChances[var3] > 1.0F;
                if (var4 != null && (p_82160_1_ || var5) && this.rand.nextFloat() - p_82160_2_ * 0.01F < this.equipmentDropChances[var3])
                {
                        if (!var5 && var4.isItemStackDamageable())
                        {
                                final int var6 = Math.max(var4.getMaxDamage() - 25, 1);
                                int var7 = var4.getMaxDamage() - this.rand.nextInt(this.rand.nextInt(var6) + 1);
                                if (var7 > var6)
                                        var7 = var6;
                                if (var7 < 1)
                                        var7 = 1;
                                var4.setItemDamage(var7);
                        }
                        this.entityDropItem(var4, 0.0F);
                }
        }
}
 

实例 6


@SubscribeEvent
public void onPlayerEvent(PlayerEvent.ItemCraftedEvent event)
{
        ItemStack currentItem = event.player.getCurrentEquippedItem();
        if (currentItem != null)
        {
                if (Item.getIdFromItem(currentItem.getItem()) == Item.getIdFromItem(CommonProxy.portableItem))
                {
                        currentItem.damageItem(1, event.player);
                        event.player.worldObj.playSoundAtEntity(event.player, "simpleportables:Crafting", 1, 1);
                }
        }
}
 

实例 7


/**
 * Helper function to replace a single item with a group of items in a
 * recipe.
 * 
 * @param recipe
 * @param ingredient
 * @param group
 */
private void replaceRecipeIngredientWithGroup( final ShapedArcaneRecipe recipe, final ItemStack ingredient, final ArrayList<ItemStack> group )
{
        // Get the input
        Object[] input = recipe.getInput();
        // For every listed slot change the input to the group
        for( int index = 0; index < input.length; index++ )
        {
                Object slot = input[index];
                // Is this slot an itemstack?
                if( slot instanceof ItemStack )
                {
                        // Does it match the stack to replace?
                        if( ingredient.isItemEqual( (ItemStack)slot ) )
                        {
                                // Replace it
                                input[index] = group;
                        }
                }
        }
}
 

实例 8


@Override
public ItemStack decrStackSize(int slot,int amount){
        ItemStack stack = getStackInSlot(slot);
        if(stack != null){
                if(stack.stackSize > amount){
                        stack = stack.splitStack(amount);
                        markDirty();
                }else{
                        setInventorySlotContents(slot, null);
                }
        }
        return stack;
}
 

实例 9


/**
 * stores an itemstack in the users inventory
 */
private int storeItemStack(final ItemStack p_70432_1_)
{
        for (int var2 = 0; var2 < this.mainInventory.length; ++var2)
                if (this.mainInventory[var2] != null && this.mainInventory[var2].getItem() == p_70432_1_.getItem() && this.mainInventory[var2].isStackable() && this.mainInventory[var2].stackSize < this.mainInventory[var2].getMaxStackSize() && this.mainInventory[var2].stackSize < this.getInventoryStackLimit() && (!this.mainInventory[var2].getHasSubtypes() || this.mainInventory[var2].getMetadata() == p_70432_1_.getMetadata()) && ItemStack.areItemStackTagsEqual(this.mainInventory[var2], p_70432_1_))
                        return var2;
        return -1;
}
 

实例 10


/**
 * Called when the block is placed in the world.
 */
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityPlayer, ItemStack itemStack)
{
    int l = MathHelper.floor_double((double) (entityPlayer.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
    if (l == 0)
    {
        world.setBlockMetadataWithNotify(x, y, z, 2, 2);
    }
    if (l == 1)
    {
        world.setBlockMetadataWithNotify(x, y, z, 5, 2);
    }
    if (l == 2)
    {
        world.setBlockMetadataWithNotify(x, y, z, 3, 2);
    }
    if (l == 3)
    {
        world.setBlockMetadataWithNotify(x, y, z, 4, 2);
    }
    if (itemStack.hasDisplayName())
    {
        ((TileEntityAlabasterOven)world.getTileEntity(x, y, z)).setGuiDisplayName(itemStack.getDisplayName());
    }
}
 

实例 11


/**
 * Executes a container/inventory slot manipulation as indicated by the packet. Sends the serverside result if they didn't match the indicated result and prevents further manipulation by the player until he confirms that it has the same open container/inventory
 */
@Override
public void processClickWindow(final C0EPacketClickWindow packetIn)
{
        PacketThreadUtil.func_180031_a(packetIn, this, this.playerEntity.getServerForPlayer());
        this.playerEntity.markPlayerActive();
        if (this.playerEntity.openContainer.windowId == packetIn.getWindowId() && this.playerEntity.openContainer.getCanCraft(this.playerEntity))
                if (this.playerEntity.func_175149_v())
                {
                        final ArrayList var2 = Lists.newArrayList();
                        for (int var3 = 0; var3 < this.playerEntity.openContainer.inventorySlots.size(); ++var3)
                                var2.add(((Slot) this.playerEntity.openContainer.inventorySlots.get(var3)).getStack());
                        this.playerEntity.updateCraftingInventory(this.playerEntity.openContainer, var2);
                }
                else
                {
                        final ItemStack var5 = this.playerEntity.openContainer.slotClick(packetIn.getSlotId(), packetIn.getUsedButton(), packetIn.getMode(), this.playerEntity);
                        if (ItemStack.areItemStacksEqual(packetIn.getClickedItem(), var5))
                        {
                                this.playerEntity.playerNetServerHandler.sendPacket(new S32PacketConfirmTransaction(packetIn.getWindowId(), packetIn.getActionNumber(), true));
                                this.playerEntity.isChangingQuantityOnly = true;
                                this.playerEntity.openContainer.detectAndSendChanges();
                                this.playerEntity.updateHeldItem();
                                this.playerEntity.isChangingQuantityOnly = false;
                        }
                        else
                        {
                                this.field_147372_n.addKey(this.playerEntity.openContainer.windowId, Short.valueOf(packetIn.getActionNumber()));
                                this.playerEntity.playerNetServerHandler.sendPacket(new S32PacketConfirmTransaction(packetIn.getWindowId(), packetIn.getActionNumber(), false));
                                this.playerEntity.openContainer.setCanCraft(this.playerEntity, false);
                                final ArrayList var6 = Lists.newArrayList();
                                for (int var4 = 0; var4 < this.playerEntity.openContainer.inventorySlots.size(); ++var4)
                                        var6.add(((Slot) this.playerEntity.openContainer.inventorySlots.get(var4)).getStack());
                                this.playerEntity.updateCraftingInventory(this.playerEntity.openContainer, var6);
                        }
                }
}
 

实例 12


/**
 * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
 */
public ItemStack onItemRightClick(ItemStack equippedItemStack, World world, EntityPlayer player)
{
    if (FMLCommonHandler.instance().getEffectiveSide().isClient())
    {
        return equippedItemStack;
    }
    // Only run on server
    if (player.getCurrentEquippedItem() != null && FMLCommonHandler.instance().getEffectiveSide().isServer())
    {
        logger.logInfo("Clicked with Biome Compass");
        // Check for custom name
        if (!equippedItemStack.getDisplayName().equals(equippedItemStack.getItem().getItemStackDisplayName(equippedItemStack))){
            // Item has metadata, so it was probably renamed
            String currentEquippedItemName = equippedItemStack.getDisplayName();
            if (!world.isRemote){
                if (isValidBiomeName(currentEquippedItemName)){
                    // Valid biome name
                    // Search for biome matching name
                    if ((scanForBiomeMatch(player, getScanRadius(), currentEquippedItemName))
                            && (equippedItemStack.getItem() instanceof ItemBiomeCompassEnhanced)){
                        // if this is a single-use compass, return vanilla compass
                        ItemBiomeCompassBasic itemBiomeCompass1 = new ItemBiomeCompassBasic();
                        return new ItemStack(GameRegistry.findItem(BiomeCompass.MODID, itemBiomeCompass1.getUnlocalizedName())).setStackDisplayName(currentEquippedItemName);
                    }
                }
                else
                {
                    // Invalid Biome name
                    String msgBiomeCompassInvalidBiomeName = StatCollector.translateToLocalFormatted("strInvalidBiomeName", currentEquippedItemName) ;
                    player.addChatMessage(new ChatComponentText(msgBiomeCompassInvalidBiomeName));
                }
            }
        }
        else
        {
            // Does not have a custom name, so 'not activated'
            player.addChatMessage(new ChatComponentText(StatCollector.translateToLocal("strBiomeCompassNotActivated")));
        }
    }
    return equippedItemStack;
}
 

实例 13


/**
 * Determines if the specified stacks are equal.
 * 
 * @param stack1
 * @param stack2
 * @return
 */
public boolean areStacksEqualIgnoreAmount( final ItemStack stack1, final ItemStack stack2 )
{
        // Nulls never match
        if( ( stack1 == null ) || ( stack2 == null ) || ( stack1.getItem() == null ) || ( stack2.getItem() == null ) )
        {
                return false;
        }
        if( stack1.getItem().equals( stack2.getItem() ) )
        {
                if( stack1.getHasSubtypes() )
                {
                        if( stack1.getItemDamage() == stack2.getItemDamage() )
                        {
                                return true;
                        }
                }
                else
                {
                        return true;
                }
        }
        return false;
}
 

实例 14


/**
 * Copy the ItemStack contents from another InventoryPlayer instance
 */
public void copyInventory(final InventoryPlayer p_70455_1_)
{
        int var2;
        for (var2 = 0; var2 < this.mainInventory.length; ++var2)
                this.mainInventory[var2] = ItemStack.copyItemStack(p_70455_1_.mainInventory[var2]);
        for (var2 = 0; var2 < this.armorInventory.length; ++var2)
                this.armorInventory[var2] = ItemStack.copyItemStack(p_70455_1_.armorInventory[var2]);
        this.currentItem = p_70455_1_.currentItem;
}
 

实例 15


@SideOnly(Side.CLIENT)
public IIcon getItemIcon(ItemStack p_70620_1_, int p_70620_2_) {
        ;
        ;
        ;
        return p_70620_1_.getItem().requiresMultipleRenderPasses() ? p_70620_1_
                        .getItem().getIconFromDamageForRenderPass(
                                        p_70620_1_.getItemDamage(), p_70620_2_) : p_70620_1_
                                        .getIconIndex();
}
 
讨论
淘淘あ西西 profile image