Skip to content

Commit

Permalink
Config and API for tick acceleration blocking (GregTechCEu#2381)
Browse files Browse the repository at this point in the history
  • Loading branch information
serenibyss authored Feb 11, 2024
1 parent a7cf3c6 commit c6d494e
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 @@ -67,6 +67,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 @@ -152,6 +153,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 @@ -809,6 +811,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 @@ -824,6 +833,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 c6d494e

Please sign in to comment.