Skip to content

Commit

Permalink
add GT6-style pipes/cables with config
Browse files Browse the repository at this point in the history
  • Loading branch information
DStrand1 committed Jun 27, 2021
1 parent 9e3803b commit 53d040a
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 4 deletions.
27 changes: 27 additions & 0 deletions src/main/java/gregtech/api/block/machines/BlockMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import gregtech.api.cover.IFacadeCover;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.MetaTileEntityHolder;
import gregtech.api.pipenet.tile.AttachmentType;
import gregtech.api.pipenet.tile.IPipeTile;
import gregtech.api.render.MetaTileEntityRenderer;
import gregtech.api.util.GTUtility;
import gregtech.common.ConfigHolder;
import gregtech.common.tools.DamageValues;
import gregtech.api.render.IBlockAppearance;
Expand Down Expand Up @@ -235,6 +238,30 @@ public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, Enti
if (metaTileEntity.isValidFrontFacing(placeFacing)) {
metaTileEntity.setFrontFacing(placeFacing);
}
if (ConfigHolder.U.GT6.gt6StylePipesCables) {
if (placer instanceof EntityPlayer) {
EntityPlayer player = (EntityPlayer) placer;
RayTraceResult rt1 = GTUtility.getBlockLookingAt(player);
RayTraceResult rt2 = GTUtility.getBlockLookingAt(player, pos);
for (EnumFacing facing : EnumFacing.VALUES) {
BlockPos pipePos = null;

if (rt1 != null)
if (GTUtility.arePosEqual(rt1.getBlockPos(), pos.offset(facing, 1)))
pipePos = rt1.getBlockPos();
if (rt2 != null)
if (GTUtility.arePosEqual(rt2.getBlockPos(), pos.offset(facing, 1)))
pipePos = rt2.getBlockPos();
if (pipePos != null) {
TileEntity tileEntity = placer.world.getTileEntity(pipePos);
if (tileEntity instanceof IPipeTile) {
IPipeTile<?, ?> pipeTile = (IPipeTile<?, ?>) tileEntity;
pipeTile.setConnectionBlocked(AttachmentType.PIPE, facing.getOpposite(), false, true);
}
}
}
}
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions src/main/java/gregtech/api/pipenet/block/BlockPipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
import gregtech.api.cover.ICoverable.CoverSideData;
import gregtech.api.cover.ICoverable.PrimaryBoxData;
import gregtech.api.cover.IFacadeCover;
import gregtech.api.metatileentity.MetaTileEntityHolder;
import gregtech.api.pipenet.PipeNet;
import gregtech.api.pipenet.WorldPipeNet;
import gregtech.api.pipenet.tile.AttachmentType;
import gregtech.api.pipenet.tile.IPipeTile;
import gregtech.api.pipenet.tile.TileEntityPipeBase;
import gregtech.api.util.GTUtility;
import gregtech.common.ConfigHolder;
import gregtech.common.tools.DamageValues;
import gregtech.api.render.IBlockAppearance;
import gregtech.integration.ctm.IFacadeWrapper;
Expand Down Expand Up @@ -120,6 +123,34 @@ public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, Enti
IPipeTile<PipeType, NodeDataType> pipeTile = getPipeTileEntity(worldIn, pos);
if (pipeTile != null) {
setTileEntityData((TileEntityPipeBase<PipeType, NodeDataType>) pipeTile, stack);
if (ConfigHolder.U.GT6.gt6StylePipesCables) {
if (placer instanceof EntityPlayer) {
EntityPlayer player = (EntityPlayer) placer;
RayTraceResult rt1 = GTUtility.getBlockLookingAt(player);
RayTraceResult rt2 = GTUtility.getBlockLookingAt(player, pos);
for (EnumFacing facing : EnumFacing.VALUES) {
BlockPos otherPipePos = null;

if (rt1 != null)
if (GTUtility.arePosEqual(rt1.getBlockPos(), pos.offset(facing, 1)))
otherPipePos = rt1.getBlockPos();
if (rt2 != null)
if (GTUtility.arePosEqual(rt2.getBlockPos(), pos.offset(facing, 1)))
otherPipePos = rt2.getBlockPos();
if (otherPipePos != null) {
TileEntity tileEntity = placer.world.getTileEntity(otherPipePos);
if (tileEntity instanceof IPipeTile) {
IPipeTile<?, ?> otherPipeTE = (IPipeTile<?, ?>) tileEntity;
if (otherPipeTE.getPipeBlock().getPipeTypeClass() == this.getPipeTypeClass()) {
pipeTile.setConnectionBlocked(AttachmentType.PIPE, facing, false, false);
}
} else if (tileEntity instanceof MetaTileEntityHolder) {
pipeTile.setConnectionBlocked(AttachmentType.PIPE, facing, false, true);
}
}
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import gregtech.api.pipenet.WorldPipeNet;
import gregtech.api.pipenet.block.BlockPipe;
import gregtech.api.pipenet.block.IPipeType;
import gregtech.common.ConfigHolder;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagCompound;
Expand Down Expand Up @@ -37,6 +38,10 @@ public abstract class TileEntityPipeBase<PipeType extends Enum<PipeType> & IPipe
private boolean wasInDetachedConversionMode;

public TileEntityPipeBase() {
if (ConfigHolder.U.GT6.gt6StylePipesCables) {
blockedConnectionsMap.put(AttachmentType.PIPE.ordinal(), 0b111111);
recomputeBlockedConnections();
}
}

public void setDetachedConversionMode(boolean detachedConversionMode) {
Expand Down Expand Up @@ -151,7 +156,7 @@ public boolean isConnectionBlocked(AttachmentType type, EnumFacing side) {
@Override
public void setConnectionBlocked(AttachmentType attachmentType, EnumFacing side, boolean blocked, boolean fromNeighbor) {
// fix desync between two connections. Can happen if a pipe side is blocked, and a new pipe is placed next to it.
if (isConnectionBlocked(attachmentType, side) != isNeighborPipeBlocked(attachmentType, side) && !fromNeighbor) {
if (attachmentType == AttachmentType.PIPE && isConnectionBlocked(attachmentType, side) != isNeighborPipeBlocked(attachmentType, side) && !fromNeighbor) {
syncPipeConnections(attachmentType, side);
return;
}
Expand All @@ -165,7 +170,7 @@ public void setConnectionBlocked(AttachmentType attachmentType, EnumFacing side,
writeCustomData(-2, buffer -> buffer.writeVarInt(this.blockedConnections));
markDirty();
}
if (!fromNeighbor) {
if (attachmentType == AttachmentType.PIPE && !fromNeighbor) {
setNeighborPipeBlocked(attachmentType, side, blocked);
}
}
Expand Down
136 changes: 134 additions & 2 deletions src/main/java/gregtech/api/util/GTUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import gregtech.common.ConfigHolder;
import net.minecraft.block.Block;
import net.minecraft.block.BlockRedstoneWire;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
Expand All @@ -33,8 +34,7 @@
import net.minecraft.potion.PotionEffect;
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.math.*;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.BiomeDictionary;
Expand Down Expand Up @@ -716,4 +716,136 @@ public static Comparator<ItemStack> createItemStackComparator() {
public static int getRandomIntXSTR(int bound) {
return random.nextInt(bound);
}

public static RayTraceResult getBlockLookingAt(EntityPlayer player) {
Vec3d pos2 = player.getPositionVector().add(0, player.getEyeHeight(), 0);
RayTraceResult result = player.world.rayTraceBlocks(pos2, pos2.add(player.getLookVec().scale(12)), false, true, true);
if (result != null && result.typeOfHit == RayTraceResult.Type.BLOCK)
return result;
return null;
}

public static RayTraceResult getBlockLookingAt(EntityPlayer player, BlockPos exclude) {
Vec3d pos2 = player.getPositionVector().add(0, player.getEyeHeight(), 0);
RayTraceResult result = rayTraceBlocks(pos2, pos2.add(player.getLookVec().scale(12)), false, true, true, player.world, exclude);
if (result != null && result.typeOfHit == RayTraceResult.Type.BLOCK)
return result;
return null;
}

public static RayTraceResult rayTraceBlocks(Vec3d vec31, Vec3d vec32, boolean stopOnLiquid, boolean ignoreBlockWithoutBoundingBox, boolean returnLastUncollidableBlock, World world, BlockPos ignore) {
if (!Double.isNaN(vec31.x) && !Double.isNaN(vec31.y) && !Double.isNaN(vec31.z)) {
if (!Double.isNaN(vec32.x) && !Double.isNaN(vec32.y) && !Double.isNaN(vec32.z)) {
int i = MathHelper.floor(vec32.x);
int j = MathHelper.floor(vec32.y);
int k = MathHelper.floor(vec32.z);
int l = MathHelper.floor(vec31.x);
int i1 = MathHelper.floor(vec31.y);
int j1 = MathHelper.floor(vec31.z);
BlockPos blockpos = new BlockPos(l, i1, j1);
IBlockState iblockstate = world.getBlockState(blockpos);
Block block = iblockstate.getBlock();

if ((!ignoreBlockWithoutBoundingBox || iblockstate.getCollisionBoundingBox(world, blockpos) != Block.NULL_AABB) && block.canCollideCheck(iblockstate, stopOnLiquid) && !arePosEqual(ignore, blockpos))
return iblockstate.collisionRayTrace(world, blockpos, vec31, vec32);

RayTraceResult raytraceresult2 = null;
int k1 = 200;

while (k1-- >= 0) {
if (Double.isNaN(vec31.x) || Double.isNaN(vec31.y) || Double.isNaN(vec31.z))
return null;

if (l == i && i1 == j && j1 == k)
return returnLastUncollidableBlock ? raytraceresult2 : null;

boolean flag2 = true;
boolean flag = true;
boolean flag1 = true;
double d0 = 999.0D;
double d1 = 999.0D;
double d2 = 999.0D;

if (i > l)
d0 = (double)l + 1.0D;
else if (i < l)
d0 = (double)l + 0.0D;
else
flag2 = false;

if (j > i1)
d1 = (double)i1 + 1.0D;
else if (j < i1)
d1 = (double)i1 + 0.0D;
else
flag = false;

if (k > j1)
d2 = (double)j1 + 1.0D;
else if (k < j1)
d2 = (double)j1 + 0.0D;
else
flag1 = false;

double d3 = 999.0D;
double d4 = 999.0D;
double d5 = 999.0D;
double d6 = vec32.x - vec31.x;
double d7 = vec32.y - vec31.y;
double d8 = vec32.z - vec31.z;

if (flag2)
d3 = (d0 - vec31.x) / d6;

if (flag)
d4 = (d1 - vec31.y) / d7;

if (flag1)
d5 = (d2 - vec31.z) / d8;

if (d3 == -0.0D)
d3 = -1.0E-4D;

if (d4 == -0.0D)
d4 = -1.0E-4D;

if (d5 == -0.0D)
d5 = -1.0E-4D;

EnumFacing enumfacing;

if (d3 < d4 && d3 < d5) {
enumfacing = i > l ? EnumFacing.WEST : EnumFacing.EAST;
vec31 = new Vec3d(d0, vec31.y + d7 * d3, vec31.z + d8 * d3);
} else if (d4 < d5) {
enumfacing = j > i1 ? EnumFacing.DOWN : EnumFacing.UP;
vec31 = new Vec3d(vec31.x + d6 * d4, d1, vec31.z + d8 * d4);
} else {
enumfacing = k > j1 ? EnumFacing.NORTH : EnumFacing.SOUTH;
vec31 = new Vec3d(vec31.x + d6 * d5, vec31.y + d7 * d5, d2);
}

l = MathHelper.floor(vec31.x) - (enumfacing == EnumFacing.EAST ? 1 : 0);
i1 = MathHelper.floor(vec31.y) - (enumfacing == EnumFacing.UP ? 1 : 0);
j1 = MathHelper.floor(vec31.z) - (enumfacing == EnumFacing.SOUTH ? 1 : 0);
blockpos = new BlockPos(l, i1, j1);
IBlockState iblockstate1 = world.getBlockState(blockpos);
Block block1 = iblockstate1.getBlock();

if (!ignoreBlockWithoutBoundingBox || iblockstate1.getMaterial() == Material.PORTAL || iblockstate1.getCollisionBoundingBox(world, blockpos) != Block.NULL_AABB && !arePosEqual(blockpos, ignore)) {
if (block1.canCollideCheck(iblockstate1, stopOnLiquid)) {
return iblockstate1.collisionRayTrace(world, blockpos, vec31, vec32);
} else {
raytraceresult2 = new RayTraceResult(RayTraceResult.Type.MISS, vec31, enumfacing, blockpos);
}
}
}
return returnLastUncollidableBlock ? raytraceresult2 : null;
} else return null;
} else return null;
}

public static boolean arePosEqual(BlockPos pos1, BlockPos pos2) {
return pos1.getX() == pos2.getX() & pos1.getY() == pos2.getY() & pos1.getZ() == pos2.getZ();
}
}
11 changes: 11 additions & 0 deletions src/main/java/gregtech/common/ConfigHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ public static class UnofficialOptions {
@Config.Name("GregTech 5 Unofficial Options")
public GT5U GT5u = new GT5U();

@Config.Comment("Config category for GT6 inspired features.")
@Config.Name("GregTech 6 Options")
public GT6 GT6 = new GT6();

@Config.Comment("Should Drums be enabled? Default: true")
@Config.RequiresMcRestart
public boolean registerDrums = true;
Expand Down Expand Up @@ -205,6 +209,13 @@ public static class GT5U {
public boolean requireWrenchForMachines = false;
}

public static class GT6 {

@Config.Comment("Whether or not to use GT6-style pipe and cable connections, meaning they will not auto-connect " +
"unless placed directly onto another pipe or cable. Default: false")
public boolean gt6StylePipesCables = false;
}

public static class HighTierMachines {

@Config.Comment("Enable all LuV-UV Machines, overrides individual values if true. Default: false")
Expand Down

1 comment on commit 53d040a

@serenibyss
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GTUtility.rayTraceBlocks could use some cleanup, but I adapted it from MC code and left it basically as-is except for adding an excluded BlockPos parameter

Please sign in to comment.