2015-05-25 12:37:24 +02:00
--- a/net/minecraft/server/ItemStack.java
+++ b/net/minecraft/server/ItemStack.java
2016-05-10 13:47:39 +02:00
@@ -6,6 +6,19 @@
2014-11-25 22:32:16 +01:00
import java.util.Random;
2016-05-10 13:47:39 +02:00
import javax.annotation.Nullable;
2014-11-25 22:32:16 +01:00
+// CraftBukkit start
+import java.util.List;
2015-03-16 10:48:01 +01:00
+import java.util.Map;
2014-11-25 22:32:16 +01:00
+
+import org.bukkit.Location;
+import org.bukkit.TreeType;
+import org.bukkit.block.BlockState;
+import org.bukkit.craftbukkit.block.CraftBlockState;
+import org.bukkit.craftbukkit.util.CraftMagicNumbers;
+import org.bukkit.entity.Player;
+import org.bukkit.event.world.StructureGrowEvent;
+// CraftBukkit end
+
public final class ItemStack {
2016-11-17 02:41:03 +01:00
public static final ItemStack a = new ItemStack((Item) null);
2017-01-02 06:13:25 +01:00
@@ -42,32 +55,68 @@
this(item, i, 0);
}
+ // CraftBukkit start
public ItemStack(Item item, int i, int j) {
+ this(item, i, j, true);
+ }
+
+ public ItemStack(Item item, int i, int j, boolean convert) {
+ // CraftBukkit end
2014-11-25 22:32:16 +01:00
this.item = item;
2016-11-17 02:41:03 +01:00
this.damage = j;
2014-11-25 22:32:16 +01:00
this.count = i;
+ // CraftBukkit start - Pass to setData to do filtering
2016-03-25 00:20:27 +01:00
+ if (MinecraftServer.getServer() != null) {
2016-11-17 02:41:03 +01:00
+ this.setData(j);
+ }
2017-01-02 06:13:25 +01:00
+ if (convert) {
+ this.convertStack();
+ }
2016-11-17 02:41:03 +01:00
+ // CraftBukkit end
if (this.damage < 0) {
2016-11-18 05:12:11 +01:00
- this.damage = 0;
+ // this.damage = 0; // CraftBukkit - remove this.
2016-03-25 00:20:27 +01:00
}
2016-11-18 05:12:11 +01:00
this.F();
2016-11-17 02:41:03 +01:00
}
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
+ public void convertStack() {
+ if (MinecraftServer.getServer() != null) {
+ NBTTagCompound savedStack = new NBTTagCompound();
+ this.save(savedStack);
+ MinecraftServer.getServer().getDataConverterManager().a(DataConverterTypes.ITEM_INSTANCE, savedStack); // PAIL: convert
+ this.load(savedStack);
+ }
+ }
+
2016-11-17 02:41:03 +01:00
private void F() {
+ if (this.g && this == ItemStack.a) throw new AssertionError("TRAP"); // CraftBukkit
this.g = this.isEmpty();
}
- public ItemStack(NBTTagCompound nbttagcompound) {
+ // CraftBukkit - break into own method
+ public void load(NBTTagCompound nbttagcompound) {
this.item = Item.b(nbttagcompound.getString("id"));
this.count = nbttagcompound.getByte("Count");
- this.damage = Math.max(0, nbttagcompound.getShort("Damage"));
+ // CraftBukkit start - Route through setData for filtering
+ // this.damage = Math.max(0, nbttagcompound.getShort("Damage"));
2016-11-18 05:12:11 +01:00
+ this.setData(nbttagcompound.getShort("Damage"));
2014-11-25 22:32:16 +01:00
+ // CraftBukkit end
2016-11-17 02:41:03 +01:00
+
if (nbttagcompound.hasKeyOfType("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) nbttagcompound.getCompound("tag").clone();
if (this.item != null) {
- this.item.a(nbttagcompound);
+ this.item.a(this.tag);
+ // CraftBukkit end
}
}
+ }
2015-02-26 23:41:06 +01:00
2016-11-17 02:41:03 +01:00
+ public ItemStack(NBTTagCompound nbttagcompound) {
+ this.load(nbttagcompound);
+ // CraftBukkit end
this.F();
2014-11-25 22:32:16 +01:00
}
2017-01-02 06:13:25 +01:00
@@ -94,11 +143,138 @@
2014-11-25 22:32:16 +01:00
}
2016-02-29 22:32:46 +01:00
public EnumInteractionResult placeItem(EntityHuman entityhuman, World world, BlockPosition blockposition, EnumHand enumhand, EnumDirection enumdirection, float f, float f1, float f2) {
2014-11-25 22:32:16 +01:00
+ // CraftBukkit start - handle all block place event logic here
2016-11-17 02:41:03 +01:00
+ int oldData = this.getData();
+ int oldCount = this.getCount();
2014-11-25 22:32:16 +01:00
+
+ if (!(this.getItem() instanceof ItemBucket)) { // if not bucket
+ world.captureBlockStates = true;
+ // special case bonemeal
+ if (this.getItem() instanceof ItemDye && this.getData() == 15) {
+ Block block = world.getType(blockposition).getBlock();
+ if (block == Blocks.SAPLING || block instanceof BlockMushroom) {
+ world.captureTreeGeneration = true;
+ }
+ }
+ }
2016-11-17 02:41:03 +01:00
EnumInteractionResult enuminteractionresult = this.getItem().a(entityhuman, world, blockposition, enumhand, enumdirection, f, f1, f2);
2014-11-25 22:32:16 +01:00
+ int newData = this.getData();
2016-11-17 02:41:03 +01:00
+ int newCount = this.getCount();
+ this.setCount(oldCount);
+ this.setData(oldData);
2014-11-25 22:32:16 +01:00
+ world.captureBlockStates = false;
2016-02-29 22:32:46 +01:00
+ if (enuminteractionresult == EnumInteractionResult.SUCCESS && world.captureTreeGeneration && world.capturedBlockStates.size() > 0) {
2014-11-25 22:32:16 +01: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 = (List<BlockState>) world.capturedBlockStates.clone();
+ world.capturedBlockStates.clear();
+ StructureGrowEvent event = null;
+ if (treeType != null) {
2016-11-17 02:41:03 +01:00
+ boolean isBonemeal = getItem() == Items.DYE && oldData == 15;
2015-07-08 23:50:32 +02:00
+ event = new StructureGrowEvent(location, treeType, isBonemeal, (Player) entityhuman.getBukkitEntity(), blocks);
2014-11-25 22:32:16 +01:00
+ org.bukkit.Bukkit.getPluginManager().callEvent(event);
+ }
+ if (event == null || !event.isCancelled()) {
+ // Change the stack to its new contents if it hasn't been tampered with.
2016-11-17 02:41:03 +01:00
+ if (this.getCount() == oldCount && this.getData() == oldData) {
2014-11-25 22:32:16 +01:00
+ this.setData(newData);
2016-11-17 02:41:03 +01:00
+ this.setCount(newCount);
2014-11-25 22:32:16 +01:00
+ }
+ for (BlockState blockstate : blocks) {
+ blockstate.update(true);
+ }
+ }
+
2016-02-29 22:32:46 +01:00
+ return enuminteractionresult;
2014-11-25 22:32:16 +01:00
+ }
+ world.captureTreeGeneration = false;
2016-02-29 22:32:46 +01:00
if (enuminteractionresult == EnumInteractionResult.SUCCESS) {
- entityhuman.b(StatisticList.b(this.item));
2014-11-25 22:32:16 +01:00
+ org.bukkit.event.block.BlockPlaceEvent placeEvent = null;
+ List<BlockState> blocks = (List<BlockState>) world.capturedBlockStates.clone();
+ world.capturedBlockStates.clear();
+ if (blocks.size() > 1) {
2016-03-01 22:37:08 +01:00
+ placeEvent = org.bukkit.craftbukkit.event.CraftEventFactory.callBlockMultiPlaceEvent(world, entityhuman, enumhand, blocks, blockposition.getX(), blockposition.getY(), blockposition.getZ());
2014-11-25 22:32:16 +01:00
+ } else if (blocks.size() == 1) {
2016-03-01 22:37:08 +01:00
+ placeEvent = org.bukkit.craftbukkit.event.CraftEventFactory.callBlockPlaceEvent(world, entityhuman, enumhand, blocks.get(0), blockposition.getX(), blockposition.getY(), blockposition.getZ());
2014-11-25 22:32:16 +01:00
+ }
+
+ if (placeEvent != null && (placeEvent.isCancelled() || !placeEvent.canBuild())) {
2016-02-29 22:32:46 +01:00
+ enuminteractionresult = EnumInteractionResult.FAIL; // cancel placement
2016-03-06 02:34:51 +01:00
+ // PAIL: Remove this when MC-99075 fixed
+ placeEvent.getPlayer().updateInventory();
2014-11-25 22:32:16 +01:00
+ // revert back all captured blocks
+ for (BlockState blockstate : blocks) {
+ blockstate.update(true, false);
+ }
+ } else {
+ // Change the stack to its new contents if it hasn't been tampered with.
2016-11-17 02:41:03 +01:00
+ if (this.getCount() == oldCount && this.getData() == oldData) {
2014-11-25 22:32:16 +01:00
+ this.setData(newData);
2016-11-17 02:41:03 +01:00
+ this.setCount(newCount);
2014-11-25 22:32:16 +01:00
+ }
2016-11-23 23:30:01 +01:00
+
+ for (Map.Entry<BlockPosition, TileEntity> e : world.capturedTileEntities.entrySet()) {
+ world.setTileEntity(e.getKey(), e.getValue());
+ }
+
2014-11-25 22:32:16 +01:00
+ for (BlockState blockstate : blocks) {
+ int x = blockstate.getX();
+ int y = blockstate.getY();
+ int z = blockstate.getZ();
+ int updateFlag = ((CraftBlockState) blockstate).getFlag();
+ org.bukkit.Material mat = blockstate.getType();
+ Block oldBlock = CraftMagicNumbers.getBlock(mat);
+ BlockPosition newblockposition = new BlockPosition(x, y, z);
+ IBlockData block = world.getType(newblockposition);
+
2016-11-28 00:13:49 +01:00
+ if (!(block.getBlock() instanceof BlockTileEntity)) { // Containers get placed automatically
2014-11-25 22:32:16 +01:00
+ block.getBlock().onPlace(world, newblockposition, block);
+ }
+
2016-02-29 22:32:46 +01:00
+ world.notifyAndUpdatePhysics(newblockposition, null, oldBlock.getBlockData(), block, updateFlag); // send null chunk as chunk.k() returns false by this point
2014-11-25 22:32:16 +01:00
+ }
2015-02-02 22:55:33 +01:00
+
2015-04-10 13:24:34 +02:00
+ // Special case juke boxes as they update their tile entity. Copied from ItemRecord.
2016-03-01 10:07:44 +01:00
+ // PAIL: checkme on updates.
2016-11-17 23:20:14 +01:00
+ if (this.item instanceof ItemRecord) {
2015-04-10 13:24:34 +02:00
+ ((BlockJukeBox) Blocks.JUKEBOX).a(world, blockposition, world.getType(blockposition), this);
2016-03-01 10:07:44 +01:00
+ world.a((EntityHuman) null, 1010, blockposition, Item.getId(this.item));
2016-11-17 02:41:03 +01:00
+ this.subtract(1);
2016-03-01 10:07:44 +01:00
+ entityhuman.b(StatisticList.Z);
2015-04-10 13:24:34 +02:00
+ }
+
2016-11-17 23:20:14 +01:00
+ if (this.item == Items.SKULL) { // Special case skulls to allow wither spawns to be cancelled
2015-02-02 22:55:33 +01:00
+ BlockPosition bp = blockposition;
2015-02-26 23:41:06 +01:00
+ if (!world.getType(blockposition).getBlock().a(world, blockposition)) {
2016-02-29 22:32:46 +01:00
+ if (!world.getType(blockposition).getMaterial().isBuildable()) {
2015-02-02 22:55:33 +01:00
+ bp = null;
+ } else {
+ bp = bp.shift(enumdirection);
+ }
+ }
+ if (bp != null) {
+ TileEntity te = world.getTileEntity(bp);
+ if (te instanceof TileEntitySkull) {
+ Blocks.SKULL.a(world, bp, (TileEntitySkull) te);
+ }
+ }
+ }
+
2016-06-30 07:39:58 +02:00
+ // SPIGOT-1288 - play sound stripped from ItemBlock
2016-11-17 23:20:14 +01:00
+ if (this.item instanceof ItemBlock) {
+ SoundEffectType soundeffecttype = ((ItemBlock) this.item).getBlock().getStepSound();
2016-06-30 07:39:58 +02:00
+ world.a(entityhuman, blockposition, soundeffecttype.e(), SoundCategory.BLOCKS, (soundeffecttype.a() + 1.0F) / 2.0F, soundeffecttype.b() * 0.8F);
+ }
+
2016-02-29 22:32:46 +01:00
+ entityhuman.b(StatisticList.b(this.item));
2014-11-25 22:32:16 +01:00
+ }
}
2015-03-16 10:48:01 +01:00
+ world.capturedTileEntities.clear();
2014-11-25 22:32:16 +01:00
+ world.capturedBlockStates.clear();
+ // CraftBukkit end
2016-02-29 22:32:46 +01:00
return enuminteractionresult;
2014-11-25 22:32:16 +01:00
}
2017-01-02 06:13:25 +01:00
@@ -122,7 +298,7 @@
2014-11-25 22:32:16 +01:00
nbttagcompound.setByte("Count", (byte) this.count);
nbttagcompound.setShort("Damage", (short) this.damage);
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;
2017-01-02 06:13:25 +01:00
@@ -157,11 +333,30 @@
2014-11-25 22:32:16 +01:00
}
public void setData(int i) {
+ // CraftBukkit start - Filter out data for items that shouldn't have it
+ // The crafting system uses this value for a special purpose so we have to allow it
+ if (i == 32767) {
+ this.damage = i;
+ return;
+ }
+
+ // Is this a block?
+ if (CraftMagicNumbers.getBlock(CraftMagicNumbers.getId(this.getItem())) != Blocks.AIR) {
+ // If vanilla doesn't use data on it don't allow any
+ if (!(this.usesData() || this.getItem().usesDurability())) {
+ i = 0;
+ }
+ }
+
+ // Filter invalid plant data
+ if (CraftMagicNumbers.getBlock(CraftMagicNumbers.getId(this.getItem())) == Blocks.DOUBLE_PLANT && (i > 5 || i < 0)) {
+ i = 0;
+ }
+ // CraftBukkit end
this.damage = i;
2016-02-29 22:32:46 +01:00
if (this.damage < 0) {
2016-11-18 05:12:11 +01:00
- this.damage = 0;
+ // this.damage = 0; // CraftBukkit - remove this.
2016-03-01 10:16:34 +01:00
}
2016-03-25 00:20:27 +01:00
-
2016-03-01 10:16:34 +01:00
}
2016-03-25 00:20:27 +01:00
2016-11-17 02:41:03 +01:00
public int k() {
2017-01-02 06:13:25 +01:00
@@ -198,6 +393,11 @@
2016-12-17 23:26:36 +01:00
if (this.f()) {
if (this.isDamaged(i, entityliving.getRandom())) {
entityliving.b(this);
2014-11-25 22:32:16 +01:00
+ // CraftBukkit start - Check for item breaking
2016-12-17 23:26:36 +01:00
+ if (this.count == 1 && entityliving instanceof EntityHuman) {
2014-11-25 22:32:16 +01:00
+ org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemBreakEvent((EntityHuman) entityliving, this);
+ }
+ // CraftBukkit end
2016-12-17 23:26:36 +01:00
this.subtract(1);
if (entityliving instanceof EntityHuman) {
EntityHuman entityhuman = (EntityHuman) entityliving;
2017-01-02 06:13:25 +01:00
@@ -239,7 +439,7 @@
}
public ItemStack cloneItemStack() {
- ItemStack itemstack = new ItemStack(this.item, this.count, this.damage);
+ ItemStack itemstack = new ItemStack(this.item, this.count, this.damage, false); // CraftBukkit
if (this.tag != null) {
itemstack.tag = this.tag.g();
@@ -509,6 +709,12 @@
2016-11-17 02:41:03 +01:00
nbttaglist.add(nbttagcompound);
2014-11-25 22:32:16 +01:00
}
2016-11-17 02:41:03 +01:00
+ @Deprecated
+ public void setItem(Item item) {
+ this.item = item;
+ this.setData(this.getData()); // CraftBukkit - Set data again to ensure it is filtered properly
+ }
+
public IChatBaseComponent C() {
ChatComponentText chatcomponenttext = new ChatComponentText(this.getName());