From 43eb12ef77fb1d2854fbe09883955ccff78fd14d Mon Sep 17 00:00:00 2001 From: tterrag1098 Date: Tue, 13 Jan 2015 20:17:20 -0500 Subject: [PATCH] Fix top slabs (Closes #127) and rewrite slab placement code --- .../com/cricketcraft/chisel/Features.java | 17 +-- .../chisel/block/BlockCarvableSlab.java | 1 + .../chisel/item/ItemCarvableSlab.java | 72 ++++++++++++ .../chisel/item/ItemMarbleSlab.java | 108 ------------------ 4 files changed, 83 insertions(+), 115 deletions(-) create mode 100644 src/main/java/com/cricketcraft/chisel/item/ItemCarvableSlab.java delete mode 100644 src/main/java/com/cricketcraft/chisel/item/ItemMarbleSlab.java diff --git a/src/main/java/com/cricketcraft/chisel/Features.java b/src/main/java/com/cricketcraft/chisel/Features.java index b916e08a..ff8df7c1 100644 --- a/src/main/java/com/cricketcraft/chisel/Features.java +++ b/src/main/java/com/cricketcraft/chisel/Features.java @@ -63,8 +63,8 @@ import com.cricketcraft.chisel.init.ChiselItems; import com.cricketcraft.chisel.init.ChiselTabs; import com.cricketcraft.chisel.item.ItemBallOMoss; import com.cricketcraft.chisel.item.ItemCarvable; +import com.cricketcraft.chisel.item.ItemCarvableSlab; import com.cricketcraft.chisel.item.ItemCloudInABottle; -import com.cricketcraft.chisel.item.ItemMarbleSlab; import com.cricketcraft.chisel.item.ItemSmashingRock; import com.cricketcraft.chisel.item.ItemUpgrade; import com.cricketcraft.chisel.item.chisel.ItemChisel; @@ -1279,7 +1279,7 @@ public enum Features { OreDictionary.registerOre("limestone", limestone); Carving.chisel.registerOre("limestone", "limestone"); - BlockCarvable limestone_slab = (BlockCarvableSlab) new BlockCarvableSlab(limestone).setCreativeTab(ChiselTabs.tabStoneChiselBlocks).setHardness(2.0F).setResistance(10F); + BlockCarvableSlab limestone_slab = (BlockCarvableSlab) new BlockCarvableSlab(limestone).setCreativeTab(ChiselTabs.tabStoneChiselBlocks).setHardness(2.0F).setResistance(10F); limestone_slab.carverHelper.setChiselBlockName("Limestone Slab"); limestone_slab.carverHelper.addVariation("tile.limestoneSlab.0.desc", 0, "limestone"); limestone_slab.carverHelper.addVariation("tile.limestoneSlab.1.desc", 1, "limestone/terrain-cobbsmalltilelight"); @@ -1297,7 +1297,8 @@ public enum Features { limestone_slab.carverHelper.addVariation("tile.limestoneSlab.13.desc", 13, "limestone/terrain-pistonback-lightfour"); limestone_slab.carverHelper.addVariation("tile.limestoneSlab.14.desc", 14, "limestone/terrain-pistonback-lightmarker"); limestone_slab.carverHelper.addVariation("tile.limestoneSlab.15.desc", 15, "limestone/terrain-pistonback-lightpanel"); - limestone_slab.carverHelper.register(limestone_slab, "limestone_slab", ItemMarbleSlab.class); + limestone_slab.carverHelper.register(limestone_slab, "limestone_slab", ItemCarvableSlab.class); + GameRegistry.registerBlock(limestone_slab.top, Block.blockRegistry.getNameForObject(limestone_slab) + "_top"); Carving.chisel.registerOre("limestone_slab", "limestone_slab"); BlockMarbleStairsMaker makerLimestoneStairs = new BlockMarbleStairsMaker(limestone); @@ -1356,7 +1357,7 @@ public enum Features { OreDictionary.registerOre("blockMarble", marble); Carving.chisel.registerOre("marble", "marble"); - BlockCarvable marble_slab = (BlockCarvableSlab) new BlockCarvableSlab(marble).setHardness(2.0F).setResistance(10F); + BlockCarvableSlab marble_slab = (BlockCarvableSlab) new BlockCarvableSlab(marble).setHardness(2.0F).setResistance(10F); marble_slab.carverHelper.setChiselBlockName("Marble Slab"); marble_slab.carverHelper.addVariation("tile.marbleSlab.0.desc", 0, "marble"); marble_slab.carverHelper.addVariation("tile.marbleSlab.1.desc", 1, "marbleslab/a1-stoneornamental-marblebrick"); @@ -1374,7 +1375,8 @@ public enum Features { marble_slab.carverHelper.addVariation("tile.marbleSlab.13.desc", 13, "marbleslab/marble-arranged-bricks"); marble_slab.carverHelper.addVariation("tile.marbleSlab.14.desc", 14, "marbleslab/marble-fancy-bricks"); marble_slab.carverHelper.addVariation("tile.marbleSlab.15.desc", 15, "marbleslab/marble-blocks"); - marble_slab.carverHelper.register(marble_slab, "marble_slab", ItemMarbleSlab.class); + marble_slab.carverHelper.register(marble_slab, "marble_slab", ItemCarvableSlab.class); + GameRegistry.registerBlock(marble_slab.top, Block.blockRegistry.getNameForObject(marble_slab) + "_top"); Carving.chisel.registerOre("marble_slab", "marble_slab"); BlockMarbleStairsMaker makerMarbleStairs = new BlockMarbleStairsMaker(marble); @@ -1455,7 +1457,7 @@ public enum Features { marble_pillar.carverHelper.register(marble_pillar, "marble_pillar"); Carving.chisel.setGroupClass("marble_pillar", "marble"); - BlockCarvable marble_pillar_slab = (BlockCarvableSlab) new BlockCarvableSlab(marble_pillar).setCreativeTab(ChiselTabs.tabStoneChiselBlocks).setHardness(2.0F).setResistance(10F) + BlockCarvableSlab marble_pillar_slab = (BlockCarvableSlab) new BlockCarvableSlab(marble_pillar).setCreativeTab(ChiselTabs.tabStoneChiselBlocks).setHardness(2.0F).setResistance(10F) .setStepSound(Block.soundTypeStone); marble_pillar_slab.carverHelper.setChiselBlockName("Marble Pillar Slab"); if (Configurations.oldPillars) { @@ -1493,7 +1495,8 @@ public enum Features { marble_pillar_slab.carverHelper.addVariation("tile.marblePillarSlab.14.desc", 14, "marblepillarslab/carved"); marble_pillar_slab.carverHelper.addVariation("tile.marblePillarSlab.15.desc", 15, "marblepillarslab/ornamental"); } - marble_pillar_slab.carverHelper.register(marble_pillar_slab, "marble_pillar_slab", ItemMarbleSlab.class); + marble_pillar_slab.carverHelper.register(marble_pillar_slab, "marble_pillar_slab", ItemCarvableSlab.class); + GameRegistry.registerBlock(marble_pillar_slab.top, Block.blockRegistry.getNameForObject(marble_pillar_slab) + "_top"); } @Override diff --git a/src/main/java/com/cricketcraft/chisel/block/BlockCarvableSlab.java b/src/main/java/com/cricketcraft/chisel/block/BlockCarvableSlab.java index 34dc90df..23d4afcc 100644 --- a/src/main/java/com/cricketcraft/chisel/block/BlockCarvableSlab.java +++ b/src/main/java/com/cricketcraft/chisel/block/BlockCarvableSlab.java @@ -13,6 +13,7 @@ import net.minecraft.util.IIcon; import net.minecraft.util.MovingObjectPosition; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; +import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; diff --git a/src/main/java/com/cricketcraft/chisel/item/ItemCarvableSlab.java b/src/main/java/com/cricketcraft/chisel/item/ItemCarvableSlab.java new file mode 100644 index 00000000..a7e02f45 --- /dev/null +++ b/src/main/java/com/cricketcraft/chisel/item/ItemCarvableSlab.java @@ -0,0 +1,72 @@ +package com.cricketcraft.chisel.item; + +import net.minecraft.block.Block; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; + +import com.cricketcraft.chisel.block.BlockCarvableSlab; + +public class ItemCarvableSlab extends ItemCarvable { + + public ItemCarvableSlab(Block block) { + super(block); + } + + @Override + public boolean func_150936_a(World world, int x, int y, int z, int side, EntityPlayer player, ItemStack stack) { + return true; + } + + @Override + public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hx, float hy, float hz) { + BlockCarvableSlab block = (BlockCarvableSlab) Block.getBlockFromItem(this); + int meta = stack.getItemDamage(); + + ForgeDirection dir = ForgeDirection.VALID_DIRECTIONS[side]; + Block clicked = world.getBlock(x, y, z); + + // the position of the block that we are attempting to place at + int x2 = x + dir.offsetX; + int y2 = y + dir.offsetY; + int z2 = z + dir.offsetZ; + + Block at = world.getBlock(x2, y2, z2); + + boolean metaEquals = world.getBlockMetadata(x2, y2, z2) == meta; + // if the metadata at the place target matches, and the block there matches either the top of bottom slab, try to fill in the rest of the block + if (metaEquals && ((at == block.top && (dir == ForgeDirection.DOWN || hy == 1.0D)) || (at == block.bottom && (dir == ForgeDirection.UP || hy == 0)))) { + place(stack, world, x2, y2, z2, block.master, meta); + return true; + } + + // else if the block clicked on is a top or bottom slab of this type, try to fill in that block if possible + boolean clickedMetaEquals = world.getBlockMetadata(x, y, z) == meta; + if (clickedMetaEquals && ((clicked == block.bottom && dir == ForgeDirection.UP) || (clicked == block.top && dir == ForgeDirection.DOWN))) { + place(stack, world, x, y, z, block.master, meta); + return true; + } + + // finally just try to place a normal slab + if (metaEquals || at.isReplaceable(world, x, y, z)) { + boolean top = hy > 0.5D && dir != ForgeDirection.UP || dir == ForgeDirection.DOWN; + Block toPlace = top ? block.top : block.bottom; + // if we can replace the clicked block do so + if (clicked.isReplaceable(world, x, y, z)) { + place(stack, world, x, y, z, toPlace, meta); + } else { + place(stack, world, x2, y2, z2, toPlace, meta); + } + return true; + } + return false; + } + + private void place(ItemStack stack, World world, int x, int y, int z, Block toPlace, int metadata) { + world.setBlock(x, y, z, toPlace, metadata, 2); + stack.stackSize -= 1; + world.playSoundEffect(x + 0.5f, y + 0.5f, z + 0.5f, this.field_150939_a.stepSound.func_150496_b(), (this.field_150939_a.stepSound.getVolume() + 1.0F) / 2.0F, + this.field_150939_a.stepSound.getPitch() * 0.8F); + } +} diff --git a/src/main/java/com/cricketcraft/chisel/item/ItemMarbleSlab.java b/src/main/java/com/cricketcraft/chisel/item/ItemMarbleSlab.java deleted file mode 100644 index 884e364c..00000000 --- a/src/main/java/com/cricketcraft/chisel/item/ItemMarbleSlab.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.cricketcraft.chisel.item; - -import net.minecraft.block.Block; -import net.minecraft.entity.Entity; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; -import net.minecraft.world.World; - -import com.cricketcraft.chisel.block.BlockCarvableSlab; - -public class ItemMarbleSlab extends ItemCarvable { - - public ItemMarbleSlab(Block block) { - super(block); - } - - @Override - public boolean func_150936_a(World world, int x, int y, int z, int side, EntityPlayer player, ItemStack stack) { - BlockCarvableSlab block = (BlockCarvableSlab) Block.getBlockFromItem(this); - - switch (side) { - case 0: - --y; - break; - case 1: - ++y; - break; - case 2: - --z; - break; - case 3: - ++z; - break; - case 4: - --x; - break; - case 5: - ++x; - break; - } - - Block targetBlock = world.getBlock(x, y, z); - int meta = world.getBlockMetadata(x, y, z); - - return (targetBlock.equals(block) || targetBlock.equals(block.top)) && meta == stack.getItemDamage() || world.canPlaceEntityOnSide(block, x, y, z, false, side, (Entity) null, stack); - - } - - @Override - public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hx, float hy, float hz) { - BlockCarvableSlab block = (BlockCarvableSlab) Block.getBlockFromItem(this); - - Block targetBlock = world.getBlock(x, y, z); - int meta = world.getBlockMetadata(x, y, z); - boolean metaMatches = meta == stack.getItemDamage(); - - if (metaMatches && side == 0 && targetBlock.equals(block.top)) { - world.setBlock(x, y, z, block.master, meta, 2); - stack.stackSize -= 1; - return true; - } else if (metaMatches && side == 1 && targetBlock.equals(block.bottom)) { - world.setBlock(x, y, z, block.master, meta, 2); - stack.stackSize -= 1; - return true; - } - - boolean result = super.onItemUse(stack, player, world, x, y, z, side, hz, hy, hz); - - switch (side) { - case 0: - --y; - break; - case 1: - ++y; - break; - case 2: - --z; - break; - case 3: - ++z; - break; - case 4: - --x; - break; - case 5: - ++x; - break; - } - - targetBlock = world.getBlock(x, y, z); - meta = world.getBlockMetadata(x, y, z); - - if (!result && (targetBlock.equals(block.top) || targetBlock.equals(block.bottom)) && meta == stack.getItemDamage()) { - world.setBlock(x, y, z, block.master, meta, 2); - return true; - } - - if (!result) - return false; - - if (side != 0 && (side == 1 || hy <= 0.5D)) - return true; - - // TODO allow top slabs - // world.setBlock(x, y, z, block.top, meta, 2); - return true; - } -}