implement custom property

This commit is contained in:
Justin Parsell 2021-04-30 15:01:40 -04:00
parent e12051bc8c
commit cbfce1a065
2 changed files with 59 additions and 28 deletions

View File

@ -0,0 +1,32 @@
package me.parsell.glowstonewire.common;
import net.minecraft.util.StringIdentifiable;
public enum GlowWireConnection implements StringIdentifiable{
UP("up"),
SIDE("side"),
BROKE("broke"),
NONE("none");
private final String name;
private GlowWireConnection(String name){
this.name = name;
}
public String toString() {
return this.asString();
}
public String asString() {
return this.name;
}
public boolean isBroken() {
return this != BROKE;
}
public boolean isConnected() {
return this != NONE;
}
}

View File

@ -8,7 +8,6 @@ import com.google.common.collect.Maps;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.ShapeContext; import net.minecraft.block.ShapeContext;
import net.minecraft.block.enums.WireConnection;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemPlacementContext;
import net.minecraft.state.StateManager; import net.minecraft.state.StateManager;
@ -29,13 +28,13 @@ import net.minecraft.world.WorldView;
public class GlowstoneWireBlock extends Block{ public class GlowstoneWireBlock extends Block{
// These are the states of the wires coming off our block // These are the states of the wires coming off our block
public static final net.minecraft.state.property.EnumProperty<net.minecraft.block.enums.WireConnection> WIRE_CONNECTION_NORTH; public static final EnumProperty<GlowWireConnection> WIRE_CONNECTION_NORTH;
public static final net.minecraft.state.property.EnumProperty<net.minecraft.block.enums.WireConnection> WIRE_CONNECTION_EAST; public static final EnumProperty<GlowWireConnection> WIRE_CONNECTION_SOUTH;
public static final net.minecraft.state.property.EnumProperty<net.minecraft.block.enums.WireConnection> WIRE_CONNECTION_SOUTH; public static final EnumProperty<GlowWireConnection> WIRE_CONNECTION_EAST;
public static final net.minecraft.state.property.EnumProperty<net.minecraft.block.enums.WireConnection> WIRE_CONNECTION_WEST; public static final EnumProperty<GlowWireConnection> WIRE_CONNECTION_WEST;
// Locally defined DIRECTION <-> WIRE_CONNECTION_DIRECTION // Locally defined DIRECTION <-> WIRE_CONNECTION_DIRECTION
public static final Map<Direction, EnumProperty<WireConnection>> DIRECTION_TO_WIRE_CONNECTION_PROPERTY; public static final Map<Direction, EnumProperty<GlowWireConnection>> DIRECTION_TO_WIRE_CONNECTION_PROPERTY;
// Determines the hitbox for our block // Determines the hitbox for our block
public static final VoxelShape DOT_SHAPE; public static final VoxelShape DOT_SHAPE;
@ -46,8 +45,8 @@ public class GlowstoneWireBlock extends Block{
public GlowstoneWireBlock(Settings settings) { public GlowstoneWireBlock(Settings settings) {
super(settings); super(settings);
// Set these properties to be none as start, so we start with a dot // Set these properties to be none as start, so we start with a dot
this.setDefaultState((BlockState)((BlockState)((BlockState)((BlockState)((BlockState)((BlockState)this.stateManager.getDefaultState()).with(WIRE_CONNECTION_NORTH, WireConnection.NONE)).with(WIRE_CONNECTION_EAST, WireConnection.NONE)).with(WIRE_CONNECTION_SOUTH, WireConnection.NONE)).with(WIRE_CONNECTION_WEST, WireConnection.NONE))); this.setDefaultState((BlockState)((BlockState)((BlockState)((BlockState)((BlockState)((BlockState)this.stateManager.getDefaultState()).with(WIRE_CONNECTION_NORTH, GlowWireConnection.NONE)).with(WIRE_CONNECTION_EAST, GlowWireConnection.NONE)).with(WIRE_CONNECTION_SOUTH, GlowWireConnection.NONE)).with(WIRE_CONNECTION_WEST, GlowWireConnection.NONE)));
this.FULL_STATE = (BlockState)((BlockState)((BlockState)((BlockState)this.getDefaultState().with(WIRE_CONNECTION_NORTH, WireConnection.SIDE)).with(WIRE_CONNECTION_EAST, WireConnection.SIDE)).with(WIRE_CONNECTION_SOUTH, WireConnection.SIDE)).with(WIRE_CONNECTION_WEST, WireConnection.SIDE); this.FULL_STATE = (BlockState)((BlockState)((BlockState)((BlockState)this.getDefaultState().with(WIRE_CONNECTION_NORTH, GlowWireConnection.SIDE)).with(WIRE_CONNECTION_EAST, GlowWireConnection.SIDE)).with(WIRE_CONNECTION_SOUTH, GlowWireConnection.SIDE)).with(WIRE_CONNECTION_WEST, GlowWireConnection.SIDE);
} }
// Make sure out block has the properties for us to manage // Make sure out block has the properties for us to manage
@ -57,24 +56,24 @@ public class GlowstoneWireBlock extends Block{
} }
private static boolean isFullyConnected(BlockState state) { private static boolean isFullyConnected(BlockState state) {
return ((WireConnection)state.get(WIRE_CONNECTION_NORTH)).isConnected() && return ((GlowWireConnection)state.get(WIRE_CONNECTION_NORTH)).isConnected() &&
((WireConnection)state.get(WIRE_CONNECTION_SOUTH)).isConnected() && ((GlowWireConnection)state.get(WIRE_CONNECTION_SOUTH)).isConnected() &&
((WireConnection)state.get(WIRE_CONNECTION_EAST)).isConnected() && ((GlowWireConnection)state.get(WIRE_CONNECTION_EAST)).isConnected() &&
((WireConnection)state.get(WIRE_CONNECTION_WEST)).isConnected(); ((GlowWireConnection)state.get(WIRE_CONNECTION_WEST)).isConnected();
} }
private static boolean isNotConnected(BlockState state) { private static boolean isNotConnected(BlockState state) {
return !((WireConnection)state.get(WIRE_CONNECTION_NORTH)).isConnected() && return !((GlowWireConnection)state.get(WIRE_CONNECTION_NORTH)).isConnected() &&
!((WireConnection)state.get(WIRE_CONNECTION_SOUTH)).isConnected() && !((GlowWireConnection)state.get(WIRE_CONNECTION_SOUTH)).isConnected() &&
!((WireConnection)state.get(WIRE_CONNECTION_EAST)).isConnected() && !((GlowWireConnection)state.get(WIRE_CONNECTION_EAST)).isConnected() &&
!((WireConnection)state.get(WIRE_CONNECTION_WEST)).isConnected(); !((GlowWireConnection)state.get(WIRE_CONNECTION_WEST)).isConnected();
} }
private static boolean hasConnection(BlockState state) { private static boolean hasConnection(BlockState state) {
return ((WireConnection)state.get(WIRE_CONNECTION_NORTH)).isConnected() || return ((GlowWireConnection)state.get(WIRE_CONNECTION_NORTH)).isConnected() ||
((WireConnection)state.get(WIRE_CONNECTION_SOUTH)).isConnected() || ((GlowWireConnection)state.get(WIRE_CONNECTION_SOUTH)).isConnected() ||
((WireConnection)state.get(WIRE_CONNECTION_EAST)).isConnected() || ((GlowWireConnection)state.get(WIRE_CONNECTION_EAST)).isConnected() ||
((WireConnection)state.get(WIRE_CONNECTION_WEST)).isConnected(); ((GlowWireConnection)state.get(WIRE_CONNECTION_WEST)).isConnected();
} }
private BlockState determineState(WorldAccess world, BlockState state, BlockPos pos){ private BlockState determineState(WorldAccess world, BlockState state, BlockPos pos){
@ -89,12 +88,12 @@ public class GlowstoneWireBlock extends Block{
return state; return state;
} }
private WireConnection determineConnection(WorldAccess world, BlockPos pos, Direction direction){ private GlowWireConnection determineConnection(WorldAccess world, BlockPos pos, Direction direction){
if(world.getBlockState(pos.offset(direction).up()).isOf(this)) if(world.getBlockState(pos.offset(direction).up()).isOf(this))
return WireConnection.UP; return GlowWireConnection.UP;
if(world.getBlockState(pos.offset(direction)).isOf(this) || world.getBlockState(pos.offset(direction).down()).isOf(this)) if(world.getBlockState(pos.offset(direction)).isOf(this) || world.getBlockState(pos.offset(direction).down()).isOf(this))
return WireConnection.SIDE; return GlowWireConnection.SIDE;
return WireConnection.NONE; return GlowWireConnection.NONE;
} }
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) { public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
@ -180,10 +179,10 @@ public class GlowstoneWireBlock extends Block{
} }
static { static {
WIRE_CONNECTION_NORTH = Properties.NORTH_WIRE_CONNECTION; WIRE_CONNECTION_NORTH = EnumProperty.of("north", GlowWireConnection.class);
WIRE_CONNECTION_EAST = Properties.EAST_WIRE_CONNECTION; WIRE_CONNECTION_EAST = EnumProperty.of("east", GlowWireConnection.class);
WIRE_CONNECTION_SOUTH = Properties.SOUTH_WIRE_CONNECTION; WIRE_CONNECTION_SOUTH = EnumProperty.of("south", GlowWireConnection.class);
WIRE_CONNECTION_WEST = Properties.WEST_WIRE_CONNECTION; WIRE_CONNECTION_WEST = EnumProperty.of("west", GlowWireConnection.class);
DIRECTION_TO_WIRE_CONNECTION_PROPERTY = Maps.newEnumMap((Map)ImmutableMap.of(Direction.NORTH, WIRE_CONNECTION_NORTH, Direction.EAST, WIRE_CONNECTION_EAST, Direction.SOUTH, WIRE_CONNECTION_SOUTH, Direction.WEST, WIRE_CONNECTION_WEST)); DIRECTION_TO_WIRE_CONNECTION_PROPERTY = Maps.newEnumMap((Map)ImmutableMap.of(Direction.NORTH, WIRE_CONNECTION_NORTH, Direction.EAST, WIRE_CONNECTION_EAST, Direction.SOUTH, WIRE_CONNECTION_SOUTH, Direction.WEST, WIRE_CONNECTION_WEST));