Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic BlockState Support and Misc Delegate Methods to LevelJS #464

Merged
merged 12 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
import net.minecraft.world.item.Tier;
import net.minecraft.world.item.Tiers;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.levelgen.GenerationStep;
import net.minecraft.world.level.levelgen.structure.templatesystem.RuleTest;
import net.minecraft.world.level.storage.loot.LootContext;
Expand Down Expand Up @@ -348,6 +349,8 @@ public void addBindings(BindingsEvent event) {
event.add("Vec3i", Vec3i.class);
event.add("BlockPos", BlockPos.class);

event.add("BlockProperties", BlockStateProperties.class);

KubeJS.PROXY.clientBindings(event);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.architectury.registry.client.rendering.ColorHandlerRegistry;
import dev.architectury.registry.client.rendering.RenderTypeRegistry;
import dev.latvian.mods.kubejs.BuilderBase;
import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.RegistryObjectBuilderTypes;
import dev.latvian.mods.kubejs.generator.AssetJsonGenerator;
import dev.latvian.mods.kubejs.generator.DataJsonGenerator;
Expand All @@ -22,15 +23,19 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;

Expand All @@ -53,6 +58,7 @@ public abstract class BlockBuilder extends BuilderBase<Block> {
public transient List<AABB> customShape;
public transient boolean noCollision;
public transient boolean notSolid;
@Deprecated
public transient boolean waterlogged;
public transient float slipperiness = 0.6F;
public transient float speedFactor = 1.0F;
Expand All @@ -66,6 +72,9 @@ public abstract class BlockBuilder extends BuilderBase<Block> {
public transient boolean viewBlocking;
public transient boolean redstoneConductor;
public transient boolean transparent;
public transient Set<Property<?>> blockStateProperties;
public transient Consumer<BlockStateModifyCallbackJS> defaultStateModification;
public transient Consumer<BlockStateModifyPlacementCallbackJS> placementStateModification;

public BlockBuilder(ResourceLocation i) {
super(i);
Expand Down Expand Up @@ -102,6 +111,9 @@ public BlockBuilder(ResourceLocation i) {
viewBlocking = true;
redstoneConductor = true;
transparent = false;
blockStateProperties = new HashSet<>();
defaultStateModification = null;
placementStateModification = null;
}

@Override
Expand Down Expand Up @@ -427,8 +439,10 @@ public BlockBuilder notSolid() {
return this;
}

@Deprecated
public BlockBuilder waterlogged() {
waterlogged = true;
property(BlockStateProperties.WATERLOGGED);
KubeJS.startupScriptManager.type.console.warn("Use of deprecated method \"BlockBuilder.waterlogged\". Please use \"BlockBuilder.property(BlockProperties.WATERLOGGED)\" moving forward.");
return this;
}

Expand Down Expand Up @@ -516,6 +530,27 @@ public BlockBuilder tagItem(ResourceLocation tag) {
return this;
}

public BlockBuilder defaultState(Consumer<BlockStateModifyCallbackJS> callbackJS) {
defaultStateModification = callbackJS;
return this;
}

public BlockBuilder placementState(Consumer<BlockStateModifyPlacementCallbackJS> callbackJS) {
placementStateModification = callbackJS;
return this;
}

public BlockBuilder property(Property<?> property) {
if(property.getPossibleValues().size() <= 1) {
throw new IllegalArgumentException(String.format("Block \"%s\" has an illegal Blockstate Property \"%s\" which has <= 1 possible values. (%d possible values)", id, property.getName(), property.getPossibleValues().size()));
}
if(property == BlockStateProperties.WATERLOGGED){
waterlogged = true;
}
blockStateProperties.add(property);
return this;
}

public Block.Properties createProperties() {
var properties = BlockBehaviour.Properties.of(material.getMinecraftMaterial());
properties.sound(material.getSound());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package dev.latvian.mods.kubejs.block;

import com.google.common.collect.ImmutableMap;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.block.state.properties.Property;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;

public class BlockStateModifyCallbackJS {
private BlockState state;
public BlockStateModifyCallbackJS(BlockState state) {
this.state = state;
}

public <T extends Comparable<T>> BlockStateModifyCallbackJS cycle(Property<T> property) {
this.state = state.cycle(property);
return this;
}

public BlockState getState() {
return state;
}

@Override
public String toString() {
return state.toString();
}

public Collection<Property<?>> getProperties() {
return state.getProperties();
}

public <T extends Comparable<T>> boolean hasProperty(Property<T> property) {
return state.hasProperty(property);
}

public <T extends Comparable<T>> T getValue(Property<T> property) {
return state.getValue(property);
}

public <T extends Comparable<T>> T get(Property<T> property) {
return state.getValue(property);
}

public <T extends Comparable<T>> Optional<T> getOptionalValue(Property<T> property) {
return state.getOptionalValue(property);
}

public <T extends Comparable<T>, V extends T> BlockStateModifyCallbackJS setValue(Property<T> property, V comparable) {
this.state = state.setValue(property, comparable);
return this;
}

public BlockStateModifyCallbackJS set(BooleanProperty property, boolean value) {
this.state = state.setValue(property, value);
return this;
}

public BlockStateModifyCallbackJS set(IntegerProperty property, Integer value) {
this.state = state.setValue(property, value);
return this;
}

public <T extends Enum<T> & StringRepresentable> BlockStateModifyCallbackJS set(EnumProperty<T> property, T value) {
this.state = state.setValue(property, value);
return this;
}

public <T extends Enum<T> & StringRepresentable> BlockStateModifyCallbackJS set(EnumProperty<T> property, String value) {
this.state = state.setValue(property, property.getValue(value).get());
return this;
}


public BlockStateModifyCallbackJS populateNeighbours(Map<Map<Property<?>, Comparable<?>>, BlockState> map) {
state.populateNeighbours(map);
return this;
}

public ImmutableMap<Property<?>, Comparable<?>> getValues() {
return state.getValues();
}

public BlockStateModifyCallbackJS rotate(Rotation rotation) {
this.state = state.rotate(rotation);
return this;
}

public BlockStateModifyCallbackJS mirror(Mirror mirror) {
this.state = state.mirror(mirror);
return this;
}

public BlockStateModifyCallbackJS updateShape(Direction direction, BlockState blockState, LevelAccessor levelAccessor, BlockPos blockPos, BlockPos blockPos2) {
this.state = state.updateShape(direction, blockState, levelAccessor, blockPos, blockPos2);
return this;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package dev.latvian.mods.kubejs.block;

import dev.latvian.mods.kubejs.item.ItemStackJS;
import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.level.LevelJS;
import dev.latvian.mods.kubejs.player.PlayerJS;
import dev.latvian.mods.kubejs.util.UtilsJS;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Nullable;

public class BlockStateModifyPlacementCallbackJS extends BlockStateModifyCallbackJS {
public final BlockPlaceContext context;
public final Block minecraftBlock;
public BlockContainerJS block;

public BlockStateModifyPlacementCallbackJS(BlockPlaceContext context, Block block) {
super(block.defaultBlockState());
this.context = context;
this.minecraftBlock = block;
this.block = new BlockContainerJS(context.getLevel(), context.getClickedPos());
}

public BlockPos getClickedPos() {
return context.getClickedPos();
}

public BlockContainerJS getClickedBlock() {
return getLevel().getBlock(context.getClickedPos());
}

public boolean canPlace() {
return context.canPlace();
}

public boolean replacingClickedOnBlock() {
return context.replacingClickedOnBlock();
}

public Direction getNearestLookingDirection() {
return context.getNearestLookingDirection();
}

public Direction getNearestLookingVerticalDirection() {
return context.getNearestLookingVerticalDirection();
}

public Direction[] getNearestLookingDirections() {
return context.getNearestLookingDirections();
}

public Direction getClickedFace() {
return context.getClickedFace();
}

public Vec3 getClickLocation() {
return context.getClickLocation();
}

public boolean isInside() {
return context.isInside();
}

public ItemStackJS getItem() {
return ItemStackJS.of(context.getItemInHand());
}

@Nullable
public PlayerJS<?> getPlayer() {
return getLevel().getPlayer(context.getPlayer());
}

public InteractionHand getHand() {
return context.getHand();
}

public LevelJS getLevel() {
return UtilsJS.getLevel(context.getLevel());
}

public Direction getHorizontalDirection() {
return context.getHorizontalDirection();
}

public boolean isSecondaryUseActive() {
return context.isSecondaryUseActive();
}

public float getRotation() {
return context.getRotation();
}

public FluidState getFluidStateAtClickedPos() {
return context.getLevel().getFluidState(context.getClickedPos());
}

public boolean isClickedPosIn(Fluid fluid) {
return getFluidStateAtClickedPos().is(fluid);
}

public BlockStateModifyPlacementCallbackJS waterlogged(boolean waterlogged) {
setValue(BlockStateProperties.WATERLOGGED, waterlogged);
return this;
}

public BlockStateModifyPlacementCallbackJS waterlogged() {
waterlogged(isInWater());
return this;
}

public boolean isInWater() {
return getFluidStateAtClickedPos().getType() == Fluids.WATER;
}
}
Loading