Skip to content

Commit

Permalink
Various Cleanroom Improvements (GregTechCEu#2480)
Browse files Browse the repository at this point in the history
  • Loading branch information
vrejhead authored Jun 4, 2024
1 parent 97dd250 commit 1328835
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 40 deletions.
2 changes: 2 additions & 0 deletions src/main/java/gregtech/api/GregTechAPI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gregtech.api;

import gregtech.api.advancement.IAdvancementManager;
import gregtech.api.block.ICleanroomFilter;
import gregtech.api.block.IHeatingCoilBlockStats;
import gregtech.api.block.machines.BlockMachine;
import gregtech.api.command.ICommandManager;
Expand Down Expand Up @@ -68,6 +69,7 @@ public class GregTechAPI {
public static final Map<Material, Map<StoneType, IBlockOre>> oreBlockTable = new HashMap<>();
public static final Object2ObjectMap<IBlockState, IHeatingCoilBlockStats> HEATING_COILS = new Object2ObjectOpenHashMap<>();
public static final Object2ObjectMap<IBlockState, IBatteryData> PSS_BATTERIES = new Object2ObjectOpenHashMap<>();
public static final Object2ObjectMap<IBlockState, ICleanroomFilter> CLEANROOM_FILTERS = new Object2ObjectOpenHashMap<>();

/** Will be available at the Pre-Initialization stage */
public static boolean isHighTier() {
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/gregtech/api/block/ICleanroomFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gregtech.api.block;

import gregtech.api.GTValues;
import gregtech.api.metatileentity.multiblock.CleanroomType;

import org.jetbrains.annotations.Nullable;

public interface ICleanroomFilter {

/**
* @return The {@link CleanroomType} this filter should provide,
* can be <code>null</code> if the block isn't a filter
*/
@Nullable
CleanroomType getCleanroomType();

/**
* @return The "tier" of the filter, for use in JEI previews
*/
int getTier();

/**
* @return The minimum voltage tier a cleanroom with this filter will accept
*/
default int getMinTier() {
return GTValues.LV;
}
}
13 changes: 12 additions & 1 deletion src/main/java/gregtech/api/capability/impl/CleanroomLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class CleanroomLogic {
private int maxProgress = 0;
private int progressTime = 0;

private final int minEnergyTier;
private int minEnergyTier;

private final MetaTileEntity metaTileEntity;
private final boolean hasMaintenance;
Expand Down Expand Up @@ -48,6 +48,9 @@ public void updateLogic() {
// all maintenance problems not fixed means the machine does not run
if (hasMaintenance && ((IMaintenance) metaTileEntity).getNumMaintenanceProblems() > 5) return;

// if the energy tier is below min tier then do nothing
if (!isVoltageHighEnough()) return;

// drain the energy
if (consumeEnergy(true)) {
consumeEnergy(false);
Expand Down Expand Up @@ -128,6 +131,10 @@ public void setWorkingEnabled(boolean workingEnabled) {
}
}

public boolean isVoltageHighEnough() {
return minEnergyTier <= ((ICleanroomProvider) metaTileEntity).getEnergyTier();
}

/**
* @return whether working is enabled for the logic
*/
Expand Down Expand Up @@ -165,6 +172,10 @@ protected int getTierDifference() {
return ((ICleanroomProvider) metaTileEntity).getEnergyTier() - minEnergyTier;
}

public void setMinEnergyTier(int energyTier) {
this.minEnergyTier = energyTier;
}

/**
* writes all needed values to NBT
* This MUST be called and returned in the MetaTileEntity's {@link MetaTileEntity#writeToNBT(NBTTagCompound)} method
Expand Down
25 changes: 20 additions & 5 deletions src/main/java/gregtech/common/blocks/BlockCleanroomCasing.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package gregtech.common.blocks;

import gregtech.api.block.ICleanroomFilter;
import gregtech.api.block.IStateHarvestLevel;
import gregtech.api.block.VariantBlock;
import gregtech.api.items.toolitem.ToolClasses;
import gregtech.api.metatileentity.multiblock.CleanroomType;

import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
Expand Down Expand Up @@ -38,16 +40,18 @@ public boolean canCreatureSpawn(@NotNull IBlockState state, @NotNull IBlockAcces
return false;
}

public enum CasingType implements IStringSerializable {
public enum CasingType implements IStringSerializable, ICleanroomFilter {

PLASCRETE("plascrete"),
FILTER_CASING("filter_casing"),
FILTER_CASING_STERILE("filter_casing_sterile");
PLASCRETE("plascrete", null),
FILTER_CASING("filter_casing", CleanroomType.CLEANROOM),
FILTER_CASING_STERILE("filter_casing_sterile", CleanroomType.STERILE_CLEANROOM);

private final String name;
private final CleanroomType cleanroomType;

CasingType(String name) {
CasingType(String name, CleanroomType cleanroomType) {
this.name = name;
this.cleanroomType = cleanroomType;
}

@NotNull
Expand All @@ -61,6 +65,17 @@ public String getName() {
public String toString() {
return getName();
}

@Override
@Nullable
public CleanroomType getCleanroomType() {
return cleanroomType;
}

@Override
public int getTier() {
return this.ordinal() - 1;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gregtech.common.metatileentities.multi.electric;

import gregtech.api.GTValues;
import gregtech.api.GregTechAPI;
import gregtech.api.block.ICleanroomFilter;
import gregtech.api.capability.GregtechDataCodes;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IEnergyContainer;
Expand Down Expand Up @@ -44,7 +46,6 @@
import gregtech.common.metatileentities.multi.electric.centralmonitor.MetaTileEntityCentralMonitor;
import gregtech.core.sound.GTSoundEvents;

import net.minecraft.block.Block;
import net.minecraft.block.BlockDoor;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.resources.I18n;
Expand All @@ -61,6 +62,7 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
Expand All @@ -73,14 +75,14 @@
import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.pipeline.IVertexOperation;
import codechicken.lib.vec.Matrix4;
import org.apache.commons.lang3.ArrayUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -105,6 +107,7 @@ public class MetaTileEntityCleanroom extends MultiblockWithDisplayBase

private IEnergyContainer energyContainer;

private ICleanroomFilter cleanroomFilter;
private final CleanroomLogic cleanroomLogic;
private final Collection<ICleanroomReceiver> cleanroomReceivers = new HashSet<>();

Expand All @@ -130,21 +133,15 @@ private void resetTileAbilities() {
protected void formStructure(PatternMatchContext context) {
super.formStructure(context);
initializeAbilities();
Object type = context.get("FilterType");
if (type instanceof BlockCleanroomCasing.CasingType) {
BlockCleanroomCasing.CasingType casingType = (BlockCleanroomCasing.CasingType) type;

if (casingType.equals(BlockCleanroomCasing.CasingType.FILTER_CASING)) {
this.cleanroomType = CleanroomType.CLEANROOM;
} else if (casingType.equals(BlockCleanroomCasing.CasingType.FILTER_CASING_STERILE)) {
this.cleanroomType = CleanroomType.STERILE_CLEANROOM;
}
}
this.cleanroomFilter = context.get("FilterType");
this.cleanroomType = cleanroomFilter.getCleanroomType();

// max progress is based on the dimensions of the structure: (x^3)-(x^2)
// taller cleanrooms take longer than wider ones
// minimum of 100 is a 5x5x5 cleanroom: 125-25=100 ticks
this.cleanroomLogic.setMaxProgress(Math.max(100,
((lDist + rDist + 1) * (bDist + fDist + 1) * hDist) - ((lDist + rDist + 1) * (bDist + fDist + 1))));
this.cleanroomLogic.setMinEnergyTier(cleanroomFilter.getMinTier());
}

@Override
Expand Down Expand Up @@ -390,27 +387,26 @@ protected BlockPattern createStructurePattern() {
protected TraceabilityPredicate filterPredicate() {
return new TraceabilityPredicate(blockWorldState -> {
IBlockState blockState = blockWorldState.getBlockState();
Block block = blockState.getBlock();
if (block instanceof BlockCleanroomCasing) {
BlockCleanroomCasing.CasingType casingType = ((BlockCleanroomCasing) blockState.getBlock())
.getState(blockState);
if (casingType.equals(BlockCleanroomCasing.CasingType.PLASCRETE)) return false;

Object currentFilter = blockWorldState.getMatchContext().getOrPut("FilterType", casingType);
if (!currentFilter.toString().equals(casingType.getName())) {
if (GregTechAPI.CLEANROOM_FILTERS.containsKey(blockState)) {
ICleanroomFilter cleanroomFilter = GregTechAPI.CLEANROOM_FILTERS.get(blockState);
if (cleanroomFilter.getCleanroomType() == null) return false;

ICleanroomFilter currentFilter = blockWorldState.getMatchContext().getOrPut("FilterType",
cleanroomFilter);
if (!currentFilter.getCleanroomType().equals(cleanroomFilter.getCleanroomType())) {
blockWorldState.setError(new PatternStringError("gregtech.multiblock.pattern.error.filters"));
return false;
}
blockWorldState.getMatchContext().getOrPut("VABlock", new LinkedList<>()).add(blockWorldState.getPos());
return true;
}
return false;
}, () -> ArrayUtils.addAll(
Arrays.stream(BlockCleanroomCasing.CasingType.values())
.filter(type -> !type.equals(BlockCleanroomCasing.CasingType.PLASCRETE))
.map(type -> new BlockInfo(MetaBlocks.CLEANROOM_CASING.getState(type), null))
.toArray(BlockInfo[]::new)))
.addTooltips("gregtech.multiblock.pattern.error.filters");
}, () -> GregTechAPI.CLEANROOM_FILTERS.entrySet().stream()
.filter(entry -> entry.getValue().getCleanroomType() != null)
.sorted(Comparator.comparingInt(entry -> entry.getValue().getTier()))
.map(entry -> new BlockInfo(entry.getKey(), null))
.toArray(BlockInfo[]::new))
.addTooltips("gregtech.multiblock.pattern.error.filters");
}

@SideOnly(Side.CLIENT)
Expand Down Expand Up @@ -453,10 +449,9 @@ protected TraceabilityPredicate innerPredicate() {
return false;

// the machine does not need a cleanroom, so do nothing more
if (!(metaTileEntity instanceof ICleanroomReceiver)) return true;
if (!(metaTileEntity instanceof ICleanroomReceiver cleanroomReceiver)) return true;

// give the machine this cleanroom if it doesn't have this one
ICleanroomReceiver cleanroomReceiver = (ICleanroomReceiver) metaTileEntity;
if (cleanroomReceiver.getCleanroom() != this) {
cleanroomReceiver.setCleanroom(this);
cleanroomReceivers.add(cleanroomReceiver);
Expand Down Expand Up @@ -511,6 +506,15 @@ protected void addDisplayText(List<ITextComponent> textList) {
cleanState));
}
})
.addCustom(tl -> {
if (!cleanroomLogic.isVoltageHighEnough()) {
ITextComponent energyNeeded = new TextComponentString(
GTValues.VNF[cleanroomFilter.getMinTier()]);
tl.add(TextComponentUtil.translationWithColor(TextFormatting.YELLOW,
"gregtech.multiblock.cleanroom.low_tier", energyNeeded));
}
})
.addEnergyUsageExactLine(isClean() ? 4 : GTValues.VA[getEnergyTier()])
.addWorkingStatusLine()
.addProgressLine(getProgressPercent() / 100.0);
}
Expand Down Expand Up @@ -593,7 +597,8 @@ public boolean isClean() {
@Override
public List<ITextComponent> getDataInfo() {
return Collections.singletonList(new TextComponentTranslation(
isClean() ? "gregtech.multiblock.cleanroom.clean_state" : "gregtech.multiblock.cleanroom.dirty_state"));
isClean() ? "gregtech.multiblock.cleanroom.clean_state" : "gregtech.multiblock.cleanroom.dirty_state",
this.cleanAmount));
}

@Override
Expand Down Expand Up @@ -639,7 +644,7 @@ public long getEnergyInputPerSecond() {
}

public boolean drainEnergy(boolean simulate) {
long energyToDrain = isClean() ? (long) Math.min(4, Math.pow(4, getEnergyTier())) :
long energyToDrain = isClean() ? 4 :
GTValues.VA[getEnergyTier()];
long resultEnergy = energyContainer.getEnergyStored() - energyToDrain;
if (resultEnergy >= 0L && resultEnergy <= energyContainer.getEnergyCapacity()) {
Expand Down Expand Up @@ -761,10 +766,11 @@ public List<MultiblockShapeInfo> getMatchingShapes() {
.where('R', Blocks.IRON_DOOR.getDefaultState().withProperty(BlockDoor.FACING, EnumFacing.NORTH)
.withProperty(BlockDoor.HALF, BlockDoor.EnumDoorHalf.UPPER));

Arrays.stream(BlockCleanroomCasing.CasingType.values())
.filter(casingType -> !casingType.equals(BlockCleanroomCasing.CasingType.PLASCRETE))
.forEach(casingType -> shapeInfo
.add(builder.where('F', MetaBlocks.CLEANROOM_CASING.getState(casingType)).build()));
GregTechAPI.CLEANROOM_FILTERS.entrySet().stream()
.filter(entry -> entry.getValue().getCleanroomType() != null)
.sorted(Comparator.comparingInt(entry -> entry.getValue().getTier()))
.forEach(entry -> shapeInfo.add(builder.where('F', entry.getKey()).build()));

return shapeInfo;
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/gregtech/core/CoreModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import gregtech.common.ConfigHolder;
import gregtech.common.MetaEntities;
import gregtech.common.blocks.BlockBatteryPart;
import gregtech.common.blocks.BlockCleanroomCasing;
import gregtech.common.blocks.BlockWireCoil;
import gregtech.common.blocks.MetaBlocks;
import gregtech.common.command.CommandHand;
Expand Down Expand Up @@ -207,6 +208,9 @@ public void preInit(FMLPreInitializationEvent event) {
for (BlockBatteryPart.BatteryPartType type : BlockBatteryPart.BatteryPartType.values()) {
PSS_BATTERIES.put(MetaBlocks.BATTERY_BLOCK.getState(type), type);
}
for (BlockCleanroomCasing.CasingType type : BlockCleanroomCasing.CasingType.values()) {
CLEANROOM_FILTERS.put(MetaBlocks.CLEANROOM_CASING.getState(type), type);
}
/* End API Block Registration */

proxy.onPreLoad();
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/gregtech/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -4831,6 +4831,7 @@ gregtech.multiblock.cleanroom.clean_status=Status: %s
gregtech.multiblock.cleanroom.clean_state=Clean (%s%%)
gregtech.multiblock.cleanroom.dirty_state=Contaminated (%s%%)
gregtech.multiblock.cleanroom.warning_contaminated=Contaminated!
gregtech.multiblock.cleanroom.low_tier=Energy tier too low, minimum of %s!

gregtech.machine.charcoal_pile.name=Charcoal Pile Igniter
gregtech.machine.charcoal_pile.tooltip.1=Turns Logs into §aCharcoal§7 when §cignited§7.
Expand Down

0 comments on commit 1328835

Please sign in to comment.