summaryrefslogtreecommitdiffstats
path: root/nms-patches/ItemStack.patch
blob: 7fd816b5714274916e74efac8d7c7169a1ffdd81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
--- a/net/minecraft/server/ItemStack.java
+++ b/net/minecraft/server/ItemStack.java
@@ -16,6 +16,22 @@
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+// CraftBukkit start
+import com.mojang.datafixers.Dynamic;
+import java.util.List;
+import java.util.Map;
+
+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.util.CraftMagicNumbers;
+import org.bukkit.entity.Player;
+import org.bukkit.event.block.BlockFertilizeEvent;
+import org.bukkit.event.world.StructureGrowEvent;
+// CraftBukkit end
+
 public final class ItemStack {
 
     private static final Logger c = LogManager.getLogger();
@@ -50,25 +66,49 @@
         this.E();
     }
 
+    // Called to run this stack through the data converter to handle older storage methods and serialized items
+    public void convertStack() {
+        if (false && MinecraftServer.getServer() != null) { // Skip for now, causes more issues than it solves
+            // Don't convert some things - both the old and new data values are valid
+            // Conversion would make getting then impossible
+            if (this.item == Blocks.PUMPKIN.getItem() || this.item == Blocks.GRASS.getItem() || this.item == Blocks.SNOW.getItem()) {
+                return;
+            }
+
+            NBTTagCompound savedStack = new NBTTagCompound();
+            this.save(savedStack);
+            savedStack = (NBTTagCompound) MinecraftServer.getServer().dataConverterManager.update(DataConverterTypes.ITEM_STACK, new Dynamic(DynamicOpsNBT.a, savedStack), -1, CraftMagicNumbers.DATA_VERSION).getValue();
+            this.load(savedStack);
+        }
+    }
+
     private void E() {
+        if (this.h && this == ItemStack.a) throw new AssertionError("TRAP"); // CraftBukkit
         this.h = false;
         this.h = this.isEmpty();
     }
 
-    private ItemStack(NBTTagCompound nbttagcompound) {
+    // CraftBukkit - break into own method
+    private void load(NBTTagCompound nbttagcompound) {
         Item item = (Item) Item.REGISTRY.get(new MinecraftKey(nbttagcompound.getString("id")));
 
         this.item = item == null ? Items.AIR : item;
         this.count = nbttagcompound.getByte("Count");
         if (nbttagcompound.hasKeyOfType("tag", 10)) {
-            this.tag = nbttagcompound.getCompound("tag");
-            this.getItem().a(nbttagcompound);
+            // CraftBukkit start - make defensive copy as this data may be coming from the save thread
+            this.tag = (NBTTagCompound) nbttagcompound.getCompound("tag").clone();
+            this.getItem().a(this.tag);
+            // CraftBukkit end
         }
 
         if (this.getItem().usesDurability()) {
             this.setDamage(this.getDamage());
         }
+    }
 
+    private ItemStack(NBTTagCompound nbttagcompound) {
+        this.load(nbttagcompound);
+        // CraftBukkit end
         this.E();
     }
 
@@ -98,7 +138,7 @@
         return this.h ? Items.AIR : this.item;
     }
 
-    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);
@@ -106,12 +146,142 @@
         if (entityhuman != null && !entityhuman.abilities.mayBuild && !this.b(itemactioncontext.getWorld().E(), shapedetectorblock)) {
             return EnumInteractionResult.PASS;
         } else {
+            // CraftBukkit start - handle all block place event logic here
+            int oldCount = this.getCount();
+            World world = itemactioncontext.getWorld();
+
+            if (!(this.getItem() instanceof ItemBucket)) { // 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.a(itemactioncontext);
+            int newCount = this.getCount();
+            this.setCount(oldCount);
+            world.captureBlockStates = false;
+            if (enuminteractionresult == EnumInteractionResult.SUCCESS && 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<BlockState> blocks = (List<BlockState>) world.capturedBlockStates.clone();
+                world.capturedBlockStates.clear();
+                StructureGrowEvent structureEvent = null;
+                if (treeType != null) {
+                    boolean isBonemeal = getItem() == Items.BONE_MEAL;
+                    structureEvent = new StructureGrowEvent(location, treeType, isBonemeal, (Player) entityhuman.getBukkitEntity(), blocks);
+                    org.bukkit.Bukkit.getPluginManager().callEvent(structureEvent);
+                }
+
+                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()) {
+                    // Change the stack to its new contents if it hasn't been tampered with.
+                    if (this.getCount() == oldCount) {
+                        this.setCount(newCount);
+                    }
+                    for (BlockState blockstate : blocks) {
+                        blockstate.update(true);
+                    }
+                }
+
+                return enuminteractionresult;
+            }
+            world.captureTreeGeneration = false;
 
             if (entityhuman != null && enuminteractionresult == EnumInteractionResult.SUCCESS) {
-                entityhuman.b(StatisticList.ITEM_USED.b(item));
+                org.bukkit.event.block.BlockPlaceEvent placeEvent = null;
+                List<BlockState> blocks = (List<BlockState>) world.capturedBlockStates.clone();
+                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
+                    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)));
+                    }
+                } else {
+                    // Change the stack to its new contents if it hasn't been tampered with.
+                    if (this.getCount() == oldCount) {
+                        this.setCount(newCount);
+                    }
+
+                    for (Map.Entry<BlockPosition, TileEntity> e : world.capturedTileEntities.entrySet()) {
+                        world.setTileEntity(e.getKey(), e.getValue());
+                    }
+
+                    for (BlockState blockstate : blocks) {
+                        int x = blockstate.getX();
+                        int y = blockstate.getY();
+                        int z = blockstate.getZ();
+                        int updateFlag = ((CraftBlockState) blockstate).getFlag();
+                        IBlockData oldBlock = ((CraftBlockState) blockstate).getHandle();
+                        BlockPosition newblockposition = new BlockPosition(x, y, z);
+                        IBlockData block = world.getType(newblockposition);
+
+                        if (!(block.getBlock() instanceof BlockTileEntity)) { // Containers get placed automatically
+                            block.getBlock().onPlace(block, world, newblockposition, oldBlock);
+                        }
+
+                        world.notifyAndUpdatePhysics(newblockposition, null, oldBlock, block, world.getType(newblockposition), updateFlag); // 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) {
+                        ((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);
+                    }
+
+                    if (this.item == Items.WITHER_SKELETON_SKULL) { // Special case skulls to allow wither spawns to be cancelled
+                        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-1288 - play sound stripped from ItemBlock
+                    if (this.item instanceof ItemBlock) {
+                        SoundEffectType soundeffecttype = ((ItemBlock) this.item).getBlock().getStepSound();
+                        world.a(entityhuman, blockposition, soundeffecttype.e(), SoundCategory.BLOCKS, (soundeffecttype.a() + 1.0F) / 2.0F, soundeffecttype.b() * 0.8F);
+                    }
+
+                    entityhuman.b(StatisticList.ITEM_USED.b(item));
+                }
             }
+            world.capturedTileEntities.clear();
+            world.capturedBlockStates.clear();
+            // CraftBukkit end
 
             return enuminteractionresult;
         }
@@ -135,7 +305,7 @@
         nbttagcompound.setString("id", minecraftkey == null ? "minecraft:air" : minecraftkey.toString());
         nbttagcompound.setByte("Count", (byte) this.count);
         if (this.tag != null) {
-            nbttagcompound.set("tag", this.tag);
+            nbttagcompound.set("tag", this.tag.clone()); // CraftBukkit - make defensive copy, data is going to another thread
         }
 
         return nbttagcompound;
@@ -213,6 +383,11 @@
                 if (this.isDamaged(i, entityliving.getRandom(), entityliving instanceof EntityPlayer ? (EntityPlayer) entityliving : null)) {
                     entityliving.c(this);
                     Item item = this.getItem();
+                    // CraftBukkit start - Check for item breaking
+                    if (this.count == 1 && entityliving instanceof EntityHuman) {
+                        org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemBreakEvent((EntityHuman) entityliving, this);
+                    }
+                    // CraftBukkit end
 
                     this.subtract(1);
                     if (entityliving instanceof EntityHuman) {
@@ -480,6 +655,14 @@
     }
 
     public void setRepairCost(int i) {
+        // CraftBukkit start - remove RepairCost tag when 0 (SPIGOT-3945)
+        if (i == 0) {
+            if (this.hasTag()) {
+                this.tag.remove("RepairCost");
+            }
+            return;
+        }
+        // CraftBukkit end
         this.getOrCreateTag().setInt("RepairCost", i);
     }
 
@@ -522,6 +705,13 @@
         nbttaglist.add((NBTBase) nbttagcompound);
     }
 
+    // CraftBukkit start
+    @Deprecated
+    public void setItem(Item item) {
+        this.item = item;
+    }
+    // CraftBukkit end
+
     public IChatBaseComponent A() {
         IChatBaseComponent ichatbasecomponent = (new ChatComponentText("")).addSibling(this.getName());