Skip to content

Commit

Permalink
Remove a good number of capturing lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
pupnewfster committed Apr 2, 2024
1 parent 3ffdc70 commit eb36b37
Show file tree
Hide file tree
Showing 47 changed files with 509 additions and 315 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,21 @@ public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction
if (side != null && side.getAxis().isHorizontal() && renderType == RenderType.translucent()) {
if (data.has(TileEntityFissionAssembly.GLOWING)) {
//TODO: Eventually we may want to make the glow component be part of the json so resource packs can customize it more
List<BakedQuad> allQuads = cachedGlows.computeIfAbsent(side, s -> {
Vec3 startPos = switch (s) {
List<BakedQuad> allQuads = cachedGlows.get(side);
if (allQuads == null) {
Vec3 startPos = switch (side) {
case NORTH, EAST -> NORTH_EAST;
case SOUTH, WEST -> SOUTH_WEST;
default -> throw new IllegalStateException("Unexpected face");
};
Quad.Builder quadBuilder = new Quad.Builder(MekanismRenderer.whiteIcon, s)
Quad.Builder quadBuilder = new Quad.Builder(MekanismRenderer.whiteIcon, side)
.light(LightTexture.FULL_BLOCK, LightTexture.FULL_SKY)
.uv(0, 0, 16, 16)
.color(GLOW_ARGB)
.rect(startPos, 0.9, height, 1);
return List.of(quadBuilder.build().bake());
});
allQuads = List.of(quadBuilder.build().bake());
cachedGlows.put(side, allQuads);
}
if (quads.isEmpty()) {
return allQuads;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import mekanism.client.render.ModelRenderer;
import mekanism.client.render.RenderResizableCuboid.FaceDisplay;
import mekanism.client.render.tileentity.MekanismTileEntityRenderer;
import mekanism.common.util.EnumUtils;
import mekanism.generators.common.GeneratorsProfilerConstants;
import mekanism.generators.common.tile.TileEntityBioGenerator;
import net.minecraft.client.renderer.LightTexture;
Expand Down Expand Up @@ -59,28 +60,33 @@ public boolean shouldRender(TileEntityBioGenerator tile, Vec3 camera) {
}

private Model3D getModel(FluidStack fluid, Direction side, float fluidScale) {
return fuelModels.computeIfAbsent(side, s -> new Int2ObjectOpenHashMap<>())
.computeIfAbsent(ModelRenderer.getStage(fluid, stages, fluidScale), stage -> {
Direction opposite = side.getOpposite();
Model3D model = new Model3D()
.setTexture(MekanismRenderer.getFluidTexture(fluid, FluidTextureType.STILL))
.yBounds(0.4385F, 0.4385F + 0.4375F * (stage / (float) stages))
.setSideRender(dir -> dir == Direction.UP || dir == opposite);
return switch (side) {
case NORTH -> model
.xBounds(0.188F, 0.821F)
.zBounds(0.499F, 0.875F);
case SOUTH -> model
.xBounds(0.188F, 0.821F)
.zBounds(0.125F, 0.499F);
case WEST -> model
.xBounds(0.499F, 0.875F)
.zBounds(0.187F, 0.821F);
case EAST -> model
.xBounds(0.125F, 0.499F)
.zBounds(0.186F, 0.821F);
default -> model;
};
});
Int2ObjectMap<Model3D> modelMap = fuelModels.computeIfAbsent(side, s -> new Int2ObjectOpenHashMap<>());
int stage = ModelRenderer.getStage(fluid, stages, fluidScale);
Model3D model = modelMap.get(stage);
if (model == null) {
model = new Model3D()
.setTexture(MekanismRenderer.getFluidTexture(fluid, FluidTextureType.STILL))
.yBounds(0.4385F, 0.4385F + 0.4375F * (stage / (float) stages));
Direction opposite = side.getOpposite();
for (Direction direction : EnumUtils.DIRECTIONS) {
model.setSideRender(direction, direction == Direction.UP || direction == opposite);
}
switch (side) {
case NORTH -> model
.xBounds(0.188F, 0.821F)
.zBounds(0.499F, 0.875F);
case SOUTH -> model
.xBounds(0.188F, 0.821F)
.zBounds(0.125F, 0.499F);
case WEST -> model
.xBounds(0.499F, 0.875F)
.zBounds(0.187F, 0.821F);
case EAST -> model
.xBounds(0.125F, 0.499F)
.zBounds(0.186F, 0.821F);
}
modelMap.put(stage, model);
}
return model;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import mekanism.api.heat.IHeatHandler;
import mekanism.api.math.FloatingLong;
import mekanism.common.attachments.containers.ContainerType;
import mekanism.common.capabilities.Capabilities;
import mekanism.common.capabilities.fluid.BasicFluidTank;
import mekanism.common.capabilities.fluid.VariableCapacityFluidTank;
import mekanism.common.capabilities.heat.BasicHeatCapacitor;
Expand Down Expand Up @@ -40,12 +39,10 @@
import mekanism.generators.common.slot.FluidFuelInventorySlot;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.neoforged.neoforge.capabilities.BlockCapabilityCache;
import net.neoforged.neoforge.common.CommonHooks;
import net.neoforged.neoforge.fluids.FluidStack;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -73,8 +70,6 @@ public class TileEntityHeatGenerator extends TileEntityGenerator {
private FloatingLong producingEnergy = FloatingLong.ZERO;
private double lastTransferLoss;
private double lastEnvironmentLoss;
@Nullable
private BlockCapabilityCache<IHeatHandler, @Nullable Direction> adjacentHeatCap;

@WrappingComputerMethod(wrapper = ComputerHeatCapacitorWrapper.class, methodNames = "getTemperature", docPlaceholder = "generator")
BasicHeatCapacitor heatCapacitor;
Expand Down Expand Up @@ -198,13 +193,7 @@ public HeatTransfer simulate() {
@Nullable
@Override
public IHeatHandler getAdjacent(@NotNull Direction side) {
if (side == Direction.DOWN) {
if (adjacentHeatCap == null) {
adjacentHeatCap = BlockCapabilityCache.create(Capabilities.HEAT, (ServerLevel) level, worldPosition.relative(side), side.getOpposite());
}
return adjacentHeatCap.getCapability();
}
return null;
return side == Direction.DOWN ? getAdjacentUnchecked(side) : null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import mekanism.api.Action;
import mekanism.api.IContentsListener;
import mekanism.api.chemical.gas.Gas;
Expand Down Expand Up @@ -43,6 +44,7 @@ public class TileEntityFissionReactorPort extends TileEntityFissionReactorCasing
Capabilities.GAS.block(),
Capabilities.FLUID.block()
);
private final Predicate<FissionPortMode> MODE_MATCHES = mode -> mode == getMode();

public TileEntityFissionReactorPort(BlockPos pos, BlockState state) {
super(GeneratorsBlocks.FISSION_REACTOR_PORT, pos, state);
Expand All @@ -55,8 +57,7 @@ public IHeatHandler getAdjacent(@NotNull Direction side) {
if (WorldUtils.getBlockState(level, getBlockPos().relative(side))
.filter(state -> !state.is(GeneratorsBlocks.FISSION_REACTOR_PORT.getBlock()))
.isPresent()) {
return adjacentHeatCaps.computeIfAbsent(side, s -> BlockCapabilityCache.create(Capabilities.HEAT, (ServerLevel) level, worldPosition.relative(s),
s.getOpposite())).getCapability();
return getAdjacentUnchecked(side);
}
}
return null;
Expand Down Expand Up @@ -89,10 +90,12 @@ public boolean persists(ContainerType<?, ?, ?> type) {
}

public void addGasTargetCapability(List<AdvancedCapabilityOutputTarget<IGasHandler, FissionPortMode>> outputTargets, Direction side) {
outputTargets.add(new AdvancedCapabilityOutputTarget<>(
capabilityCaches.computeIfAbsent(side, s -> Capabilities.GAS.createCache((ServerLevel) level, worldPosition.relative(s), s.getOpposite())),
mode -> mode == getMode()
));
BlockCapabilityCache<IGasHandler, @Nullable Direction> cache = capabilityCaches.get(side);
if (cache == null) {
cache = Capabilities.GAS.createCache((ServerLevel) level, worldPosition.relative(side), side.getOpposite());
capabilityCaches.put(side, cache);
}
outputTargets.add(new AdvancedCapabilityOutputTarget<>(cache, MODE_MATCHES));
}

@ComputerMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,21 @@ public boolean persists(ContainerType<?, ?, ?> type) {
}

public void addGasTargetCapability(List<CapabilityOutputTarget<IGasHandler>> outputTargets, Direction side) {
outputTargets.add(new CapabilityOutputTarget<>(
chemicalCapabilityCaches.computeIfAbsent(side, s -> Capabilities.GAS.createCache((ServerLevel) level, worldPosition.relative(s), s.getOpposite())),
this::getActive
));
BlockCapabilityCache<IGasHandler, @Nullable Direction> cache = chemicalCapabilityCaches.get(side);
if (cache == null) {
cache = Capabilities.GAS.createCache((ServerLevel) level, worldPosition.relative(side), side.getOpposite());
chemicalCapabilityCaches.put(side, cache);
}
outputTargets.add(new CapabilityOutputTarget<>(cache, this::getActive));
}

public void addEnergyTargetCapability(List<EnergyOutputTarget> outputTargets, Direction side) {
outputTargets.add(new EnergyOutputTarget(
energyCapabilityCaches.computeIfAbsent(side, s -> BlockEnergyCapabilityCache.create((ServerLevel) level, worldPosition.relative(s), s.getOpposite())),
this::getActive
));
BlockEnergyCapabilityCache cache = energyCapabilityCaches.get(side);
if (cache == null) {
cache = BlockEnergyCapabilityCache.create((ServerLevel) level, worldPosition.relative(side), side.getOpposite());
energyCapabilityCaches.put(side, cache);
}
outputTargets.add(new EnergyOutputTarget(cache, this::getActive));
}

@Nullable
Expand All @@ -99,8 +103,7 @@ public IHeatHandler getAdjacent(@NotNull Direction side) {
if (WorldUtils.getBlockState(level, getBlockPos().relative(side))
.filter(state -> !state.is(GeneratorsBlocks.FUSION_REACTOR_PORT.getBlock()))
.isPresent()) {
return adjacentHeatCaps.computeIfAbsent(side, s -> BlockCapabilityCache.create(Capabilities.HEAT, (ServerLevel) level, worldPosition.relative(s),
s.getOpposite())).getCapability();
return getAdjacentUnchecked(side);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ public boolean persists(ContainerType<?, ?, ?> type) {
}

public void addEnergyTargetCapability(List<BlockEnergyCapabilityCache> outputTargets, Direction side) {
outputTargets.add(energyCapabilityCaches.computeIfAbsent(side, s -> BlockEnergyCapabilityCache.create((ServerLevel) level, worldPosition.relative(s), s.getOpposite())));
BlockEnergyCapabilityCache cache = energyCapabilityCaches.get(side);
if (cache == null) {
cache = BlockEnergyCapabilityCache.create((ServerLevel) level, worldPosition.relative(side), side.getOpposite());
energyCapabilityCaches.put(side, cache);
}
outputTargets.add(cache);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public boolean persists(ContainerType<?, ?, ?> type) {
}

public void addFluidTargetCapability(List<BlockCapabilityCache<IFluidHandler, @Nullable Direction>> outputTargets, Direction side) {
outputTargets.add(capabilityCaches.computeIfAbsent(side, s -> Capabilities.FLUID.createCache((ServerLevel) level, worldPosition.relative(s), s.getOpposite())));
BlockCapabilityCache<IFluidHandler, @Nullable Direction> cache = capabilityCaches.get(side);
if (cache == null) {
cache = Capabilities.FLUID.createCache((ServerLevel) level, worldPosition.relative(side), side.getOpposite());
capabilityCaches.put(side, cache);
}
outputTargets.add(cache);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ public void renderForeground(GuiGraphics guiGraphics, int mouseX, int mouseY) {
Upgrade selectedType = scrollList.getSelection();
int amount = tile.getComponent().getUpgrades(selectedType);
int textY = relativeY + 20;
WrappedTextRenderer textRenderer = upgradeTypeData.computeIfAbsent(selectedType, type -> new WrappedTextRenderer(this, MekanismLang.UPGRADE_TYPE.translate(type)));
WrappedTextRenderer textRenderer = upgradeTypeData.get(selectedType);
if (textRenderer == null) {
textRenderer = new WrappedTextRenderer(this, MekanismLang.UPGRADE_TYPE.translate(selectedType));
upgradeTypeData.put(selectedType, textRenderer);
}
int lines = textRenderer.renderWithScale(guiGraphics, relativeX + 74, textY, screenTextColor(), 56, 0.6F);
textY += 6 * lines + 2;
drawTextWithScale(guiGraphics, MekanismLang.UPGRADE_COUNT.translate(amount, selectedType.getMax()), relativeX + 74, textY, screenTextColor(), 0.6F);
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/mekanism/client/gui/item/GuiSeismicReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,16 @@ protected void drawForegroundText(@NotNull GuiGraphics guiGraphics, int mouseX,
Block block = blockList.get(currentLayer).block();
Component displayName = block.getName();
drawTextScaledBound(guiGraphics, displayName, 10, 16, screenTextColor(), 57);
frequency = frequencies.computeIfAbsent(block, (Block b) -> (int) blockList.stream().filter(info -> info.state().is(b)).count());
if (frequencies.containsKey(block)) {
frequency = frequencies.getInt(block);
} else {
for (BlockInfo info : blockList) {
if (info.state().is(block)) {
frequency++;
}
}
frequencies.put(block, frequency);
}
}
drawTextScaledBound(guiGraphics, MekanismLang.ABUNDANCY.translate(frequency), 10, 26, screenTextColor(), 57);
super.drawForegroundText(guiGraphics, mouseX, mouseY);
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/mekanism/client/model/BaseModelCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,16 @@ protected void setup(RegisterAdditional event) {
}

public BakedModel bake(IGeometryBakingContext config) {
return bakedMap.computeIfAbsent(config, c -> {
BakedModel bakedModel = bakedMap.get(config);
if (bakedModel == null) {
ModelBaker baker = Minecraft.getInstance().getModelManager().getModelBakery().new ModelBakerImpl(
(modelLoc, material) -> material.sprite(),
rl
);
return model.bake(c, baker, Material::sprite, BlockModelRotation.X0_Y0, ItemOverrides.EMPTY, rl);
});
bakedModel = model.bake(config, baker, Material::sprite, BlockModelRotation.X0_Y0, ItemOverrides.EMPTY, rl);
bakedMap.put(config, bakedModel);
}
return bakedModel;
}

public IUnbakedGeometry<?> getModel() {
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/mekanism/client/model/MekanismModelCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,20 @@ public void reloadCallback(Runnable callback) {

@Nullable
public BakedModel getRobitSkin(@NotNull SkinLookup skinLookup) {
JSONModelData data = ROBIT_SKINS.computeIfAbsent(skinLookup.location(), skinName -> {
ResourceLocation skinName = skinLookup.location();
JSONModelData data;
if (ROBIT_SKINS.containsKey(skinName)) {
data = ROBIT_SKINS.get(skinName);
} else {
ResourceLocation customModel = skinLookup.skin().customModel();
if (customModel != null) {
//If multiple skins make use of the same custom model, have them all point at the same model data object
return CUSTOM_ROBIT_MODELS.computeIfAbsent(customModel, this::registerJSONAndBake);
data = CUSTOM_ROBIT_MODELS.computeIfAbsent(customModel, this::registerJSONAndBake);
} else {
data = null;
}
return null;
});
ROBIT_SKINS.put(skinName, data);
}
return data == null ? BASE_ROBIT : data.getBakedModel();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,12 @@ record TrackedIngredients<SLOT extends RVRecipeSlot>(SLOT view, Set<HashedItem>
if (elements == 1) {
shuffleLookup.put(source, Collections.singletonList(actualSources));
} else {
shuffleLookup.computeIfAbsent(source, s -> new ArrayList<>(elements)).add(actualSources);
List<List<SingularHashedItemSource>> list = shuffleLookup.get(source);
if (list == null) {
list = new ArrayList<>(elements);
shuffleLookup.put(source, list);
}
list.add(actualSources);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,13 @@ private IDrawable getOverlay(GuiGauge<?> gauge) {
if (overlayLookup == null) {
overlayLookup = new EnumMap<>(GaugeOverlay.class);
}
return overlayLookup.computeIfAbsent(gauge.getGaugeOverlay(), overlay -> createDrawable(guiHelper, overlay));
GaugeOverlay overlay = gauge.getGaugeOverlay();
IDrawable drawable = overlayLookup.get(overlay);
if (drawable == null) {
drawable = createDrawable(guiHelper, overlay);
overlayLookup.put(overlay, drawable);
}
return drawable;
}

private IDrawable createDrawable(IGuiHelper helper, GaugeOverlay gaugeOverlay) {
Expand Down
Loading

0 comments on commit eb36b37

Please sign in to comment.