Skip to content

Commit 8347662

Browse files
authored
Improve gui constructing for the MachineBuilder system (#131)
1 parent f82712b commit 8347662

17 files changed

+984
-260
lines changed

src/main/java/net/swedz/tesseract/neoforge/compat/mi/helper/MachineInventoryHelper.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@
1616

1717
public final class MachineInventoryHelper
1818
{
19+
public static SlotPositions.Builder combine(SlotPositions... positions)
20+
{
21+
var combined = new SlotPositions.Builder();
22+
for(var group : positions)
23+
{
24+
for(int i = 0; i < group.size(); i++)
25+
{
26+
combined.addSlot(group.getX(i), group.getY(i));
27+
}
28+
}
29+
return combined;
30+
}
31+
1932
public static MachineInventoryComponent buildInventoryComponent(
2033
int itemInputCount, int itemOutputCount, int fluidInputCount, int fluidOutputCount,
2134
SlotPositions itemPositions, SlotPositions fluidPositions, int steamBuckets, int ioBucketCapacity

src/main/java/net/swedz/tesseract/neoforge/compat/mi/hook/context/listener/MultiblockMachinesMIHookContext.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.MachineBuilder;
1919
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.SpecialMachineBuilder;
2020
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockEntityFactory;
21+
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockEntityWithGuiFactory;
2122
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockFactory;
2223
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockRegistrators;
2324
import net.swedz.tesseract.neoforge.registry.holder.BlockWithItemHolder;
@@ -34,11 +35,16 @@ public MultiblockMachinesMIHookContext(MIHook hook)
3435
super(hook);
3536
}
3637

37-
public SpecialMachineBuilder builder(String name, String englishName, MachineBlockEntityFactory factory)
38+
public SpecialMachineBuilder builder(String name, String englishName, MachineBlockEntityWithGuiFactory factory)
3839
{
3940
return MachineBuilder.special(hook, name, englishName, true, factory);
4041
}
4142

43+
public SpecialMachineBuilder builder(String name, String englishName, MachineBlockEntityFactory factory)
44+
{
45+
return this.builder(name, englishName, (bep, gui) -> factory.create(bep));
46+
}
47+
4248
@Deprecated(forRemoval = true)
4349
@SafeVarargs
4450
public final void register(String englishName, String name,

src/main/java/net/swedz/tesseract/neoforge/compat/mi/hook/context/listener/SingleBlockSpecialMachinesMIHookContext.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.MachineBuilder;
1515
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.SpecialMachineBuilder;
1616
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockEntityFactory;
17+
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockEntityWithGuiFactory;
1718
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockFactory;
1819
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockRegistrators;
1920
import net.swedz.tesseract.neoforge.registry.holder.BlockWithItemHolder;
@@ -29,11 +30,16 @@ public SingleBlockSpecialMachinesMIHookContext(MIHook hook)
2930
super(hook);
3031
}
3132

32-
public SpecialMachineBuilder builder(String name, String englishName, MachineBlockEntityFactory factory)
33+
public SpecialMachineBuilder builder(String name, String englishName, MachineBlockEntityWithGuiFactory factory)
3334
{
3435
return MachineBuilder.special(hook, name, englishName, false, factory);
3536
}
3637

38+
public SpecialMachineBuilder builder(String name, String englishName, MachineBlockEntityFactory factory)
39+
{
40+
return this.builder(name, englishName, (bep, gui) -> factory.create(bep));
41+
}
42+
3743
@Deprecated(forRemoval = true)
3844
@SafeVarargs
3945
public final void register(String englishName, String name,

src/main/java/net/swedz/tesseract/neoforge/compat/mi/machine/builder/MachineBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.google.common.collect.Lists;
77
import net.swedz.tesseract.neoforge.api.Assert;
88
import net.swedz.tesseract.neoforge.compat.mi.hook.MIHook;
9-
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockEntityFactory;
9+
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockEntityWithGuiFactory;
1010
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockFactory;
1111
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockHolderModifier;
1212
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockPropertiesModifier;
@@ -27,7 +27,7 @@ public static SingleBlockCraftingMachineBuilder singleBlockCrafting(MIHook hook,
2727
public static SpecialMachineBuilder special(MIHook hook,
2828
String name, String englishName,
2929
boolean isMultiblock,
30-
MachineBlockEntityFactory factory)
30+
MachineBlockEntityWithGuiFactory factory)
3131
{
3232
return new SpecialMachineBuilder(hook, name, englishName, isMultiblock, factory);
3333
}
Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
package net.swedz.tesseract.neoforge.compat.mi.machine.builder;
2+
3+
import aztech.modern_industrialization.compat.rei.machines.MachineCategoryParams;
4+
import aztech.modern_industrialization.compat.rei.machines.SteamMode;
5+
import aztech.modern_industrialization.machines.MachineBlockEntity;
6+
import aztech.modern_industrialization.machines.components.CrafterComponent;
7+
import aztech.modern_industrialization.machines.components.MachineInventoryComponent;
8+
import aztech.modern_industrialization.machines.gui.MachineGuiParameters;
9+
import aztech.modern_industrialization.machines.guicomponents.EnergyBar;
10+
import aztech.modern_industrialization.machines.guicomponents.ProgressBar;
11+
import aztech.modern_industrialization.machines.guicomponents.RecipeEfficiencyBar;
12+
import aztech.modern_industrialization.machines.recipe.MachineRecipeType;
13+
import net.minecraft.resources.ResourceLocation;
14+
import net.swedz.tesseract.neoforge.api.Assert;
15+
import net.swedz.tesseract.neoforge.compat.mi.hack.HackedMachineRegistrationHelper;
16+
import net.swedz.tesseract.neoforge.compat.mi.hook.MIHook;
17+
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineRecipePredicate;
18+
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.slots.MachineSlotConfiguration;
19+
20+
import java.util.function.Consumer;
21+
import java.util.function.Supplier;
22+
23+
/**
24+
* <p>Represents a GUI configuration for a machine.</p>
25+
*
26+
* <p>This is effectively immutable and should be treated as such. Whenever a modification is made, a copy is created
27+
* and the modification is applied to the copy, rather than the original. This allows for branching of GUI
28+
* configurations without modifying the original instance. That capability is important for being able to add a steam
29+
* input slot, for example, without the steam slot also being included in the electric machines.</p>
30+
*/
31+
public final class MachineGuiConfiguration
32+
{
33+
private final boolean isMultiblock;
34+
private final SteamMode steamMode;
35+
private final MachineRecipeType recipeType;
36+
37+
private int guiHeight = 166;
38+
private boolean lockButton = true;
39+
40+
private final MachineSlotConfiguration.Builder inventoryOnlySlots = new MachineSlotConfiguration.Builder();
41+
private final MachineSlotConfiguration.Builder slots = new MachineSlotConfiguration.Builder();
42+
43+
private ProgressBar.Parameters progressBar;
44+
private EnergyBar.Parameters energyBar;
45+
private RecipeEfficiencyBar.Parameters efficiencyBar;
46+
47+
private MachineRecipePredicate predicate = (recipe) -> true;
48+
49+
MachineGuiConfiguration(boolean isMultiblock, SteamMode steamMode, MachineRecipeType recipeType)
50+
{
51+
this.isMultiblock = isMultiblock;
52+
this.steamMode = steamMode;
53+
this.recipeType = recipeType;
54+
}
55+
56+
private MachineGuiConfiguration copy()
57+
{
58+
var copy = new MachineGuiConfiguration(isMultiblock, steamMode, recipeType);
59+
copy.guiHeight = guiHeight;
60+
copy.lockButton = lockButton;
61+
copy.inventoryOnlySlots.append(inventoryOnlySlots);
62+
copy.slots.append(slots);
63+
copy.progressBar = progressBar;
64+
copy.energyBar = energyBar;
65+
copy.efficiencyBar = efficiencyBar;
66+
copy.predicate = predicate;
67+
return copy;
68+
}
69+
70+
public boolean isMultiblock()
71+
{
72+
return isMultiblock;
73+
}
74+
75+
public SteamMode getSteamMode()
76+
{
77+
return steamMode;
78+
}
79+
80+
public MachineRecipeType getRecipeType()
81+
{
82+
return recipeType;
83+
}
84+
85+
public int getGuiHeight()
86+
{
87+
return guiHeight;
88+
}
89+
90+
public boolean hasLockButton()
91+
{
92+
return lockButton;
93+
}
94+
95+
public MachineSlotConfiguration getInventoryOnlySlots()
96+
{
97+
return inventoryOnlySlots.build();
98+
}
99+
100+
public MachineSlotConfiguration getSlots()
101+
{
102+
return slots.build();
103+
}
104+
105+
public ProgressBar.Parameters getProgressBar()
106+
{
107+
return progressBar;
108+
}
109+
110+
public EnergyBar.Parameters getEnergyBar()
111+
{
112+
return energyBar;
113+
}
114+
115+
public RecipeEfficiencyBar.Parameters getEfficiencyBar()
116+
{
117+
return efficiencyBar;
118+
}
119+
120+
public MachineRecipePredicate getPredicate()
121+
{
122+
return predicate;
123+
}
124+
125+
public MachineGuiConfiguration guiHeight(int guiHeight)
126+
{
127+
var copy = this.copy();
128+
copy.guiHeight = guiHeight;
129+
return copy;
130+
}
131+
132+
public MachineGuiConfiguration lockButton(boolean lockButton)
133+
{
134+
var copy = this.copy();
135+
copy.lockButton = lockButton;
136+
return copy;
137+
}
138+
139+
public MachineGuiConfiguration inventoryOnlySlots(Consumer<MachineSlotConfiguration.Builder> builder)
140+
{
141+
var copy = this.copy();
142+
builder.accept(copy.inventoryOnlySlots);
143+
return copy;
144+
}
145+
146+
public MachineGuiConfiguration slots(Consumer<MachineSlotConfiguration.Builder> builder)
147+
{
148+
var copy = this.copy();
149+
builder.accept(copy.slots);
150+
return copy;
151+
}
152+
153+
public MachineGuiConfiguration progressBar(int renderX, int renderY, String progressBarType, boolean isVertical)
154+
{
155+
var copy = this.copy();
156+
copy.progressBar = new ProgressBar.Parameters(renderX, renderY, progressBarType, isVertical);
157+
return copy;
158+
}
159+
160+
public MachineGuiConfiguration progressBar(int renderX, int renderY, String progressBarType)
161+
{
162+
return this.progressBar(renderX, renderY, progressBarType, false);
163+
}
164+
165+
public MachineGuiConfiguration energyBar(int renderX, int renderY)
166+
{
167+
var copy = this.copy();
168+
copy.energyBar = new EnergyBar.Parameters(renderX, renderY);
169+
return copy;
170+
}
171+
172+
public MachineGuiConfiguration efficiencyBar(int renderX, int renderY)
173+
{
174+
var copy = this.copy();
175+
copy.efficiencyBar = new RecipeEfficiencyBar.Parameters(renderX, renderY);
176+
return copy;
177+
}
178+
179+
public MachineGuiConfiguration predicate(MachineRecipePredicate predicate)
180+
{
181+
Assert.notNull(predicate);
182+
var copy = this.copy();
183+
copy.predicate = predicate;
184+
return copy;
185+
}
186+
187+
public MachineGuiParameters createGuiParams(ResourceLocation blockId)
188+
{
189+
return new MachineGuiParameters.Builder(blockId, lockButton)
190+
.backgroundHeight(guiHeight)
191+
.build();
192+
}
193+
194+
public MachineInventoryComponent buildInventory()
195+
{
196+
return MachineSlotConfiguration.combine(inventoryOnlySlots.build(), slots.build())
197+
.build()
198+
.toInventoryComponent();
199+
}
200+
201+
public void registerProgressBar(MachineBlockEntity machine, Supplier<Float> progress)
202+
{
203+
Assert.noneNull(machine, progress);
204+
if(progressBar != null)
205+
{
206+
machine.guiComponents.register(new ProgressBar.Server(progressBar, progress));
207+
}
208+
}
209+
210+
public void registerEnergyBar(MachineBlockEntity machine, Supplier<Long> euSupplier, Supplier<Long> maxEuSupplier)
211+
{
212+
Assert.noneNull(machine, euSupplier, maxEuSupplier);
213+
if(energyBar != null)
214+
{
215+
machine.guiComponents.register(new EnergyBar.Server(energyBar, euSupplier, maxEuSupplier));
216+
}
217+
}
218+
219+
public void registerEfficiencyBar(MachineBlockEntity machine, CrafterComponent crafter)
220+
{
221+
Assert.noneNull(machine, crafter);
222+
if(efficiencyBar != null)
223+
{
224+
machine.guiComponents.register(new RecipeEfficiencyBar.Server(efficiencyBar, crafter));
225+
}
226+
}
227+
228+
boolean hasRecipeCategory()
229+
{
230+
return recipeType != null;
231+
}
232+
233+
void registerRecipeCategory(MIHook hook,
234+
String name, String englishName,
235+
int tiers)
236+
{
237+
Assert.notNull(recipeType, "Recipe type must be provided");
238+
Assert.notNull(progressBar, "Progress bar must be configured");
239+
240+
var slotPositions = slots.build().toSlotPositions();
241+
Assert.that(slotPositions.hasInputs() && slotPositions.hasOutputs(), "At least one input and one output slot must be provided");
242+
243+
HackedMachineRegistrationHelper.registerReiTiers(
244+
hook,
245+
englishName, name, recipeType,
246+
// The null and false constants are populated by registerReiTiers
247+
new MachineCategoryParams(
248+
null, null,
249+
slotPositions.itemInputs(), slotPositions.itemOutputs(),
250+
slotPositions.fluidInputs(), slotPositions.fluidOutputs(),
251+
progressBar,
252+
null, null, false, null
253+
),
254+
tiers
255+
);
256+
}
257+
258+
void registerRecipeCategory(MIHook hook,
259+
String name, String englishName)
260+
{
261+
Assert.notNull(recipeType, "Recipe type must be provided");
262+
Assert.notNull(progressBar, "Progress bar must be configured");
263+
264+
var slotPositions = slots.build().toSlotPositions();
265+
Assert.that(slotPositions.hasInputs() && slotPositions.hasOutputs(), "At least one input and one output slot must be provided");
266+
267+
HackedMachineRegistrationHelper.registerRecipeCategory(
268+
hook,
269+
name, englishName, recipeType,
270+
slotPositions.itemInputs(), slotPositions.itemOutputs(),
271+
slotPositions.fluidInputs(), slotPositions.fluidOutputs(),
272+
progressBar,
273+
(recipe) -> predicate.test(recipe),
274+
isMultiblock, steamMode
275+
);
276+
}
277+
}

0 commit comments

Comments
 (0)