Skip to content

Commit 857ff78

Browse files
committed
Block registration configuration builder
1 parent e3e31ab commit 857ff78

4 files changed

Lines changed: 38 additions & 7 deletions

File tree

‎src/impl/java/pl/olafcio/avoid_impl/net/block/BlockNative.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import pl.olafcio.avoid_impl.net.world.block_data.BlockDataNative;
2323
import pl.olafcio.avoid_impl.net.world.vect3.Vect3Native;
2424

25+
import java.lang.reflect.AnnotatedElement;
26+
2527
@Native
2628
@ApiStatus.Internal
2729
public final class BlockNative {
@@ -33,7 +35,10 @@ public interface IAvoidBlock {
3335
}
3436

3537
static Block make(pl.olafcio.avoid.net.block.Block avoidBlock, BlockBehaviour.Properties properties) {
36-
var abc = avoidBlock.getClass();
38+
return make(avoidBlock, avoidBlock.getClass(), properties);
39+
}
40+
41+
static Block make(pl.olafcio.avoid.net.block.Block avoidBlock, AnnotatedElement abc, BlockBehaviour.Properties properties) {
3742
if (abc.isAnnotationPresent(_liquid.class)) {
3843
var liquid = abc.getDeclaredAnnotation(_liquid.class);
3944
if (liquid.fluid() != Fluid.class)

‎src/impl/java/pl/olafcio/avoid_impl/net/block/Blocks.java‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import pl.olafcio.avoid_impl.net.world.block_data.BlockDataNative;
3333

3434
import java.lang.annotation.Annotation;
35+
import java.lang.reflect.AnnotatedElement;
3536
import java.util.Map;
3637
import java.util.Optional;
3738
import java.util.function.Function;
@@ -51,19 +52,20 @@ public static BlockData create(Identification id) {
5152
return BlockDataNative.convertFrom(BuiltInRegistries.BLOCK.getValue(IdentificationNative.convert(id)).defaultBlockState());
5253
}
5354

54-
public static void register(Identification blockID, Supplier<? extends pl.olafcio.avoid.net.block.Block> constructor) {
55+
public static void register(Identification blockID, Supplier<? extends pl.olafcio.avoid.net.block.Block> constructor, AnnotatedElement config) {
5556
var id = IdentificationNative.convert(blockID);
5657

5758
Function<Properties, Block> callback = properties -> BlockNative.make(
5859
constructor.get(),
60+
config,
5961
properties
6062
);
6163

6264
var inst = constructor.get();
6365
var block = net.minecraft.world.level.block.Blocks.register(
6466
ResourceKey.create(Registries.BLOCK, id),
6567
callback,
66-
getProperties(inst, inst.getClass(), blockID)
68+
getProperties(inst, config, blockID)
6769
); //at:register
6870

6971
Registry.register(BuiltInRegistries.BLOCK_TYPE, id, simpleCodec(callback));
@@ -102,7 +104,7 @@ public static void register(Identification blockID, Supplier<? extends pl.olafci
102104

103105
private record ChosenPreset(Properties properties, String preset) {}
104106

105-
private static <T extends pl.olafcio.avoid.net.block.Block> Properties getProperties(T instance, Class<? extends T> block, Identification blockID) {
107+
private static <T extends pl.olafcio.avoid.net.block.Block> Properties getProperties(T instance, AnnotatedElement block, Identification blockID) {
106108
var ret = presetProperties(block);
107109

108110
var properties = ret.properties();
@@ -265,7 +267,7 @@ private static MapColor createMapColor(String colorName, int colorRGB) {
265267
}
266268

267269
@NotNull
268-
private static ChosenPreset presetProperties(Class<? extends pl.olafcio.avoid.net.block.Block> block) {
270+
private static ChosenPreset presetProperties(AnnotatedElement block) {
269271
var presets = Map.<Class<? extends Annotation>, Function<? extends Annotation, Properties>>
270272
//at:presets
271273
of(_presetNetherStem.class, (_presetNetherStem preset) -> IBlocks.netherStemProperties(
@@ -297,7 +299,7 @@ private static ChosenPreset presetProperties(Class<? extends pl.olafcio.avoid.ne
297299
}
298300

299301
@SuppressWarnings("unchecked")
300-
private static <T extends Annotation> T getUnsafe(Class<?> clazz, Class<?> annotation) {
302+
private static <T extends Annotation> T getUnsafe(AnnotatedElement clazz, Class<?> annotation) {
301303
return clazz.getDeclaredAnnotation((Class<T>) annotation);
302304
}
303305
}

‎src/main/java/pl/olafcio/avoid/net/block/Blocks.java‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pl.olafcio.avoid.net.id.Identification;
77
import pl.olafcio.avoid.net.world.block_data.BlockData;
88

9+
import java.lang.reflect.AnnotatedElement;
910
import java.util.function.Supplier;
1011

1112
/**
@@ -33,7 +34,19 @@ public static BlockData create(Identification id) {
3334
* <u><i>The annotations on it will be considered.</i></u>
3435
*/
3536
public static void register(Identification blockID, Supplier<? extends pl.olafcio.avoid.net.block.Block> constructor) {
36-
pl.olafcio.avoid_impl.net.block.Blocks.register(blockID, constructor);
37+
pl.olafcio.avoid_impl.net.block.Blocks.register(blockID, constructor, constructor.get().getClass());
38+
}
39+
40+
/**
41+
* Registers a new dynamic block type from the specified parameters.
42+
* @param blockID The block ID of the new block type.
43+
* @param constructor Should return a new instance of the class of the block type.<br/>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&ensp;&emsp;
44+
* Annotations on it will not be considered.
45+
* @param config {@linkplain BlockBuilder Annotation configuration} for the block.<br/>&emsp;&emsp;&emsp;&emsp;&nbsp;&ensp;
46+
* Useful when you don't have a class with all the annotations needed.
47+
*/
48+
public static void register(Identification blockID, Supplier<? extends pl.olafcio.avoid.net.block.Block> constructor, AnnotatedElement config) {
49+
pl.olafcio.avoid_impl.net.block.Blocks.register(blockID, constructor, config);
3750
}
3851

3952
@ApiStatus.Internal
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package pl.olafcio.avoid.net.block;
2+
3+
import pl.olafcio.avoid.net.id.Identification;
4+
import java.util.function.Supplier;
5+
6+
make an annotation builder:
7+
out of "properties/"
8+
onto:
9+
public void buildAndRegister(Identification blockID, Supplier<? extends pl.olafcio.avoid.net.block.Block> constructor) {
10+
Blocks.register(blockID, constructor, build());
11+
}

0 commit comments

Comments
 (0)