From f6bb63adde5351a0645d741acef940542e56f091 Mon Sep 17 00:00:00 2001 From: LatvianModder Date: Thu, 15 Aug 2024 13:35:54 +0300 Subject: [PATCH] Added BlockBuilder#copyPropertiesFrom(block) --- .../mods/kubejs/block/BlockBuilder.java | 26 ++++++++++++++----- .../kubejs/block/KubeJSBlockProperties.java | 23 +++++++++++++++- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/main/java/dev/latvian/mods/kubejs/block/BlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/BlockBuilder.java index 369b36b60..a551815f5 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/BlockBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/BlockBuilder.java @@ -68,6 +68,7 @@ public abstract class BlockBuilder extends BuilderBase { private static final BlockBehaviour.StatePredicate ALWAYS_FALSE_STATE_PREDICATE = (blockState, blockGetter, blockPos) -> false; private static final BlockBehaviour.StateArgumentPredicate ALWAYS_FALSE_STATE_ARG_PREDICATE = (blockState, blockGetter, blockPos, type) -> false; + public transient Block copyPropertiesFrom; public transient SoundType soundType; public transient Function mapColorFn; public transient float hardness; @@ -112,8 +113,8 @@ public abstract class BlockBuilder extends BuilderBase { public BlockBuilder(ResourceLocation i) { super(i); - soundType = SoundType.WOOD; - mapColorFn = MapColorHelper.NONE; + soundType = null; + mapColorFn = null; hardness = 1.5F; resistance = 3F; lightLevel = 0F; @@ -311,6 +312,11 @@ protected boolean areAllTexturesEqual(JsonObject tex, String t) { return true; } + public BlockBuilder copyPropertiesFrom(Block block) { + copyPropertiesFrom = block; + return this; + } + @Info("Sets the block's sound type. Defaults to wood.") public BlockBuilder soundType(SoundType m) { if (m == null || m == SoundType.EMPTY) { @@ -804,9 +810,15 @@ public BlockBuilder blockEntity(Consumer callback) { } public Block.Properties createProperties() { - var properties = new KubeJSBlockProperties(this); - properties.sound(soundType); - properties.mapColor(mapColorFn); + var properties = new KubeJSBlockProperties(this, copyPropertiesFrom); + + if (soundType != null) { + properties.sound(soundType); + } + + if (mapColorFn != null) { + properties.mapColor(mapColorFn); + } if (resistance >= 0F) { properties.strength(hardness, resistance); @@ -814,7 +826,9 @@ public Block.Properties createProperties() { properties.strength(hardness); } - properties.lightLevel(state -> (int) (lightLevel * 15F)); + if (lightLevel > 0F) { + properties.lightLevel(state -> (int) (lightLevel * 15F)); + } if (noCollision) { properties.noCollission(); diff --git a/src/main/java/dev/latvian/mods/kubejs/block/KubeJSBlockProperties.java b/src/main/java/dev/latvian/mods/kubejs/block/KubeJSBlockProperties.java index 1a13da8c1..36b545252 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/KubeJSBlockProperties.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/KubeJSBlockProperties.java @@ -1,12 +1,33 @@ package dev.latvian.mods.kubejs.block; +import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockBehaviour; +import org.jetbrains.annotations.Nullable; + +import java.lang.reflect.Modifier; public class KubeJSBlockProperties extends BlockBehaviour.Properties { public final BlockBuilder blockBuilder; - public KubeJSBlockProperties(BlockBuilder blockBuilder) { + public KubeJSBlockProperties(BlockBuilder blockBuilder, @Nullable Block copyPropertiesFrom) { super(); this.blockBuilder = blockBuilder; + + if (copyPropertiesFrom != null) { + // incredibly cursed but alternative is ATing every field + + try { + var from = copyPropertiesFrom.properties(); + + for (var field : BlockBehaviour.Properties.class.getDeclaredFields()) { + if (!Modifier.isStatic(field.getModifiers()) && !Modifier.isFinal(field.getModifiers())) { + field.setAccessible(true); + field.set(this, field.get(from)); + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } } }