Skip to content

Commit

Permalink
Misc improvements and fix GregTechCEu#2582
Browse files Browse the repository at this point in the history
  • Loading branch information
M-W-K committed Aug 15, 2024
1 parent 6f0fd56 commit 28a2752
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 24 deletions.
10 changes: 10 additions & 0 deletions src/main/java/gregtech/api/graphnet/AbstractGroupData.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package gregtech.api.graphnet;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class AbstractGroupData {

protected NetGroup group;

public void withGroup(NetGroup group) {
this.group = group;
}

public abstract boolean mergeAllowed(@NotNull AbstractGroupData other);

@Nullable
public AbstractGroupData merge(@NotNull AbstractGroupData other) {
return null;
}
}
7 changes: 4 additions & 3 deletions src/main/java/gregtech/api/graphnet/GraphNetBacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public void removeVertex(GraphVertex vertex) {
}

@Nullable
public NetEdge addEdge(NetNode source, NetNode target, double weight) {
public NetEdge addEdge(@NotNull NetNode source, @NotNull NetNode target, double weight) {
if (!NetGroup.isEdgeAllowed(source, target)) return null;
GraphEdge graphEdge = getGraph().addEdge(source.wrapper, target.wrapper);
if (graphEdge != null) {
getGraph().setEdgeWeight(graphEdge, weight);
Expand All @@ -107,12 +108,12 @@ public NetEdge addEdge(NetNode source, NetNode target, double weight) {
}

@Nullable
public NetEdge getEdge(NetNode source, NetNode target) {
public NetEdge getEdge(@NotNull NetNode source, @NotNull NetNode target) {
GraphEdge graphEdge = getGraph().getEdge(source.wrapper, target.wrapper);
return graphEdge == null ? null : graphEdge.wrapped;
}

public boolean removeEdge(NetNode source, NetNode target) {
public boolean removeEdge(@NotNull NetNode source, NetNode target) {
NetGroup group = source.getGroupUnsafe();
if (group == null) {
// weird since there should always be a group for two joined nodes, but whatever
Expand Down
30 changes: 24 additions & 6 deletions src/main/java/gregtech/api/graphnet/NetGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Iterator;
Expand All @@ -16,13 +17,13 @@ public class NetGroup {

private final Set<NetNode> nodes;

private final AbstractGroupData data;
private AbstractGroupData data;

public NetGroup(IGraphNet net) {
this(net, new ObjectOpenHashSet<>());
}

public NetGroup(IGraphNet net,
public NetGroup(@NotNull IGraphNet net,
Set<NetNode> nodes) {
this.net = net;
this.data = net.getBlankGroupData();
Expand Down Expand Up @@ -53,17 +54,29 @@ protected void clearNodes() {
this.nodes.clear();
}

protected void onAddedToGroup(NetNode node) {
protected void onAddedToGroup(@NotNull NetNode node) {
node.setGroup(this);
}

public static boolean isEdgeAllowed(@NotNull NetNode source, @NotNull NetNode target) {
NetGroup sourceGroup = source.getGroupUnsafe();
NetGroup targetGroup = target.getGroupUnsafe();

if (sourceGroup == null || targetGroup == null) return true;
AbstractGroupData sourceData = sourceGroup.getData();
AbstractGroupData targetData = targetGroup.getData();
if (sourceData == null || targetData == null) return true;

return sourceData.mergeAllowed(targetData) && targetData.mergeAllowed(sourceData);
}

/**
* Merges the groups of an edge if necessary. Does not actually perform the edge creation.
*
* @param source the source node of the edge
* @param target the target node of the edge
*/
public static void mergeEdge(NetNode source, NetNode target) {
public static void mergeEdge(@NotNull NetNode source, @NotNull NetNode target) {
NetGroup sourceGroup = source.getGroupUnsafe();
NetGroup targetGroup = target.getGroupUnsafe();
if (sourceGroup == targetGroup) {
Expand All @@ -81,11 +94,16 @@ public static void mergeEdge(NetNode source, NetNode target) {
}
}

protected void mergeNode(NetNode node) {
protected void mergeNode(@NotNull NetNode node) {
NetGroup group = node.getGroupUnsafe();
if (group != null) {
this.addNodes(group.getNodes());
group.clearNodes();
AbstractGroupData data = group.getData();
if (data != null) {
if (this.data == null) this.data = data;
else this.data.merge(data);
}
} else addNode(node);
this.clearPathCaches();
}
Expand Down Expand Up @@ -134,7 +152,7 @@ public void splitNode(NetNode source) {
* @param target target of the edge
* @return Whether the edge existed in the graph
*/
public boolean splitEdge(NetNode source, NetNode target) {
public boolean splitEdge(@NotNull NetNode source, @NotNull NetNode target) {
if (this.net.getBacker().removeEdge(source.wrapper, target.wrapper) != null) {
this.clearPathCaches();
Set<NetNode> targetGroup = new ObjectOpenHashSet<>();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/gregtech/api/graphnet/edge/NetEdge.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public void setData(@NotNull NetLogicData data) {
if (this.wrapper == null) this.data = data;
}

/**
* This data is transient and should not be written to.
*/
public @NotNull NetLogicData getData() {
if (this.data == null) {
this.data = NetLogicData.unionNullable(getSource() == null ? null : getSource().getData(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public T cast(NetLogicEntry<?, ?> entry) {

/**
* Controls whether this {@link NetLogicEntry} will be synced to the client or not.
*
* @return
*/
public boolean shouldEncode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,4 @@ public void encode(PacketBuffer buf, boolean fullChange) {}

@Override
public void decode(PacketBuffer buf, boolean fullChange) {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.BlockFaceShape;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
Expand Down Expand Up @@ -63,11 +64,34 @@

import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

public abstract class PipeBlock extends BuiltInRenderBlock {

public static final PropertyBool NORTH = PropertyBool.create("north");
public static final PropertyBool EAST = PropertyBool.create("east");
public static final PropertyBool SOUTH = PropertyBool.create("south");
public static final PropertyBool WEST = PropertyBool.create("west");
public static final PropertyBool UP = PropertyBool.create("up");
public static final PropertyBool DOWN = PropertyBool.create("down");

public static final EnumMap<EnumFacing, PropertyBool> FACINGS = buildFacings();

private static @NotNull EnumMap<EnumFacing, PropertyBool> buildFacings() {
EnumMap<EnumFacing, PropertyBool> map = new EnumMap<>(EnumFacing.class);
map.put(EnumFacing.NORTH, NORTH);
map.put(EnumFacing.EAST, EAST);
map.put(EnumFacing.SOUTH, SOUTH);
map.put(EnumFacing.WEST, WEST);
map.put(EnumFacing.UP, UP);
map.put(EnumFacing.DOWN, DOWN);
return map;
}

public static final PropertyBool FRAMED = PropertyBool.create("framed");

// do not touch these two unless you know what you are doing
protected final ThreadLocal<BlockPos> lastTilePos = ThreadLocal.withInitial(() -> new BlockPos(0, 0, 0));
protected final ThreadLocal<WeakReference<PipeTileEntity>> lastTile = ThreadLocal
Expand Down Expand Up @@ -583,24 +607,58 @@ public String getToolClass() {

// blockstate //

@Override
public int getMetaFromState(@NotNull IBlockState state) {
return 0;
}

@Override
protected @NotNull BlockStateContainer createBlockState() {
return constructState(new BlockStateContainer.Builder(this)).build();
return constructState(new BlockStateContainer.Builder(this))
.add(NORTH, SOUTH, EAST, WEST, UP, DOWN, FRAMED)
.build();
}

protected @NotNull BlockStateContainer.Builder constructState(BlockStateContainer.@NotNull Builder builder) {
return builder.add(AbstractPipeModel.THICKNESS_PROPERTY).add(AbstractPipeModel.CONNECTION_MASK_PROPERTY)
.add(AbstractPipeModel.CLOSED_MASK_PROPERTY).add(AbstractPipeModel.BLOCKED_MASK_PROPERTY)
.add(AbstractPipeModel.COLOR_PROPERTY).add(AbstractPipeModel.FRAME_MATERIAL_PROPERTY)
.add(AbstractPipeModel.FRAME_MASK_PROPERTY).add(CoverRendererPackage.PROPERTY);
return builder.add(AbstractPipeModel.THICKNESS_PROPERTY).add(AbstractPipeModel.CLOSED_MASK_PROPERTY)
.add(AbstractPipeModel.BLOCKED_MASK_PROPERTY).add(AbstractPipeModel.COLOR_PROPERTY)
.add(AbstractPipeModel.FRAME_MATERIAL_PROPERTY).add(AbstractPipeModel.FRAME_MASK_PROPERTY)
.add(CoverRendererPackage.PROPERTY);
}

@SuppressWarnings("deprecation")
@Override
public @NotNull IBlockState getActualState(@NotNull IBlockState state, @NotNull IBlockAccess worldIn,
@NotNull BlockPos pos) {
PipeTileEntity tile = getTileEntity(worldIn, pos);
if (tile == null) return state;
state = writeConnectionMask(state, tile.getConnectionMask());
return state.withProperty(FRAMED, tile.getFrameMaterial() != null);
}

public static IBlockState writeConnectionMask(@NotNull IBlockState state, byte connectionMask) {
for (EnumFacing facing : EnumFacing.VALUES) {
state = state.withProperty(FACINGS.get(facing), GTUtility.evalMask(facing, connectionMask));
}
return state;
}

public static byte readConnectionMask(@NotNull IBlockState state) {
byte mask = 0;
for (EnumFacing facing : EnumFacing.VALUES) {
if (state.getValue(FACINGS.get(facing))) {
mask |= 1 << facing.ordinal();
}
}
return mask;
}

@Override
public @NotNull IBlockState getExtendedState(@NotNull IBlockState state, @NotNull IBlockAccess world,
@NotNull BlockPos pos) {
PipeTileEntity tile = getTileEntity(world, pos);
if (tile == null) return state;
else return tile.getRenderInformation((IExtendedBlockState) state);
else return tile.getRenderInformation((IExtendedBlockState) state.getActualState(world, pos));
}

// tile entity //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,6 @@ public IExtendedBlockState getRenderInformation(IExtendedBlockState state) {
}
frameMask = (byte) ~frameMask;
return state.withProperty(AbstractPipeModel.THICKNESS_PROPERTY, this.getStructure().getRenderThickness())
.withProperty(AbstractPipeModel.CONNECTION_MASK_PROPERTY, connectionMask)
.withProperty(AbstractPipeModel.CLOSED_MASK_PROPERTY, renderMask)
.withProperty(AbstractPipeModel.BLOCKED_MASK_PROPERTY, blockedMask)
.withProperty(AbstractPipeModel.COLOR_PROPERTY, getPaintingColor())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void deserializeNBT(NBTTagCompound nbt) {
}

@Override
public Object getEquivalencyData() {
public DimensionPos getEquivalencyData() {
return pos;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void deserializeNBT(NBTTagCompound nbt) {
}

@Override
public Object getEquivalencyData() {
public BlockPos getEquivalencyData() {
return pos;
}
}
2 changes: 1 addition & 1 deletion src/main/java/gregtech/api/pattern/BlockPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public PatternMatchContext checkPatternFastAt(World world, BlockPos centerPos, E
boolean pass = true;
for (Map.Entry<Long, BlockInfo> entry : cache.entrySet()) {
BlockPos pos = BlockPos.fromLong(entry.getKey());
IBlockState blockState = world.getBlockState(pos);
IBlockState blockState = world.getBlockState(pos).getActualState(world, pos);
if (blockState != entry.getValue().getBlockState()) {
pass = false;
break;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gregtech/api/pattern/BlockWorldState.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public PatternMatchContext getMatchContext() {

public IBlockState getBlockState() {
if (this.state == null) {
this.state = this.world.getBlockState(this.pos);
this.state = this.world.getBlockState(this.pos).getActualState(this.world, this.pos);
}

return this.state;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gregtech/api/util/GTUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ public static int[] convertARGBtoArray(int argb) {

@Contract(pure = true)
public static boolean evalMask(@NotNull Enum<?> anEnum, byte mask) {
return (mask & (1L << anEnum.ordinal())) > 0;
return (mask & (1 << anEnum.ordinal())) > 0;
}

@Contract(pure = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gregtech.api.block.UnlistedFloatProperty;
import gregtech.api.block.UnlistedIntegerProperty;
import gregtech.api.block.UnlistedPropertyMaterial;
import gregtech.api.graphnet.pipenet.physical.block.PipeBlock;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.info.MaterialIconType;
import gregtech.api.util.GTUtility;
Expand Down Expand Up @@ -51,7 +52,6 @@ public abstract class AbstractPipeModel<K extends CacheKey> implements IBakedMod
public static UnlistedPropertyMaterial FRAME_MATERIAL_PROPERTY = new UnlistedPropertyMaterial("frame_material");
public static UnlistedByteProperty FRAME_MASK_PROPERTY = new UnlistedByteProperty("frame_mask");

public static UnlistedByteProperty CONNECTION_MASK_PROPERTY = new UnlistedByteProperty("connection_mask");
public static UnlistedByteProperty CLOSED_MASK_PROPERTY = new UnlistedByteProperty("closed_mask");
public static UnlistedByteProperty BLOCKED_MASK_PROPERTY = new UnlistedByteProperty("blocked_mask");

Expand All @@ -75,7 +75,7 @@ public AbstractPipeModel(ModelResourceLocation loc) {
CoverRendererPackage rendererPackage = ext.getValue(CoverRendererPackage.PROPERTY);
byte coverMask = rendererPackage == null ? 0 : rendererPackage.getMask();
if (canRenderInLayer(getCurrentRenderLayer())) {
quads = getQuads(toKey(ext), safeByte(ext.getValue(CONNECTION_MASK_PROPERTY)),
quads = getQuads(toKey(ext), PipeBlock.readConnectionMask(ext),
safeByte(ext.getValue(CLOSED_MASK_PROPERTY)), safeByte(ext.getValue(BLOCKED_MASK_PROPERTY)),
data, ext.getValue(FRAME_MATERIAL_PROPERTY),
safeByte(ext.getValue(FRAME_MASK_PROPERTY)), coverMask);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import gregtech.api.graphnet.AbstractGroupData;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class EnergyGroupData extends AbstractGroupData {

private long lastEnergyInPerSec;
Expand Down Expand Up @@ -43,4 +46,16 @@ public void clearCache() {
energyInPerSec = 0;
energyOutPerSec = 0;
}

@Override
public boolean mergeAllowed(@NotNull AbstractGroupData other) {
return true;
}

@Override
public @Nullable AbstractGroupData merge(@NotNull AbstractGroupData other) {
if (other instanceof EnergyGroupData)
return new EnergyGroupData();
else return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gregtech.common.pipelike.net.energy;

import gregtech.api.capability.GregtechCapabilities;
import gregtech.api.graphnet.AbstractGroupData;
import gregtech.api.graphnet.alg.DynamicWeightsShortestPathsAlgorithm;
import gregtech.api.graphnet.edge.NetFlowEdge;
import gregtech.api.graphnet.edge.SimulatorKey;
Expand Down Expand Up @@ -66,6 +67,11 @@ public Iterator<FlowWorldPipeNetPath> getPaths(WorldPipeNetNode node, IPredicate
return new NetFlowEdge(1);
}

@Override
public AbstractGroupData getBlankGroupData() {
return new EnergyGroupData();
}

@Override
public int getNetworkID() {
return 0;
Expand Down

0 comments on commit 28a2752

Please sign in to comment.