ItemInWorldManager

Survival interaction handler: block break/place and creative resync emit S23 corrections and S38 list updates.

net/minecraft/server/management/ItemInWorldManager.java

Updated 2026-09-12

Deep Analysis

Server-side per-player survival, creative, adventure and spectator interaction controller. Validates dig timing, block harvesting, item use and block activation from NetHandlerPlayServer. Sends corrections and progress animations plus gamemode broadcasts to other players.

Key Methods in Depth (8)

setGameType

Change gamemode and broadcast to all players

setGameType changes the owned player's gamemode and broadcasts it to the whole server. It stores the new WorldSettings.GameType in gameType for later survivalOrAdventure and isCreative checks. It calls type.configurePlayerCapabilities on thisPlayerMP.capabilities to set fly, edit, and damage flags. It calls thisPlayerMP.sendPlayerAbilities to push those flags to the owner, updating its own client permissions. It then broadcasts S38PacketPlayerListItem with Action UPDATE_GAME_MODE and an array containing thisPlayerMP via sendPacketToAllPlayers. Every connected client receives S38 and updates the tab-list gamemode icon plus adventure interaction rules for that player. The owner also gets the broadcast in addition to the direct abilities packet. No block or inventory change happens here.

    public void setGameType(WorldSettings.GameType type)
    {
        this.gameType = type;
        type.configurePlayerCapabilities(this.thisPlayerMP.capabilities);
        this.thisPlayerMP.sendPlayerAbilities();
        this.thisPlayerMP.mcServer.getConfigurationManager().sendPacketToAllPlayers(new S38PacketPlayerListItem(S38PacketPlayerListItem.Action.UPDATE_GAME_MODE, new EntityPlayerMP[] {this.thisPlayerMP}));
    }

Storing gameType updates local mode used by dig and use guards. configurePlayerCapabilities maps survival, creative, adventure, and spectator to capability booleans. sendPlayerAbilities pushes those flags to the owner client. Broadcasting S38 UPDATE_GAME_MODE informs all other clients of the new mode for rendering and permissions.

onBlockClicked

Begin survival dig or instant creative break

onBlockClicked starts block breaking when the client sends a dig-start packet. In creative it tries extinguishFire first to put out fire without breaking, and if not fire it calls tryHarvestBlock for instant removal. In survival and adventure it loads the target Block and enforces adventure canDestroy rules via isAllowEdit and held stack canDestroy, returning early for spectator or disallowed tools. It extinguishes fire at the position, snapshots initialDamage from curblockDamage, and queries getPlayerRelativeBlockHardness for dig speed. It notifies the block via onBlockClicked for doors and special handling. If hardness is at least 1.0 it calls tryHarvestBlock immediately for instant crops or soft blocks. Otherwise it sets isDestroyingBlock true, records field_180240_f position, and calls theWorld.sendBlockBreakProgress with staged crack 0 to 10. That call emits S25PacketBlockBreakAnim to watching clients so all see the crack overlay grow.

    public void onBlockClicked(BlockPos pos, EnumFacing side)
    {
        if (this.isCreative())
        {
            if (!this.theWorld.extinguishFire((EntityPlayer)null, pos, side))
            {
                this.tryHarvestBlock(pos);
            }
        }
        else
        {
            Block block = this.theWorld.getBlockState(pos).getBlock();

The isCreative branch handles instant break versus fire extinguish without progress packets. The else branch loads the server block for survival hardness and adventure checks. Adventure validation returns early when the held item cannot destroy the block. Later code sets isDestroyingBlock and sends S25 crack progress to trackers.

updateBlockRemoving

Tick ongoing dig progress and finish harvest

updateBlockRemoving ticks every server tick to advance survival digging and emit S25 progress. It increments curblockDamage as the tick counter for both pending-finish and active-dig paths. If receivedFinishDiggingPacket is true meaning stop-dig arrived early, it computes elapsed ticks, loads the Block at field_180241_i, and clears the flag if now air. It multiplies getPlayerRelativeBlockHardness by elapsed plus one to get fraction f and scales to 0 to 10 stage j. If j differs from durabilityRemainingOnBlock it calls theWorld.sendBlockBreakProgress with player ID, position, and stage, which sends S25PacketBlockBreakAnim to trackers. When f reaches 1.0 it clears the flag and calls tryHarvestBlock to remove and loot the block. Else if isDestroyingBlock is active it performs similar progress for field_180240_f, sending -1 reset and clearing the flag if the block turned to air. Stage caching avoids spamming S25 every tick when unchanged.

    public void updateBlockRemoving()
    {
        ++this.curblockDamage;

        if (this.receivedFinishDiggingPacket)
        {
            int i = this.curblockDamage - this.initialBlockDamage;
            Block block = this.theWorld.getBlockState(this.field_180241_i).getBlock();

            if (block.getMaterial() == Material.air)
            {
                this.receivedFinishDiggingPacket = false;
            }
            else
            {
                float f = block.getPlayerRelativeBlockHardness(this.thisPlayerMP, this.thisPlayerMP.worldObj, this.field_180241_i) * (float)(i + 1);
                int j = (int)(f * 10.0F);

                if (j != this.durabilityRemainingOnBlock)
                {
                    this.theWorld.sendBlockBreakProgress(this.thisPlayerMP.getEntityId(), this.field_180241_i, j);
                    this.durabilityRemainingOnBlock = j;
                }

                if (f >= 1.0F)
                {
                    this.receivedFinishDiggingPacket = false;
                    this.tryHarvestBlock(this.field_180241_i);
                }
            }
        }
        else if (this.isDestroyingBlock)
        {
            Block block1 = this.theWorld.getBlockState(this.field_180240_f).getBlock();

Incrementing curblockDamage drives both dig paths forward each tick. The receivedFinishDiggingPacket branch finishes blocks whose stop packet arrived before hardness completed. Computing f from hardness times elapsed gives break fraction scaled to S25 stage 0 to 10. sendBlockBreakProgress emits S25 only when the stage changed to limit traffic. Reaching 1.0 harvests the block while the isDestroyingBlock branch maintains live crack for held dig.

blockRemoving

Handle stop-digging packet mid-break

blockRemoving handles the stop-digging C07 packet when it arrives mid-break. It first verifies pos equals field_180240_f so stale or moved targets are ignored. It computes elapsed ticks as curblockDamage minus initialDamage and loads the server Block. If material is air it does nothing because onUpdate or harvest already cleared it. It multiplies getPlayerRelativeBlockHardness by elapsed plus one to estimate break fraction f. If f is at least 0.7 it treats the dig as complete: clears isDestroyingBlock, sends progress -1 via sendBlockBreakProgress to erase crack via S25, and calls tryHarvestBlock to remove and drop loot. If below 0.7 and receivedFinishDiggingPacket is not already set, it parks the request by clearing isDestroyingBlock, setting receivedFinishDiggingPacket true, and copying position and initial damage into field_180241_i and initialBlockDamage. updateBlockRemoving will then finish it over subsequent ticks with staged S25 updates.

    public void blockRemoving(BlockPos pos)
    {
        if (pos.equals(this.field_180240_f))
        {
            int i = this.curblockDamage - this.initialDamage;
            Block block = this.theWorld.getBlockState(pos).getBlock();

            if (block.getMaterial() != Material.air)
            {
                float f = block.getPlayerRelativeBlockHardness(this.thisPlayerMP, this.thisPlayerMP.worldObj, pos) * (float)(i + 1);

                if (f >= 0.7F)
                {
                    this.isDestroyingBlock = false;
                    this.theWorld.sendBlockBreakProgress(this.thisPlayerMP.getEntityId(), pos, -1);
                    this.tryHarvestBlock(pos);
                }
                else if (!this.receivedFinishDiggingPacket)
                {
                    this.isDestroyingBlock = false;
                    this.receivedFinishDiggingPacket = true;
                    this.field_180241_i = pos;
                    this.initialBlockDamage = this.initialDamage;
                }
            }
        }
    }

The pos equality guard ensures only the active dig target can be finished. Elapsed ticks times hardness estimates how far digging progressed. The 0.7 threshold allows slightly early stops to succeed while sending S25 -1 to clear crack. Parking below threshold defers harvest to updateBlockRemoving with staged S25. Air targets are ignored as already broken.

cancelDestroyingBlock

Abort dig and clear crack overlay

cancelDestroyingBlock aborts an in-progress survival dig when the client sends abort or switches target. It unconditionally sets isDestroyingBlock false to stop updateBlockRemoving from advancing crack stages. It calls theWorld.sendBlockBreakProgress with the breaker entity ID, field_180240_f position, and -1 stage. WorldServer forwards that as S25PacketBlockBreakAnim with destroy stage -1 to all clients tracking the position. Receivers clear the crack overlay for that breaker ID. No block removal, loot, or tool damage occurs on cancel. The curblockDamage counter is left for the next onBlockClicked to re-baseline via initialDamage. Duplicate cancels simply resend -1 which clients treat as idempotent clear.

    /**
     * Stops the block breaking process
     */
    public void cancelDestroyingBlock()
    {
        this.isDestroyingBlock = false;
        this.theWorld.sendBlockBreakProgress(this.thisPlayerMP.getEntityId(), this.field_180240_f, -1);
    }

    /**

Clearing isDestroyingBlock halts tick progress in updateBlockRemoving. Sending break progress -1 emits S25 telling trackers to remove the crack. The breaker ID plus position key the overlay so other diggers are unaffected. No harvest or drops follow because cancellation is visual-only.

tryHarvestBlock

Remove block, drop loot and correct creative client

tryHarvestBlock removes the block, plays break effects, and corrects the client as needed. It first blocks creative sword use from breaking blocks to preserve attack semantics. It loads IBlockState and TileEntity, then enforces adventure and spectator rules with isAllowEdit and canDestroy checks. It plays AuxSFX 2001 break particles and sound at the position via playAuxSFXAtEntity for nearby clients. It calls removeBlock which fires onBlockHarvested and setBlockToAir plus onBlockDestroyedByPlayer. In creative it sends S23PacketBlockChange with world and position directly to the breaker via playerNetServerHandler so the client matches instant server removal. In survival it damages the held stack, destroys it at zero size, and if both removed and harvestable calls harvestBlock to drop items and XP. The boolean return reports whether setBlockToAir succeeded.

            this.theWorld.playAuxSFXAtEntity(this.thisPlayerMP, 2001, pos, Block.getStateId(iblockstate));
            boolean flag1 = this.removeBlock(pos);

            if (this.isCreative())
            {
                this.thisPlayerMP.playerNetServerHandler.sendPacket(new S23PacketBlockChange(this.theWorld, pos));
            }
            else
            {
                ItemStack itemstack1 = this.thisPlayerMP.getCurrentEquippedItem();
                boolean flag = this.thisPlayerMP.canHarvestBlock(iblockstate.getBlock());

Playing AuxSFX 2001 broadcasts break particles and sound to nearby clients. removeBlock performs the actual setBlockToAir with harvest callbacks. The creative branch sends S23PacketBlockChange to the breaker to force client chunk match after instant break. The survival branch inspects the held stack and harvest permission to decide drops and tool damage.

tryUseItem

Apply right-click item use with creative restore

tryUseItem applies right-click item use from NetHandlerPlayServer C08 handling. It immediately returns false for spectator mode to block consumables, throws, and placements. It snapshots stackSize and metadata before calling stack.useItemRightClick with world and player for potions, food, buckets, or projectiles. If the returned stack differs by identity, size, max use duration, or metadata, it writes the result into the current hotbar slot. In creative it restores stackSize and item damage so blocks and items are infinite while still triggering effects. If size reached zero it clears the slot to null to remove consumed items. If the player is not in a use animation it calls sendContainerToPlayer to resync the inventory container, correcting any client-server drift. True signals the interaction was consumed while false lets the caller try block activation instead. No block-change packet is sent here beyond container sync.

    public boolean tryUseItem(EntityPlayer player, World worldIn, ItemStack stack)
    {
        if (this.gameType == WorldSettings.GameType.SPECTATOR)
        {
            return false;
        }
        else
        {
            int i = stack.stackSize;
            int j = stack.getMetadata();
            ItemStack itemstack = stack.useItemRightClick(worldIn, player);

            if (itemstack != stack || itemstack != null && (itemstack.stackSize != i || itemstack.getMaxItemUseDuration() > 0 || itemstack.getMetadata() != j))
            {
                player.inventory.mainInventory[player.inventory.currentItem] = itemstack;

                if (this.isCreative())
                {
                    itemstack.stackSize = i;

                    if (itemstack.isItemStackDamageable())
                    {
                        itemstack.setItemDamage(j);
                    }
                }

The spectator guard blocks all right-click use for observers. Snapshotting size and metadata detects consumption or transformation from useItemRightClick. Writing the result into mainInventory keeps server inventory authoritative. Creative restore plus damage reset preserves infinite items while still firing use effects.

activateBlockOrUseItem

Activate block or place held item

activateBlockOrUseItem handles right-click on a block position, choosing block activation versus item placement. Spectators only open chest and inventory GUIs via displayGUIChest without editing, returning false for other blocks. For players it first tries onBlockActivated when not sneaking or when empty-handed, letting doors, chests, furnaces, and levers consume the click with true. If that fails and stack is null it returns false to end the interaction. In creative it records metadata and size, calls stack.onItemUse for placement, then restores damage and size for infinite blocks before returning the result. In survival it directly returns stack.onItemUse which places blocks, consumes items, and triggers TileEntity updates. Block changes from placement flow through WorldServer mark-and-notify as S22 and S23 packets rather than from this method. Deduplication with NetHandlerPlayServer position and face validation prevents ghost blocks.

        else
        {
            if (!player.isSneaking() || player.getHeldItem() == null)
            {
                IBlockState iblockstate = worldIn.getBlockState(pos);

                if (iblockstate.getBlock().onBlockActivated(worldIn, pos, iblockstate, player, side, offsetX, offsetY, offsetZ))
                {
                    return true;
                }
            }

            if (stack == null)
            {
                return false;
            }
            else if (this.isCreative())
            {
                int j = stack.getMetadata();
                int i = stack.stackSize;
                boolean flag = stack.onItemUse(player, worldIn, pos, side, offsetX, offsetY, offsetZ);
                stack.setItemDamage(j);
                stack.stackSize = i;
                return flag;
            }
            else
            {
                return stack.onItemUse(player, worldIn, pos, side, offsetX, offsetY, offsetZ);
            }
        }
    }

The sneaking check lets players place blocks instead of opening GUIs when holding items. onBlockActivated gives the target block first chance to consume the click. Null stack returns false after failed activation to stop further work. Creative placement restores metadata and size after onItemUse while survival delegates directly for consumption.

Packet Usage

PacketDirTrigger
C07/C08handledNetHandlerPlayServer dig and block-place drive these methods
S25sentsendBlockBreakProgress in dig start, tick and cancel
S23senttryHarvestBlock creative correction to breaker
S38sentsetGameType UPDATE_GAME_MODE broadcast

Internal State

theWorldServer world where block edits are applied
thisPlayerMPOwning player for abilities and packet replies
gameTypeCurrent survival, creative, adventure or spectator mode
isDestroyingBlockWhether survival dig progress is active
receivedFinishDiggingPacketPending finish when stop arrived early
durabilityRemainingOnBlockLast sent 0-10 crack stage to avoid resends

Related Vanilla Internals

Full Source

ItemInWorldManager.java455 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.server.management;
2
3
import net.minecraft.block.Block;
4
import net.minecraft.block.BlockChest;
5
import net.minecraft.block.material.Material;
6
import net.minecraft.block.state.IBlockState;
7
import net.minecraft.entity.player.EntityPlayer;
8
import net.minecraft.entity.player.EntityPlayerMP;
9
import net.minecraft.inventory.IInventory;
10
import net.minecraft.item.ItemStack;
11
import net.minecraft.item.ItemSword;
12
import net.minecraft.network.play.server.S23PacketBlockChange;
13
import net.minecraft.network.play.server.S38PacketPlayerListItem;
14
import net.minecraft.tileentity.TileEntity;
15
import net.minecraft.tileentity.TileEntityChest;
16
import net.minecraft.util.BlockPos;
17
import net.minecraft.util.EnumFacing;
18
import net.minecraft.world.ILockableContainer;
19
import net.minecraft.world.World;
20
import net.minecraft.world.WorldServer;
21
import net.minecraft.world.WorldSettings;
22
23
public class ItemInWorldManager
24
{
25
    /** The world object that this object is connected to. */
26
    public World theWorld;
27
28
    /** The EntityPlayerMP object that this object is connected to. */
29
    public EntityPlayerMP thisPlayerMP;
30
    private WorldSettings.GameType gameType = WorldSettings.GameType.NOT_SET;
31
32
    /** True if the player is destroying a block */
33
    private boolean isDestroyingBlock;
34
    private int initialDamage;
35
    private BlockPos field_180240_f = BlockPos.ORIGIN;
36
    private int curblockDamage;
37
38
    /**
39
     * Set to true when the "finished destroying block" packet is received but the block wasn't fully damaged yet. The
40
     * block will not be destroyed while this is false.
41
     */
42
    private boolean receivedFinishDiggingPacket;
43
    private BlockPos field_180241_i = BlockPos.ORIGIN;
44
    private int initialBlockDamage;
45
    private int durabilityRemainingOnBlock = -1;
46
47
    public ItemInWorldManager(World worldIn)
48
    {
49
        this.theWorld = worldIn;
50
    }
51
52
    public void setGameType(WorldSettings.GameType type)
53
    {
54
        this.gameType = type;
55
        type.configurePlayerCapabilities(this.thisPlayerMP.capabilities);
56
        this.thisPlayerMP.sendPlayerAbilities();
57
        this.thisPlayerMP.mcServer.getConfigurationManager().sendPacketToAllPlayers(new S38PacketPlayerListItem(S38PacketPlayerListItem.Action.UPDATE_GAME_MODE, new EntityPlayerMP[] {this.thisPlayerMP}));
58
    }
59
60
    public WorldSettings.GameType getGameType()
61
    {
62
        return this.gameType;
63
    }
64
65
    public boolean survivalOrAdventure()
66
    {
67
        return this.gameType.isSurvivalOrAdventure();
68
    }
69
70
    /**
71
     * Get if we are in creative game mode.
72
     */
73
    public boolean isCreative()
74
    {
75
        return this.gameType.isCreative();
76
    }
77
78
    /**
79
     * if the gameType is currently NOT_SET then change it to par1
80
     */
81
    public void initializeGameType(WorldSettings.GameType type)
82
    {
83
        if (this.gameType == WorldSettings.GameType.NOT_SET)
84
        {
85
            this.gameType = type;
86
        }
87
88
        this.setGameType(this.gameType);
89
    }
90
91
    public void updateBlockRemoving()
92
    {
93
        ++this.curblockDamage;
94
95
        if (this.receivedFinishDiggingPacket)
96
        {
97
            int i = this.curblockDamage - this.initialBlockDamage;
98
            Block block = this.theWorld.getBlockState(this.field_180241_i).getBlock();
99
100
            if (block.getMaterial() == Material.air)
101
            {
102
                this.receivedFinishDiggingPacket = false;
103
            }
104
            else
105
            {
106
                float f = block.getPlayerRelativeBlockHardness(this.thisPlayerMP, this.thisPlayerMP.worldObj, this.field_180241_i) * (float)(i + 1);
107
                int j = (int)(f * 10.0F);
108
109
                if (j != this.durabilityRemainingOnBlock)
110
                {
111
                    this.theWorld.sendBlockBreakProgress(this.thisPlayerMP.getEntityId(), this.field_180241_i, j);
112
                    this.durabilityRemainingOnBlock = j;
113
                }
114
115
                if (f >= 1.0F)
116
                {
117
                    this.receivedFinishDiggingPacket = false;
118
                    this.tryHarvestBlock(this.field_180241_i);
119
                }
120
            }
121
        }
122
        else if (this.isDestroyingBlock)
123
        {
124
            Block block1 = this.theWorld.getBlockState(this.field_180240_f).getBlock();
125
126
            if (block1.getMaterial() == Material.air)
127
            {
128
                this.theWorld.sendBlockBreakProgress(this.thisPlayerMP.getEntityId(), this.field_180240_f, -1);
129
                this.durabilityRemainingOnBlock = -1;
130
                this.isDestroyingBlock = false;
131
            }
132
            else
133
            {
134
                int k = this.curblockDamage - this.initialDamage;
135
                float f1 = block1.getPlayerRelativeBlockHardness(this.thisPlayerMP, this.thisPlayerMP.worldObj, this.field_180241_i) * (float)(k + 1);
136
                int l = (int)(f1 * 10.0F);
137
138
                if (l != this.durabilityRemainingOnBlock)
139
                {
140
                    this.theWorld.sendBlockBreakProgress(this.thisPlayerMP.getEntityId(), this.field_180240_f, l);
141
                    this.durabilityRemainingOnBlock = l;
142
                }
143
            }
144
        }
145
    }
146
147
    /**
148
     * If not creative, it calls sendBlockBreakProgress until the block is broken first. tryHarvestBlock can also be the
149
     * result of this call.
150
     */
151
    public void onBlockClicked(BlockPos pos, EnumFacing side)
152
    {
153
        if (this.isCreative())
154
        {
155
            if (!this.theWorld.extinguishFire((EntityPlayer)null, pos, side))
156
            {
157
                this.tryHarvestBlock(pos);
158
            }
159
        }
160
        else
161
        {
162
            Block block = this.theWorld.getBlockState(pos).getBlock();
163
164
            if (this.gameType.isAdventure())
165
            {
166
                if (this.gameType == WorldSettings.GameType.SPECTATOR)
167
                {
168
                    return;
169
                }
170
171
                if (!this.thisPlayerMP.isAllowEdit())
172
                {
173
                    ItemStack itemstack = this.thisPlayerMP.getCurrentEquippedItem();
174
175
                    if (itemstack == null)
176
                    {
177
                        return;
178
                    }
179
180
                    if (!itemstack.canDestroy(block))
181
                    {
182
                        return;
183
                    }
184
                }
185
            }
186
187
            this.theWorld.extinguishFire((EntityPlayer)null, pos, side);
188
            this.initialDamage = this.curblockDamage;
189
            float f = 1.0F;
190
191
            if (block.getMaterial() != Material.air)
192
            {
193
                block.onBlockClicked(this.theWorld, pos, this.thisPlayerMP);
194
                f = block.getPlayerRelativeBlockHardness(this.thisPlayerMP, this.thisPlayerMP.worldObj, pos);
195
            }
196
197
            if (block.getMaterial() != Material.air && f >= 1.0F)
198
            {
199
                this.tryHarvestBlock(pos);
200
            }
201
            else
202
            {
203
                this.isDestroyingBlock = true;
204
                this.field_180240_f = pos;
205
                int i = (int)(f * 10.0F);
206
                this.theWorld.sendBlockBreakProgress(this.thisPlayerMP.getEntityId(), pos, i);
207
                this.durabilityRemainingOnBlock = i;
208
            }
209
        }
210
    }
211
212
    public void blockRemoving(BlockPos pos)
213
    {
214
        if (pos.equals(this.field_180240_f))
215
        {
216
            int i = this.curblockDamage - this.initialDamage;
217
            Block block = this.theWorld.getBlockState(pos).getBlock();
218
219
            if (block.getMaterial() != Material.air)
220
            {
221
                float f = block.getPlayerRelativeBlockHardness(this.thisPlayerMP, this.thisPlayerMP.worldObj, pos) * (float)(i + 1);
222
223
                if (f >= 0.7F)
224
                {
225
                    this.isDestroyingBlock = false;
226
                    this.theWorld.sendBlockBreakProgress(this.thisPlayerMP.getEntityId(), pos, -1);
227
                    this.tryHarvestBlock(pos);
228
                }
229
                else if (!this.receivedFinishDiggingPacket)
230
                {
231
                    this.isDestroyingBlock = false;
232
                    this.receivedFinishDiggingPacket = true;
233
                    this.field_180241_i = pos;
234
                    this.initialBlockDamage = this.initialDamage;
235
                }
236
            }
237
        }
238
    }
239
240
    /**
241
     * Stops the block breaking process
242
     */
243
    public void cancelDestroyingBlock()
244
    {
245
        this.isDestroyingBlock = false;
246
        this.theWorld.sendBlockBreakProgress(this.thisPlayerMP.getEntityId(), this.field_180240_f, -1);
247
    }
248
249
    /**
250
     * Removes a block and triggers the appropriate events
251
     */
252
    private boolean removeBlock(BlockPos pos)
253
    {
254
        IBlockState iblockstate = this.theWorld.getBlockState(pos);
255
        iblockstate.getBlock().onBlockHarvested(this.theWorld, pos, iblockstate, this.thisPlayerMP);
256
        boolean flag = this.theWorld.setBlockToAir(pos);
257
258
        if (flag)
259
        {
260
            iblockstate.getBlock().onBlockDestroyedByPlayer(this.theWorld, pos, iblockstate);
261
        }
262
263
        return flag;
264
    }
265
266
    /**
267
     * Attempts to harvest a block
268
     */
269
    public boolean tryHarvestBlock(BlockPos pos)
270
    {
271
        if (this.gameType.isCreative() && this.thisPlayerMP.getHeldItem() != null && this.thisPlayerMP.getHeldItem().getItem() instanceof ItemSword)
272
        {
273
            return false;
274
        }
275
        else
276
        {
277
            IBlockState iblockstate = this.theWorld.getBlockState(pos);
278
            TileEntity tileentity = this.theWorld.getTileEntity(pos);
279
280
            if (this.gameType.isAdventure())
281
            {
282
                if (this.gameType == WorldSettings.GameType.SPECTATOR)
283
                {
284
                    return false;
285
                }
286
287
                if (!this.thisPlayerMP.isAllowEdit())
288
                {
289
                    ItemStack itemstack = this.thisPlayerMP.getCurrentEquippedItem();
290
291
                    if (itemstack == null)
292
                    {
293
                        return false;
294
                    }
295
296
                    if (!itemstack.canDestroy(iblockstate.getBlock()))
297
                    {
298
                        return false;
299
                    }
300
                }
301
            }
302
303
            this.theWorld.playAuxSFXAtEntity(this.thisPlayerMP, 2001, pos, Block.getStateId(iblockstate));
304
            boolean flag1 = this.removeBlock(pos);
305
306
            if (this.isCreative())
307
            {
308
                this.thisPlayerMP.playerNetServerHandler.sendPacket(new S23PacketBlockChange(this.theWorld, pos));
309
            }
310
            else
311
            {
312
                ItemStack itemstack1 = this.thisPlayerMP.getCurrentEquippedItem();
313
                boolean flag = this.thisPlayerMP.canHarvestBlock(iblockstate.getBlock());
314
315
                if (itemstack1 != null)
316
                {
317
                    itemstack1.onBlockDestroyed(this.theWorld, iblockstate.getBlock(), pos, this.thisPlayerMP);
318
319
                    if (itemstack1.stackSize == 0)
320
                    {
321
                        this.thisPlayerMP.destroyCurrentEquippedItem();
322
                    }
323
                }
324
325
                if (flag1 && flag)
326
                {
327
                    iblockstate.getBlock().harvestBlock(this.theWorld, this.thisPlayerMP, pos, iblockstate, tileentity);
328
                }
329
            }
330
331
            return flag1;
332
        }
333
    }
334
335
    /**
336
     * Attempts to right-click use an item by the given EntityPlayer in the given World
337
     */
338
    public boolean tryUseItem(EntityPlayer player, World worldIn, ItemStack stack)
339
    {
340
        if (this.gameType == WorldSettings.GameType.SPECTATOR)
341
        {
342
            return false;
343
        }
344
        else
345
        {
346
            int i = stack.stackSize;
347
            int j = stack.getMetadata();
348
            ItemStack itemstack = stack.useItemRightClick(worldIn, player);
349
350
            if (itemstack != stack || itemstack != null && (itemstack.stackSize != i || itemstack.getMaxItemUseDuration() > 0 || itemstack.getMetadata() != j))
351
            {
352
                player.inventory.mainInventory[player.inventory.currentItem] = itemstack;
353
354
                if (this.isCreative())
355
                {
356
                    itemstack.stackSize = i;
357
358
                    if (itemstack.isItemStackDamageable())
359
                    {
360
                        itemstack.setItemDamage(j);
361
                    }
362
                }
363
364
                if (itemstack.stackSize == 0)
365
                {
366
                    player.inventory.mainInventory[player.inventory.currentItem] = null;
367
                }
368
369
                if (!player.isUsingItem())
370
                {
371
                    ((EntityPlayerMP)player).sendContainerToPlayer(player.inventoryContainer);
372
                }
373
374
                return true;
375
            }
376
            else
377
            {
378
                return false;
379
            }
380
        }
381
    }
382
383
    /**
384
     * Activate the clicked on block, otherwise use the held item.
385
     */
386
    public boolean activateBlockOrUseItem(EntityPlayer player, World worldIn, ItemStack stack, BlockPos pos, EnumFacing side, float offsetX, float offsetY, float offsetZ)
387
    {
388
        if (this.gameType == WorldSettings.GameType.SPECTATOR)
389
        {
390
            TileEntity tileentity = worldIn.getTileEntity(pos);
391
392
            if (tileentity instanceof ILockableContainer)
393
            {
394
                Block block = worldIn.getBlockState(pos).getBlock();
395
                ILockableContainer ilockablecontainer = (ILockableContainer)tileentity;
396
397
                if (ilockablecontainer instanceof TileEntityChest && block instanceof BlockChest)
398
                {
399
                    ilockablecontainer = ((BlockChest)block).getLockableContainer(worldIn, pos);
400
                }
401
402
                if (ilockablecontainer != null)
403
                {
404
                    player.displayGUIChest(ilockablecontainer);
405
                    return true;
406
                }
407
            }
408
            else if (tileentity instanceof IInventory)
409
            {
410
                player.displayGUIChest((IInventory)tileentity);
411
                return true;
412
            }
413
414
            return false;
415
        }
416
        else
417
        {
418
            if (!player.isSneaking() || player.getHeldItem() == null)
419
            {
420
                IBlockState iblockstate = worldIn.getBlockState(pos);
421
422
                if (iblockstate.getBlock().onBlockActivated(worldIn, pos, iblockstate, player, side, offsetX, offsetY, offsetZ))
423
                {
424
                    return true;
425
                }
426
            }
427
428
            if (stack == null)
429
            {
430
                return false;
431
            }
432
            else if (this.isCreative())
433
            {
434
                int j = stack.getMetadata();
435
                int i = stack.stackSize;
436
                boolean flag = stack.onItemUse(player, worldIn, pos, side, offsetX, offsetY, offsetZ);
437
                stack.setItemDamage(j);
438
                stack.stackSize = i;
439
                return flag;
440
            }
441
            else
442
            {
443
                return stack.onItemUse(player, worldIn, pos, side, offsetX, offsetY, offsetZ);
444
            }
445
        }
446
    }
447
448
    /**
449
     * Sets the world instance.
450
     */
451
    public void setWorld(WorldServer serverWorld)
452
    {
453
        this.theWorld = serverWorld;
454
    }
455
}

Deobfuscated MCP 1.8.9 source of ItemInWorldManager from Marcelektro/MavenMCP-1.8.9 (net/minecraft/server/management/ItemInWorldManager.java), stored verbatim.