ItemBlock .java

net.minecraft.item.ItemBlock (class): members: setTileEntityNBT, setUnlocalizedName, getCreativeTab, getBlock, canPlaceBlockOnSide, getSubItems, block.

net.minecraft.itemclass180 linescommonextends Item5.8 KB

API Surface (9)

ItemBlock setUnlocalizedName(String unlocalizedName)
boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
boolean setTileEntityNBT(World worldIn, EntityPlayer pos, BlockPos stack, ItemStack p_179224_3_)
boolean canPlaceBlockOnSide(World worldIn, BlockPos pos, EnumFacing side, EntityPlayer player, ItemStack stack)
String getUnlocalizedName(ItemStack stack)
String getUnlocalizedName()
CreativeTabs getCreativeTab()
void getSubItems(Item itemIn, CreativeTabs tab, List<ItemStack> subItems)
Block getBlock()

Fields & Constants (1)

block

String literals (1)

"BlockEntityTag"

Full Source

ItemBlock.java180 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.item;
2
3
import java.util.List;
4
import net.minecraft.block.Block;
5
import net.minecraft.block.state.IBlockState;
6
import net.minecraft.creativetab.CreativeTabs;
7
import net.minecraft.entity.Entity;
8
import net.minecraft.entity.player.EntityPlayer;
9
import net.minecraft.init.Blocks;
10
import net.minecraft.nbt.NBTTagCompound;
11
import net.minecraft.server.MinecraftServer;
12
import net.minecraft.tileentity.TileEntity;
13
import net.minecraft.util.BlockPos;
14
import net.minecraft.util.EnumFacing;
15
import net.minecraft.world.World;
16
17
public class ItemBlock extends Item
18
{
19
    protected final Block block;
20
21
    public ItemBlock(Block block)
22
    {
23
        this.block = block;
24
    }
25
26
    /**
27
     * Sets the unlocalized name of this item to the string passed as the parameter, prefixed by "item."
28
     */
29
    public ItemBlock setUnlocalizedName(String unlocalizedName)
30
    {
31
        super.setUnlocalizedName(unlocalizedName);
32
        return this;
33
    }
34
35
    /**
36
     * Called when a Block is right-clicked with this Item
37
     */
38
    public boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
39
    {
40
        IBlockState iblockstate = worldIn.getBlockState(pos);
41
        Block block = iblockstate.getBlock();
42
43
        if (!block.isReplaceable(worldIn, pos))
44
        {
45
            pos = pos.offset(side);
46
        }
47
48
        if (stack.stackSize == 0)
49
        {
50
            return false;
51
        }
52
        else if (!playerIn.canPlayerEdit(pos, side, stack))
53
        {
54
            return false;
55
        }
56
        else if (worldIn.canBlockBePlaced(this.block, pos, false, side, (Entity)null, stack))
57
        {
58
            int i = this.getMetadata(stack.getMetadata());
59
            IBlockState iblockstate1 = this.block.onBlockPlaced(worldIn, pos, side, hitX, hitY, hitZ, i, playerIn);
60
61
            if (worldIn.setBlockState(pos, iblockstate1, 3))
62
            {
63
                iblockstate1 = worldIn.getBlockState(pos);
64
65
                if (iblockstate1.getBlock() == this.block)
66
                {
67
                    setTileEntityNBT(worldIn, playerIn, pos, stack);
68
                    this.block.onBlockPlacedBy(worldIn, pos, iblockstate1, playerIn, stack);
69
                }
70
71
                worldIn.playSoundEffect((double)((float)pos.getX() + 0.5F), (double)((float)pos.getY() + 0.5F), (double)((float)pos.getZ() + 0.5F), this.block.stepSound.getPlaceSound(), (this.block.stepSound.getVolume() + 1.0F) / 2.0F, this.block.stepSound.getFrequency() * 0.8F);
72
                --stack.stackSize;
73
            }
74
75
            return true;
76
        }
77
        else
78
        {
79
            return false;
80
        }
81
    }
82
83
    public static boolean setTileEntityNBT(World worldIn, EntityPlayer pos, BlockPos stack, ItemStack p_179224_3_)
84
    {
85
        MinecraftServer minecraftserver = MinecraftServer.getServer();
86
87
        if (minecraftserver == null)
88
        {
89
            return false;
90
        }
91
        else
92
        {
93
            if (p_179224_3_.hasTagCompound() && p_179224_3_.getTagCompound().hasKey("BlockEntityTag", 10))
94
            {
95
                TileEntity tileentity = worldIn.getTileEntity(stack);
96
97
                if (tileentity != null)
98
                {
99
                    if (!worldIn.isRemote && tileentity.func_183000_F() && !minecraftserver.getConfigurationManager().canSendCommands(pos.getGameProfile()))
100
                    {
101
                        return false;
102
                    }
103
104
                    NBTTagCompound nbttagcompound = new NBTTagCompound();
105
                    NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttagcompound.copy();
106
                    tileentity.writeToNBT(nbttagcompound);
107
                    NBTTagCompound nbttagcompound2 = (NBTTagCompound)p_179224_3_.getTagCompound().getTag("BlockEntityTag");
108
                    nbttagcompound.merge(nbttagcompound2);
109
                    nbttagcompound.setInteger("x", stack.getX());
110
                    nbttagcompound.setInteger("y", stack.getY());
111
                    nbttagcompound.setInteger("z", stack.getZ());
112
113
                    if (!nbttagcompound.equals(nbttagcompound1))
114
                    {
115
                        tileentity.readFromNBT(nbttagcompound);
116
                        tileentity.markDirty();
117
                        return true;
118
                    }
119
                }
120
            }
121
122
            return false;
123
        }
124
    }
125
126
    public boolean canPlaceBlockOnSide(World worldIn, BlockPos pos, EnumFacing side, EntityPlayer player, ItemStack stack)
127
    {
128
        Block block = worldIn.getBlockState(pos).getBlock();
129
130
        if (block == Blocks.snow_layer)
131
        {
132
            side = EnumFacing.UP;
133
        }
134
        else if (!block.isReplaceable(worldIn, pos))
135
        {
136
            pos = pos.offset(side);
137
        }
138
139
        return worldIn.canBlockBePlaced(this.block, pos, false, side, (Entity)null, stack);
140
    }
141
142
    /**
143
     * Returns the unlocalized name of this item. This version accepts an ItemStack so different stacks can have
144
     * different names based on their damage or NBT.
145
     */
146
    public String getUnlocalizedName(ItemStack stack)
147
    {
148
        return this.block.getUnlocalizedName();
149
    }
150
151
    /**
152
     * Returns the unlocalized name of this item.
153
     */
154
    public String getUnlocalizedName()
155
    {
156
        return this.block.getUnlocalizedName();
157
    }
158
159
    /**
160
     * gets the CreativeTab this item is displayed on
161
     */
162
    public CreativeTabs getCreativeTab()
163
    {
164
        return this.block.getCreativeTabToDisplayOn();
165
    }
166
167
    /**
168
     * returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
169
     */
170
    public void getSubItems(Item itemIn, CreativeTabs tab, List<ItemStack> subItems)
171
    {
172
        this.block.getSubBlocks(itemIn, tab, subItems);
173
    }
174
175
    public Block getBlock()
176
    {
177
        return this.block;
178
    }
179
}
180

Deobfuscated MCP 1.8.9 source of ItemBlock from Marcelektro/MavenMCP-1.8.9 (net/minecraft/item/ItemBlock.java), stored verbatim.