--- a/net/minecraft/world/item/ItemBlock.java +++ b/net/minecraft/world/item/ItemBlock.java @@ -33,6 +33,13 @@ import net.minecraft.world.level.gameevent.GameEvent; import net.minecraft.world.phys.shapes.VoxelShapeCollision; +// CraftBukkit start +import net.minecraft.server.level.WorldServer; +import org.bukkit.craftbukkit.block.CraftBlock; +import org.bukkit.craftbukkit.block.data.CraftBlockData; +import org.bukkit.event.block.BlockCanBuildEvent; +// CraftBukkit end + public class ItemBlock extends Item { public static final String BLOCK_ENTITY_TAG = "BlockEntityTag"; @@ -71,6 +78,12 @@ return EnumInteractionResult.FAIL; } else { IBlockData iblockdata = this.getPlacementState(blockactioncontext1); + // CraftBukkit start - special case for handling block placement with water lilies and snow buckets + org.bukkit.block.BlockState blockstate = null; + if (this instanceof PlaceOnWaterBlockItem || this instanceof SolidBucketItem) { + blockstate = org.bukkit.craftbukkit.block.CraftBlockStates.getBlockState(blockactioncontext1.getLevel(), blockactioncontext1.getClickedPos()); + } + // CraftBukkit end if (iblockdata == null) { return EnumInteractionResult.FAIL; @@ -87,6 +100,19 @@ iblockdata1 = this.updateBlockStateFromTag(blockposition, world, itemstack, iblockdata1); this.updateCustomBlockEntityTag(blockposition, world, entityhuman, itemstack, iblockdata1); iblockdata1.getBlock().setPlacedBy(world, blockposition, iblockdata1, entityhuman, itemstack); + // CraftBukkit start + if (blockstate != null) { + org.bukkit.event.block.BlockPlaceEvent placeEvent = org.bukkit.craftbukkit.event.CraftEventFactory.callBlockPlaceEvent((WorldServer) world, entityhuman, blockactioncontext1.getHand(), blockstate, blockposition.getX(), blockposition.getY(), blockposition.getZ()); + if (placeEvent != null && (placeEvent.isCancelled() || !placeEvent.canBuild())) { + blockstate.update(true, false); + + if (this instanceof SolidBucketItem) { + ((EntityPlayer) entityhuman).getBukkitEntity().updateInventory(); // SPIGOT-4541 + } + return EnumInteractionResult.FAIL; + } + } + // CraftBukkit end if (entityhuman instanceof EntityPlayer) { CriterionTriggers.PLACED_BLOCK.trigger((EntityPlayer) entityhuman, blockposition, itemstack); } @@ -94,9 +120,9 @@ SoundEffectType soundeffecttype = iblockdata1.getSoundType(); - world.playSound(entityhuman, blockposition, this.getPlaceSound(iblockdata1), SoundCategory.BLOCKS, (soundeffecttype.getVolume() + 1.0F) / 2.0F, soundeffecttype.getPitch() * 0.8F); + // world.playSound(entityhuman, blockposition, this.getPlaceSound(iblockdata1), SoundCategory.BLOCKS, (soundeffecttype.getVolume() + 1.0F) / 2.0F, soundeffecttype.getPitch() * 0.8F); world.gameEvent(GameEvent.BLOCK_PLACE, blockposition, GameEvent.a.of(entityhuman, iblockdata1)); - if (entityhuman == null || !entityhuman.getAbilities().instabuild) { + if ((entityhuman == null || !entityhuman.getAbilities().instabuild) && itemstack != ItemStack.EMPTY) { // CraftBukkit itemstack.shrink(1); } @@ -132,6 +158,21 @@ if (nbttagcompound != null) { NBTTagCompound nbttagcompound1 = nbttagcompound.getCompound("BlockStateTag"); + // CraftBukkit start + iblockdata1 = getBlockState(iblockdata1, nbttagcompound1); + } + + if (iblockdata1 != iblockdata) { + world.setBlock(blockposition, iblockdata1, 2); + } + + return iblockdata1; + } + + public static IBlockData getBlockState(IBlockData iblockdata, NBTTagCompound nbttagcompound1) { + IBlockData iblockdata1 = iblockdata; + { + // CraftBukkit end BlockStateList blockstatelist = iblockdata.getBlock().getStateDefinition(); Iterator iterator = nbttagcompound1.getAllKeys().iterator(); @@ -146,11 +187,6 @@ } } } - - if (iblockdata1 != iblockdata) { - world.setBlock(blockposition, iblockdata1, 2); - } - return iblockdata1; } @@ -163,8 +199,15 @@ protected boolean canPlace(BlockActionContext blockactioncontext, IBlockData iblockdata) { EntityHuman entityhuman = blockactioncontext.getPlayer(); VoxelShapeCollision voxelshapecollision = entityhuman == null ? VoxelShapeCollision.empty() : VoxelShapeCollision.of(entityhuman); + // CraftBukkit start - store default return + boolean defaultReturn = (!this.mustSurvive() || iblockdata.canSurvive(blockactioncontext.getLevel(), blockactioncontext.getClickedPos())) && blockactioncontext.getLevel().isUnobstructed(iblockdata, blockactioncontext.getClickedPos(), voxelshapecollision); + org.bukkit.entity.Player player = (blockactioncontext.getPlayer() instanceof EntityPlayer) ? (org.bukkit.entity.Player) blockactioncontext.getPlayer().getBukkitEntity() : null; + + BlockCanBuildEvent event = new BlockCanBuildEvent(CraftBlock.at(blockactioncontext.getLevel(), blockactioncontext.getClickedPos()), player, CraftBlockData.fromData(iblockdata), defaultReturn); + blockactioncontext.getLevel().getCraftServer().getPluginManager().callEvent(event); - return (!this.mustSurvive() || iblockdata.canSurvive(blockactioncontext.getLevel(), blockactioncontext.getClickedPos())) && blockactioncontext.getLevel().isUnobstructed(iblockdata, blockactioncontext.getClickedPos(), voxelshapecollision); + return event.isBuildable(); + // CraftBukkit end } protected boolean mustSurvive() { @@ -239,7 +282,7 @@ if (nbttagcompound != null && nbttagcompound.contains("Items", 9)) { NBTTagList nbttaglist = nbttagcompound.getList("Items", 10); - Stream stream = nbttaglist.stream(); + Stream stream = nbttaglist.stream(); // CraftBukkit - decompile error Objects.requireNonNull(NBTTagCompound.class); ItemLiquidUtil.onContainerDestroyed(entityitem, stream.map(NBTTagCompound.class::cast).map(ItemStack::of));