From acdcfd0a5628e09cbe7ecfa3916d20f005e94af0 Mon Sep 17 00:00:00 2001 From: LexManos Date: Fri, 10 Aug 2012 02:09:11 -0700 Subject: [PATCH] Added new pick block hook for blocks and entites to allow for better grained control of the result. --- .../net/minecraftforge/common/ForgeHooks.java | 63 +++++++++++- .../common/net/minecraft/src/Block.java.patch | 26 ++++- .../net/minecraft/src/Entity.java.patch | 34 ++++++- .../net/minecraft/client/Minecraft.java.patch | 95 +++++++++++++++++++ 4 files changed, 215 insertions(+), 3 deletions(-) create mode 100644 patches/minecraft/net/minecraft/client/Minecraft.java.patch diff --git a/common/net/minecraftforge/common/ForgeHooks.java b/common/net/minecraftforge/common/ForgeHooks.java index c2a745bd9..baffa8074 100644 --- a/common/net/minecraftforge/common/ForgeHooks.java +++ b/common/net/minecraftforge/common/ForgeHooks.java @@ -221,7 +221,68 @@ public class ForgeHooks public static boolean onEntityInteract(EntityPlayer entityPlayer, Entity par1Entity, boolean b) { - return false; } + + /** + * Called when a player uses 'pick block', calls new Entity and Block hooks. + */ + public static boolean onPickBlock(MovingObjectPosition target, EntityPlayer player, World world) + { + ItemStack result = null; + boolean isCreative = player.capabilities.isCreativeMode; + + if (target.typeOfHit == EnumMovingObjectType.TILE) + { + int x = target.blockX; + int y = target.blockY; + int z = target.blockZ; + Block var8 = Block.blocksList[world.getBlockId(x, y, z)]; + + if (var8 == null) + { + return false; + } + + result = var8.getPickBlock(target, world, x, y, z); + } + else + { + if (target.typeOfHit != EnumMovingObjectType.ENTITY || target.entityHit == null || !isCreative) + { + return false; + } + + result = target.entityHit.getPickedResult(target); + } + + if (result == null) + { + return false; + } + + for (int x = 0; x < 9; x++) + { + ItemStack stack = player.inventory.getStackInSlot(x); + if (stack != null && stack.isItemEqual(result)) + { + player.inventory.currentItem = x; + return true; + } + } + + if (!isCreative) + { + return false; + } + + int slot = player.inventory.getFirstEmptyStack(); + if (slot < 0 || slot >= 9) + { + return false; + } + + player.inventory.setInventorySlotContents(slot, result); + return true; + } } diff --git a/patches/common/net/minecraft/src/Block.java.patch b/patches/common/net/minecraft/src/Block.java.patch index 1e6c6a9df..cf0da2eef 100644 --- a/patches/common/net/minecraft/src/Block.java.patch +++ b/patches/common/net/minecraft/src/Block.java.patch @@ -104,7 +104,7 @@ { ItemStack var8 = this.createStackedBlock(par6); -@@ -1249,4 +1257,645 @@ +@@ -1249,4 +1257,669 @@ canBlockGrass[0] = true; StatList.initBreakableStats(); } @@ -748,5 +748,29 @@ + public boolean canRenderInPass(int pass) + { + return pass == getRenderBlockPass(); ++ } ++ ++ /** ++ * Called when a user uses the creative pick block button on this block ++ * ++ * @param target The full target the player is looking at ++ * @return A ItemStack to add to the player's inventory, Null if nothing should be added. ++ */ ++ public ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z) ++ { ++ int id = idPicked(world, x, y, z); ++ ++ if (id == 0) ++ { ++ return null; ++ } ++ ++ Item item = Item.itemsList[id]; ++ if (item == null) ++ { ++ return null; ++ } ++ ++ return new ItemStack(id, 1, getDamageValue(world, x, y, z)); + } } diff --git a/patches/common/net/minecraft/src/Entity.java.patch b/patches/common/net/minecraft/src/Entity.java.patch index 866f34460..f89a728e9 100644 --- a/patches/common/net/minecraft/src/Entity.java.patch +++ b/patches/common/net/minecraft/src/Entity.java.patch @@ -67,11 +67,12 @@ } /** -@@ -2107,4 +2128,27 @@ +@@ -2107,4 +2128,59 @@ { return String.format("%s[\'%s\'/%d, l=\'%s\', x=%.2f, y=%.2f, z=%.2f]", new Object[] {this.getClass().getSimpleName(), this.getEntityName(), Integer.valueOf(this.entityId), this.worldObj == null ? "~NULL~" : this.worldObj.getWorldInfo().getWorldName(), Double.valueOf(this.posX), Double.valueOf(this.posY), Double.valueOf(this.posZ)}); } + ++ /* ================================== Forge Start =====================================*/ + /** + * Returns a NBTTagCompound that can be used to store custom data for this entity. + * It will be written, and read from disc, so it persists over world saves. @@ -93,5 +94,36 @@ + public boolean shouldRiderSit() + { + return true; ++ } ++ ++ /** ++ * Called when a user uses the creative pick block button on this entity. ++ * ++ * @param target The full target the player is looking at ++ * @return A ItemStack to add to the player's inventory, Null if nothing should be added. ++ */ ++ public ItemStack getPickedResult(MovingObjectPosition target) ++ { ++ if (this instanceof EntityPainting) ++ { ++ return new ItemStack(Item.painting); ++ } ++ else if (this instanceof EntityMinecart) ++ { ++ return ((EntityMinecart)this).getCartItem(); ++ } ++ else if (this instanceof EntityBoat) ++ { ++ return new ItemStack(Item.boat); ++ } ++ else ++ { ++ int id = EntityList.getEntityID(this); ++ if (id > 0 || EntityList.entityEggs.containsKey(id)) ++ { ++ return new ItemStack(Item.monsterPlacer, 1, id); ++ } ++ } ++ return null; + } } diff --git a/patches/minecraft/net/minecraft/client/Minecraft.java.patch b/patches/minecraft/net/minecraft/client/Minecraft.java.patch new file mode 100644 index 000000000..ac57841b4 --- /dev/null +++ b/patches/minecraft/net/minecraft/client/Minecraft.java.patch @@ -0,0 +1,95 @@ +--- ../src_base/minecraft/net/minecraft/client/Minecraft.java ++++ ../src_work/minecraft/net/minecraft/client/Minecraft.java +@@ -17,6 +17,7 @@ + import javax.swing.JPanel; + + import net.minecraft.src.*; ++import net.minecraftforge.common.ForgeHooks; + + import org.lwjgl.LWJGLException; + import org.lwjgl.Sys; +@@ -2102,80 +2103,12 @@ + if (this.objectMouseOver != null) + { + boolean var1 = this.thePlayer.capabilities.isCreativeMode; +- int var3 = 0; +- boolean var4 = false; +- int var2; + int var5; + +- if (this.objectMouseOver.typeOfHit == EnumMovingObjectType.TILE) +- { +- var5 = this.objectMouseOver.blockX; +- int var6 = this.objectMouseOver.blockY; +- int var7 = this.objectMouseOver.blockZ; +- Block var8 = Block.blocksList[this.theWorld.getBlockId(var5, var6, var7)]; +- +- if (var8 == null) +- { +- return; +- } +- +- var2 = var8.idPicked(this.theWorld, var5, var6, var7); +- +- if (var2 == 0) +- { +- return; +- } +- +- var4 = Item.itemsList[var2].getHasSubtypes(); +- int var9 = var2 >= 256 ? var8.blockID : var2; +- var3 = Block.blocksList[var9].getDamageValue(this.theWorld, var5, var6, var7); +- } +- else +- { +- if (this.objectMouseOver.typeOfHit != EnumMovingObjectType.ENTITY || this.objectMouseOver.entityHit == null || !var1) +- { +- return; +- } +- +- if (this.objectMouseOver.entityHit instanceof EntityPainting) +- { +- var2 = Item.painting.shiftedIndex; +- } +- else if (this.objectMouseOver.entityHit instanceof EntityMinecart) +- { +- EntityMinecart var10 = (EntityMinecart)this.objectMouseOver.entityHit; +- +- if (var10.minecartType == 2) +- { +- var2 = Item.minecartPowered.shiftedIndex; +- } +- else if (var10.minecartType == 1) +- { +- var2 = Item.minecartCrate.shiftedIndex; +- } +- else +- { +- var2 = Item.minecartEmpty.shiftedIndex; +- } +- } +- else if (this.objectMouseOver.entityHit instanceof EntityBoat) +- { +- var2 = Item.boat.shiftedIndex; +- } +- else +- { +- var2 = Item.monsterPlacer.shiftedIndex; +- var3 = EntityList.getEntityID(this.objectMouseOver.entityHit); +- var4 = true; +- +- if (var3 <= 0 || !EntityList.entityEggs.containsKey(Integer.valueOf(var3))) +- { +- return; +- } +- } +- } +- +- this.thePlayer.inventory.setCurrentItem(var2, var3, var4, var1); ++ if (!ForgeHooks.onPickBlock(this.objectMouseOver, this.thePlayer, this.theWorld)) ++ { ++ return; ++ } + + if (var1) + {