From 93e55ff73fb4e0d24b691ddc1b0c64d767ac94b6 Mon Sep 17 00:00:00 2001 From: James McKay Date: Tue, 8 Dec 2015 20:20:31 +1100 Subject: [PATCH] Bugfixes of various kinds. Stopping crashes, exploits, etc. Closes #1 --- .../bbw/BetterBuildersWandsMod.java | 3 +- .../portablejim/bbw/core/BlockEvents.java | 27 +++++++------ .../java/portablejim/bbw/core/WandWorker.java | 24 +++++++++--- .../bbw/core/items/ItemBasicWand.java | 39 +++++++++++-------- .../bbw/core/wands/RestrictedWand.java | 3 ++ .../portablejim/bbw/proxy/ClientProxy.java | 5 +++ .../bbw/shims/BasicPlayerShim.java | 2 + 7 files changed, 67 insertions(+), 36 deletions(-) diff --git a/src/main/java/portablejim/bbw/BetterBuildersWandsMod.java b/src/main/java/portablejim/bbw/BetterBuildersWandsMod.java index 3c0dae1..377af1b 100644 --- a/src/main/java/portablejim/bbw/BetterBuildersWandsMod.java +++ b/src/main/java/portablejim/bbw/BetterBuildersWandsMod.java @@ -33,7 +33,7 @@ import portablejim.bbw.proxy.IProxy; @Mod(modid = BetterBuildersWandsMod.MODID, version = BetterBuildersWandsMod.VERSION) public class BetterBuildersWandsMod { public static final String MODID = "betterbuilderswands"; - public static final String VERSION = "0.1"; + public static final String VERSION = "0.1.1"; public static final String LANGID = "bbw"; @Mod.Instance @@ -77,7 +77,6 @@ public class BetterBuildersWandsMod { @EventHandler public void init(FMLInitializationEvent event) { - MinecraftForge.EVENT_BUS.register(new BlockEvents()); proxy.RegisterEvents(); } diff --git a/src/main/java/portablejim/bbw/core/BlockEvents.java b/src/main/java/portablejim/bbw/core/BlockEvents.java index 24dde3d..2897ddf 100644 --- a/src/main/java/portablejim/bbw/core/BlockEvents.java +++ b/src/main/java/portablejim/bbw/core/BlockEvents.java @@ -2,6 +2,7 @@ package portablejim.bbw.core; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import net.minecraft.client.renderer.RenderGlobal; +import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.MovingObjectPosition; @@ -41,20 +42,22 @@ public class BlockEvents { Point3d clickedPos = new Point3d(event.target.blockX, event.target.blockY, event.target.blockZ); ItemStack targetItemstack = worker.getEquivalentItemStack(clickedPos); - int numBlocks = Math.min(wand.getMaxBlocks(event.currentItem), playerShim.countItems(targetItemstack)); + if (targetItemstack != null && targetItemstack.isItemEqual(new ItemStack(worldShim.getBlock(clickedPos), 1, worldShim.getMetadata(clickedPos)))) { + int numBlocks = Math.min(wand.getMaxBlocks(event.currentItem), playerShim.countItems(targetItemstack)); - LinkedList blocks = worker.getBlockPositionList(clickedPos, ForgeDirection.getOrientation(event.target.sideHit), numBlocks, wandItem.getMode(event.currentItem), wandItem.getFaceLock(event.currentItem)); - if (blocks.size() > 0) { - GL11.glDisable(GL11.GL_TEXTURE_2D); - GL11.glEnable(GL11.GL_BLEND); - GL11.glDepthMask(true); - GL11.glLineWidth(2.5F); - for (Point3d block : blocks) { - AxisAlignedBB boundingBox = AxisAlignedBB.getBoundingBox(block.x, block.y, block.z, block.x + 1, block.y + 1, block.z + 1).contract(0.005, 0.005, 0.005); - RenderGlobal.drawOutlinedBoundingBox(boundingBox.getOffsetBoundingBox(-event.player.posX, -event.player.posY, -event.player.posZ), 0xC0C0C0); + LinkedList blocks = worker.getBlockPositionList(clickedPos, ForgeDirection.getOrientation(event.target.sideHit), numBlocks, wandItem.getMode(event.currentItem), wandItem.getFaceLock(event.currentItem)); + if (blocks.size() > 0) { + GL11.glDisable(GL11.GL_TEXTURE_2D); + GL11.glEnable(GL11.GL_BLEND); + GL11.glDepthMask(true); + GL11.glLineWidth(2.5F); + for (Point3d block : blocks) { + AxisAlignedBB boundingBox = AxisAlignedBB.getBoundingBox(block.x, block.y, block.z, block.x + 1, block.y + 1, block.z + 1).contract(0.005, 0.005, 0.005); + RenderGlobal.drawOutlinedBoundingBox(boundingBox.getOffsetBoundingBox(-event.player.posX, -event.player.posY, -event.player.posZ), 0xC0C0C0); + } + GL11.glEnable(GL11.GL_TEXTURE_2D); + GL11.glDisable(GL11.GL_BLEND); } - GL11.glEnable(GL11.GL_TEXTURE_2D); - GL11.glDisable(GL11.GL_BLEND); } } } diff --git a/src/main/java/portablejim/bbw/core/WandWorker.java b/src/main/java/portablejim/bbw/core/WandWorker.java index e4049d1..174b9b4 100644 --- a/src/main/java/portablejim/bbw/core/WandWorker.java +++ b/src/main/java/portablejim/bbw/core/WandWorker.java @@ -2,6 +2,8 @@ package portablejim.bbw.core; import cpw.mods.fml.common.FMLLog; import net.minecraft.block.Block; +import net.minecraft.block.BlockCrops; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.util.AxisAlignedBB; import net.minecraftforge.common.util.ForgeDirection; @@ -34,7 +36,22 @@ public class WandWorker { public ItemStack getEquivalentItemStack(Point3d blockPos) { Block block = world.getBlock(blockPos); - return block == null ? null : new ItemStack(block.getItem(world.getWorld(), blockPos.x, blockPos.y, blockPos.z), 1, block.getDamageValue(world.getWorld(), blockPos.x, blockPos.y, blockPos.z)); + return block == null ? null : new ItemStack(Item.getItemFromBlock(block), 1, block.getDamageValue(world.getWorld(), blockPos.x, blockPos.y, blockPos.z)); + } + + private boolean shouldContinue(Point3d currentCandidate, Block targetBlock, int targetMetadata, Block candidateSupportingBlock, int candidateSupportingMeta, AxisAlignedBB blockBB) { + if(!world.blockIsAir(currentCandidate)) return false; + /*if((FluidRegistry.getFluid("water").getBlock().equals(world.getBlock(currentCandidate)) || FluidRegistry.getFluid("lava").getBlock().equals(world.getBlock(currentCandidate))) + && world.getMetadata(currentCandidate) == 0){ + return false; + }*/ + if(!targetBlock.equals(candidateSupportingBlock)) return false; + if(targetMetadata != candidateSupportingMeta) return false; + //if(targetBlock instanceof BlockCrops) return false; + if(!targetBlock.canBlockStay(world.getWorld(), currentCandidate.x, currentCandidate.y, currentCandidate.z)) return false; + + return !world.entitiesInBox(blockBB); + } public LinkedList getBlockPositionList(Point3d blockLookedAt, ForgeDirection placeDirection, int maxBlocks, EnumLock directionLock, EnumLock faceLock) { @@ -60,10 +77,7 @@ public class WandWorker { Block candidateSupportingBlock = world.getBlock(supportingPoint); int candidateSupportingMeta = world.getMetadata(supportingPoint); AxisAlignedBB blockBB =targetBlock.getCollisionBoundingBoxFromPool(world.getWorld(), currentCandidate.x, currentCandidate.y, currentCandidate.z); - if((world.blockIsAir(currentCandidate) || (FluidRegistry.getFluid("water").getBlock().equals(world.getBlock(currentCandidate)) || FluidRegistry.getFluid("water").getBlock().equals(world.getBlock(currentCandidate))) && world.getMetadata(currentCandidate) != 0) - && targetBlock.equals(candidateSupportingBlock) - && targetMetadata == candidateSupportingMeta - && !world.entitiesInBox(blockBB) + if(shouldContinue(currentCandidate, targetBlock, targetMetadata, candidateSupportingBlock, candidateSupportingMeta, blockBB) && allCandidates.add(currentCandidate)) { toPlace.add(currentCandidate); diff --git a/src/main/java/portablejim/bbw/core/items/ItemBasicWand.java b/src/main/java/portablejim/bbw/core/items/ItemBasicWand.java index 23f3ac3..ebacbd7 100644 --- a/src/main/java/portablejim/bbw/core/items/ItemBasicWand.java +++ b/src/main/java/portablejim/bbw/core/items/ItemBasicWand.java @@ -6,6 +6,7 @@ import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; +import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.StatCollector; @@ -43,24 +44,28 @@ public abstract class ItemBasicWand extends Item implements IWandItem{ return false; } - IPlayerShim playerShim = new BasicPlayerShim(player); - if(player.capabilities.isCreativeMode) { - playerShim = new CreativePlayerShim(player); + if(!world.isRemote) { + IPlayerShim playerShim = new BasicPlayerShim(player); + if (player.capabilities.isCreativeMode) { + playerShim = new CreativePlayerShim(player); + } + IWorldShim worldShim = new BasicWorldShim(world); + + WandWorker worker = new WandWorker(this.wand, playerShim, worldShim); + + Point3d clickedPos = new Point3d(x, y, z); + + ItemStack targetItemstack = worker.getEquivalentItemStack(clickedPos); + int numBlocks = Math.min(this.wand.getMaxBlocks(itemstack), playerShim.countItems(targetItemstack)); + FMLLog.info("Max blocks: %d (%d|%d", numBlocks, this.wand.getMaxBlocks(itemstack), playerShim.countItems(targetItemstack)); + + if (targetItemstack != null && targetItemstack.isItemEqual(new ItemStack(worldShim.getBlock(clickedPos), 1, worldShim.getMetadata(clickedPos)))) { + LinkedList blocks = worker.getBlockPositionList(clickedPos, ForgeDirection.getOrientation(side), numBlocks, getMode(itemstack), getFaceLock(itemstack)); + + worker.placeBlocks(itemstack, blocks, clickedPos); + } + } - IWorldShim worldShim = new BasicWorldShim(world); - - WandWorker worker = new WandWorker(this.wand, playerShim, worldShim); - - Point3d clickedPos = new Point3d(x, y, z); - - ItemStack targetItemstack = worker.getEquivalentItemStack(clickedPos); - int numBlocks = Math.min(this.wand.getMaxBlocks(itemstack), playerShim.countItems(targetItemstack)); - FMLLog.info("Max blocks: %d (%d|%d", numBlocks, this.wand.getMaxBlocks(itemstack), playerShim.countItems(targetItemstack)); - - LinkedList blocks = worker.getBlockPositionList(clickedPos, ForgeDirection.getOrientation(side), numBlocks, getMode(itemstack), getFaceLock(itemstack)); - - worker.placeBlocks(itemstack, blocks, clickedPos); - return true; } diff --git a/src/main/java/portablejim/bbw/core/wands/RestrictedWand.java b/src/main/java/portablejim/bbw/core/wands/RestrictedWand.java index 6547844..661c2f0 100644 --- a/src/main/java/portablejim/bbw/core/wands/RestrictedWand.java +++ b/src/main/java/portablejim/bbw/core/wands/RestrictedWand.java @@ -20,6 +20,9 @@ public class RestrictedWand implements IWand { @Override public boolean placeBlock(ItemStack itemStack, EntityLivingBase entityLivingBase) { itemStack.damageItem(1, entityLivingBase); + if(itemStack.stackSize > 0 && itemStack.getItemDamage() == itemStack.getMaxDamage()) { + itemStack.stackSize = 0; + } return true; } } diff --git a/src/main/java/portablejim/bbw/proxy/ClientProxy.java b/src/main/java/portablejim/bbw/proxy/ClientProxy.java index e868cc0..a9fc039 100644 --- a/src/main/java/portablejim/bbw/proxy/ClientProxy.java +++ b/src/main/java/portablejim/bbw/proxy/ClientProxy.java @@ -1,5 +1,7 @@ package portablejim.bbw.proxy; +import net.minecraftforge.common.MinecraftForge; +import portablejim.bbw.core.BlockEvents; import portablejim.bbw.core.client.KeyEvents; /** @@ -7,10 +9,13 @@ import portablejim.bbw.core.client.KeyEvents; */ public class ClientProxy extends CommonProxy { private KeyEvents keyevents; + private BlockEvents blockEvents; @Override public void RegisterEvents() { keyevents = new KeyEvents(); + blockEvents = new BlockEvents(); + MinecraftForge.EVENT_BUS.register(blockEvents); } } diff --git a/src/main/java/portablejim/bbw/shims/BasicPlayerShim.java b/src/main/java/portablejim/bbw/shims/BasicPlayerShim.java index 43915b3..5f6a5bd 100644 --- a/src/main/java/portablejim/bbw/shims/BasicPlayerShim.java +++ b/src/main/java/portablejim/bbw/shims/BasicPlayerShim.java @@ -44,9 +44,11 @@ public class BasicPlayerShim implements IPlayerShim { ItemStack inventoryStack = player.inventory.mainInventory[i]; if(inventoryStack != null && itemStack.isItemEqual(inventoryStack)) { inventoryStack.stackSize -= 1; + player.inventory.setInventorySlotContents(i, inventoryStack); if(inventoryStack.stackSize == 0) { player.inventory.setInventorySlotContents(i, null); } + player.inventoryContainer.detectAndSendChanges(); return true; } }