Paper/nms-patches/ItemStack.patch

326 lines
15 KiB
Diff
Raw Normal View History

2015-05-25 12:37:24 +02:00
--- a/net/minecraft/server/ItemStack.java
+++ b/net/minecraft/server/ItemStack.java
2020-06-25 02:00:00 +02:00
@@ -19,6 +19,24 @@
2018-07-15 02:00:00 +02:00
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
+// CraftBukkit start
2020-06-25 02:00:00 +02:00
+import com.mojang.serialization.Dynamic;
+import java.util.List;
+import java.util.Map;
+
+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;
+import org.bukkit.craftbukkit.block.CraftBlockState;
2018-12-25 22:00:00 +01:00
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
+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;
+import org.bukkit.event.world.StructureGrowEvent;
+// CraftBukkit end
+
public final class ItemStack {
2020-06-25 02:00:00 +02:00
public static final Codec<ItemStack> a = RecordCodecBuilder.create((instance) -> {
@@ -32,7 +50,7 @@
});
2019-04-25 04:00:00 +02:00
private static final Logger LOGGER = LogManager.getLogger();
2020-06-25 02:00:00 +02:00
public static final ItemStack b = new ItemStack((Item) null);
- public static final DecimalFormat c = (DecimalFormat) SystemUtils.a((Object) (new DecimalFormat("#.##")), (decimalformat) -> {
+ public static final DecimalFormat c = (DecimalFormat) SystemUtils.a((new DecimalFormat("#.##")), (decimalformat) -> { // CraftBukkit - decompile error
decimalformat.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ROOT));
});
2020-08-11 23:00:00 +02:00
private static final ChatModifier e = ChatModifier.a.setColor(EnumChatFormat.DARK_PURPLE).setItalic(true);
2020-06-25 02:00:00 +02:00
@@ -67,23 +85,42 @@
2019-04-23 04:00:00 +02:00
this.checkEmpty();
2016-11-17 02:41:03 +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()) {
+ NBTTagCompound savedStack = new NBTTagCompound();
+ this.save(savedStack);
2019-04-23 04:00:00 +02:00
+ savedStack = (NBTTagCompound) MinecraftServer.getServer().dataConverterManager.update(DataConverterTypes.ITEM_STACK, new Dynamic(DynamicOpsNBT.a, savedStack), version, CraftMagicNumbers.INSTANCE.getDataVersion()).getValue();
+ this.load(savedStack);
+ }
+ }
+
2019-04-23 04:00:00 +02:00
private void checkEmpty() {
2020-06-25 02:00:00 +02:00
+ if (this.j && this == ItemStack.b) throw new AssertionError("TRAP"); // CraftBukkit
this.j = false;
this.j = this.isEmpty();
2016-11-17 02:41:03 +01:00
}
2018-07-15 02:00:00 +02:00
- private ItemStack(NBTTagCompound nbttagcompound) {
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) {
2019-04-23 04:00:00 +02:00
this.item = (Item) IRegistry.ITEM.get(new MinecraftKey(nbttagcompound.getString("id")));
2016-11-17 02:41:03 +01:00
this.count = nbttagcompound.getByte("Count");
if (nbttagcompound.hasKeyOfType("tag", 10)) {
- this.tag = nbttagcompound.getCompound("tag");
2020-06-25 02:00:00 +02:00
- this.getItem().b(nbttagcompound);
2016-11-17 02:41:03 +01:00
+ // CraftBukkit start - make defensive copy as this data may be coming from the save thread
+ this.tag = (NBTTagCompound) nbttagcompound.getCompound("tag").clone();
2020-06-25 02:00:00 +02:00
+ this.getItem().b(this.tag);
2018-07-15 02:00:00 +02:00
+ // CraftBukkit end
}
if (this.getItem().usesDurability()) {
this.setDamage(this.getDamage());
2016-11-17 02:41:03 +01:00
}
2015-02-26 23:41:06 +01:00
2019-04-23 04:00:00 +02: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
2019-04-23 04:00:00 +02:00
this.checkEmpty();
}
2020-06-25 02:00:00 +02:00
@@ -113,7 +150,7 @@
return this.j ? Items.AIR : this.item;
}
2018-07-15 02:00:00 +02:00
- public EnumInteractionResult placeItem(ItemActionContext itemactioncontext) {
+ public EnumInteractionResult placeItem(ItemActionContext itemactioncontext, EnumHand enumhand) { // CraftBukkit - add hand
EntityHuman entityhuman = itemactioncontext.getEntity();
BlockPosition blockposition = itemactioncontext.getClickPosition();
ShapeDetectorBlock shapedetectorblock = new ShapeDetectorBlock(itemactioncontext.getWorld(), blockposition, false);
2020-06-25 02:00:00 +02:00
@@ -121,12 +158,153 @@
if (entityhuman != null && !entityhuman.abilities.mayBuild && !this.b(itemactioncontext.getWorld().p(), shapedetectorblock)) {
2018-07-15 02:00:00 +02:00
return EnumInteractionResult.PASS;
} else {
+ // CraftBukkit start - handle all block place event logic here
+ NBTTagCompound oldData = this.getTagClone();
2018-07-15 02:00:00 +02:00
+ int oldCount = this.getCount();
2020-06-25 02:00:00 +02:00
+ WorldServer world = (WorldServer) itemactioncontext.getWorld();
2018-07-15 02:00:00 +02:00
+
+ if (!(this.getItem() instanceof ItemBucket)) { // if not bucket
+ world.captureBlockStates = true;
+ // special case bonemeal
+ if (this.getItem() == Items.BONE_MEAL) {
2018-07-26 12:44:09 +02:00
+ world.captureTreeGeneration = true;
+ }
+ }
2018-07-15 02:00:00 +02:00
Item item = this.getItem();
EnumInteractionResult enuminteractionresult = item.a(itemactioncontext);
+ NBTTagCompound newData = this.getTagClone();
2018-07-15 02:00:00 +02:00
+ int newCount = this.getCount();
+ this.setCount(oldCount);
+ this.setTagClone(oldData);
2018-07-15 02:00:00 +02:00
+ world.captureBlockStates = false;
2020-06-25 02:00:00 +02:00
+ if (enuminteractionresult.a() && world.captureTreeGeneration && world.capturedBlockStates.size() > 0) {
2018-07-15 02:00:00 +02:00
+ world.captureTreeGeneration = false;
+ Location location = new Location(world.getWorld(), blockposition.getX(), blockposition.getY(), blockposition.getZ());
+ TreeType treeType = BlockSapling.treeType;
+ BlockSapling.treeType = null;
+ List<BlockState> 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;
2018-07-26 12:44:09 +02:00
+ structureEvent = new StructureGrowEvent(location, treeType, isBonemeal, (Player) entityhuman.getBukkitEntity(), blocks);
+ org.bukkit.Bukkit.getPluginManager().callEvent(structureEvent);
+ }
2018-07-26 12:44:09 +02:00
+
+ BlockFertilizeEvent fertilizeEvent = new BlockFertilizeEvent(CraftBlock.at(world, blockposition), (Player) entityhuman.getBukkitEntity(), blocks);
+ 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.
+ if (this.getCount() == oldCount && Objects.equals(this.tag, oldData)) {
+ this.setTag(newData);
2018-07-15 02:00:00 +02:00
+ this.setCount(newCount);
+ }
+ for (BlockState blockstate : blocks) {
+ blockstate.update(true);
+ }
+ }
+
2018-07-15 02:00:00 +02:00
+ return enuminteractionresult;
+ }
2018-07-15 02:00:00 +02:00
+ world.captureTreeGeneration = false;
2020-06-25 02:00:00 +02:00
if (entityhuman != null && enuminteractionresult.a()) {
2018-07-15 02:00:00 +02:00
- entityhuman.b(StatisticList.ITEM_USED.b(item));
+ org.bukkit.event.block.BlockPlaceEvent placeEvent = null;
+ 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());
+ }
+
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
+ for (BlockState blockstate : blocks) {
+ blockstate.update(true, false);
+ }
+
+ // Brute force all possible updates
+ BlockPosition placedPos = ((CraftBlock) placeEvent.getBlock()).getPosition();
+ for (EnumDirection dir : EnumDirection.values()) {
+ ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutBlockChange(world, placedPos.shift(dir)));
+ }
2018-07-15 02:00:00 +02:00
+ } 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);
2018-07-15 02:00:00 +02:00
+ this.setCount(newCount);
+ }
+
2018-07-15 02:00:00 +02:00
+ for (Map.Entry<BlockPosition, TileEntity> e : world.capturedTileEntities.entrySet()) {
+ world.setTileEntity(e.getKey(), e.getValue());
+ }
+
2018-07-15 02:00:00 +02:00
+ for (BlockState blockstate : blocks) {
+ int updateFlag = ((CraftBlockState) blockstate).getFlag();
+ IBlockData oldBlock = ((CraftBlockState) blockstate).getHandle();
+ BlockPosition newblockposition = ((CraftBlockState) blockstate).getPosition();
2018-07-15 02:00:00 +02:00
+ IBlockData block = world.getType(newblockposition);
+
+ 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
+ }
+
2020-07-07 01:35:47 +02:00
+ world.notifyAndUpdatePhysics(newblockposition, null, oldBlock, block, world.getType(newblockposition), updateFlag, 512); // send null chunk as chunk.k() returns false by this point
2018-07-15 02:00:00 +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) {
+ ((BlockJukeBox) Blocks.JUKEBOX).a(world, blockposition, world.getType(blockposition), this);
+ world.a((EntityHuman) null, 1010, blockposition, Item.getId(this.item));
+ this.subtract(1);
+ entityhuman.a(StatisticList.PLAY_RECORD);
+ }
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;
+ if (!world.getType(blockposition).getMaterial().isReplaceable()) {
+ if (!world.getType(blockposition).getMaterial().isBuildable()) {
+ bp = null;
+ } else {
+ bp = bp.shift(itemactioncontext.getClickedFace());
+ }
+ }
+ if (bp != null) {
+ TileEntity te = world.getTileEntity(bp);
+ if (te instanceof TileEntitySkull) {
+ BlockWitherSkull.a(world, bp, (TileEntitySkull) te);
+ }
+ }
+ }
+
+ // SPIGOT-4678
+ if (this.item instanceof ItemSign && ItemSign.openSign != null) {
+ try {
+ entityhuman.openSign((TileEntitySign) world.getTileEntity(ItemSign.openSign));
+ } finally {
+ ItemSign.openSign = null;
+ }
+ }
+
2018-07-15 02:00:00 +02:00
+ // SPIGOT-1288 - play sound stripped from ItemBlock
+ if (this.item instanceof ItemBlock) {
2019-04-23 04:00:00 +02:00
+ SoundEffectType soundeffecttype = ((ItemBlock) this.item).getBlock().stepSound;
2019-07-20 01:00:00 +02:00
+ world.playSound(entityhuman, blockposition, soundeffecttype.e(), SoundCategory.BLOCKS, (soundeffecttype.a() + 1.0F) / 2.0F, soundeffecttype.b() * 0.8F);
2018-07-15 02:00:00 +02:00
+ }
+
2018-07-15 02:00:00 +02:00
+ entityhuman.b(StatisticList.ITEM_USED.b(item));
+ }
}
+ world.capturedTileEntities.clear();
+ world.capturedBlockStates.clear();
+ // CraftBukkit end
2018-07-15 02:00:00 +02:00
return enuminteractionresult;
}
2020-06-25 02:00:00 +02:00
@@ -207,6 +385,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;
}
2020-06-25 02:00:00 +02:00
@@ -228,6 +421,11 @@
2019-04-23 04:00:00 +02:00
if (this.isDamaged(i, t0.getRandom(), t0 instanceof EntityPlayer ? (EntityPlayer) t0 : null)) {
consumer.accept(t0);
2018-07-15 02:00:00 +02:00
Item item = this.getItem();
+ // 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);
+ }
+ // CraftBukkit end
2018-07-15 02:00:00 +02:00
this.subtract(1);
2019-04-23 04:00:00 +02:00
if (t0 instanceof EntityHuman) {
2020-06-25 02:00:00 +02:00
@@ -359,6 +557,17 @@
return this.tag;
}
+ // CraftBukkit start
+ @Nullable
+ private NBTTagCompound getTagClone() {
+ return this.tag == null ? null : this.tag.clone();
+ }
+
+ private void setTagClone(@Nullable NBTTagCompound nbtttagcompound) {
+ this.setTag(nbtttagcompound == null ? null : nbtttagcompound.clone());
+ }
+ // CraftBukkit end
+
public NBTTagCompound getOrCreateTag() {
if (this.tag == null) {
this.setTag(new NBTTagCompound());
2020-08-11 23:00:00 +02:00
@@ -522,6 +731,12 @@
}
public void setRepairCost(int i) {
+ // CraftBukkit start - remove RepairCost tag when 0 (SPIGOT-3945)
+ if (i == 0) {
2019-04-23 04:00:00 +02:00
+ this.removeTag("RepairCost");
+ return;
+ }
+ // CraftBukkit end
2018-07-15 02:00:00 +02:00
this.getOrCreateTag().setInt("RepairCost", i);
}
2020-08-11 23:00:00 +02:00
@@ -571,6 +786,13 @@
2019-04-23 04:00:00 +02:00
nbttaglist.add(nbttagcompound);
}
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
+
2020-06-25 02:00:00 +02:00
public IChatBaseComponent C() {
IChatMutableComponent ichatmutablecomponent = (new ChatComponentText("")).addSibling(this.getName());
2016-11-17 02:41:03 +01:00