Skip to content

Commit

Permalink
Added BlockBuilder#copyPropertiesFrom(block)
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Aug 15, 2024
1 parent 53d56c1 commit f6bb63a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
26 changes: 20 additions & 6 deletions src/main/java/dev/latvian/mods/kubejs/block/BlockBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public abstract class BlockBuilder extends BuilderBase<Block> {
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<BlockState, MapColor> mapColorFn;
public transient float hardness;
Expand Down Expand Up @@ -112,8 +113,8 @@ public abstract class BlockBuilder extends BuilderBase<Block> {

public BlockBuilder(ResourceLocation i) {
super(i);
soundType = SoundType.WOOD;
mapColorFn = MapColorHelper.NONE;
soundType = null;
mapColorFn = null;
hardness = 1.5F;
resistance = 3F;
lightLevel = 0F;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -804,17 +810,25 @@ public BlockBuilder blockEntity(Consumer<BlockEntityInfo> 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);
} else {
properties.strength(hardness);
}

properties.lightLevel(state -> (int) (lightLevel * 15F));
if (lightLevel > 0F) {
properties.lightLevel(state -> (int) (lightLevel * 15F));
}

if (noCollision) {
properties.noCollission();
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}

0 comments on commit f6bb63a

Please sign in to comment.