Skip to content

Commit

Permalink
Improve ticker & spotlessify
Browse files Browse the repository at this point in the history
  • Loading branch information
M-W-K committed Jan 23, 2024
1 parent 886bad4 commit fcd2c92
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 181 deletions.
1 change: 0 additions & 1 deletion src/main/java/gregtech/api/pipenet/FlowChannelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;

import org.jetbrains.annotations.Nullable;

import java.util.Map;
Expand Down
34 changes: 24 additions & 10 deletions src/main/java/gregtech/api/pipenet/FlowChannelTicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,52 @@

import gregtech.api.GTValues;

import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;

import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArraySet;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;

import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.Set;

// Ok, I admit, truly tickless fluidpipes would mean sacrificing behavior. I'd have to make them act like itempipes.
// To get true 'flow' behavior, all sources and all destinations must be defined, then a single evaluation performed.
@Mod.EventBusSubscriber(modid = GTValues.MODID)
public final class FlowChannelTicker {

private final static Set<WeakReference<FlowChannelManager<?, ?>>> MANAGERS = new ObjectOpenHashSet<>();
private final static Map<World, Set<WeakReference<FlowChannelManager<?, ?>>>> MANAGERS = new Object2ObjectOpenHashMap<>();
private final static Map<World, Integer> TICK_COUNTS = new Object2ObjectOpenHashMap<>();

private final static Set<WeakReference<FlowChannelManager<?, ?>>> EMPTY = new ObjectArraySet<>(0);

@SubscribeEvent
public static void onServerTick(TickEvent.ServerTickEvent event) {
if (FMLCommonHandler.instance().getMinecraftServerInstance().getTickCounter() % 20 != 0) return;
for (WeakReference<FlowChannelManager<?, ?>> ref : MANAGERS) {
public static void onWorldTick(TickEvent.WorldTickEvent event) {
if (event.world.isRemote) return;
TICK_COUNTS.compute(event.world, (k, v) -> {
if (v == null) v = 0;
return v % 10 + 1;
});
if (TICK_COUNTS.get(event.world) != 10) return;

for (WeakReference<FlowChannelManager<?, ?>> ref : MANAGERS.getOrDefault(event.world, EMPTY)) {
FlowChannelManager<?, ?> manager = ref.get();
if (manager != null) {
manager.tick();
} else {
MANAGERS.remove(ref);
MANAGERS.get(event.world).remove(ref);
}
}
}

public static void addManager(FlowChannelManager<?, ?> manager) {
MANAGERS.add(new WeakReference<>(manager));
public static void addManager(World world, FlowChannelManager<?, ?> manager) {
if (!MANAGERS.containsKey(world)) {
MANAGERS.put(world, new ObjectOpenHashSet<>());
}
MANAGERS.get(world).add(new WeakReference<>(manager));
}
}
1 change: 0 additions & 1 deletion src/main/java/gregtech/api/pipenet/NetGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import gregtech.api.pipenet.block.IPipeType;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.INBTSerializable;

import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/gregtech/api/pipenet/WorldPipeFlowNetG.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package gregtech.api.pipenet;

import gregtech.api.pipenet.block.IPipeType;

import gregtech.api.pipenet.tile.TileEntityPipeBase;

import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.nbt.NBTTagList;

import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jgrapht.graph.SimpleDirectedWeightedGraph;
Expand Down Expand Up @@ -65,7 +62,8 @@ public List<NetPath<PipeType, NodeDataType>> getPaths(@Nullable NodeG<PipeType,
for (NetEdge edge : pipeGraph.edgeSet()) {
// prevent contamination by our manager edges
if (edge.getSource() == superSource || edge.getTarget() == superSource ||
edge.getSource() == superSink || edge.getTarget() == superSink) continue;
edge.getSource() == superSink || edge.getTarget() == superSink)
continue;
allNetEdges.appendTag(edge.serializeNBT());
}
compound.setTag("NetEdges", allNetEdges);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

import gregtech.api.pipenet.INodeData;
import gregtech.api.pipenet.NetEdge;
import gregtech.api.pipenet.NetPath;
import gregtech.api.pipenet.NodeG;
import gregtech.api.pipenet.block.IPipeType;

import it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import org.jgrapht.Graph;
import org.jgrapht.alg.flow.PushRelabelMFImpl;

import java.util.List;
import java.util.Map;

/**
* Secretly just {@link PushRelabelMFImpl}, but may be modified in the future.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import gregtech.common.covers.filter.FluidFilter;
import gregtech.common.pipelike.fluidpipe.FluidPipeType;
import gregtech.common.pipelike.fluidpipe.tile.TileEntityFluidPipe;
import gregtech.common.pipelike.fluidpipe.tile.TileEntityFluidPipeTickable;

import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gregtech.common.pipelike.fluidpipe.net;

import gregtech.common.pipelike.fluidpipe.tile.TileEntityFluidPipe;
import gregtech.common.pipelike.fluidpipe.tile.TileEntityFluidPipeTickable;

import net.minecraft.util.EnumFacing;
import net.minecraftforge.fluids.FluidStack;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import gregtech.api.util.EntityDamageUtil;
import gregtech.api.util.TextFormattingUtil;
import gregtech.common.pipelike.fluidpipe.FluidPipeType;

import gregtech.common.pipelike.fluidpipe.net.FluidChannel;
import gregtech.common.pipelike.fluidpipe.net.PipeTankList;

Expand All @@ -37,9 +36,6 @@
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;

import net.minecraftforge.fluids.capability.IFluidHandler;

import net.minecraftforge.fml.common.FMLCommonHandler;

import org.jetbrains.annotations.NotNull;
Expand All @@ -50,7 +46,7 @@
import java.util.List;

public class TileEntityFluidPipe extends TileEntityMaterialPipeBase<FluidPipeType, FluidPipeProperties>
implements IDataInfoProvider {
implements IDataInfoProvider {

private PipeTankList pipeTankList;
private final EnumMap<EnumFacing, PipeTankList> tankLists = new EnumMap<>(EnumFacing.class);
Expand Down Expand Up @@ -340,7 +336,8 @@ public void readFromNBT(@NotNull NBTTagCompound nbt) {
if (stack == null) continue;
fluidTanks[i].setFluid(stack);
// the best part is that this naturally gives us backwards compatibility.
FluidChannel channel = FluidChannel.getChannelFromGroup(stack.getFluid(), this.getNode().getGroupSafe());
FluidChannel channel = FluidChannel.getChannelFromGroup(stack.getFluid(),
this.getNode().getGroupSafe());
channel.addSource(this.getNode());
}
}
Expand Down
Loading

0 comments on commit fcd2c92

Please sign in to comment.