Skip to content

Commit

Permalink
Updated Rhino, added WIP Block.registerBuildingMaterial(), removed MapJS
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Dec 13, 2024
1 parent f1befc4 commit 20f9cbc
Show file tree
Hide file tree
Showing 30 changed files with 264 additions and 113 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod_version=2101.7.2
neoforge_version=21.1.83
parchment_mc_version=1.21
parchment_mapping_version=2024.11.10
rhino_version=2101.2.5-build.54
rhino_version=2101.2.6-build.56
tiny_server_version=1.0.0-build.25
gif_lib_version=1.7

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/bindings/BlockWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import dev.latvian.mods.kubejs.block.predicate.BlockEntityPredicate;
import dev.latvian.mods.kubejs.block.predicate.BlockIDPredicate;
import dev.latvian.mods.kubejs.block.predicate.BlockPredicate;
import dev.latvian.mods.kubejs.registry.RegistryKubeEvent;
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.util.Cast;
import dev.latvian.mods.kubejs.util.KubeResourceLocation;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.kubejs.util.Tags;
import dev.latvian.mods.rhino.Context;
Expand Down Expand Up @@ -182,4 +184,12 @@ public static BlockState withProperties(BlockState state, Map<?, ?> properties)

return state;
}

public static void registerBuildingMaterial(Context cx, RegistryKubeEvent<Block> event, KubeResourceLocation id, BuildingMaterialProperties properties) {
properties.register(cx, event, id);
}

public static void registerBuildingMaterial(Context cx, RegistryKubeEvent<Block> event, KubeResourceLocation id) {
registerBuildingMaterial(cx, event, id, (BuildingMaterialProperties) cx.jsToJava(Map.of(), BuildingMaterialProperties.TYPE_INFO));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package dev.latvian.mods.kubejs.bindings;

import dev.latvian.mods.kubejs.block.BlockBuilder;
import dev.latvian.mods.kubejs.block.custom.ButtonOrPressurePlateBuilder;
import dev.latvian.mods.kubejs.registry.RegistryKubeEvent;
import dev.latvian.mods.kubejs.util.KubeResourceLocation;
import dev.latvian.mods.kubejs.util.TickDuration;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.type.TypeInfo;
import dev.latvian.mods.rhino.util.HideFromJS;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.properties.BlockSetType;

import java.util.ArrayList;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;

public record BuildingMaterialProperties(
Blocks blocks,
Optional<Boolean> baseBlock,
Optional<Boolean> baseBlockSuffix,
Consumer<BlockBuilder> properties,
Optional<BlockSetType> behaviour,
Optional<TickDuration> ticksToStayPressed
) {
public record Blocks(
Optional<Boolean> slab,
Optional<Boolean> stairs,
Optional<Boolean> fence,
Optional<Boolean> fenceGate,
Optional<Boolean> wall,
Optional<Boolean> pressurePlate,
Optional<Boolean> button,
Optional<Boolean> trapdoor,
Optional<Boolean> door
) {
}

public static final TypeInfo TYPE_INFO = TypeInfo.of(BuildingMaterialProperties.class);

private boolean add(Function<Blocks, Optional<Boolean>> func) {
return blocks == null || func.apply(blocks).orElse(true);
}

@HideFromJS
public void register(Context cx, RegistryKubeEvent<Block> event, KubeResourceLocation id) {
var builder = new ArrayList<BlockBuilder>();

if (baseBlock.orElse(true)) {
boolean _baseBlockSuffix = baseBlockSuffix.orElse(true);
var baseBlock = (BlockBuilder) event.create(cx, _baseBlockSuffix ? id.withPath(p -> p + "_block") : id);
builder.add(baseBlock);

if (_baseBlockSuffix) {
baseBlock.texture(id.wrapped().withPath(p -> "block/" + p).toString());
}
}

if (add(Blocks::slab)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_slab"), "slab"));
}

if (add(Blocks::stairs)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_stairs"), "stairs"));
}

if (add(Blocks::fence)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_fence"), "fence"));
}

if (add(Blocks::fenceGate)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_fence_gate"), "fence_gate"));
}

if (add(Blocks::wall)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_wall"), "wall"));
}

if (add(Blocks::pressurePlate)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_pressure_plate"), "pressure_plate"));
}

if (add(Blocks::button)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_button"), "button"));
}

if (add(Blocks::trapdoor)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_trapdoor"), "trapdoor"));
}

if (add(Blocks::door)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_door"), "door"));
}

for (var b : builder) {
if (properties != null) {
properties.accept(b);
}

if (b instanceof ButtonOrPressurePlateBuilder p) {
behaviour.ifPresent(p::behaviour);
ticksToStayPressed.ifPresent(p::ticksToStayPressed);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.latvian.mods.kubejs.item.ingredient.IngredientJS;
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.type.TypeInfo;
import dev.latvian.mods.rhino.util.HideFromJS;
import net.minecraft.tags.TagKey;
Expand Down Expand Up @@ -39,7 +40,7 @@ static SizedIngredient ofTag(TagKey<Item> tag, int count) {
}

@HideFromJS
static SizedIngredient wrap(RegistryAccessContainer registries, Object from) {
static SizedIngredient wrap(Context cx, Object from) {
if (from instanceof SizedIngredient s) {
return s;
} else if (from instanceof Ingredient ingredient) {
Expand All @@ -50,13 +51,13 @@ static SizedIngredient wrap(RegistryAccessContainer registries, Object from) {
return Ingredient.of(item).kjs$asStack();
} else if (from instanceof CharSequence) {
try {
return read(registries, new StringReader(from.toString()));
return read(RegistryAccessContainer.of(cx), new StringReader(from.toString()));
} catch (Exception ex) {
return empty;
}
}

return IngredientJS.wrap(registries, from).kjs$asStack();
return IngredientJS.wrap(cx, from).kjs$asStack();
}

@HideFromJS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.latvian.mods.kubejs.client.ModelGenerator;
import dev.latvian.mods.kubejs.client.VariantBlockStateGenerator;
import dev.latvian.mods.kubejs.generator.KubeAssetGenerator;
import dev.latvian.mods.kubejs.util.TickDuration;
import dev.latvian.mods.rhino.util.ReturnsSelf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BlockTags;
Expand All @@ -11,7 +12,7 @@
import net.minecraft.world.level.block.state.properties.BlockSetType;

@ReturnsSelf
public class ButtonBlockBuilder extends ShapedBlockBuilder {
public class ButtonBlockBuilder extends ShapedBlockBuilder implements ButtonOrPressurePlateBuilder {
public static final ResourceLocation[] BUTTON_TAGS = {
BlockTags.BUTTONS.location(),
};
Expand All @@ -31,13 +32,15 @@ public ButtonBlockBuilder(ResourceLocation i) {
ticksToStayPressed = 30;
}

public ButtonBlockBuilder behaviour(BlockSetType wt) {
behaviour = wt;
@Override
public ButtonBlockBuilder behaviour(BlockSetType behaviour) {
this.behaviour = behaviour;
return this;
}

public ButtonBlockBuilder ticksToStayPressed(int t) {
ticksToStayPressed = t;
@Override
public ButtonBlockBuilder ticksToStayPressed(TickDuration ticks) {
this.ticksToStayPressed = (int) ticks.ticks();
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.latvian.mods.kubejs.block.custom;

import dev.latvian.mods.kubejs.block.BlockBuilder;
import dev.latvian.mods.kubejs.util.TickDuration;
import dev.latvian.mods.rhino.util.ReturnsSelf;
import net.minecraft.world.level.block.state.properties.BlockSetType;

public interface ButtonOrPressurePlateBuilder {
@ReturnsSelf
BlockBuilder behaviour(BlockSetType behaviour);

@ReturnsSelf
BlockBuilder ticksToStayPressed(TickDuration ticks);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.latvian.mods.kubejs.client.VariantBlockStateGenerator;
import dev.latvian.mods.kubejs.generator.KubeAssetGenerator;
import dev.latvian.mods.kubejs.util.ID;
import dev.latvian.mods.kubejs.util.TickDuration;
import dev.latvian.mods.rhino.util.ReturnsSelf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BlockTags;
Expand All @@ -11,43 +12,55 @@
import net.minecraft.world.level.block.state.properties.BlockSetType;

@ReturnsSelf
public class PressurePlateBlockBuilder extends ShapedBlockBuilder {
public class PressurePlateBlockBuilder extends ShapedBlockBuilder implements ButtonOrPressurePlateBuilder {
public static final ResourceLocation[] PRESSURE_PLATE_TAGS = {
BlockTags.PRESSURE_PLATES.location(),
};

private static final ResourceLocation MODEL = ResourceLocation.withDefaultNamespace("block/pressure_plate_up");
private static final ResourceLocation PRESSED_MODEL = ResourceLocation.withDefaultNamespace("block/pressure_plate_down");

private static class KubePressurePlateBlock extends PressurePlateBlock {
private final int pressedTime;

public KubePressurePlateBlock(BlockSetType type, int pressedTime, Properties properties) {
super(type, properties);
this.pressedTime = pressedTime;
}

@Override
protected int getPressedTime() {
return pressedTime;
}
}

public transient BlockSetType behaviour;
public transient int ticksToStayPressed;

public PressurePlateBlockBuilder(ResourceLocation i) {
super(i, "_pressure_plate");
noCollision();
tagBoth(PRESSURE_PLATE_TAGS);
// tagBoth(BlockTags.WOODEN_PRESSURE_PLATES.location());
behaviour = BlockSetType.OAK;
ticksToStayPressed = 20;
}

public PressurePlateBlockBuilder behaviour(BlockSetType wt) {
behaviour = wt;
@Override
public PressurePlateBlockBuilder behaviour(BlockSetType behaviour) {
this.behaviour = behaviour;
return this;
}

public PressurePlateBlockBuilder behaviour(String wt) {
for (var type : BlockSetType.values().toList()) {
if (type.name().equals(wt)) {
behaviour = type;
return this;
}
}

@Override
public PressurePlateBlockBuilder ticksToStayPressed(TickDuration ticks) {
this.ticksToStayPressed = (int) ticks.ticks();
return this;
}

@Override
public Block createObject() {
return new PressurePlateBlock(behaviour, createProperties());
return new KubePressurePlateBlock(behaviour, ticksToStayPressed, createProperties());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import dev.latvian.mods.kubejs.level.ruletest.InvertRuleTest;
import dev.latvian.mods.kubejs.recipe.match.ReplacementMatch;
import dev.latvian.mods.kubejs.util.ListJS;
import dev.latvian.mods.kubejs.util.MapJS;
import dev.latvian.mods.kubejs.util.NBTUtils;
import dev.latvian.mods.kubejs.util.RegExpKJS;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
Expand Down Expand Up @@ -103,7 +102,8 @@ static BlockStatePredicate wrap(Context cx, Object o) {
return predicates.isEmpty() ? Simple.NONE : predicates.size() == 1 ? predicates.getFirst() : new OrMatch(predicates);
}

var map = MapJS.of(o);
var map = cx.optionalMapOf(o);

if (map != null) {
if (map.isEmpty()) {
return Simple.ALL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import dev.latvian.mods.kubejs.item.ingredient.IngredientJS;
import dev.latvian.mods.kubejs.recipe.match.ItemMatch;
import dev.latvian.mods.kubejs.recipe.match.Replaceable;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.kubejs.util.WithCodec;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
Expand Down Expand Up @@ -76,7 +75,7 @@ default Codec<?> getCodec(Context cx) {
@Override
default Object replaceThisWith(Context cx, Object with) {
var t = kjs$self();
var r = IngredientJS.wrap(RegistryAccessContainer.of(cx), with);
var r = IngredientJS.wrap(cx, with);

if (!r.equals(t)) {
return r;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ default boolean specialEquals(Context cx, Object o, boolean shallow) {
return kjs$equalsIgnoringCount(s);
}

return kjs$equalsIgnoringCount(ItemStackJS.wrap(RegistryAccessContainer.of(cx), o));
return kjs$equalsIgnoringCount(ItemStackJS.wrap(cx, o));
}

default boolean kjs$equalsIgnoringCount(ItemStack stack) {
Expand Down Expand Up @@ -275,7 +275,7 @@ default Codec<ItemStack> getCodec(Context cx) {
@Override
default Object replaceThisWith(Context cx, Object with) {
var t = kjs$self();
var r = ItemStackJS.wrap(RegistryAccessContainer.of(cx), with);
var r = ItemStackJS.wrap(cx, with);

if (!ItemStack.isSameItemSameComponents(t, r)) {
r.setCount(t.getCount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import dev.latvian.mods.kubejs.item.ingredient.IngredientJS;
import dev.latvian.mods.kubejs.recipe.match.ItemMatch;
import dev.latvian.mods.kubejs.recipe.match.Replaceable;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.rhino.Context;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
Expand All @@ -16,7 +15,7 @@ public interface SizedIngredientKJS extends Replaceable, IngredientSupplierKJS,

@Override
default Object replaceThisWith(Context cx, Object with) {
var ingredient = IngredientJS.wrap(RegistryAccessContainer.of(cx), with);
var ingredient = IngredientJS.wrap(cx, with);

if (!ingredient.equals(kjs$self().ingredient())) {
return new SizedIngredient(ingredient, kjs$self().count());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import dev.latvian.mods.kubejs.core.IngredientSupplierKJS;
import dev.latvian.mods.kubejs.item.ingredient.IngredientJS;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.rhino.BaseFunction;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.type.TypeInfo;
Expand Down Expand Up @@ -35,7 +34,7 @@ static ItemPredicate wrap(Context cx, Object from) {
}
}

var in = IngredientJS.wrap(RegistryAccessContainer.of(cx), from);
var in = IngredientJS.wrap(cx, from);

if (in.isEmpty()) {
return NONE;
Expand Down
Loading

0 comments on commit 20f9cbc

Please sign in to comment.