From ba69c43653e6c39edb7727c4163e9f176259b169 Mon Sep 17 00:00:00 2001 From: Managarmr Date: Mon, 30 Apr 2018 01:39:32 +0100 Subject: [PATCH 1/3] Check if mimicked block fits cable --- .../xnet/blocks/cables/ConnectorBlock.java | 14 ++++++++ .../xnet/blocks/cables/NetCableBlock.java | 10 ++++++ .../xnet/blocks/facade/FacadeItemBlock.java | 32 +++++++++++++++++-- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/main/java/mcjty/xnet/blocks/cables/ConnectorBlock.java b/src/main/java/mcjty/xnet/blocks/cables/ConnectorBlock.java index 8144b7c0..96f0914a 100644 --- a/src/main/java/mcjty/xnet/blocks/cables/ConnectorBlock.java +++ b/src/main/java/mcjty/xnet/blocks/cables/ConnectorBlock.java @@ -448,4 +448,18 @@ public static boolean isAdvancedConnector(World world, BlockPos pos) { public boolean isAdvancedConnector() { return false; } + + @Override + public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) { + AxisAlignedBB boundingBox = AABB_CENTER; + for (EnumFacing facing : EnumFacing.VALUES) { + ConnectorType connectorType = getConnectorType(state.getValue(COLOR), source, pos, facing); + if (connectorType == ConnectorType.CABLE) { + boundingBox = boundingBox.union(AABBS[facing.getIndex()]); + } else if (connectorType == ConnectorType.BLOCK) { + boundingBox = boundingBox.union(AABBS_CONNECTOR[facing.getIndex()]); + } + } + return boundingBox; + } } diff --git a/src/main/java/mcjty/xnet/blocks/cables/NetCableBlock.java b/src/main/java/mcjty/xnet/blocks/cables/NetCableBlock.java index 36de8446..3daa4651 100644 --- a/src/main/java/mcjty/xnet/blocks/cables/NetCableBlock.java +++ b/src/main/java/mcjty/xnet/blocks/cables/NetCableBlock.java @@ -80,4 +80,14 @@ protected ConnectorType getConnectorType(@Nonnull CableColor color, IBlockAccess } } + @Override + public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) { + AxisAlignedBB boundingBox = AABB_CENTER; + for (EnumFacing facing : EnumFacing.VALUES) { + if (getConnectorType(state.getValue(COLOR), source, pos, facing) != ConnectorType.NONE) { + boundingBox = boundingBox.union(AABBS[facing.getIndex()]); + } + } + return boundingBox; + } } diff --git a/src/main/java/mcjty/xnet/blocks/facade/FacadeItemBlock.java b/src/main/java/mcjty/xnet/blocks/facade/FacadeItemBlock.java index e4534170..1efd303c 100644 --- a/src/main/java/mcjty/xnet/blocks/facade/FacadeItemBlock.java +++ b/src/main/java/mcjty/xnet/blocks/facade/FacadeItemBlock.java @@ -15,6 +15,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.*; +import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextFormatting; @@ -66,8 +67,15 @@ public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos ItemStack itemstack = player.getHeldItem(hand); if (!itemstack.isEmpty()) { - if (block == NetCableSetup.netCableBlock) { + IBlockState mimicBlock = getMimicBlock(itemstack); + if (!canMimicFit(state.getBoundingBox(world, pos), mimicBlock.getBoundingBox(world, pos))) { + if (world.isRemote) { + player.sendStatusMessage(new TextComponentString(mimicBlock.getBlock().getLocalizedName() + " is too small to fit"), false); + } + return EnumActionResult.FAIL; + } + int i = this.getMetadata(itemstack.getMetadata()); FacadeBlock facadeBlock = (FacadeBlock) this.block; IBlockState placementState = facadeBlock.getPlacementState(world, pos, facing, hitX, hitY, hitZ, i, player).withProperty(GenericCableBlock.COLOR, state.getValue(GenericCableBlock.COLOR)); @@ -77,17 +85,24 @@ public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos world.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F); TileEntity te = world.getTileEntity(pos); if (te instanceof FacadeTileEntity) { - ((FacadeTileEntity) te).setMimicBlock(getMimicBlock(itemstack)); + ((FacadeTileEntity) te).setMimicBlock(mimicBlock); } int amount = -1; itemstack.grow(amount); } } else if (block == NetCableSetup.connectorBlock || block == NetCableSetup.advancedConnectorBlock) { + IBlockState mimicBlock = getMimicBlock(itemstack); + if (!canMimicFit(state.getBoundingBox(world, pos), mimicBlock.getBoundingBox(world, pos))) { + if (world.isRemote) { + player.sendStatusMessage(new TextComponentString(mimicBlock.getBlock().getLocalizedName() + " is too small to fit"), false); + } + return EnumActionResult.FAIL; + } TileEntity te = world.getTileEntity(pos); if (te instanceof ConnectorTileEntity) { ConnectorTileEntity connectorTileEntity = (ConnectorTileEntity) te; if (connectorTileEntity.getMimicBlock() == null) { - connectorTileEntity.setMimicBlock(getMimicBlock(itemstack)); + connectorTileEntity.setMimicBlock(mimicBlock); SoundType soundtype = world.getBlockState(pos).getBlock().getSoundType(world.getBlockState(pos), world, pos, player); world.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F); int amount = -1; @@ -99,6 +114,13 @@ public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos } else if (block == ModBlocks.facadeBlock) { return EnumActionResult.FAIL; } else { + if (!canMimicFit(GenericCableBlock.AABB_CENTER, state.getBoundingBox(world, pos))) { + if (world.isRemote) { + player.sendStatusMessage(new TextComponentString(block.getLocalizedName() + " is too small to mimic"), false); + } + return EnumActionResult.FAIL; + } + setMimicBlock(itemstack, state); if (world.isRemote) { player.sendStatusMessage(new TextComponentString("Facade is now mimicing " + block.getLocalizedName()), false); @@ -129,4 +151,8 @@ public void addInformation(ItemStack stack, @Nullable World playerIn, List= cableBB.maxX && mimicBB.minY <= cableBB.maxY && mimicBB.maxY >= cableBB.maxY && mimicBB.minZ <= cableBB.minZ && mimicBB.maxZ >= cableBB.maxZ; + } } From b91d4b2b5004cc66e60be6e9bab1f39ca0ad34b6 Mon Sep 17 00:00:00 2001 From: Managarmr Date: Mon, 30 Apr 2018 01:43:07 +0100 Subject: [PATCH 2/3] Removed duplicate code --- .../xnet/blocks/facade/FacadeItemBlock.java | 53 +++++++++---------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/src/main/java/mcjty/xnet/blocks/facade/FacadeItemBlock.java b/src/main/java/mcjty/xnet/blocks/facade/FacadeItemBlock.java index 1efd303c..69839ad2 100644 --- a/src/main/java/mcjty/xnet/blocks/facade/FacadeItemBlock.java +++ b/src/main/java/mcjty/xnet/blocks/facade/FacadeItemBlock.java @@ -67,7 +67,7 @@ public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos ItemStack itemstack = player.getHeldItem(hand); if (!itemstack.isEmpty()) { - if (block == NetCableSetup.netCableBlock) { + if (block == NetCableSetup.netCableBlock || block == NetCableSetup.connectorBlock || block == NetCableSetup.advancedConnectorBlock) { IBlockState mimicBlock = getMimicBlock(itemstack); if (!canMimicFit(state.getBoundingBox(world, pos), mimicBlock.getBoundingBox(world, pos))) { if (world.isRemote) { @@ -76,39 +76,34 @@ public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos return EnumActionResult.FAIL; } - int i = this.getMetadata(itemstack.getMetadata()); - FacadeBlock facadeBlock = (FacadeBlock) this.block; - IBlockState placementState = facadeBlock.getPlacementState(world, pos, facing, hitX, hitY, hitZ, i, player).withProperty(GenericCableBlock.COLOR, state.getValue(GenericCableBlock.COLOR)); + if (block == NetCableSetup.netCableBlock) { + int i = this.getMetadata(itemstack.getMetadata()); + FacadeBlock facadeBlock = (FacadeBlock) this.block; + IBlockState placementState = facadeBlock.getPlacementState(world, pos, facing, hitX, hitY, hitZ, i, player).withProperty(GenericCableBlock.COLOR, state.getValue(GenericCableBlock.COLOR)); - if (placeBlockAt(itemstack, player, world, pos, facing, hitX, hitY, hitZ, placementState)) { - SoundType soundtype = world.getBlockState(pos).getBlock().getSoundType(world.getBlockState(pos), world, pos, player); - world.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F); - TileEntity te = world.getTileEntity(pos); - if (te instanceof FacadeTileEntity) { - ((FacadeTileEntity) te).setMimicBlock(mimicBlock); - } - int amount = -1; - itemstack.grow(amount); - } - } else if (block == NetCableSetup.connectorBlock || block == NetCableSetup.advancedConnectorBlock) { - IBlockState mimicBlock = getMimicBlock(itemstack); - if (!canMimicFit(state.getBoundingBox(world, pos), mimicBlock.getBoundingBox(world, pos))) { - if (world.isRemote) { - player.sendStatusMessage(new TextComponentString(mimicBlock.getBlock().getLocalizedName() + " is too small to fit"), false); - } - return EnumActionResult.FAIL; - } - TileEntity te = world.getTileEntity(pos); - if (te instanceof ConnectorTileEntity) { - ConnectorTileEntity connectorTileEntity = (ConnectorTileEntity) te; - if (connectorTileEntity.getMimicBlock() == null) { - connectorTileEntity.setMimicBlock(mimicBlock); + if (placeBlockAt(itemstack, player, world, pos, facing, hitX, hitY, hitZ, placementState)) { SoundType soundtype = world.getBlockState(pos).getBlock().getSoundType(world.getBlockState(pos), world, pos, player); world.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F); + TileEntity te = world.getTileEntity(pos); + if (te instanceof FacadeTileEntity) { + ((FacadeTileEntity) te).setMimicBlock(mimicBlock); + } int amount = -1; itemstack.grow(amount); - } else { - return EnumActionResult.FAIL; + } + } else { + TileEntity te = world.getTileEntity(pos); + if (te instanceof ConnectorTileEntity) { + ConnectorTileEntity connectorTileEntity = (ConnectorTileEntity) te; + if (connectorTileEntity.getMimicBlock() == null) { + connectorTileEntity.setMimicBlock(mimicBlock); + SoundType soundtype = world.getBlockState(pos).getBlock().getSoundType(world.getBlockState(pos), world, pos, player); + world.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F); + int amount = -1; + itemstack.grow(amount); + } else { + return EnumActionResult.FAIL; + } } } } else if (block == ModBlocks.facadeBlock) { From 4c4464eb55dedd891ebad77a56a87319755dcdf6 Mon Sep 17 00:00:00 2001 From: Managarmr Date: Wed, 2 May 2018 11:52:53 +0100 Subject: [PATCH 3/3] Add missing import --- src/main/java/mcjty/xnet/blocks/cables/NetCableBlock.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/mcjty/xnet/blocks/cables/NetCableBlock.java b/src/main/java/mcjty/xnet/blocks/cables/NetCableBlock.java index 3daa4651..244fa3bf 100644 --- a/src/main/java/mcjty/xnet/blocks/cables/NetCableBlock.java +++ b/src/main/java/mcjty/xnet/blocks/cables/NetCableBlock.java @@ -13,6 +13,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.util.EnumFacing; import net.minecraft.util.NonNullList; +import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World;