ItemSkull .java

net.minecraft.item.ItemSkull (class, 200 lines): members: skullTypes, updateItemStackNBT, getItemStackDisplayName, getSubItems, getMetadata, onItemUse.

net.minecraft.itemclass200 linescommonextends Item6.7 KB

API Surface (6)

boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
void getSubItems(Item itemIn, CreativeTabs tab, List<ItemStack> subItems)
int getMetadata(int damage)
String getUnlocalizedName(ItemStack stack)
String getItemStackDisplayName(ItemStack stack)
boolean updateItemStackNBT(NBTTagCompound nbt)

Fields & Constants (1)

skullTypes

String literals (7)

"skeleton"
"wither"
"zombie"
"char"
"creeper"
"SkullOwner"
"item.skull.player.name"

Full Source

ItemSkull.java200 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.item;
2
3
import com.mojang.authlib.GameProfile;
4
import java.util.List;
5
import java.util.UUID;
6
import net.minecraft.block.Block;
7
import net.minecraft.block.BlockSkull;
8
import net.minecraft.block.state.IBlockState;
9
import net.minecraft.creativetab.CreativeTabs;
10
import net.minecraft.entity.player.EntityPlayer;
11
import net.minecraft.init.Blocks;
12
import net.minecraft.nbt.NBTTagCompound;
13
import net.minecraft.nbt.NBTUtil;
14
import net.minecraft.tileentity.TileEntity;
15
import net.minecraft.tileentity.TileEntitySkull;
16
import net.minecraft.util.BlockPos;
17
import net.minecraft.util.EnumFacing;
18
import net.minecraft.util.MathHelper;
19
import net.minecraft.util.StatCollector;
20
import net.minecraft.world.World;
21
22
public class ItemSkull extends Item
23
{
24
    private static final String[] skullTypes = new String[] {"skeleton", "wither", "zombie", "char", "creeper"};
25
26
    public ItemSkull()
27
    {
28
        this.setCreativeTab(CreativeTabs.tabDecorations);
29
        this.setMaxDamage(0);
30
        this.setHasSubtypes(true);
31
    }
32
33
    /**
34
     * Called when a Block is right-clicked with this Item
35
     */
36
    public boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
37
    {
38
        if (side == EnumFacing.DOWN)
39
        {
40
            return false;
41
        }
42
        else
43
        {
44
            IBlockState iblockstate = worldIn.getBlockState(pos);
45
            Block block = iblockstate.getBlock();
46
            boolean flag = block.isReplaceable(worldIn, pos);
47
48
            if (!flag)
49
            {
50
                if (!worldIn.getBlockState(pos).getBlock().getMaterial().isSolid())
51
                {
52
                    return false;
53
                }
54
55
                pos = pos.offset(side);
56
            }
57
58
            if (!playerIn.canPlayerEdit(pos, side, stack))
59
            {
60
                return false;
61
            }
62
            else if (!Blocks.skull.canPlaceBlockAt(worldIn, pos))
63
            {
64
                return false;
65
            }
66
            else
67
            {
68
                if (!worldIn.isRemote)
69
                {
70
                    worldIn.setBlockState(pos, Blocks.skull.getDefaultState().withProperty(BlockSkull.FACING, side), 3);
71
                    int i = 0;
72
73
                    if (side == EnumFacing.UP)
74
                    {
75
                        i = MathHelper.floor_double((double)(playerIn.rotationYaw * 16.0F / 360.0F) + 0.5D) & 15;
76
                    }
77
78
                    TileEntity tileentity = worldIn.getTileEntity(pos);
79
80
                    if (tileentity instanceof TileEntitySkull)
81
                    {
82
                        TileEntitySkull tileentityskull = (TileEntitySkull)tileentity;
83
84
                        if (stack.getMetadata() == 3)
85
                        {
86
                            GameProfile gameprofile = null;
87
88
                            if (stack.hasTagCompound())
89
                            {
90
                                NBTTagCompound nbttagcompound = stack.getTagCompound();
91
92
                                if (nbttagcompound.hasKey("SkullOwner", 10))
93
                                {
94
                                    gameprofile = NBTUtil.readGameProfileFromNBT(nbttagcompound.getCompoundTag("SkullOwner"));
95
                                }
96
                                else if (nbttagcompound.hasKey("SkullOwner", 8) && nbttagcompound.getString("SkullOwner").length() > 0)
97
                                {
98
                                    gameprofile = new GameProfile((UUID)null, nbttagcompound.getString("SkullOwner"));
99
                                }
100
                            }
101
102
                            tileentityskull.setPlayerProfile(gameprofile);
103
                        }
104
                        else
105
                        {
106
                            tileentityskull.setType(stack.getMetadata());
107
                        }
108
109
                        tileentityskull.setSkullRotation(i);
110
                        Blocks.skull.checkWitherSpawn(worldIn, pos, tileentityskull);
111
                    }
112
113
                    --stack.stackSize;
114
                }
115
116
                return true;
117
            }
118
        }
119
    }
120
121
    /**
122
     * returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
123
     */
124
    public void getSubItems(Item itemIn, CreativeTabs tab, List<ItemStack> subItems)
125
    {
126
        for (int i = 0; i < skullTypes.length; ++i)
127
        {
128
            subItems.add(new ItemStack(itemIn, 1, i));
129
        }
130
    }
131
132
    /**
133
     * Converts the given ItemStack damage value into a metadata value to be placed in the world when this Item is
134
     * placed as a Block (mostly used with ItemBlocks).
135
     */
136
    public int getMetadata(int damage)
137
    {
138
        return damage;
139
    }
140
141
    /**
142
     * Returns the unlocalized name of this item. This version accepts an ItemStack so different stacks can have
143
     * different names based on their damage or NBT.
144
     */
145
    public String getUnlocalizedName(ItemStack stack)
146
    {
147
        int i = stack.getMetadata();
148
149
        if (i < 0 || i >= skullTypes.length)
150
        {
151
            i = 0;
152
        }
153
154
        return super.getUnlocalizedName() + "." + skullTypes[i];
155
    }
156
157
    public String getItemStackDisplayName(ItemStack stack)
158
    {
159
        if (stack.getMetadata() == 3 && stack.hasTagCompound())
160
        {
161
            if (stack.getTagCompound().hasKey("SkullOwner", 8))
162
            {
163
                return StatCollector.translateToLocalFormatted("item.skull.player.name", new Object[] {stack.getTagCompound().getString("SkullOwner")});
164
            }
165
166
            if (stack.getTagCompound().hasKey("SkullOwner", 10))
167
            {
168
                NBTTagCompound nbttagcompound = stack.getTagCompound().getCompoundTag("SkullOwner");
169
170
                if (nbttagcompound.hasKey("Name", 8))
171
                {
172
                    return StatCollector.translateToLocalFormatted("item.skull.player.name", new Object[] {nbttagcompound.getString("Name")});
173
                }
174
            }
175
        }
176
177
        return super.getItemStackDisplayName(stack);
178
    }
179
180
    /**
181
     * Called when an ItemStack with NBT data is read to potentially that ItemStack's NBT data
182
     */
183
    public boolean updateItemStackNBT(NBTTagCompound nbt)
184
    {
185
        super.updateItemStackNBT(nbt);
186
187
        if (nbt.hasKey("SkullOwner", 8) && nbt.getString("SkullOwner").length() > 0)
188
        {
189
            GameProfile gameprofile = new GameProfile((UUID)null, nbt.getString("SkullOwner"));
190
            gameprofile = TileEntitySkull.updateGameprofile(gameprofile);
191
            nbt.setTag("SkullOwner", NBTUtil.writeGameProfile(new NBTTagCompound(), gameprofile));
192
            return true;
193
        }
194
        else
195
        {
196
            return false;
197
        }
198
    }
199
}
200

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