|
| 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