Skip to content

Commit

Permalink
Config and API for tick acceleration blocking (#2381)
Browse files Browse the repository at this point in the history
  • Loading branch information
serenibyss committed Feb 19, 2024
1 parent 26c341e commit 0750ca5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
18 changes: 18 additions & 0 deletions src/main/java/gregtech/api/metatileentity/MetaTileEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import net.minecraftforge.fluids.FluidUtil;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Optional.Method;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
Expand Down Expand Up @@ -125,6 +126,7 @@ public abstract class MetaTileEntity implements ISyncedTileEntity, CoverHolder,
protected boolean muffled = false;

private int playSoundCooldown = 0;
private int lastTick = 0;

public MetaTileEntity(ResourceLocation metaTileEntityId) {
this.metaTileEntityId = metaTileEntityId;
Expand Down Expand Up @@ -754,6 +756,13 @@ private void updateLightValue() {
}

public void update() {
if (!allowTickAcceleration()) {
int currentTick = FMLCommonHandler.instance().getMinecraftServerInstance().getTickCounter();
if (currentTick == lastTick) {
return;
}
lastTick = currentTick;
}
for (MTETrait mteTrait : this.mteTraits.values()) {
if (shouldUpdate(mteTrait)) {
mteTrait.update();
Expand All @@ -769,6 +778,15 @@ public void update() {
}
}

/**
* @return Whether this machine is allowed to be tick accelerated by external means. This does NOT
* apply to World Accelerators from GT, those will never work on machines. This refers to effects
* like Time in a Bottle, or Torcherino, or similar.
*/
public boolean allowTickAcceleration() {
return ConfigHolder.machines.allowTickAcceleration;
}

protected boolean shouldUpdate(MTETrait trait) {
return true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/gregtech/common/ConfigHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ public static class MachineOptions {
"Default: false" })
@Config.RequiresMcRestart
public boolean highTierContent = false;

@Config.Comment({ "Whether tick acceleration effects are allowed to affect GT machines.",
"This does NOT apply to the World Accelerator, but to external effects like Time in a Bottle.",
"Default: true" })
public boolean allowTickAcceleration = true;
}

public static class WorldGenOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fml.common.FMLCommonHandler;

import codechicken.lib.raytracer.CuboidRayTraceResult;
import codechicken.lib.render.CCRenderState;
Expand Down Expand Up @@ -57,7 +56,6 @@ public class MetaTileEntityWorldAccelerator extends TieredMetaTileEntity impleme
private boolean tileMode = false;
private boolean isActive = false;
private boolean isPaused = false;
private int lastTick;

// Variables for Random Tick mode optimization
// limit = ((tier - min) / (max - min)) * 2^tier
Expand All @@ -67,7 +65,6 @@ public class MetaTileEntityWorldAccelerator extends TieredMetaTileEntity impleme

public MetaTileEntityWorldAccelerator(ResourceLocation metaTileEntityId, int tier) {
super(metaTileEntityId, tier);
this.lastTick = 0;
this.speed = (int) Math.pow(2, tier);
this.successLimit = SUCCESS_LIMITS[tier - 1];
initializeInventory();
Expand Down Expand Up @@ -124,22 +121,23 @@ protected long getTEModeAmperage() {
return 6L;
}

@Override
public boolean allowTickAcceleration() {
return false;
}

@Override
public void update() {
super.update();
if (!getWorld().isRemote) {
if (isPaused && isActive) {
setActive(false);
} else if (!isPaused) {
int currentTick = FMLCommonHandler.instance().getMinecraftServerInstance().getTickCounter();
if (currentTick != lastTick) { // Prevent other tick accelerators from accelerating us
lastTick = currentTick;
boolean wasSuccessful = isTEMode() ? handleTEMode() : handleRandomTickMode();
if (!wasSuccessful) {
setActive(false);
} else if (!isActive) {
setActive(true);
}
boolean wasSuccessful = isTEMode() ? handleTEMode() : handleRandomTickMode();
if (!wasSuccessful) {
setActive(false);
} else if (!isActive) {
setActive(true);
}
}
}
Expand Down

0 comments on commit 0750ca5

Please sign in to comment.