--- a/net/minecraft/world/item/ItemStack.java +++ b/net/minecraft/world/item/ItemStack.java @@ -75,6 +75,40 @@ import net.minecraft.world.level.block.state.pattern.ShapeDetectorBlock; import org.slf4j.Logger; +// CraftBukkit start +import com.mojang.serialization.Dynamic; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import net.minecraft.core.EnumDirection; +import net.minecraft.nbt.DynamicOpsNBT; +import net.minecraft.network.protocol.game.PacketPlayOutBlockChange; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.WorldServer; +import net.minecraft.sounds.SoundCategory; +import net.minecraft.util.datafix.fixes.DataConverterTypes; +import net.minecraft.world.level.block.BlockSapling; +import net.minecraft.world.level.block.BlockTileEntity; +import net.minecraft.world.level.block.BlockWitherSkull; +import net.minecraft.world.level.block.SoundEffectType; +import net.minecraft.world.level.block.entity.TileEntity; +import net.minecraft.world.level.block.entity.TileEntityJukeBox; +import net.minecraft.world.level.block.entity.TileEntitySign; +import net.minecraft.world.level.block.entity.TileEntitySkull; +import net.minecraft.world.level.gameevent.GameEvent; +import org.bukkit.Location; +import org.bukkit.TreeType; +import org.bukkit.block.BlockState; +import org.bukkit.craftbukkit.block.CraftBlock; +import org.bukkit.craftbukkit.block.CraftBlockState; +import org.bukkit.craftbukkit.inventory.CraftItemStack; +import org.bukkit.craftbukkit.util.CraftMagicNumbers; +import org.bukkit.entity.Player; +import org.bukkit.event.block.BlockFertilizeEvent; +import org.bukkit.event.player.PlayerItemDamageEvent; +import org.bukkit.event.world.StructureGrowEvent; +// CraftBukkit end + public final class ItemStack { public static final Codec CODEC = RecordCodecBuilder.create((instance) -> { @@ -151,16 +185,30 @@ this.updateEmptyCacheFlag(); } + // Called to run this stack through the data converter to handle older storage methods and serialized items + public void convertStack(int version) { + if (0 < version && version < CraftMagicNumbers.INSTANCE.getDataVersion()) { + NBTTagCompound savedStack = new NBTTagCompound(); + this.save(savedStack); + savedStack = (NBTTagCompound) MinecraftServer.getServer().fixerUpper.update(DataConverterTypes.ITEM_STACK, new Dynamic(DynamicOpsNBT.INSTANCE, savedStack), version, CraftMagicNumbers.INSTANCE.getDataVersion()).getValue(); + this.load(savedStack); + } + } + private void updateEmptyCacheFlag() { + if (this.emptyCacheFlag && this == ItemStack.EMPTY) throw new AssertionError("TRAP"); // CraftBukkit this.emptyCacheFlag = false; this.emptyCacheFlag = this.isEmpty(); } - private ItemStack(NBTTagCompound nbttagcompound) { + // CraftBukkit - break into own method + private void load(NBTTagCompound nbttagcompound) { this.item = (Item) BuiltInRegistries.ITEM.get(new MinecraftKey(nbttagcompound.getString("id"))); this.count = nbttagcompound.getByte("Count"); if (nbttagcompound.contains("tag", 10)) { - this.tag = nbttagcompound.getCompound("tag"); + // CraftBukkit start - make defensive copy as this data may be coming from the save thread + this.tag = nbttagcompound.getCompound("tag").copy(); + // CraftBukkit end this.getItem().verifyTagAfterLoad(this.tag); } @@ -168,6 +216,11 @@ this.setDamageValue(this.getDamageValue()); } + } + + private ItemStack(NBTTagCompound nbttagcompound) { + this.load(nbttagcompound); + // CraftBukkit end this.updateEmptyCacheFlag(); } @@ -225,7 +278,7 @@ return this.getItem().builtInRegistryHolder().tags(); } - public EnumInteractionResult useOn(ItemActionContext itemactioncontext) { + public EnumInteractionResult useOn(ItemActionContext itemactioncontext, EnumHand enumhand) { // CraftBukkit - add hand EntityHuman entityhuman = itemactioncontext.getPlayer(); BlockPosition blockposition = itemactioncontext.getClickedPos(); ShapeDetectorBlock shapedetectorblock = new ShapeDetectorBlock(itemactioncontext.getLevel(), blockposition, false); @@ -233,12 +286,171 @@ if (entityhuman != null && !entityhuman.getAbilities().mayBuild && !this.hasAdventureModePlaceTagForBlock(itemactioncontext.getLevel().registryAccess().registryOrThrow(Registries.BLOCK), shapedetectorblock)) { return EnumInteractionResult.PASS; } else { + // CraftBukkit start - handle all block place event logic here + NBTTagCompound oldData = this.getTagClone(); + int oldCount = this.getCount(); + WorldServer world = (WorldServer) itemactioncontext.getLevel(); + + if (!(this.getItem() instanceof ItemBucket || this.getItem() instanceof SolidBucketItem)) { // if not bucket + world.captureBlockStates = true; + // special case bonemeal + if (this.getItem() == Items.BONE_MEAL) { + world.captureTreeGeneration = true; + } + } Item item = this.getItem(); EnumInteractionResult enuminteractionresult = item.useOn(itemactioncontext); + NBTTagCompound newData = this.getTagClone(); + int newCount = this.getCount(); + this.setCount(oldCount); + this.setTagClone(oldData); + world.captureBlockStates = false; + if (enuminteractionresult.consumesAction() && world.captureTreeGeneration && world.capturedBlockStates.size() > 0) { + world.captureTreeGeneration = false; + Location location = new Location(world.getWorld(), blockposition.getX(), blockposition.getY(), blockposition.getZ()); + TreeType treeType = BlockSapling.treeType; + BlockSapling.treeType = null; + List blocks = new java.util.ArrayList<>(world.capturedBlockStates.values()); + world.capturedBlockStates.clear(); + StructureGrowEvent structureEvent = null; + if (treeType != null) { + boolean isBonemeal = getItem() == Items.BONE_MEAL; + structureEvent = new StructureGrowEvent(location, treeType, isBonemeal, (Player) entityhuman.getBukkitEntity(), (List< BlockState>) (List) blocks); + org.bukkit.Bukkit.getPluginManager().callEvent(structureEvent); + } + + BlockFertilizeEvent fertilizeEvent = new BlockFertilizeEvent(CraftBlock.at(world, blockposition), (Player) entityhuman.getBukkitEntity(), (List< BlockState>) (List) blocks); + fertilizeEvent.setCancelled(structureEvent != null && structureEvent.isCancelled()); + org.bukkit.Bukkit.getPluginManager().callEvent(fertilizeEvent); + + if (!fertilizeEvent.isCancelled()) { + // Change the stack to its new contents if it hasn't been tampered with. + if (this.getCount() == oldCount && Objects.equals(this.tag, oldData)) { + this.setTag(newData); + this.setCount(newCount); + } + for (CraftBlockState blockstate : blocks) { + world.setBlock(blockstate.getPosition(),blockstate.getHandle(), blockstate.getFlag()); // SPIGOT-7248 - manual update to avoid physics where appropriate + } + entityhuman.awardStat(StatisticList.ITEM_USED.get(item)); // SPIGOT-7236 - award stat + } + + ItemSign.openSign = null; // SPIGOT-6758 - Reset on early return + return enuminteractionresult; + } + world.captureTreeGeneration = false; if (entityhuman != null && enuminteractionresult.shouldAwardStats()) { - entityhuman.awardStat(StatisticList.ITEM_USED.get(item)); + org.bukkit.event.block.BlockPlaceEvent placeEvent = null; + List blocks = new java.util.ArrayList<>(world.capturedBlockStates.values()); + world.capturedBlockStates.clear(); + if (blocks.size() > 1) { + placeEvent = org.bukkit.craftbukkit.event.CraftEventFactory.callBlockMultiPlaceEvent(world, entityhuman, enumhand, blocks, blockposition.getX(), blockposition.getY(), blockposition.getZ()); + } else if (blocks.size() == 1) { + placeEvent = org.bukkit.craftbukkit.event.CraftEventFactory.callBlockPlaceEvent(world, entityhuman, enumhand, blocks.get(0), blockposition.getX(), blockposition.getY(), blockposition.getZ()); + } + + if (placeEvent != null && (placeEvent.isCancelled() || !placeEvent.canBuild())) { + enuminteractionresult = EnumInteractionResult.FAIL; // cancel placement + // PAIL: Remove this when MC-99075 fixed + placeEvent.getPlayer().updateInventory(); + // revert back all captured blocks + world.preventPoiUpdated = true; // CraftBukkit - SPIGOT-5710 + for (BlockState blockstate : blocks) { + blockstate.update(true, false); + } + world.preventPoiUpdated = false; + + // Brute force all possible updates + BlockPosition placedPos = ((CraftBlock) placeEvent.getBlock()).getPosition(); + for (EnumDirection dir : EnumDirection.values()) { + ((EntityPlayer) entityhuman).connection.send(new PacketPlayOutBlockChange(world, placedPos.relative(dir))); + } + ItemSign.openSign = null; // SPIGOT-6758 - Reset on early return + } else { + // Change the stack to its new contents if it hasn't been tampered with. + if (this.getCount() == oldCount && Objects.equals(this.tag, oldData)) { + this.setTag(newData); + this.setCount(newCount); + } + + for (Map.Entry e : world.capturedTileEntities.entrySet()) { + world.setBlockEntity(e.getValue()); + } + + for (BlockState blockstate : blocks) { + int updateFlag = ((CraftBlockState) blockstate).getFlag(); + IBlockData oldBlock = ((CraftBlockState) blockstate).getHandle(); + BlockPosition newblockposition = ((CraftBlockState) blockstate).getPosition(); + IBlockData block = world.getBlockState(newblockposition); + + if (!(block.getBlock() instanceof BlockTileEntity)) { // Containers get placed automatically + block.getBlock().onPlace(block, world, newblockposition, oldBlock, true); + } + + world.notifyAndUpdatePhysics(newblockposition, null, oldBlock, block, world.getBlockState(newblockposition), updateFlag, 512); // send null chunk as chunk.k() returns false by this point + } + + // Special case juke boxes as they update their tile entity. Copied from ItemRecord. + // PAIL: checkme on updates. + if (this.item instanceof ItemRecord) { + TileEntity tileentity = world.getBlockEntity(blockposition); + + if (tileentity instanceof TileEntityJukeBox) { + TileEntityJukeBox tileentityjukebox = (TileEntityJukeBox) tileentity; + + // There can only be one + ItemStack record = this.copy(); + if (!record.isEmpty()) { + record.setCount(1); + } + + tileentityjukebox.setFirstItem(record); + world.gameEvent(GameEvent.BLOCK_CHANGE, blockposition, GameEvent.a.of(entityhuman, world.getBlockState(blockposition))); + } + + this.shrink(1); + entityhuman.awardStat(StatisticList.PLAY_RECORD); + } + + if (this.item == Items.WITHER_SKELETON_SKULL) { // Special case skulls to allow wither spawns to be cancelled + BlockPosition bp = blockposition; + if (!world.getBlockState(blockposition).getMaterial().isReplaceable()) { + if (!world.getBlockState(blockposition).getMaterial().isSolid()) { + bp = null; + } else { + bp = bp.relative(itemactioncontext.getClickedFace()); + } + } + if (bp != null) { + TileEntity te = world.getBlockEntity(bp); + if (te instanceof TileEntitySkull) { + BlockWitherSkull.checkSpawn(world, bp, (TileEntitySkull) te); + } + } + } + + // SPIGOT-4678 + if (this.item instanceof ItemSign && ItemSign.openSign != null) { + try { + entityhuman.openTextEdit((TileEntitySign) world.getBlockEntity(ItemSign.openSign)); + } finally { + ItemSign.openSign = null; + } + } + + // SPIGOT-1288 - play sound stripped from ItemBlock + if (this.item instanceof ItemBlock) { + SoundEffectType soundeffecttype = ((ItemBlock) this.item).getBlock().getSoundType(null); + world.playSound(entityhuman, blockposition, soundeffecttype.getPlaceSound(), SoundCategory.BLOCKS, (soundeffecttype.getVolume() + 1.0F) / 2.0F, soundeffecttype.getPitch() * 0.8F); + } + + entityhuman.awardStat(StatisticList.ITEM_USED.get(item)); + } } + world.capturedTileEntities.clear(); + world.capturedBlockStates.clear(); + // CraftBukkit end return enuminteractionresult; } @@ -319,6 +531,21 @@ } i -= k; + // CraftBukkit start + if (entityplayer != null) { + PlayerItemDamageEvent event = new PlayerItemDamageEvent(entityplayer.getBukkitEntity(), CraftItemStack.asCraftMirror(this), i); + event.getPlayer().getServer().getPluginManager().callEvent(event); + + if (i != event.getDamage() || event.isCancelled()) { + event.getPlayer().updateInventory(); + } + if (event.isCancelled()) { + return false; + } + + i = event.getDamage(); + } + // CraftBukkit end if (i <= 0) { return false; } @@ -340,6 +567,11 @@ if (this.hurt(i, t0.getRandom(), t0 instanceof EntityPlayer ? (EntityPlayer) t0 : null)) { consumer.accept(t0); Item item = this.getItem(); + // CraftBukkit start - Check for item breaking + if (this.count == 1 && t0 instanceof EntityHuman) { + org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemBreakEvent((EntityHuman) t0, this); + } + // CraftBukkit end this.shrink(1); if (t0 instanceof EntityHuman) { @@ -494,6 +726,17 @@ return this.tag; } + // CraftBukkit start + @Nullable + private NBTTagCompound getTagClone() { + return this.tag == null ? null : this.tag.copy(); + } + + private void setTagClone(@Nullable NBTTagCompound nbtttagcompound) { + this.setTag(nbtttagcompound == null ? null : nbtttagcompound.copy()); + } + // CraftBukkit end + public NBTTagCompound getOrCreateTag() { if (this.tag == null) { this.setTag(new NBTTagCompound()); @@ -864,6 +1107,12 @@ } public void setRepairCost(int i) { + // CraftBukkit start - remove RepairCost tag when 0 (SPIGOT-3945) + if (i == 0) { + this.removeTagKey("RepairCost"); + return; + } + // CraftBukkit end this.getOrCreateTag().putInt("RepairCost", i); } @@ -913,6 +1162,13 @@ nbttaglist.add(nbttagcompound); } + // CraftBukkit start + @Deprecated + public void setItem(Item item) { + this.item = item; + } + // CraftBukkit end + public IChatBaseComponent getDisplayName() { IChatMutableComponent ichatmutablecomponent = IChatBaseComponent.empty().append(this.getHoverName());