Skip to content

Commit 62755b4

Browse files
committed
Make ComponentStorage iterable
1 parent c7fe6ae commit 62755b4

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

src/client/java/aztech/modern_industrialization/machines/gui/MachineScreen.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public class MachineScreen extends MIHandledScreen<MachineMenuClient> implements
6464
public MachineScreen(MachineMenuClient handler, Inventory inventory, Component title) {
6565
super(handler, inventory, title);
6666

67-
handler.components.forEach(component -> renderers.add(component.createRenderer(this)));
67+
for (GuiComponentClient component : handler.components) {
68+
renderers.add(component.createRenderer(this));
69+
}
6870

6971
this.imageHeight = handler.guiParams.backgroundHeight;
7072
this.imageWidth = handler.guiParams.backgroundWidth;

src/main/java/aztech/modern_industrialization/machines/ComponentStorage.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,24 @@
2626
import aztech.modern_industrialization.machines.gui.GuiComponent;
2727
import java.util.ArrayList;
2828
import java.util.Collections;
29+
import java.util.Iterator;
2930
import java.util.List;
3031
import java.util.Optional;
3132
import java.util.function.BiConsumer;
3233
import java.util.function.Consumer;
3334
import java.util.function.Function;
3435
import net.minecraft.resources.ResourceLocation;
36+
import org.jetbrains.annotations.NotNull;
3537

36-
public sealed class ComponentStorage<C> permits ComponentStorage.GuiServer, ComponentStorage.Server {
38+
public sealed class ComponentStorage<C> implements Iterable<C> permits ComponentStorage.GuiServer, ComponentStorage.Server {
3739
protected final List<C> components = new ArrayList<>();
3840

41+
@NotNull
42+
@Override
43+
public Iterator<C> iterator() {
44+
return components.iterator();
45+
}
46+
3947
@SafeVarargs
4048
public final void register(C... components) {
4149
Collections.addAll(this.components, components);
@@ -56,10 +64,6 @@ public final C get(int index) {
5664
return components.get(index);
5765
}
5866

59-
public final void forEach(Consumer<C> action) {
60-
components.forEach(action);
61-
}
62-
6367
public final void forEachIndexed(BiConsumer<Integer, C> action) {
6468
for (int i = 0; i < components.size(); i++) {
6569
action.accept(i, components.get(i));

src/main/java/aztech/modern_industrialization/machines/MachineBlockEntity.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ public final void writeScreenOpeningData(RegistryFriendlyByteBuf buf) {
139139
inv.fluidPositions.write(buf);
140140
buf.writeInt(guiComponents.size());
141141
// Write components
142-
guiComponents.forEach(component -> {
142+
for (GuiComponent.Server component : guiComponents) {
143143
buf.writeResourceLocation(component.getId());
144144
component.writeInitialData(buf);
145-
});
145+
}
146146
// Write GUI params
147147
guiParams.write(buf);
148148
}
@@ -201,13 +201,17 @@ public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
201201
CompoundTag tag = new CompoundTag();
202202
tag.putBoolean("remesh", syncCausesRemesh);
203203
syncCausesRemesh = false;
204-
icomponents.forEach(component -> component.writeClientNbt(tag, registries));
204+
for (IComponent component : icomponents) {
205+
component.writeClientNbt(tag, registries);
206+
}
205207
return tag;
206208
}
207209

208210
@Override
209211
public final void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
210-
icomponents.forEach(component -> component.writeNbt(tag, registries));
212+
for (IComponent component : icomponents) {
213+
component.writeNbt(tag, registries);
214+
}
211215
}
212216

213217
@Override
@@ -217,10 +221,14 @@ public final void loadAdditional(CompoundTag tag, HolderLookup.Provider registri
217221

218222
public final void load(CompoundTag tag, HolderLookup.Provider registries, boolean isUpgradingMachine) {
219223
if (!tag.contains("remesh")) {
220-
icomponents.forEach(component -> component.readNbt(tag, registries, isUpgradingMachine));
224+
for (IComponent component : icomponents) {
225+
component.readNbt(tag, registries, isUpgradingMachine);
226+
}
221227
} else {
222228
boolean forceChunkRemesh = tag.getBoolean("remesh");
223-
icomponents.forEach(component -> component.readClientNbt(tag, registries));
229+
for (IComponent component : icomponents) {
230+
component.readClientNbt(tag, registries);
231+
}
224232
if (forceChunkRemesh) {
225233
WorldHelper.forceChunkRemesh(level, worldPosition);
226234
requestModelDataUpdate();

src/main/java/aztech/modern_industrialization/machines/gui/MachineMenuCommon.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ public abstract class MachineMenuCommon extends ConfigurableScreenHandler implem
5353
}
5454

5555
// Gui components first (we want to prioritize them with shift click)
56-
guiComponents.forEach(component -> component.setupMenu(this));
56+
for (GuiComponent.Common component : guiComponents) {
57+
component.setupMenu(this);
58+
}
5759

5860
// Configurable slots
5961
for (int i = 0; i < inventory.getItemStacks().size(); ++i) {

src/main/java/aztech/modern_industrialization/machines/gui/MachineMenuServer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public MachineMenuServer(int syncId, Inventory playerInventory, MachineBlockEnti
4242
super(syncId, playerInventory, blockEntity.getInventory(), guiParams, blockEntity.getGuiComponents());
4343
this.blockEntity = blockEntity;
4444
trackedData = new ArrayList<>();
45-
blockEntity.getGuiComponents().forEach(component -> trackedData.add(component.copyData()));
45+
for (GuiComponent.Server component : blockEntity.getGuiComponents()) {
46+
trackedData.add(component.copyData());
47+
}
4648
}
4749

4850
@Override

0 commit comments

Comments
 (0)