ItemMonsterPlacer .java

net.minecraft.item.ItemMonsterPlacer (class, 221 lines): members: spawnCreature, getItemStackDisplayName, getColorFromItemStack, getSubItems, onItemUse.

net.minecraft.itemclass221 linescommonextends Item7.7 KB

API Surface (6)

String getItemStackDisplayName(ItemStack stack)
int getColorFromItemStack(ItemStack stack, int renderPass)
boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn)
Entity spawnCreature(World worldIn, int entityID, double x, double y, double z)
void getSubItems(Item itemIn, CreativeTabs tab, List<ItemStack> subItems)

String literals (2)

".name"
"entity."

Full Source

ItemMonsterPlacer.java221 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.item;
2
3
import java.util.List;
4
import net.minecraft.block.BlockFence;
5
import net.minecraft.block.BlockLiquid;
6
import net.minecraft.block.state.IBlockState;
7
import net.minecraft.creativetab.CreativeTabs;
8
import net.minecraft.entity.Entity;
9
import net.minecraft.entity.EntityList;
10
import net.minecraft.entity.EntityLiving;
11
import net.minecraft.entity.EntityLivingBase;
12
import net.minecraft.entity.IEntityLivingData;
13
import net.minecraft.entity.player.EntityPlayer;
14
import net.minecraft.init.Blocks;
15
import net.minecraft.stats.StatList;
16
import net.minecraft.tileentity.MobSpawnerBaseLogic;
17
import net.minecraft.tileentity.TileEntity;
18
import net.minecraft.tileentity.TileEntityMobSpawner;
19
import net.minecraft.util.BlockPos;
20
import net.minecraft.util.EnumFacing;
21
import net.minecraft.util.MathHelper;
22
import net.minecraft.util.MovingObjectPosition;
23
import net.minecraft.util.StatCollector;
24
import net.minecraft.world.World;
25
26
public class ItemMonsterPlacer extends Item
27
{
28
    public ItemMonsterPlacer()
29
    {
30
        this.setHasSubtypes(true);
31
        this.setCreativeTab(CreativeTabs.tabMisc);
32
    }
33
34
    public String getItemStackDisplayName(ItemStack stack)
35
    {
36
        String s = ("" + StatCollector.translateToLocal(this.getUnlocalizedName() + ".name")).trim();
37
        String s1 = EntityList.getStringFromID(stack.getMetadata());
38
39
        if (s1 != null)
40
        {
41
            s = s + " " + StatCollector.translateToLocal("entity." + s1 + ".name");
42
        }
43
44
        return s;
45
    }
46
47
    public int getColorFromItemStack(ItemStack stack, int renderPass)
48
    {
49
        EntityList.EntityEggInfo entitylist$entityegginfo = (EntityList.EntityEggInfo)EntityList.entityEggs.get(Integer.valueOf(stack.getMetadata()));
50
        return entitylist$entityegginfo != null ? (renderPass == 0 ? entitylist$entityegginfo.primaryColor : entitylist$entityegginfo.secondaryColor) : 16777215;
51
    }
52
53
    /**
54
     * Called when a Block is right-clicked with this Item
55
     */
56
    public boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
57
    {
58
        if (worldIn.isRemote)
59
        {
60
            return true;
61
        }
62
        else if (!playerIn.canPlayerEdit(pos.offset(side), side, stack))
63
        {
64
            return false;
65
        }
66
        else
67
        {
68
            IBlockState iblockstate = worldIn.getBlockState(pos);
69
70
            if (iblockstate.getBlock() == Blocks.mob_spawner)
71
            {
72
                TileEntity tileentity = worldIn.getTileEntity(pos);
73
74
                if (tileentity instanceof TileEntityMobSpawner)
75
                {
76
                    MobSpawnerBaseLogic mobspawnerbaselogic = ((TileEntityMobSpawner)tileentity).getSpawnerBaseLogic();
77
                    mobspawnerbaselogic.setEntityName(EntityList.getStringFromID(stack.getMetadata()));
78
                    tileentity.markDirty();
79
                    worldIn.markBlockForUpdate(pos);
80
81
                    if (!playerIn.capabilities.isCreativeMode)
82
                    {
83
                        --stack.stackSize;
84
                    }
85
86
                    return true;
87
                }
88
            }
89
90
            pos = pos.offset(side);
91
            double d0 = 0.0D;
92
93
            if (side == EnumFacing.UP && iblockstate instanceof BlockFence)
94
            {
95
                d0 = 0.5D;
96
            }
97
98
            Entity entity = spawnCreature(worldIn, stack.getMetadata(), (double)pos.getX() + 0.5D, (double)pos.getY() + d0, (double)pos.getZ() + 0.5D);
99
100
            if (entity != null)
101
            {
102
                if (entity instanceof EntityLivingBase && stack.hasDisplayName())
103
                {
104
                    entity.setCustomNameTag(stack.getDisplayName());
105
                }
106
107
                if (!playerIn.capabilities.isCreativeMode)
108
                {
109
                    --stack.stackSize;
110
                }
111
            }
112
113
            return true;
114
        }
115
    }
116
117
    /**
118
     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
119
     */
120
    public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn)
121
    {
122
        if (worldIn.isRemote)
123
        {
124
            return itemStackIn;
125
        }
126
        else
127
        {
128
            MovingObjectPosition movingobjectposition = this.getMovingObjectPositionFromPlayer(worldIn, playerIn, true);
129
130
            if (movingobjectposition == null)
131
            {
132
                return itemStackIn;
133
            }
134
            else
135
            {
136
                if (movingobjectposition.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK)
137
                {
138
                    BlockPos blockpos = movingobjectposition.getBlockPos();
139
140
                    if (!worldIn.isBlockModifiable(playerIn, blockpos))
141
                    {
142
                        return itemStackIn;
143
                    }
144
145
                    if (!playerIn.canPlayerEdit(blockpos, movingobjectposition.sideHit, itemStackIn))
146
                    {
147
                        return itemStackIn;
148
                    }
149
150
                    if (worldIn.getBlockState(blockpos).getBlock() instanceof BlockLiquid)
151
                    {
152
                        Entity entity = spawnCreature(worldIn, itemStackIn.getMetadata(), (double)blockpos.getX() + 0.5D, (double)blockpos.getY() + 0.5D, (double)blockpos.getZ() + 0.5D);
153
154
                        if (entity != null)
155
                        {
156
                            if (entity instanceof EntityLivingBase && itemStackIn.hasDisplayName())
157
                            {
158
                                ((EntityLiving)entity).setCustomNameTag(itemStackIn.getDisplayName());
159
                            }
160
161
                            if (!playerIn.capabilities.isCreativeMode)
162
                            {
163
                                --itemStackIn.stackSize;
164
                            }
165
166
                            playerIn.triggerAchievement(StatList.objectUseStats[Item.getIdFromItem(this)]);
167
                        }
168
                    }
169
                }
170
171
                return itemStackIn;
172
            }
173
        }
174
    }
175
176
    /**
177
     * Spawns the creature specified by the egg's type in the location specified by the last three parameters.
178
     * Parameters: world, entityID, x, y, z.
179
     */
180
    public static Entity spawnCreature(World worldIn, int entityID, double x, double y, double z)
181
    {
182
        if (!EntityList.entityEggs.containsKey(Integer.valueOf(entityID)))
183
        {
184
            return null;
185
        }
186
        else
187
        {
188
            Entity entity = null;
189
190
            for (int i = 0; i < 1; ++i)
191
            {
192
                entity = EntityList.createEntityByID(entityID, worldIn);
193
194
                if (entity instanceof EntityLivingBase)
195
                {
196
                    EntityLiving entityliving = (EntityLiving)entity;
197
                    entity.setLocationAndAngles(x, y, z, MathHelper.wrapAngleTo180_float(worldIn.rand.nextFloat() * 360.0F), 0.0F);
198
                    entityliving.rotationYawHead = entityliving.rotationYaw;
199
                    entityliving.renderYawOffset = entityliving.rotationYaw;
200
                    entityliving.onInitialSpawn(worldIn.getDifficultyForLocation(new BlockPos(entityliving)), (IEntityLivingData)null);
201
                    worldIn.spawnEntityInWorld(entity);
202
                    entityliving.playLivingSound();
203
                }
204
            }
205
206
            return entity;
207
        }
208
    }
209
210
    /**
211
     * returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
212
     */
213
    public void getSubItems(Item itemIn, CreativeTabs tab, List<ItemStack> subItems)
214
    {
215
        for (EntityList.EntityEggInfo entitylist$entityegginfo : EntityList.entityEggs.values())
216
        {
217
            subItems.add(new ItemStack(itemIn, 1, entitylist$entityegginfo.spawnedID));
218
        }
219
    }
220
}
221

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