| 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 | }
|
| 456 |
|