This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
ScriptCraft/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/block/CustomBlock.java

65 lines
2.4 KiB
Java

package com.thebrokenrail.scriptcraft.api.block;
import com.thebrokenrail.scriptcraft.core.ScriptCraftCore;
import com.thebrokenrail.scriptcraft.api.util.ValueUtil;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.Property;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import java.util.HashMap;
import java.util.Map;
@SuppressWarnings("deprecation")
public abstract class CustomBlock extends Block {
@SuppressWarnings({"unchecked", "rawtypes"})
public static class BlockStatePropertyBuilder {
private final Map<Property, Comparable> map = new HashMap<>();
public void add(Property<?> property, Comparable defaultValue) {
map.put(property, defaultValue);
}
private void appendProperties(StateManager.Builder<Block, BlockState> builder) {
for (Property property : map.keySet()) {
builder.add(property);
}
}
private BlockState updateDefaultState(BlockState defaultState) {
for (Map.Entry<Property, Comparable> entry : map.entrySet()) {
defaultState = defaultState.with(entry.getKey(), entry.getValue());
}
return defaultState;
}
}
protected final Identifier id;
public CustomBlock(Settings settings, Identifier id) {
super(settings);
this.id = id;
setDefaultState(getPropertyBuilder().updateDefaultState(getDefaultState()));
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
return ValueUtil.getEnumValue(ActionResult.class, (String) ScriptCraftCore.useBridge("CustomBlock.onUse", id.toString(), world, state, pos.getX(), pos.getY(), pos.getZ(), hit.getSide().name(), player, hand.name()), ActionResult.PASS);
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
super.appendProperties(builder);
getPropertyBuilder().appendProperties(builder);
}
protected abstract BlockStatePropertyBuilder getPropertyBuilder();
}