Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if mimicked block fits cable #261

Open
wants to merge 3 commits into
base: 1.12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/mcjty/xnet/blocks/cables/ConnectorBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
11 changes: 11 additions & 0 deletions src/main/java/mcjty/xnet/blocks/cables/NetCableBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,4 +81,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;
}
}
65 changes: 43 additions & 22 deletions src/main/java/mcjty/xnet/blocks/facade/FacadeItemBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,39 +67,55 @@ public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos
ItemStack itemstack = player.getHeldItem(hand);

if (!itemstack.isEmpty()) {

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(getMimicBlock(itemstack));
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) {
player.sendStatusMessage(new TextComponentString(mimicBlock.getBlock().getLocalizedName() + " is too small to fit"), false);
}
int amount = -1;
itemstack.grow(amount);
return EnumActionResult.FAIL;
}
} else if (block == NetCableSetup.connectorBlock || block == NetCableSetup.advancedConnectorBlock) {
TileEntity te = world.getTileEntity(pos);
if (te instanceof ConnectorTileEntity) {
ConnectorTileEntity connectorTileEntity = (ConnectorTileEntity) te;
if (connectorTileEntity.getMimicBlock() == null) {
connectorTileEntity.setMimicBlock(getMimicBlock(itemstack));

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 {
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) {
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);
Expand Down Expand Up @@ -129,4 +146,8 @@ public void addInformation(ItemStack stack, @Nullable World playerIn, List<Strin
}
}
}

public boolean canMimicFit(AxisAlignedBB cableBB, AxisAlignedBB mimicBB) {
return mimicBB.minX <= cableBB.minX && mimicBB.maxX >= cableBB.maxX && mimicBB.minY <= cableBB.maxY && mimicBB.maxY >= cableBB.maxY && mimicBB.minZ <= cableBB.minZ && mimicBB.maxZ >= cableBB.maxZ;
}
}