2021-03-15 23:00:00 +01:00
--- a/net/minecraft/world/item/ItemStack.java
+++ b/net/minecraft/world/item/ItemStack.java
2023-12-05 17:40:00 +01:00
@@ -81,6 +81,43 @@
2022-02-28 16:00:00 +01:00
import net.minecraft.world.level.block.state.pattern.ShapeDetectorBlock;
import org.slf4j.Logger;
2014-11-25 22:32:16 +01:00
+// CraftBukkit start
2020-06-25 02:00:00 +02:00
+import com.mojang.serialization.Dynamic;
2015-03-16 10:48:01 +01:00
+import java.util.Map;
2021-11-21 23:00:00 +01:00
+import java.util.Objects;
2021-03-15 23:00:00 +01:00
+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;
2023-04-07 10:56:58 +02:00
+import net.minecraft.world.level.block.BlockBed;
+import net.minecraft.world.level.block.Blocks;
2021-03-15 23:00:00 +01:00
+import net.minecraft.world.level.block.BlockSapling;
2023-06-07 17:30:00 +02:00
+import net.minecraft.world.level.block.BlockSign;
2021-03-15 23:00:00 +01:00
+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;
2023-03-14 17:30:00 +01:00
+import net.minecraft.world.level.block.entity.TileEntityJukeBox;
2021-03-15 23:00:00 +01:00
+import net.minecraft.world.level.block.entity.TileEntitySign;
+import net.minecraft.world.level.block.entity.TileEntitySkull;
2023-03-14 17:30:00 +01:00
+import net.minecraft.world.level.gameevent.GameEvent;
2014-11-25 22:32:16 +01:00
+import org.bukkit.Location;
+import org.bukkit.TreeType;
+import org.bukkit.block.BlockState;
2018-07-26 12:44:09 +02:00
+import org.bukkit.craftbukkit.block.CraftBlock;
2014-11-25 22:32:16 +01:00
+import org.bukkit.craftbukkit.block.CraftBlockState;
2018-12-25 22:00:00 +01:00
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
2023-04-19 11:59:19 +02:00
+import org.bukkit.craftbukkit.util.CraftLocation;
2014-11-25 22:32:16 +01:00
+import org.bukkit.craftbukkit.util.CraftMagicNumbers;
+import org.bukkit.entity.Player;
2018-07-26 12:44:09 +02:00
+import org.bukkit.event.block.BlockFertilizeEvent;
2018-12-25 22:00:00 +01:00
+import org.bukkit.event.player.PlayerItemDamageEvent;
2014-11-25 22:32:16 +01:00
+import org.bukkit.event.world.StructureGrowEvent;
+// CraftBukkit end
+
public final class ItemStack {
2021-06-11 07:00:00 +02:00
public static final Codec<ItemStack> CODEC = RecordCodecBuilder.create((instance) -> {
2023-12-05 17:40:00 +01:00
@@ -175,7 +212,18 @@
2023-06-07 17:30:00 +02:00
this.item = null;
2016-11-17 02:41:03 +01:00
}
2023-06-07 17:30:00 +02:00
- private ItemStack(NBTTagCompound nbttagcompound) {
2016-12-03 00:51:53 +01:00
+ // Called to run this stack through the data converter to handle older storage methods and serialized items
2019-04-23 04:00:00 +02:00
+ public void convertStack(int version) {
+ if (0 < version && version < CraftMagicNumbers.INSTANCE.getDataVersion()) {
2016-12-03 00:51:53 +01:00
+ NBTTagCompound savedStack = new NBTTagCompound();
+ this.save(savedStack);
2021-06-11 07:00:00 +02:00
+ savedStack = (NBTTagCompound) MinecraftServer.getServer().fixerUpper.update(DataConverterTypes.ITEM_STACK, new Dynamic(DynamicOpsNBT.INSTANCE, savedStack), version, CraftMagicNumbers.INSTANCE.getDataVersion()).getValue();
2016-12-03 00:51:53 +01:00
+ this.load(savedStack);
+ }
+ }
+
2016-11-17 02:41:03 +01:00
+ // CraftBukkit - break into own method
2018-07-15 02:00:00 +02:00
+ private void load(NBTTagCompound nbttagcompound) {
2022-12-07 17:00:00 +01:00
this.item = (Item) BuiltInRegistries.ITEM.get(new MinecraftKey(nbttagcompound.getString("id")));
2016-11-17 02:41:03 +01:00
this.count = nbttagcompound.getByte("Count");
2021-11-21 23:00:00 +01:00
if (nbttagcompound.contains("tag", 10)) {
2023-12-05 17:40:00 +01:00
@@ -189,6 +237,11 @@
2023-06-07 17:30:00 +02:00
}
2015-02-26 23:41:06 +01:00
2018-07-15 02:00:00 +02:00
+ private ItemStack(NBTTagCompound nbttagcompound) {
2016-11-17 02:41:03 +01:00
+ this.load(nbttagcompound);
+ // CraftBukkit end
2023-06-07 17:30:00 +02:00
+ }
+
public static ItemStack of(NBTTagCompound nbttagcompound) {
try {
return new ItemStack(nbttagcompound);
2023-12-05 17:40:00 +01:00
@@ -266,11 +319,190 @@
2018-07-15 02:00:00 +02:00
return EnumInteractionResult.PASS;
} else {
2023-07-29 02:06:05 +02:00
Item item = this.getItem();
- EnumInteractionResult enuminteractionresult = item.useOn(itemactioncontext);
2018-07-15 02:00:00 +02:00
+ // CraftBukkit start - handle all block place event logic here
2018-12-22 01:25:14 +01:00
+ NBTTagCompound oldData = this.getTagClone();
2018-07-15 02:00:00 +02:00
+ int oldCount = this.getCount();
2021-11-21 23:00:00 +01:00
+ WorldServer world = (WorldServer) itemactioncontext.getLevel();
2018-07-15 02:00:00 +02:00
+
2023-07-29 02:06:05 +02:00
+ if (!(item instanceof ItemBucket || item instanceof SolidBucketItem)) { // if not bucket
2018-07-15 02:00:00 +02:00
+ world.captureBlockStates = true;
+ // special case bonemeal
2023-07-29 02:06:05 +02:00
+ if (item == Items.BONE_MEAL) {
2018-07-26 12:44:09 +02:00
+ world.captureTreeGeneration = true;
2014-11-25 22:32:16 +01:00
+ }
+ }
2023-07-29 02:06:05 +02:00
+ EnumInteractionResult enuminteractionresult;
+ try {
+ enuminteractionresult = item.useOn(itemactioncontext);
+ } finally {
+ world.captureBlockStates = false;
+ }
2018-12-22 01:25:14 +01:00
+ NBTTagCompound newData = this.getTagClone();
2018-07-15 02:00:00 +02:00
+ int newCount = this.getCount();
+ this.setCount(oldCount);
2018-12-22 01:25:14 +01:00
+ this.setTagClone(oldData);
2021-11-21 23:00:00 +01:00
+ if (enuminteractionresult.consumesAction() && world.captureTreeGeneration && world.capturedBlockStates.size() > 0) {
2018-07-15 02:00:00 +02:00
+ world.captureTreeGeneration = false;
2023-04-19 11:59:19 +02:00
+ Location location = CraftLocation.toBukkit(blockposition, world.getWorld());
2018-07-15 02:00:00 +02:00
+ TreeType treeType = BlockSapling.treeType;
+ BlockSapling.treeType = null;
2023-01-31 09:44:37 +01:00
+ List<CraftBlockState> blocks = new java.util.ArrayList<>(world.capturedBlockStates.values());
2018-07-15 02:00:00 +02:00
+ world.capturedBlockStates.clear();
2018-07-26 12:44:09 +02:00
+ StructureGrowEvent structureEvent = null;
2018-07-15 02:00:00 +02:00
+ if (treeType != null) {
+ boolean isBonemeal = getItem() == Items.BONE_MEAL;
2023-01-31 09:44:37 +01:00
+ structureEvent = new StructureGrowEvent(location, treeType, isBonemeal, (Player) entityhuman.getBukkitEntity(), (List< BlockState>) (List<? extends BlockState>) blocks);
2018-07-26 12:44:09 +02:00
+ org.bukkit.Bukkit.getPluginManager().callEvent(structureEvent);
2014-11-25 22:32:16 +01:00
+ }
2018-07-26 12:44:09 +02:00
+
2023-01-31 09:44:37 +01:00
+ BlockFertilizeEvent fertilizeEvent = new BlockFertilizeEvent(CraftBlock.at(world, blockposition), (Player) entityhuman.getBukkitEntity(), (List< BlockState>) (List<? extends BlockState>) blocks);
2018-07-26 12:44:09 +02:00
+ fertilizeEvent.setCancelled(structureEvent != null && structureEvent.isCancelled());
+ org.bukkit.Bukkit.getPluginManager().callEvent(fertilizeEvent);
+
+ if (!fertilizeEvent.isCancelled()) {
2018-07-15 02:00:00 +02:00
+ // Change the stack to its new contents if it hasn't been tampered with.
2018-12-22 01:25:14 +01:00
+ if (this.getCount() == oldCount && Objects.equals(this.tag, oldData)) {
+ this.setTag(newData);
2018-07-15 02:00:00 +02:00
+ this.setCount(newCount);
+ }
2023-01-31 09:44:37 +01:00
+ for (CraftBlockState blockstate : blocks) {
+ world.setBlock(blockstate.getPosition(),blockstate.getHandle(), blockstate.getFlag()); // SPIGOT-7248 - manual update to avoid physics where appropriate
2018-07-15 02:00:00 +02:00
+ }
2023-01-21 01:00:43 +01:00
+ entityhuman.awardStat(StatisticList.ITEM_USED.get(item)); // SPIGOT-7236 - award stat
2014-11-25 22:32:16 +01:00
+ }
+
2021-10-16 09:48:22 +02:00
+ ItemSign.openSign = null; // SPIGOT-6758 - Reset on early return
2018-07-15 02:00:00 +02:00
+ return enuminteractionresult;
2014-11-25 22:32:16 +01:00
+ }
2018-07-15 02:00:00 +02:00
+ world.captureTreeGeneration = false;
2021-11-21 23:00:00 +01:00
if (entityhuman != null && enuminteractionresult.shouldAwardStats()) {
- entityhuman.awardStat(StatisticList.ITEM_USED.get(item));
2023-06-24 09:15:05 +02:00
+ EnumHand enumhand = itemactioncontext.getHand();
2018-07-15 02:00:00 +02:00
+ org.bukkit.event.block.BlockPlaceEvent placeEvent = null;
2020-01-27 23:48:28 +01:00
+ List<BlockState> blocks = new java.util.ArrayList<>(world.capturedBlockStates.values());
2018-07-15 02:00:00 +02:00
+ 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());
2016-11-23 23:30:01 +01:00
+ }
+
2018-07-15 02:00:00 +02:00
+ 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
2021-07-03 07:39:52 +02:00
+ world.preventPoiUpdated = true; // CraftBukkit - SPIGOT-5710
2018-07-15 02:00:00 +02:00
+ for (BlockState blockstate : blocks) {
+ blockstate.update(true, false);
+ }
2021-07-03 07:39:52 +02:00
+ world.preventPoiUpdated = false;
2018-08-03 03:34:26 +02:00
+
+ // Brute force all possible updates
+ BlockPosition placedPos = ((CraftBlock) placeEvent.getBlock()).getPosition();
+ for (EnumDirection dir : EnumDirection.values()) {
2021-11-21 23:00:00 +01:00
+ ((EntityPlayer) entityhuman).connection.send(new PacketPlayOutBlockChange(world, placedPos.relative(dir)));
2018-08-03 03:34:26 +02:00
+ }
2021-10-16 09:48:22 +02:00
+ ItemSign.openSign = null; // SPIGOT-6758 - Reset on early return
2018-07-15 02:00:00 +02:00
+ } else {
+ // Change the stack to its new contents if it hasn't been tampered with.
2018-12-22 01:25:14 +01:00
+ if (this.getCount() == oldCount && Objects.equals(this.tag, oldData)) {
+ this.setTag(newData);
2018-07-15 02:00:00 +02:00
+ this.setCount(newCount);
+ }
2014-11-25 22:32:16 +01:00
+
2018-07-15 02:00:00 +02:00
+ for (Map.Entry<BlockPosition, TileEntity> e : world.capturedTileEntities.entrySet()) {
2021-11-21 23:00:00 +01:00
+ world.setBlockEntity(e.getValue());
2014-11-25 22:32:16 +01:00
+ }
+
2018-07-15 02:00:00 +02:00
+ for (BlockState blockstate : blocks) {
+ int updateFlag = ((CraftBlockState) blockstate).getFlag();
+ IBlockData oldBlock = ((CraftBlockState) blockstate).getHandle();
2018-12-22 01:32:11 +01:00
+ BlockPosition newblockposition = ((CraftBlockState) blockstate).getPosition();
2021-11-21 23:00:00 +01:00
+ IBlockData block = world.getBlockState(newblockposition);
2018-07-15 02:00:00 +02:00
+
+ if (!(block.getBlock() instanceof BlockTileEntity)) { // Containers get placed automatically
2019-04-23 04:00:00 +02:00
+ block.getBlock().onPlace(block, world, newblockposition, oldBlock, true);
2018-07-15 02:00:00 +02:00
+ }
2015-02-02 22:55:33 +01:00
+
2021-11-21 23:00:00 +01:00
+ world.notifyAndUpdatePhysics(newblockposition, null, oldBlock, block, world.getBlockState(newblockposition), updateFlag, 512); // send null chunk as chunk.k() returns false by this point
2018-07-15 02:00:00 +02:00
+ }
2015-04-10 13:24:34 +02:00
+
2018-07-15 02:00:00 +02:00
+ // Special case juke boxes as they update their tile entity. Copied from ItemRecord.
+ // PAIL: checkme on updates.
+ if (this.item instanceof ItemRecord) {
2023-03-14 17:30:00 +01:00
+ 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);
+ }
+
2023-12-05 17:40:00 +01:00
+ tileentityjukebox.setTheItem(record);
2023-03-14 17:30:00 +01:00
+ world.gameEvent(GameEvent.BLOCK_CHANGE, blockposition, GameEvent.a.of(entityhuman, world.getBlockState(blockposition)));
+ }
+
2021-11-21 23:00:00 +01:00
+ this.shrink(1);
+ entityhuman.awardStat(StatisticList.PLAY_RECORD);
2015-02-02 22:55:33 +01:00
+ }
2018-07-15 02:00:00 +02:00
+
+ if (this.item == Items.WITHER_SKELETON_SKULL) { // Special case skulls to allow wither spawns to be cancelled
2018-07-23 03:58:43 +02:00
+ BlockPosition bp = blockposition;
2023-06-07 17:30:00 +02:00
+ if (!world.getBlockState(blockposition).canBeReplaced()) {
+ if (!world.getBlockState(blockposition).isSolid()) {
2018-07-23 03:58:43 +02:00
+ bp = null;
+ } else {
2021-11-21 23:00:00 +01:00
+ bp = bp.relative(itemactioncontext.getClickedFace());
2018-07-23 03:58:43 +02:00
+ }
+ }
+ if (bp != null) {
2021-11-21 23:00:00 +01:00
+ TileEntity te = world.getBlockEntity(bp);
2018-07-23 03:58:43 +02:00
+ if (te instanceof TileEntitySkull) {
2021-11-21 23:00:00 +01:00
+ BlockWitherSkull.checkSpawn(world, bp, (TileEntitySkull) te);
2018-07-23 03:58:43 +02:00
+ }
2015-02-02 22:55:33 +01:00
+ }
+ }
+
2019-03-24 11:34:57 +01:00
+ // SPIGOT-4678
2019-04-25 01:55:41 +02:00
+ if (this.item instanceof ItemSign && ItemSign.openSign != null) {
+ try {
2023-06-08 00:03:24 +02:00
+ if (world.getBlockEntity(ItemSign.openSign) instanceof TileEntitySign tileentitysign) {
+ if (world.getBlockState(ItemSign.openSign).getBlock() instanceof BlockSign blocksign) {
2023-08-05 01:33:42 +02:00
+ blocksign.openTextEdit(entityhuman, tileentitysign, true, org.bukkit.event.player.PlayerSignOpenEvent.Cause.PLACE); // Craftbukkit
2023-06-07 17:30:00 +02:00
+ }
+ }
2019-04-25 01:55:41 +02:00
+ } finally {
+ ItemSign.openSign = null;
+ }
2019-03-24 11:34:57 +01:00
+ }
+
2023-04-07 10:56:58 +02:00
+ // SPIGOT-7315: Moved from BlockBed#setPlacedBy
+ if (placeEvent != null && this.item instanceof ItemBed) {
+ BlockPosition position = ((CraftBlock) placeEvent.getBlock()).getPosition();
+ IBlockData blockData = world.getBlockState(position);
+
+ if (blockData.getBlock() instanceof BlockBed) {
+ world.blockUpdated(position, Blocks.AIR);
+ blockData.updateNeighbourShapes(world, position, 3);
+ }
+ }
+
2018-07-15 02:00:00 +02:00
+ // SPIGOT-1288 - play sound stripped from ItemBlock
+ if (this.item instanceof ItemBlock) {
2023-06-13 11:05:00 +02:00
+ SoundEffectType soundeffecttype = ((ItemBlock) this.item).getBlock().defaultBlockState().getSoundType(); // TODO: not strictly correct, however currently only affects decorated pots
2021-01-16 02:00:00 +01:00
+ world.playSound(entityhuman, blockposition, soundeffecttype.getPlaceSound(), SoundCategory.BLOCKS, (soundeffecttype.getVolume() + 1.0F) / 2.0F, soundeffecttype.getPitch() * 0.8F);
2018-07-15 02:00:00 +02:00
+ }
2016-06-30 07:39:58 +02:00
+
2021-11-21 23:00:00 +01:00
+ entityhuman.awardStat(StatisticList.ITEM_USED.get(item));
2018-07-15 02:00:00 +02:00
+ }
}
+ world.capturedTileEntities.clear();
+ world.capturedBlockStates.clear();
+ // CraftBukkit end
2014-11-25 22:32:16 +01:00
2018-07-15 02:00:00 +02:00
return enuminteractionresult;
}
2023-12-05 17:40:00 +01:00
@@ -351,6 +583,21 @@
2018-12-25 22:00:00 +01:00
}
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;
}
2023-12-05 17:40:00 +01:00
@@ -372,6 +619,11 @@
2021-11-21 23:00:00 +01:00
if (this.hurt(i, t0.getRandom(), t0 instanceof EntityPlayer ? (EntityPlayer) t0 : null)) {
2019-04-23 04:00:00 +02:00
consumer.accept(t0);
2018-07-15 02:00:00 +02:00
Item item = this.getItem();
2014-11-25 22:32:16 +01:00
+ // CraftBukkit start - Check for item breaking
2019-04-23 04:00:00 +02:00
+ if (this.count == 1 && t0 instanceof EntityHuman) {
+ org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemBreakEvent((EntityHuman) t0, this);
2014-11-25 22:32:16 +01:00
+ }
+ // CraftBukkit end
2018-07-15 02:00:00 +02:00
2021-11-21 23:00:00 +01:00
this.shrink(1);
2019-04-23 04:00:00 +02:00
if (t0 instanceof EntityHuman) {
2023-12-05 17:40:00 +01:00
@@ -524,6 +776,17 @@
2018-12-22 01:25:14 +01:00
return this.tag;
}
+ // CraftBukkit start
+ @Nullable
+ private NBTTagCompound getTagClone() {
2021-11-21 23:00:00 +01:00
+ return this.tag == null ? null : this.tag.copy();
2018-12-22 01:25:14 +01:00
+ }
+
+ private void setTagClone(@Nullable NBTTagCompound nbtttagcompound) {
2021-11-21 23:00:00 +01:00
+ this.setTag(nbtttagcompound == null ? null : nbtttagcompound.copy());
2018-12-22 01:25:14 +01:00
+ }
+ // CraftBukkit end
+
public NBTTagCompound getOrCreateTag() {
if (this.tag == null) {
this.setTag(new NBTTagCompound());
2023-12-05 17:40:00 +01:00
@@ -948,6 +1211,13 @@
2019-04-23 04:00:00 +02:00
nbttaglist.add(nbttagcompound);
2014-11-25 22:32:16 +01:00
}
2018-07-15 02:00:00 +02:00
+ // CraftBukkit start
2016-11-17 02:41:03 +01:00
+ @Deprecated
+ public void setItem(Item item) {
+ this.item = item;
+ }
2018-07-15 02:00:00 +02:00
+ // CraftBukkit end
2016-11-17 02:41:03 +01:00
+
2021-11-21 23:00:00 +01:00
public IChatBaseComponent getDisplayName() {
2022-06-07 18:00:00 +02:00
IChatMutableComponent ichatmutablecomponent = IChatBaseComponent.empty().append(this.getHoverName());
2016-11-17 02:41:03 +01:00