Skip to content

Commit

Permalink
Remove more capturing lambdas from various forEach calls
Browse files Browse the repository at this point in the history
  • Loading branch information
pupnewfster committed Apr 4, 2024
1 parent 303b59c commit aca4444
Show file tree
Hide file tree
Showing 25 changed files with 230 additions and 132 deletions.
Expand Up @@ -56,7 +56,9 @@ public Builder advancement() {
protected final void buildRecipes(RecipeOutput output) {
WrapperRecipeOutput trackingConsumer = new WrapperRecipeOutput(output, existingFileHelper);
addRecipes(trackingConsumer);
getSubRecipeProviders().forEach(subRecipeProvider -> subRecipeProvider.addRecipes(trackingConsumer));
for (ISubRecipeProvider subRecipeProvider : getSubRecipeProviders()) {
subRecipeProvider.addRecipes(trackingConsumer);
}
}

protected abstract void addRecipes(RecipeOutput output);
Expand Down
Expand Up @@ -112,7 +112,9 @@ protected void addRecipes(RecipeOutput consumer) {
addMiscRecipes(consumer);
addGearModuleRecipes(consumer);
addLateGameRecipes(consumer);
compatProviders.forEach(compatProvider -> compatProvider.addRecipes(consumer));
for (ISubRecipeProvider compatProvider : compatProviders) {
compatProvider.addRecipes(consumer);
}
}

@Override
Expand Down
12 changes: 8 additions & 4 deletions src/datagen/main/java/mekanism/common/tag/BaseTagProvider.java
Expand Up @@ -7,6 +7,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
Expand Down Expand Up @@ -106,18 +107,21 @@ public CompletableFuture<?> run(@NotNull CachedOutput cache) {
}
}
List<CompletableFuture<?>> futures = new ArrayList<>();
supportedTagTypes.forEach((registry, tagTypeMap) -> {
for (Map.Entry<ResourceKey<? extends Registry<?>>, Map<TagKey<?>, TagBuilder>> entry : supportedTagTypes.entrySet()) {
Map<TagKey<?>, TagBuilder> tagTypeMap = entry.getValue();
if (!tagTypeMap.isEmpty()) {
//Create a dummy provider and pass all our collected data through to it
futures.add(new TagsProvider(output, registry, lookupProvider, modid, existingFileHelper) {
futures.add(new TagsProvider(output, entry.getKey(), lookupProvider, modid, existingFileHelper) {
@Override
protected void addTags(@NotNull HolderLookup.Provider lookupProvider) {
//Add each tag builder to the wrapped provider's builder
tagTypeMap.forEach((tag, tagBuilder) -> builders.put(tag.location(), tagBuilder));
for (Map.Entry<TagKey<?>, TagBuilder> e : tagTypeMap.entrySet()) {
builders.put(e.getKey().location(), e.getValue());
}
}
}.run(cache));
}
});
}
return CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new));
});
}
Expand Down
@@ -1,6 +1,8 @@
package mekanism.generators.common.registries;

import mekanism.api.functions.TriConsumer;
import mekanism.common.block.attribute.AttributeCustomSelectionBox;
import mekanism.common.block.attribute.AttributeHasBounding.HandleBoundingBlock;
import mekanism.common.block.attribute.AttributeMultiblock;
import mekanism.common.block.attribute.AttributeParticleFX;
import mekanism.common.block.attribute.AttributeUpgradeSupport;
Expand Down Expand Up @@ -41,7 +43,10 @@
import mekanism.generators.common.tile.turbine.TileEntityTurbineRotor;
import mekanism.generators.common.tile.turbine.TileEntityTurbineValve;
import mekanism.generators.common.tile.turbine.TileEntityTurbineVent;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;

public class GeneratorsBlockTypes {

Expand Down Expand Up @@ -95,9 +100,14 @@ private GeneratorsBlockTypes() {
.with(AttributeCustomSelectionBox.JAVA)
.withSound(GeneratorsSounds.WIND_GENERATOR)
.with(AttributeUpgradeSupport.MUFFLING_ONLY)
.withBounding((pos, state, builder) -> {
for (int i = 0; i < 4; i++) {
builder.add(pos.above(i + 1));
.withBounding(new HandleBoundingBlock() {
@Override
public <DATA> void handle(Level level, BlockPos pos, BlockState state, DATA data, TriConsumer<Level, BlockPos, DATA> consumer) {
BlockPos.MutableBlockPos mutable = new BlockPos.MutableBlockPos();
for (int i = 0; i < 4; i++) {
mutable.setWithOffset(pos, 0, i + 1, 0);
consumer.accept(level, mutable, data);
}
}
})
.withComputerSupport("windGenerator")
Expand All @@ -122,11 +132,16 @@ private GeneratorsBlockTypes() {
.withCustomShape(BlockShapes.ADVANCED_SOLAR_GENERATOR)
.withSound(GeneratorsSounds.SOLAR_GENERATOR)
.with(AttributeUpgradeSupport.MUFFLING_ONLY)
.withBounding((pos, state, builder) -> {
builder.add(pos.above());
for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) {
builder.add(pos.offset(x, 2, z));
.withBounding(new HandleBoundingBlock() {
@Override
public <DATA> void handle(Level level, BlockPos pos, BlockState state, DATA data, TriConsumer<Level, BlockPos, DATA> consumer) {
BlockPos.MutableBlockPos mutable = new BlockPos.MutableBlockPos(pos.getX(), pos.getY() + 1, pos.getZ());
consumer.accept(level, mutable, data);
for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) {
mutable.setWithOffset(pos, x, 2, z);
consumer.accept(level, mutable, data);
}
}
}
})
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/mekanism/client/gui/GuiMekanism.java
Expand Up @@ -313,8 +313,9 @@ record PreviousElement(int index, GuiElement element, boolean wasFocus) {
}

//And set any persistent data that we stored
prevElements.forEach(e -> {
if (e.index() < children().size()) {
int childCount = children().size();
for (PreviousElement e : prevElements) {
if (e.index() < childCount) {
GuiEventListener widget = children().get(e.index());
// we're forced to assume that the children list is the same before and after the resize.
// for verification, we run a lightweight class equality check
Expand All @@ -327,7 +328,7 @@ record PreviousElement(int index, GuiElement element, boolean wasFocus) {
}
}
}
});
}
}

@Override
Expand Down Expand Up @@ -475,7 +476,9 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
public boolean mouseReleased(double mouseX, double mouseY, int button) {
if (hasClicked) {
// always pass mouse released events to windows for drag checks
windows.forEach(w -> w.onRelease(mouseX, mouseY));
for (GuiWindow w : windows) {
w.onRelease(mouseX, mouseY);
}
return super.mouseReleased(mouseX, mouseY, button);
}
return false;
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/mekanism/client/gui/element/GuiElement.java
Expand Up @@ -110,9 +110,13 @@ public void transferToNewGui(IGuiWrapper gui) {

private void transferToNewGuiInternal(IGuiWrapper gui) {
guiObj = gui;
children.forEach(child -> child.transferToNewGuiInternal(gui));
for (GuiElement guiElement : children) {
guiElement.transferToNewGuiInternal(gui);
}
//Transfer position only children as well
positionOnlyChildren.forEach(child -> child.transferToNewGuiInternal(gui));
for (GuiElement child : positionOnlyChildren) {
child.transferToNewGuiInternal(gui);
}
}

protected <ELEMENT extends GuiElement> ELEMENT addChild(ELEMENT element) {
Expand Down Expand Up @@ -175,8 +179,12 @@ public void tick() {
*/
public void resize(int prevLeft, int prevTop, int left, int top) {
setPosition(getX() - prevLeft + left, getY() - prevTop + top);
children.forEach(child -> child.resize(prevLeft, prevTop, left, top));
positionOnlyChildren.forEach(child -> child.resize(prevLeft, prevTop, left, top));
for (GuiElement guiElement : children) {
guiElement.resize(prevLeft, prevTop, left, top);
}
for (GuiElement child : positionOnlyChildren) {
child.resize(prevLeft, prevTop, left, top);
}
}

public boolean childrenContainsElement(Predicate<GuiElement> checker) {
Expand Down Expand Up @@ -424,7 +432,9 @@ public boolean charTyped(char c, int keyCode) {
public void onDrag(double mouseX, double mouseY, double deltaX, double deltaY) {
//TODO - 1.20.4: For this and onRelease etc do we want to somewhat do something like ContainerEventHandler does
// where it only does the focused element?
children.forEach(element -> element.onDrag(mouseX, mouseY, deltaX, deltaY));
for (GuiElement element : children) {
element.onDrag(mouseX, mouseY, deltaX, deltaY);
}
super.onDrag(mouseX, mouseY, deltaX, deltaY);
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/mekanism/client/model/BaseModelCache.java
Expand Up @@ -55,7 +55,9 @@ public void onBake(BakingCompleted evt) {
}

public void setup(RegisterAdditional event) {
modelMap.values().forEach(mekanismModelData -> mekanismModelData.setup(event));
for (MekanismModelData mekanismModelData : modelMap.values()) {
mekanismModelData.setup(event);
}
}

protected OBJModelData registerOBJ(String path) {
Expand Down
Expand Up @@ -177,8 +177,12 @@ public void draw(RECIPE recipe, IRecipeSlotsView recipeSlotsView, GuiGraphics gu

protected void renderElements(RECIPE recipe, IRecipeSlotsView recipeSlotsView, GuiGraphics guiGraphics, int x, int y) {
PoseStack pose = guiGraphics.pose();
guiElements.forEach(e -> e.renderShifted(guiGraphics, x, y, 0));
guiElements.forEach(e -> e.onDrawBackground(guiGraphics, x, y, 0));
for (GuiElement guiElement : guiElements) {
guiElement.renderShifted(guiGraphics, x, y, 0);
}
for (GuiElement e : guiElements) {
e.onDrawBackground(guiGraphics, x, y, 0);
}
//Note: We don't care that onRenderForeground updates the maxZOffset in the mekanism gui as that is just used for rendering windows
// and as our categories don't support windows we don't need to worry about that
int zOffset = 200;
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/mekanism/client/render/RenderTickHandler.java
Expand Up @@ -494,18 +494,23 @@ public void onBlockHover(RenderHighlightEvent.Block event) {
}

private void renderQuadsWireFrame(BlockState state, VertexConsumer buffer, Matrix4f matrix, RandomSource rand, int red, int green, int blue, int alpha) {
List<Vertex[]> allVertices = cachedWireFrames.computeIfAbsent(state, s -> {
BakedModel bakedModel = Minecraft.getInstance().getBlockRenderer().getBlockModel(s);
List<Vertex[]> vertices = cachedWireFrames.get(state);
if (vertices == null) {
BakedModel bakedModel = Minecraft.getInstance().getBlockRenderer().getBlockModel(state);
//TODO: Eventually we may want to add support for Model data and maybe render type
ModelData modelData = ModelData.EMPTY;
List<Vertex[]> vertices = new ArrayList<>();
vertices = new ArrayList<>();
for (Direction direction : EnumUtils.DIRECTIONS) {
QuadUtils.unpack(bakedModel.getQuads(s, direction, rand, modelData, null)).stream().map(Quad::getVertices).forEach(vertices::add);
for (Quad quad : QuadUtils.unpack(bakedModel.getQuads(state, direction, rand, modelData, null))) {
vertices.add(quad.getVertices());
}
}
for (Quad quad : QuadUtils.unpack(bakedModel.getQuads(state, null, rand, modelData, null))) {
vertices.add(quad.getVertices());
}
QuadUtils.unpack(bakedModel.getQuads(s, null, rand, modelData, null)).stream().map(Quad::getVertices).forEach(vertices::add);
return vertices;
});
renderVertexWireFrame(allVertices, buffer, matrix, red, green, blue, alpha);
cachedWireFrames.put(state, vertices);
}
renderVertexWireFrame(vertices, buffer, matrix, red, green, blue, alpha);
}

public static void renderVertexWireFrame(List<Vertex[]> allVertices, VertexConsumer buffer, Matrix4f matrix, int red, int green, int blue, int alpha) {
Expand Down
@@ -1,7 +1,5 @@
package mekanism.common.block.attribute;

import java.util.stream.Stream;
import java.util.stream.Stream.Builder;
import mekanism.api.functions.TriConsumer;
import mekanism.common.Mekanism;
import mekanism.common.block.BlockBounding;
Expand All @@ -20,14 +18,21 @@
// at some point that should be cleaned up some
public class AttributeHasBounding implements Attribute {

private final TriConsumer<BlockPos, BlockState, Builder<BlockPos>> boundingPositions;
public static final AttributeHasBounding ABOVE_ONLY = new AttributeHasBounding(new HandleBoundingBlock() {
@Override
public <DATA> void handle(Level level, BlockPos pos, BlockState state, DATA data, TriConsumer<Level, BlockPos, DATA> consumer) {
consumer.accept(level, pos.above(), data);
}
});

public AttributeHasBounding(TriConsumer<BlockPos, BlockState, Builder<BlockPos>> boundingPositions) {
this.boundingPositions = boundingPositions;
private final HandleBoundingBlock boundingPosHandlers;

public AttributeHasBounding(HandleBoundingBlock boundingPosHandlers) {
this.boundingPosHandlers = boundingPosHandlers;
}

public void removeBoundingBlocks(Level level, BlockPos pos, BlockState state) {
getPositions(pos, state).forEach(p -> {
public void removeBoundingBlocks(Level world, BlockPos pos, BlockState state) {
boundingPosHandlers.handle(world, pos, state, null, (level, p, ignored) -> {
BlockState boundingState = level.getBlockState(p);
if (!boundingState.isAir()) {
//The state might be air if we broke a bounding block first
Expand All @@ -41,25 +46,28 @@ public void removeBoundingBlocks(Level level, BlockPos pos, BlockState state) {
});
}

public void placeBoundingBlocks(Level level, BlockPos orig, BlockState state) {
getPositions(orig, state).forEach(boundingLocation -> {
public void placeBoundingBlocks(Level world, BlockPos orig, BlockState state) {
boundingPosHandlers.handle(world, orig, state, orig, (level, boundingLocation, data) -> {
BlockBounding boundingBlock = MekanismBlocks.BOUNDING_BLOCK.getBlock();
BlockState newState = BlockStateHelper.getStateForPlacement(boundingBlock, boundingBlock.defaultBlockState(), level, boundingLocation, null, Direction.NORTH);
level.setBlock(boundingLocation, newState, Block.UPDATE_ALL);
if (!level.isClientSide()) {
TileEntityBoundingBlock tile = WorldUtils.getTileEntity(TileEntityBoundingBlock.class, level, boundingLocation);
if (tile != null) {
tile.setMainLocation(orig);
tile.setMainLocation(data);
} else {
Mekanism.logger.warn("Unable to find Bounding Block Tile at: {}", boundingLocation);
}
}
});
}

public Stream<BlockPos> getPositions(BlockPos pos, BlockState state) {
Stream.Builder<BlockPos> builder = Stream.builder();
boundingPositions.accept(pos, state, builder);
return builder.build();
public <DATA> void handle(Level level, BlockPos pos, BlockState state, DATA data, TriConsumer<Level, BlockPos, DATA> consumer) {
boundingPosHandlers.handle(level, pos, state, data, consumer);
}

public interface HandleBoundingBlock {

<DATA> void handle(Level level, BlockPos pos, BlockState state, DATA data, TriConsumer<Level, BlockPos, DATA> consumer);
}
}
4 changes: 3 additions & 1 deletion src/main/java/mekanism/common/block/prefab/BlockBase.java
Expand Up @@ -51,7 +51,9 @@ public BlockBase(TYPE type, BlockBehaviour.Properties properties) {

private static <TYPE extends BlockType> BlockBehaviour.Properties hack(TYPE type, BlockBehaviour.Properties props) {
cacheType = type;
type.getAll().forEach(a -> a.adjustProperties(props));
for (Attribute a : type.getAll()) {
a.adjustProperties(props);
}
return props;
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/mekanism/common/command/ChunkCommand.java
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ColumnPos;
import net.minecraft.server.level.ServerChunkCache;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.ChunkPos;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.level.ChunkEvent;
Expand Down Expand Up @@ -129,7 +130,9 @@ private static void handleChunkEvent(ChunkEvent event, ILangEntry direction) {
ChunkPos pos = event.getChunk().getPos();
if (chunkWatchers.contains(pos.toLong())) {
Component message = direction.translateColored(EnumColor.GRAY, EnumColor.INDIGO, getPosition(pos));
event.getLevel().players().forEach(player -> player.sendSystemMessage(message));
for (Player player : event.getLevel().players()) {
player.sendSystemMessage(message);
}
}
}

Expand Down
Expand Up @@ -3,23 +3,20 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream.Builder;
import mekanism.api.functions.TriConsumer;
import mekanism.api.text.ILangEntry;
import mekanism.api.tier.ITier;
import mekanism.common.block.attribute.Attribute;
import mekanism.common.block.attribute.AttributeCustomShape;
import mekanism.common.block.attribute.AttributeHasBounding;
import mekanism.common.block.attribute.AttributeHasBounding.HandleBoundingBlock;
import mekanism.common.block.attribute.AttributeMultiblock;
import mekanism.common.block.attribute.AttributeSideConfig;
import mekanism.common.block.attribute.Attributes.AttributeComputerIntegration;
import mekanism.common.block.attribute.Attributes.AttributeLight;
import mekanism.common.block.attribute.Attributes.AttributeMobSpawn;
import mekanism.common.block.interfaces.ITypeBlock;
import mekanism.common.lib.transmitter.TransmissionType;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -117,8 +114,8 @@ public final T with(Attribute... attrs) {
return self();
}

public final T withBounding(TriConsumer<BlockPos, BlockState, Builder<BlockPos>> boundingPositions) {
return with(new AttributeHasBounding(boundingPositions));
public final T withBounding(HandleBoundingBlock boundingPosHandlers) {
return with(new AttributeHasBounding(boundingPosHandlers));
}

@SafeVarargs
Expand Down

0 comments on commit aca4444

Please sign in to comment.