Skip to content

Commit 33e1493

Browse files
authored
Add simple machine builder API for making machines instead of excessive overloaded methods (#127)
1 parent 002e40e commit 33e1493

19 files changed

+1123
-44
lines changed

src/main/java/net/swedz/tesseract/neoforge/compat/mi/hack/HackedMachineRegistrationHelper.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@
3030
import net.minecraft.world.item.BlockItem;
3131
import net.minecraft.world.level.block.Block;
3232
import net.minecraft.world.level.block.entity.BlockEntityType;
33-
import net.minecraft.world.level.block.state.BlockBehaviour;
3433
import net.minecraft.world.level.block.state.BlockState;
3534
import net.minecraft.world.level.material.MapColor;
3635
import net.swedz.tesseract.neoforge.compat.mi.helper.MachineInventoryHelper;
3736
import net.swedz.tesseract.neoforge.compat.mi.hook.MIHook;
3837
import net.swedz.tesseract.neoforge.compat.mi.hook.MIHookRegistry;
3938
import net.swedz.tesseract.neoforge.compat.mi.hook.MIHookTracker;
40-
import net.swedz.tesseract.neoforge.compat.mi.machine.MachineBlockCreator;
39+
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockEntityFactory;
40+
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockFactory;
41+
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockHolderModifier;
42+
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockPropertiesModifier;
43+
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockRegistrators;
4144
import net.swedz.tesseract.neoforge.compat.mi.mixin.accessor.MIMachineRecipeTypesAccessor;
4245
import net.swedz.tesseract.neoforge.registry.common.CommonLootTableBuilders;
4346
import net.swedz.tesseract.neoforge.registry.common.CommonModelBuilders;
@@ -60,32 +63,32 @@
6063
*
6164
* <p><b>It is recommended to not use these methods yourself.</b> They are intended for internal use within Tesseract
6265
* only. If you need to call one of these methods instead of a method in a hook context, there is something missing in
63-
* the hook context and that should be considered a bug / mistake.</p>
66+
* the hook context and that should be considered a bug / mistake. Also, I will change these methods without warning.
67+
* :)</p>
6468
*/
6569
public final class HackedMachineRegistrationHelper
6670
{
6771
/**
6872
* @see MachineRegistrationHelper#registerMachine(String, String, Function, Consumer[])
6973
*/
70-
@SafeVarargs
7174
public static Supplier<BlockEntityType<?>> registerMachine(MIHook hook,
7275
String englishName, String name,
73-
MachineBlockCreator blockCreator,
74-
Consumer<BlockWithItemHolder<?, ?>> modifyBlock,
75-
Consumer<BlockBehaviour.Properties> overrideProperties,
76+
MachineBlockFactory blockFactory,
77+
MachineBlockHolderModifier holderModifier,
78+
MachineBlockPropertiesModifier overrideProperties,
7679
boolean defaultMineableTags,
77-
Function<BEP, MachineBlockEntity> factory,
78-
Consumer<BlockEntityType<?>>... extraRegistrators)
80+
MachineBlockEntityFactory factory,
81+
MachineBlockRegistrators... extraRegistrators)
7982
{
8083
MIHookRegistry registry = hook.registry();
8184
ResourceLocation id = hook.id(name);
8285

8386
AtomicReference<BlockEntityType<?>> bet = new AtomicReference<>();
84-
BiFunction<BlockPos, BlockState, MachineBlockEntity> ctor = (pos, state) -> factory.apply(new BEP(bet.get(), pos, state));
87+
BiFunction<BlockPos, BlockState, MachineBlockEntity> ctor = (pos, state) -> factory.create(new BEP(bet.get(), pos, state));
8588

8689
BlockWithItemHolder<?, ?> blockHolder = new BlockWithItemHolder<>(
8790
id, englishName,
88-
registry.blockRegistry(), (p) -> blockCreator == null ? new MachineBlock(ctor, p) : blockCreator.create(ctor, p),
91+
registry.blockRegistry(), (p) -> blockFactory == null ? new MachineBlock(ctor, p) : blockFactory.create(ctor, p),
8992
registry.itemRegistry(), BlockItem::new
9093
);
9194
blockHolder.item().sorted(registry.sortOrderMachines());
@@ -99,7 +102,7 @@ public static Supplier<BlockEntityType<?>> registerMachine(MIHook hook,
99102
{
100103
if(overrideProperties != null)
101104
{
102-
overrideProperties.accept(properties);
105+
overrideProperties.modify(properties);
103106
}
104107
else
105108
{
@@ -123,9 +126,9 @@ public static Supplier<BlockEntityType<?>> registerMachine(MIHook hook,
123126
.customLoader((bmb, exFile) -> new FakedMachineModelBuilder<>(machineModelProperties, bmb, exFile))
124127
.end());
125128
});
126-
if(modifyBlock != null)
129+
if(holderModifier != null)
127130
{
128-
modifyBlock.accept(blockHolder);
131+
holderModifier.modify(blockHolder);
129132
}
130133
blockHolder.register();
131134

@@ -138,9 +141,9 @@ public static Supplier<BlockEntityType<?>> registerMachine(MIHook hook,
138141

139142
bet.set(BlockEntityType.Builder.of(ctor::apply, block).build(null));
140143

141-
for(Consumer<BlockEntityType<?>> extraRegistrator : extraRegistrators)
144+
for(var extraRegistrator : extraRegistrators)
142145
{
143-
extraRegistrator.accept(bet.get());
146+
extraRegistrator.apply(bet.get());
144147
}
145148

146149
registry.onBlockEntityRegister(bet.get());
@@ -149,23 +152,21 @@ public static Supplier<BlockEntityType<?>> registerMachine(MIHook hook,
149152
});
150153
}
151154

152-
@SafeVarargs
153155
public static Supplier<BlockEntityType<?>> registerMachine(MIHook hook,
154156
String englishName, String name,
155-
MachineBlockCreator blockCreator,
156-
Consumer<BlockWithItemHolder<?, ?>> modifyBlock,
157-
Consumer<BlockBehaviour.Properties> overrideProperties,
158-
Function<BEP, MachineBlockEntity> factory,
159-
Consumer<BlockEntityType<?>>... extraRegistrators)
157+
MachineBlockFactory blockCreator,
158+
MachineBlockHolderModifier modifyBlock,
159+
MachineBlockPropertiesModifier overrideProperties,
160+
MachineBlockEntityFactory factory,
161+
MachineBlockRegistrators... extraRegistrators)
160162
{
161163
return registerMachine(hook, englishName, name, blockCreator, modifyBlock, overrideProperties, true, factory, extraRegistrators);
162164
}
163165

164-
@SafeVarargs
165166
public static Supplier<BlockEntityType<?>> registerMachine(MIHook hook,
166167
String englishName, String name,
167-
Function<BEP, MachineBlockEntity> factory,
168-
Consumer<BlockEntityType<?>>... extraRegistrators)
168+
MachineBlockEntityFactory factory,
169+
MachineBlockRegistrators... extraRegistrators)
169170
{
170171
return registerMachine(hook, englishName, name, null, null, null, factory, extraRegistrators);
171172
}

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import net.swedz.tesseract.neoforge.compat.mi.hack.HackedMachineRegistrationHelper;
2121
import net.swedz.tesseract.neoforge.compat.mi.hook.MIHook;
2222
import net.swedz.tesseract.neoforge.compat.mi.hook.context.MIHookContext;
23+
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.HatchMachineBuilder;
24+
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.MachineBuilder;
25+
import net.swedz.tesseract.neoforge.compat.mi.machine.builder.function.MachineBlockRegistrators;
2326
import net.swedz.tesseract.neoforge.registry.holder.BlockWithItemHolder;
2427
import org.apache.commons.lang3.ArrayUtils;
2528

@@ -34,16 +37,24 @@ public HatchMIHookContext(MIHook hook)
3437
super(hook);
3538
}
3639

40+
public HatchMachineBuilder builder(String name, String englishName)
41+
{
42+
return MachineBuilder.hatch(hook, name, englishName);
43+
}
44+
45+
@Deprecated(forRemoval = true)
3746
public interface HatchFactory
3847
{
3948
HatchBlockEntity create(BEP bep, boolean input, ResourceLocation machineId);
4049
}
4150

51+
@Deprecated(forRemoval = true)
4252
public interface HatchModification<T>
4353
{
4454
void apply(T value, boolean input);
4555
}
4656

57+
@Deprecated(forRemoval = true)
4758
@SafeVarargs
4859
private void registerHatch(String id, String englishName, String overlayFolder, MachineCasing casing,
4960
Consumer<BlockWithItemHolder<?, ?>> modifyBlock,
@@ -53,10 +64,16 @@ private void registerHatch(String id, String englishName, String overlayFolder,
5364
boolean input,
5465
Consumer<BlockEntityType<?>>... extraRegistrators)
5566
{
56-
HackedMachineRegistrationHelper.registerMachine(hook, englishName, id, null, modifyBlock, overrideProperties, defaultMineableTags, (bep) -> factory.create(bep, input, hook.id(id)), extraRegistrators);
67+
List<MachineBlockRegistrators> wrappedRegistrators = Lists.newArrayList();
68+
for(var registrator : extraRegistrators)
69+
{
70+
wrappedRegistrators.add(registrator::accept);
71+
}
72+
HackedMachineRegistrationHelper.registerMachine(hook, englishName, id, null, modifyBlock != null ? modifyBlock::accept : null, overrideProperties != null ? overrideProperties::accept : null, defaultMineableTags, (bep) -> factory.create(bep, input, hook.id(id)), wrappedRegistrators.toArray(MachineBlockRegistrators[]::new));
5773
HackedMachineRegistrationHelper.addMachineModel(hook, id, casing, overlayFolder, true, false, true, false);
5874
}
5975

76+
@Deprecated(forRemoval = true)
6077
@SafeVarargs
6178
public final void registerHatch(String id, String englishName, String overlayFolder, MachineCasing casing,
6279
Consumer<BlockWithItemHolder<?, ?>> modifyBlock,
@@ -68,6 +85,7 @@ public final void registerHatch(String id, String englishName, String overlayFol
6885
this.registerHatch(id, englishName, overlayFolder, casing, modifyBlock, overrideProperties, defaultMineableTags, factory, false, extraRegistrators);
6986
}
7087

88+
@Deprecated(forRemoval = true)
7189
@SafeVarargs
7290
public final void registerHatch(String id, String englishName, String overlayFolder, MachineCasing casing,
7391
Consumer<BlockWithItemHolder<?, ?>> modifyBlock,
@@ -78,6 +96,7 @@ public final void registerHatch(String id, String englishName, String overlayFol
7896
this.registerHatch(id, englishName, overlayFolder, casing, modifyBlock, overrideProperties, true, factory, extraRegistrators);
7997
}
8098

99+
@Deprecated(forRemoval = true)
81100
@SafeVarargs
82101
public final void registerHatch(String id, String englishName, String overlayFolder, MachineCasing casing,
83102
HatchFactory factory,
@@ -86,6 +105,7 @@ public final void registerHatch(String id, String englishName, String overlayFol
86105
this.registerHatch(id, englishName, overlayFolder, casing, null, null, factory, extraRegistrators);
87106
}
88107

108+
@Deprecated(forRemoval = true)
89109
@SafeVarargs
90110
public final void registerHatches(String id, String englishName, String overlayFolder, MachineCasing casing,
91111
HatchModification<BlockWithItemHolder<?, ?>> modifyBlock,
@@ -108,6 +128,7 @@ public final void registerHatches(String id, String englishName, String overlayF
108128
}
109129
}
110130

131+
@Deprecated(forRemoval = true)
111132
@SafeVarargs
112133
public final void registerHatches(String id, String englishName, String overlayFolder, MachineCasing casing,
113134
HatchModification<BlockWithItemHolder<?, ?>> modifyBlock,
@@ -118,6 +139,7 @@ public final void registerHatches(String id, String englishName, String overlayF
118139
registerHatches(id, englishName, overlayFolder, casing, modifyBlock, overrideProperties, true, factory, extraRegistrators);
119140
}
120141

142+
@Deprecated(forRemoval = true)
121143
@SafeVarargs
122144
public final void registerHatches(String id, String englishName, String overlayFolder, MachineCasing casing,
123145
HatchFactory factory,
@@ -126,6 +148,7 @@ public final void registerHatches(String id, String englishName, String overlayF
126148
registerHatches(id, englishName, overlayFolder, casing, null, null, factory, extraRegistrators);
127149
}
128150

151+
@Deprecated(forRemoval = true)
129152
@SafeVarargs
130153
public final void registerItemHatches(String id, String englishName, MachineCasing casing, int rows, int columns, int xStart, int yStart,
131154
HatchModification<BlockWithItemHolder<?, ?>> modifyBlock,
@@ -157,6 +180,7 @@ public final void registerItemHatches(String id, String englishName, MachineCasi
157180
}, ArrayUtils.add(extraRegistrators, MachineBlockEntity::registerItemApi));
158181
}
159182

183+
@Deprecated(forRemoval = true)
160184
@SafeVarargs
161185
public final void registerItemHatches(String id, String englishName, MachineCasing casing, int rows, int columns, int xStart, int yStart,
162186
HatchModification<BlockWithItemHolder<?, ?>> modifyBlock,
@@ -166,13 +190,15 @@ public final void registerItemHatches(String id, String englishName, MachineCasi
166190
this.registerItemHatches(id, englishName, casing, rows, columns, xStart, yStart, modifyBlock, overrideProperties, true, extraRegistrators);
167191
}
168192

193+
@Deprecated(forRemoval = true)
169194
@SafeVarargs
170195
public final void registerItemHatches(String id, String englishName, MachineCasing casing, int rows, int columns, int xStart, int yStart,
171196
Consumer<BlockEntityType<?>>... extraRegistrators)
172197
{
173198
this.registerItemHatches(id, englishName, casing, rows, columns, xStart, yStart, null, null, extraRegistrators);
174199
}
175200

201+
@Deprecated(forRemoval = true)
176202
@SafeVarargs
177203
public final void registerFluidHatches(String id, String englishName, MachineCasing casing, int bucketCapacity,
178204
HatchModification<BlockWithItemHolder<?, ?>> modifyBlock,
@@ -195,6 +221,7 @@ public final void registerFluidHatches(String id, String englishName, MachineCas
195221
}, ArrayUtils.add(extraRegistrators, MachineBlockEntity::registerFluidApi));
196222
}
197223

224+
@Deprecated(forRemoval = true)
198225
@SafeVarargs
199226
public final void registerFluidHatches(String id, String englishName, MachineCasing casing, int bucketCapacity,
200227
HatchModification<BlockWithItemHolder<?, ?>> modifyBlock,
@@ -204,13 +231,15 @@ public final void registerFluidHatches(String id, String englishName, MachineCas
204231
this.registerFluidHatches(id, englishName, casing, bucketCapacity, modifyBlock, overrideProperties, true, extraRegistrators);
205232
}
206233

234+
@Deprecated(forRemoval = true)
207235
@SafeVarargs
208236
public final void registerFluidHatches(String id, String englishName, MachineCasing casing, int bucketCapacity,
209237
Consumer<BlockEntityType<?>>... extraRegistrators)
210238
{
211239
this.registerFluidHatches(id, englishName, casing, bucketCapacity, null, null, extraRegistrators);
212240
}
213241

242+
@Deprecated(forRemoval = true)
214243
@SafeVarargs
215244
public final void registerEnergyHatches(CableTier tier,
216245
HatchModification<BlockWithItemHolder<?, ?>> modifyBlock,
@@ -227,6 +256,7 @@ public final void registerEnergyHatches(CableTier tier,
227256
);
228257
}
229258

259+
@Deprecated(forRemoval = true)
230260
@SafeVarargs
231261
public final void registerEnergyHatches(CableTier tier,
232262
HatchModification<BlockWithItemHolder<?, ?>> modifyBlock,
@@ -236,6 +266,7 @@ public final void registerEnergyHatches(CableTier tier,
236266
this.registerEnergyHatches(tier, modifyBlock, overrideProperties, true, extraRegistrators);
237267
}
238268

269+
@Deprecated(forRemoval = true)
239270
@SafeVarargs
240271
public final void registerEnergyHatches(CableTier tier,
241272
Consumer<BlockEntityType<?>>... extraRegistrators)

0 commit comments

Comments
 (0)