Paper/nms-patches/ItemStack.patch

237 lines
9.8 KiB
Diff
Raw Normal View History

2015-02-26 23:41:06 +01:00
--- /home/matt/mc-dev-private//net/minecraft/server/ItemStack.java 2015-02-26 22:40:22.847608137 +0000
+++ src/main/java/net/minecraft/server/ItemStack.java 2015-02-26 22:40:22.847608137 +0000
@@ -5,6 +5,18 @@
import java.text.DecimalFormat;
import java.util.Random;
+// CraftBukkit start
+import java.util.List;
+
+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 {
public static final DecimalFormat a = new DecimalFormat("#.###");
2015-02-26 23:41:06 +01:00
@@ -46,10 +58,14 @@
this.k = false;
this.item = item;
this.count = i;
- this.damage = j;
- if (this.damage < 0) {
- this.damage = 0;
- }
2015-02-26 23:41:06 +01:00
+
+ // CraftBukkit start - Pass to setData to do filtering
+ this.setData(j);
+ //this.damage = j;
+ //if (this.damage < 0) {
+ // this.damage = 0;
+ //}
+ // CraftBukkit end
2015-02-26 23:41:06 +01:00
}
2015-02-26 23:41:06 +01:00
@@ -83,11 +99,114 @@
}
public boolean placeItem(EntityHuman entityhuman, World world, BlockPosition blockposition, EnumDirection enumdirection, float f, float f1, float f2) {
+ // CraftBukkit start - handle all block place event logic here
+ int data = this.getData();
+ int count = this.count;
+
+ 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;
+ }
+ }
+ }
boolean flag = this.getItem().interactWith(this, entityhuman, world, blockposition, enumdirection, f, f1, f2);
+ int newData = this.getData();
+ int newCount = this.count;
+ this.count = count;
+ this.setData(data);
+ world.captureBlockStates = false;
+ if (flag && 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 event = null;
+ if (treeType != null) {
+ event = new StructureGrowEvent(location, treeType, false, (Player) entityhuman.getBukkitEntity(), blocks);
+ 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.
+ if (this.count == count && this.getData() == data) {
+ this.setData(newData);
+ this.count = newCount;
+ }
+ for (BlockState blockstate : blocks) {
+ blockstate.update(true);
+ }
+ }
+
+ return flag;
+ }
+ world.captureTreeGeneration = false;
if (flag) {
- entityhuman.b(StatisticList.USE_ITEM_COUNT[Item.getId(this.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, blocks, blockposition.getX(), blockposition.getY(), blockposition.getZ());
+ } else if (blocks.size() == 1) {
+ placeEvent = org.bukkit.craftbukkit.event.CraftEventFactory.callBlockPlaceEvent(world, entityhuman, blocks.get(0), blockposition.getX(), blockposition.getY(), blockposition.getZ());
+ }
+
+ if (placeEvent != null && (placeEvent.isCancelled() || !placeEvent.canBuild())) {
+ flag = false; // cancel placement
+ // 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.
+ if (this.count == count && this.getData() == data) {
+ this.setData(newData);
+ this.count = newCount;
+ }
+ 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);
+
+ if (!(block instanceof BlockContainer)) { // Containers get placed automatically
+ block.getBlock().onPlace(world, newblockposition, block);
+ }
+
+ world.notifyAndUpdatePhysics(newblockposition, null, oldBlock, block.getBlock(), updateFlag); // send null chunk as chunk.k() returns false by this point
+ }
+
+ if (this.getItem() == Items.SKULL) { // Special case skulls to allow wither spawns to be cancelled
+ BlockPosition bp = blockposition;
2015-02-26 23:41:06 +01:00
+ if (!world.getType(blockposition).getBlock().a(world, blockposition)) {
+ if (!world.getType(blockposition).getBlock().getMaterial().isBuildable()) {
+ 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);
+ }
+ }
+ }
+
+ entityhuman.b(StatisticList.USE_ITEM_COUNT[Item.getId(this.item)]);
+ }
}
+ world.capturedBlockStates.clear();
+ // CraftBukkit end
return flag;
}
2015-02-26 23:41:06 +01:00
@@ -111,7 +230,7 @@
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;
2015-02-26 23:41:06 +01:00
@@ -125,13 +244,18 @@
}
this.count = nbttagcompound.getByte("Count");
+ /* CraftBukkit start - Route through setData for filtering
this.damage = nbttagcompound.getShort("Damage");
if (this.damage < 0) {
this.damage = 0;
}
+ */
+ this.setData(nbttagcompound.getShort("Damage"));
+ // CraftBukkit end
if (nbttagcompound.hasKeyOfType("tag", 10)) {
- this.tag = nbttagcompound.getCompound("tag");
+ // CraftBukkit - 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(this.tag);
}
2015-02-26 23:41:06 +01:00
@@ -168,8 +292,28 @@
}
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;
- if (this.damage < 0) {
+ if (this.damage < -1) { // CraftBukkit
this.damage = 0;
}
2015-02-26 23:41:06 +01:00
@@ -223,6 +367,12 @@
this.count = 0;
}
2015-02-26 23:41:06 +01:00
+ // CraftBukkit start - Check for item breaking
+ if (this.count == 0 && entityliving instanceof EntityHuman) {
+ org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerItemBreakEvent((EntityHuman) entityliving, this);
+ }
+ // CraftBukkit end
2015-02-26 23:41:06 +01:00
+
this.damage = 0;
}
2015-02-26 23:41:06 +01:00
@@ -489,6 +639,7 @@
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() {