Fixed hitboxes, fixed connection issues

This commit is contained in:
Justin Parsell 2021-10-22 03:16:09 -04:00
parent 833d815fd2
commit 9c8b7ab315
2 changed files with 63 additions and 57 deletions

View File

@ -10,8 +10,6 @@ public class GlowstoneWireClient implements ClientModInitializer {
@Override
public void onInitializeClient(){
BlockRenderLayerMap.INSTANCE.putBlock(glowBlocks.GLOWSTONEWIRE, RenderLayer.getCutout());
ColorProviderRegistry.BLOCK.register((state, world, pos, tintIndex) -> {
return 0xFFFF00;
}, glowBlocks.GLOWSTONEWIRE);
ColorProviderRegistry.BLOCK.register((state, world, pos, tintIndex) -> {return 0xffbc5e;}, glowBlocks.GLOWSTONEWIRE);
}
}

View File

@ -4,6 +4,7 @@ import java.util.Map;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.UnmodifiableIterator;
import org.jetbrains.annotations.Nullable;
@ -45,14 +46,20 @@ public class GlowstoneWireBlock extends Block{
// Determines the hitbox for our block
public static final VoxelShape DOT_VOXELSHAPE;
private static final Map<Direction, VoxelShape> field_24414;
private static final Map<Direction, VoxelShape> field_24415;
private static final Map<Direction, VoxelShape> DIRECTION_HITBOX, UP_HITBOX;
private final Map<BlockState, VoxelShape> STATE_TO_VOXELSHAPE = Maps.newHashMap();
public GlowstoneWireBlock(Settings settings) {
super(settings);
// 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, GlowWireConnection.NONE)).with(WIRE_CONNECTION_EAST, GlowWireConnection.NONE)).with(WIRE_CONNECTION_SOUTH, GlowWireConnection.NONE)).with(WIRE_CONNECTION_WEST, GlowWireConnection.NONE)));
UnmodifiableIterator stateItr = this.getStateManager().getStates().iterator();
while(stateItr.hasNext()) {
BlockState state = (BlockState)stateItr.next();
this.STATE_TO_VOXELSHAPE.put(state, determineVoxelShape(state));
}
}
// Make sure out block has the properties for us to manage
@ -68,13 +75,6 @@ public class GlowstoneWireBlock extends Block{
((GlowWireConnection)state.get(WIRE_CONNECTION_WEST)).isConnected();
}
private static boolean hasUpConnection(BlockState state) {
return ((GlowWireConnection)state.get(WIRE_CONNECTION_NORTH)).isConnectedUp() ||
((GlowWireConnection)state.get(WIRE_CONNECTION_SOUTH)).isConnectedUp() ||
((GlowWireConnection)state.get(WIRE_CONNECTION_EAST)).isConnectedUp() ||
((GlowWireConnection)state.get(WIRE_CONNECTION_WEST)).isConnectedUp();
}
private BlockState determineState(WorldAccess world, BlockState state, BlockPos pos){
Iterator<Direction> dirItr = Direction.Type.HORIZONTAL.iterator();
while(dirItr.hasNext()){
@ -87,6 +87,9 @@ public class GlowstoneWireBlock extends Block{
private GlowWireConnection determineConnection(WorldAccess world, BlockPos pos, Direction direction){
if((world.getBlockState(pos.offset(direction).up()).isOf(this) && world.isAir(pos.up()))) // If offset+up is me && if up is air
if(!world.getBlockState(pos.offset(direction).up()).get(DIRECTION_TO_WIRE_CONNECTION.get(direction.getOpposite())).isBroken()) //if offset+up is not broken
if(world.getBlockState(pos.offset(direction)).hasSidedTransparency())
return GlowWireConnection.SIDE;
else
return GlowWireConnection.UP;
if(world.getBlockState(pos.offset(direction).down()).isOf(this) && world.isAir(pos.offset(direction))) // if offset+down is me && if offset is air
if(!world.getBlockState(pos.offset(direction).down()).get(DIRECTION_TO_WIRE_CONNECTION.get(direction.getOpposite())).isBroken()) //if offset+down is not broken
@ -97,19 +100,28 @@ public class GlowstoneWireBlock extends Block{
return GlowWireConnection.NONE;
}
// TODO: Fix this
@Override
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
VoxelShape voxelShape = DOT_VOXELSHAPE;
Iterator var3 = Direction.Type.HORIZONTAL.iterator();
return STATE_TO_VOXELSHAPE.get(state);
}
while(var3.hasNext()) {
Direction direction = (Direction)var3.next();
public VoxelShape determineVoxelShape(BlockState state){
VoxelShape voxelShape = DOT_VOXELSHAPE;
Iterator dirItr1 = Direction.Type.HORIZONTAL.iterator();
while(dirItr1.hasNext()) {
Direction direction = (Direction)dirItr1.next();
GlowWireConnection wireConnection = state.get(DIRECTION_TO_WIRE_CONNECTION.get(direction));
if (wireConnection == GlowWireConnection.SIDE) {
voxelShape = VoxelShapes.union(voxelShape, (VoxelShape)field_24414.get(direction));
if(state.equals(this.getDefaultState().with(DIRECTION_TO_WIRE_CONNECTION.get(direction), GlowWireConnection.SIDE))) {
voxelShape = VoxelShapes.union(voxelShape, (VoxelShape)DIRECTION_HITBOX.get(direction));
return VoxelShapes.union(voxelShape, DIRECTION_HITBOX.get(direction.getOpposite()));
} else if (state.equals(this.getDefaultState().with(DIRECTION_TO_WIRE_CONNECTION.get(direction), GlowWireConnection.UP))) {
voxelShape = VoxelShapes.union(voxelShape, (VoxelShape)UP_HITBOX.get(direction));
return VoxelShapes.union(voxelShape, DIRECTION_HITBOX.get(direction.getOpposite()));
} else if (wireConnection == GlowWireConnection.SIDE) {
voxelShape = VoxelShapes.union(voxelShape, (VoxelShape)DIRECTION_HITBOX.get(direction));
} else if (wireConnection == GlowWireConnection.UP) {
voxelShape = VoxelShapes.union(voxelShape, (VoxelShape)field_24415.get(direction));
voxelShape = VoxelShapes.union(voxelShape, (VoxelShape)UP_HITBOX.get(direction));
}
}
@ -166,58 +178,37 @@ public class GlowstoneWireBlock extends Block{
world.setBlockState(pos, state);
// These call neighborUpdate not getStateForNeighborUpdate!!
//world.updateNeighborsAlways(pos, this); // Update those around me
world.updateNeighborsAlways(pos.down(), this); // Update those below me
world.updateNeighborsAlways(pos.up(), this); // Update those above me
world.updateNeighbors(pos.down(), this); // Update those below me
world.updateNeighbors(pos.up(), this); // Update those above me
return ActionResult.SUCCESS;
}
return ActionResult.PASS;
}
@Override // Update the link in the direction of the update, and if it's a block above me, break all links that are UP, down carry on as normal (i will break so dont do anything)
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState newState, WorldAccess world, BlockPos pos, BlockPos posFrom) {
System.out.println("State for Neighbor Update");
if (direction == Direction.DOWN)
return state; // Do nothing
else if(direction == Direction.UP){
if(!hasUpConnection(state))
return state;
Iterator<Direction> dirItr = Direction.Type.HORIZONTAL.iterator();
while(dirItr.hasNext()){
Direction dir = dirItr.next();
if(!state.get(DIRECTION_TO_WIRE_CONNECTION.get(dir)).isBroken())
state = state.with(DIRECTION_TO_WIRE_CONNECTION.get(dir), determineConnection(world, pos, dir));
}
return state;
} else if(state.get(DIRECTION_TO_WIRE_CONNECTION.get(direction)).isBroken())
return state;
return state.with(DIRECTION_TO_WIRE_CONNECTION.get(direction), determineConnection(world, pos, direction));
}
@Override // Get the state I should place in, and update those around me (above/below) as needed
public BlockState getPlacementState(ItemPlacementContext ctx){
System.out.println("Placement");
return determineState(ctx.getWorld(), this.getDefaultState(), ctx.getBlockPos());
}
@Override
public void onPlaced(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack itemStack) {
Iterator<Direction> dirItr = Direction.Type.HORIZONTAL.iterator();
while(dirItr.hasNext()){
Direction dir = dirItr.next();
if (state.get(DIRECTION_TO_WIRE_CONNECTION.get(dir)).isConnectedUp()) {
BlockPos posOffset = pos.offset(dir).offset(Direction.UP);
world.getBlockState(posOffset).neighborUpdate(world, posOffset, this, pos, false);
}
world.updateNeighbors(pos.offset(Direction.UP), state.getBlock());
world.updateNeighbors(pos.offset(Direction.DOWN), state.getBlock());
}
@Override // This causes w/e issue that tracks deal with.
public void onBroken(WorldAccess world, BlockPos pos, BlockState state) {
world.updateNeighbors(pos.offset(Direction.UP), state.getBlock());
world.updateNeighbors(pos.offset(Direction.DOWN), state.getBlock());
}
@Override // Called when I get told to update, so I should see what/why I need to update. I also use this to determine if I need to be drop or not.
public void neighborUpdate(BlockState state, World world, BlockPos pos, Block block, BlockPos fromPos, boolean notify) {
System.out.println(pos + " Neighbor Update from " + fromPos);
if (!world.isClient) {
if(!state.canPlaceAt(world, pos)) {
dropStacks(state, world, pos);
world.removeBlock(pos, false);
this.onBroken(world, pos, state);
} else {
Iterator<Direction> dirItr = Direction.Type.HORIZONTAL.iterator();
while(dirItr.hasNext()){
@ -236,10 +227,27 @@ public class GlowstoneWireBlock extends Block{
WIRE_CONNECTION_SOUTH = EnumProperty.of("south", GlowWireConnection.class);
WIRE_CONNECTION_WEST = EnumProperty.of("west", GlowWireConnection.class);
DIRECTION_TO_WIRE_CONNECTION = 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 = 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)
);
DOT_VOXELSHAPE = Block.createCuboidShape(3.0D, 0.0D, 3.0D, 13.0D, 1.0D, 13.0D);
field_24414 = Maps.newEnumMap((Map)ImmutableMap.of(Direction.NORTH, Block.createCuboidShape(3.0D, 0.0D, 0.0D, 13.0D, 1.0D, 13.0D), Direction.SOUTH, Block.createCuboidShape(3.0D, 0.0D, 3.0D, 13.0D, 1.0D, 16.0D), Direction.EAST, Block.createCuboidShape(3.0D, 0.0D, 3.0D, 16.0D, 1.0D, 13.0D), Direction.WEST, Block.createCuboidShape(0.0D, 0.0D, 3.0D, 13.0D, 1.0D, 13.0D)));
field_24415 = Maps.newEnumMap((Map)ImmutableMap.of(Direction.NORTH, VoxelShapes.union((VoxelShape)field_24414.get(Direction.NORTH), Block.createCuboidShape(3.0D, 0.0D, 0.0D, 13.0D, 16.0D, 1.0D)), Direction.SOUTH, VoxelShapes.union((VoxelShape)field_24414.get(Direction.SOUTH), Block.createCuboidShape(3.0D, 0.0D, 15.0D, 13.0D, 16.0D, 16.0D)), Direction.EAST, VoxelShapes.union((VoxelShape)field_24414.get(Direction.EAST), Block.createCuboidShape(15.0D, 0.0D, 3.0D, 16.0D, 16.0D, 13.0D)), Direction.WEST, VoxelShapes.union((VoxelShape)field_24414.get(Direction.WEST), Block.createCuboidShape(0.0D, 0.0D, 3.0D, 1.0D, 16.0D, 13.0D))));
DIRECTION_HITBOX = Maps.newEnumMap((Map)ImmutableMap.of(
Direction.NORTH, Block.createCuboidShape(3.0D, 0.0D, 0.0D, 13.0D, 1.0D, 13.0D),
Direction.SOUTH, Block.createCuboidShape(3.0D, 0.0D, 3.0D, 13.0D, 1.0D, 16.0D),
Direction.EAST, Block.createCuboidShape(3.0D, 0.0D, 3.0D, 16.0D, 1.0D, 13.0D),
Direction.WEST, Block.createCuboidShape(0.0D, 0.0D, 3.0D, 13.0D, 1.0D, 13.0D))
);
UP_HITBOX = Maps.newEnumMap((Map)ImmutableMap.of(
Direction.NORTH, VoxelShapes.union((VoxelShape)DIRECTION_HITBOX.get(Direction.NORTH), Block.createCuboidShape(3.0D, 0.0D, 0.0D, 13.0D, 16.0D, 1.0D)),
Direction.SOUTH, VoxelShapes.union((VoxelShape)DIRECTION_HITBOX.get(Direction.SOUTH), Block.createCuboidShape(3.0D, 0.0D, 15.0D, 13.0D, 16.0D, 16.0D)),
Direction.EAST, VoxelShapes.union((VoxelShape)DIRECTION_HITBOX.get(Direction.EAST), Block.createCuboidShape(15.0D, 0.0D, 3.0D, 16.0D, 16.0D, 13.0D)),
Direction.WEST, VoxelShapes.union((VoxelShape)DIRECTION_HITBOX.get(Direction.WEST), Block.createCuboidShape(0.0D, 0.0D, 3.0D, 1.0D, 16.0D, 13.0D)))
);
}
}