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

Pipe fixes #1713

Merged
merged 8 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
21 changes: 10 additions & 11 deletions src/main/java/gregtech/api/pipenet/PipeNetWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ public void traversePipeNet(int maxWalks) {

private boolean walk() {
if (walkers == null) {
checkPos();
if (!checkPos()) {
this.root.failed = true;
return true;
}

if (pipes.size() == 0)
return true;
Expand Down Expand Up @@ -140,19 +143,14 @@ private boolean walk() {
return !isRunning() || walkers.size() == 0;
}

private void checkPos() {
private boolean checkPos() {
pipes.clear();
TileEntity thisPipe = world.getTileEntity(currentPos);
IPipeTile<?, ?> pipeTile = (IPipeTile<?, ?>) thisPipe;
if (pipeTile == null) {
if (walkedBlocks == 1) {
// if it is the first block, it wasn't already checked
GTLog.logger.error("First PipeTile is null during walk at {}", currentPos);
this.failed = true;
return;
} else
throw new IllegalStateException("PipeTile was not null last walk, but now is");
if (!(thisPipe instanceof IPipeTile<?, ?>)) {
GTLog.logger.fatal("PipeWalker expected a pipe, but found {} at {}", thisPipe, currentPos);
return false;
}
IPipeTile<?, ?> pipeTile = (IPipeTile<?, ?>) thisPipe;
checkPipe(pipeTile, currentPos);
root.walked.add(pipeTile.getPipePos().toLong());

Expand All @@ -177,6 +175,7 @@ private void checkPos() {
checkNeighbour(pipeTile, currentPos, accessSide, tile);
}
pos.release();
return true;
}

protected boolean isWalked(IPipeTile<?, ?> pipe) {
Expand Down
32 changes: 18 additions & 14 deletions src/main/java/gregtech/api/util/PerTickLongCounter.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package gregtech.api.util;

import net.minecraft.world.World;
import java.util.function.LongSupplier;

public class PerTickLongCounter {

private final LongSupplier timeSupplier;
private final long defaultValue;

private long lastUpdatedWorldTime;

private long lastValue;
private long currentValue;

public PerTickLongCounter(long defaultValue) {
public PerTickLongCounter(LongSupplier timeSupplier) {
this(timeSupplier, 0);
}

public PerTickLongCounter(LongSupplier timeSupplier, long defaultValue) {
this.timeSupplier = timeSupplier;
this.defaultValue = defaultValue;
this.currentValue = defaultValue;
this.lastValue = defaultValue;
}

private void checkValueState(World world) {
long currentWorldTime = world.getTotalWorldTime();
private void checkValueState() {
long currentWorldTime = this.timeSupplier.getAsLong();
if (currentWorldTime != lastUpdatedWorldTime) {
if (currentWorldTime == lastUpdatedWorldTime + 1) {
//last updated time is 1 tick ago, so we can move current value to last
Expand All @@ -33,23 +37,23 @@ private void checkValueState(World world) {
}
}

public long get(World world) {
checkValueState(world);
public long get() {
checkValueState();
return currentValue;
}

public long getLast(World world) {
checkValueState(world);
public long getLast() {
checkValueState();
return lastValue;
}

public void increment(World world, long value) {
checkValueState(world);
public void increment(long value) {
checkValueState();
this.currentValue += value;
}

public void set(World world, long value) {
checkValueState(world);
public void set(long value) {
checkValueState();
this.currentValue = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
public class EnergyNetWalker extends PipeNetWalker {

public static List<RoutePath> createNetData(World world, BlockPos sourcePipe) {
if (!(world.getTileEntity(sourcePipe) instanceof TileEntityCable)) {
return null;
}
EnergyNetWalker walker = new EnergyNetWalker(world, sourcePipe, 1, new ArrayList<>());
walker.traversePipeNet();
return walker.isFailed() ? null : walker.routes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,41 @@
import net.minecraft.world.World;

import java.util.Arrays;
import java.util.function.LongSupplier;

public class AveragingPerTickCounter {

private final LongSupplier timeSupplier;
private final long defaultValue;
private final long[] values;
private long lastUpdatedWorldTime = 0;
private int currentIndex = 0;
private boolean dirty = true;
private double lastAverage = 0;

public AveragingPerTickCounter(LongSupplier timeSupplier) {
this(timeSupplier, 0, 20);
}

/**
* Averages a value over a certain amount of ticks
*
* @param defaultValue self explanatory
* @param timeSupplier current time in ticks supplier. Usually {@link World#getTotalWorldTime()}
* @param defaultValue self-explanatory
* @param length amount of ticks to average (20 for 1 second)
*/
public AveragingPerTickCounter(long defaultValue, int length) {
public AveragingPerTickCounter(LongSupplier timeSupplier, long defaultValue, int length) {
this.timeSupplier = timeSupplier;
this.defaultValue = defaultValue;
this.values = new long[length];
Arrays.fill(values, defaultValue);
}

private void checkValueState(World world) {
long currentWorldTime = world.getTotalWorldTime();
private void checkValueState() {
long currentWorldTime = this.timeSupplier.getAsLong();
if (currentWorldTime != lastUpdatedWorldTime) {
long dif = currentWorldTime - lastUpdatedWorldTime;
if (dif >= values.length) {
if (dif >= values.length || dif < 0) {
Arrays.fill(values, defaultValue);
currentIndex = 0;
} else {
Expand All @@ -52,16 +60,16 @@ private void checkValueState(World world) {
/**
* @return the value from the current tick
*/
public long getLast(World world) {
checkValueState(world);
public long getLast() {
checkValueState();
return values[currentIndex];
}

/**
* @return the average of all values
*/
public double getAverage(World world) {
checkValueState(world);
public double getAverage() {
checkValueState();
if (!dirty)
return lastAverage;
dirty = false;
Expand All @@ -71,16 +79,16 @@ public double getAverage(World world) {
/**
* @param value the value to increment the current value by
*/
public void increment(World world, long value) {
checkValueState(world);
public void increment(long value) {
checkValueState();
values[currentIndex] += value;
}

/**
* @param value the value to set current value to
*/
public void set(World world, long value) {
checkValueState(world);
public void set(long value) {
checkValueState();
values[currentIndex] = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public class TileEntityCable extends TileEntityMaterialPipeBase<Insulation, Wire
private static final int meltTemp = 3000;

private final EnumMap<EnumFacing, EnergyNetHandler> handlers = new EnumMap<>(EnumFacing.class);
private final PerTickLongCounter maxVoltageCounter = new PerTickLongCounter(0);
private final AveragingPerTickCounter averageVoltageCounter = new AveragingPerTickCounter(0, 20);
private final AveragingPerTickCounter averageAmperageCounter = new AveragingPerTickCounter(0, 20);
private final PerTickLongCounter maxVoltageCounter = new PerTickLongCounter(this::getWorldTime);
private final AveragingPerTickCounter averageVoltageCounter = new AveragingPerTickCounter(this::getWorldTime);
private final AveragingPerTickCounter averageAmperageCounter = new AveragingPerTickCounter(this::getWorldTime);
private EnergyNetHandler defaultHandler;
// the EnergyNetHandler can only be created on the server so we have a empty placeholder for the client
// the EnergyNetHandler can only be created on the server, so we have an empty placeholder for the client
private final IEnergyContainer clientCapability = IEnergyContainer.DEFAULT;
private WeakReference<EnergyNet> currentEnergyNet = new WeakReference<>(null);
@SideOnly(Side.CLIENT)
Expand All @@ -57,6 +57,10 @@ public class TileEntityCable extends TileEntityMaterialPipeBase<Insulation, Wire
private int temperature = getDefaultTemp();
private boolean isTicking = false;

public long getWorldTime() {
return hasWorld() ? getWorld().getTotalWorldTime() : 0L;
}

@Override
public Class<Insulation> getPipeTypeClass() {
return Insulation.class;
Expand Down Expand Up @@ -100,13 +104,13 @@ public void onLoad() {
* @return if the cable should be destroyed
*/
public boolean incrementAmperage(long amps, long voltage) {
if (voltage > maxVoltageCounter.get(world)) {
maxVoltageCounter.set(world, voltage);
if (voltage > maxVoltageCounter.get()) {
maxVoltageCounter.set(voltage);
}
averageVoltageCounter.increment(world, voltage);
averageAmperageCounter.increment(world, amps);
averageVoltageCounter.increment(voltage);
averageAmperageCounter.increment(amps);

int dif = (int) (averageAmperageCounter.getLast(world) - getMaxAmperage());
int dif = (int) (averageAmperageCounter.getLast() - getMaxAmperage());
if (dif > 0) {
applyHeat(dif * 40);
return true;
Expand Down Expand Up @@ -224,15 +228,15 @@ public void killParticle() {
}

public double getAverageAmperage() {
return averageAmperageCounter.getAverage(getWorld());
return averageAmperageCounter.getAverage();
}

public long getCurrentMaxVoltage() {
return maxVoltageCounter.get(getWorld());
return maxVoltageCounter.get();
}

public double getAverageVoltage() {
return averageVoltageCounter.getAverage(getWorld());
return averageVoltageCounter.getAverage();
}

public long getMaxAmperage() {
Expand Down
Loading