Made wire connections work

This commit is contained in:
Justin Parsell 2021-04-30 10:20:31 -04:00
parent bafe5feca3
commit 282da8d903
1 changed files with 61 additions and 61 deletions

View File

@ -1,14 +1,10 @@
package me.parsell.glowstonewire.common;
import java.util.Iterator;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.jetbrains.annotations.Nullable;
import me.parsell.glowstonewire.core.glowBlocks;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
@ -19,7 +15,6 @@ import net.minecraft.item.ItemPlacementContext;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.EnumProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.state.property.Property;
import net.minecraft.util.ActionResult;
import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation;
@ -57,6 +52,7 @@ public class GlowstoneWireBlock extends Block{
}
// Make sure out block has the properties for us to manage
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(WIRE_CONNECTION_NORTH, WIRE_CONNECTION_EAST, WIRE_CONNECTION_SOUTH, WIRE_CONNECTION_WEST);
}
@ -75,31 +71,36 @@ public class GlowstoneWireBlock extends Block{
!((WireConnection)state.get(WIRE_CONNECTION_WEST)).isConnected();
}
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
return DOT_SHAPE;
private static boolean hasConnection(BlockState state) {
return ((WireConnection)state.get(WIRE_CONNECTION_NORTH)).isConnected() ||
((WireConnection)state.get(WIRE_CONNECTION_SOUTH)).isConnected() ||
((WireConnection)state.get(WIRE_CONNECTION_EAST)).isConnected() ||
((WireConnection)state.get(WIRE_CONNECTION_WEST)).isConnected();
}
// This decides what I should look like when I'm placed
public BlockState getPlacecmentState(ItemPlacementContext ctx){
World world = ctx.getWorld();
BlockPos placePos = ctx.getBlockPos();
BlockState state = this.getDefaultState();
if(world.getBlockState(placePos.offset(Direction.NORTH)).isOf(this))
state = state.with(WIRE_CONNECTION_NORTH, WireConnection.SIDE);
if(world.getBlockState(placePos.offset(Direction.SOUTH)).isOf(this))
state = state.with(WIRE_CONNECTION_SOUTH, WireConnection.SIDE);
if(world.getBlockState(placePos.offset(Direction.EAST)).isOf(this))
state = state.with(WIRE_CONNECTION_EAST, WireConnection.SIDE);
if(world.getBlockState(placePos.offset(Direction.WEST)).isOf(this))
state = state.with(WIRE_CONNECTION_WEST, WireConnection.SIDE);
private BlockState determineState(WorldAccess world, BlockState state, BlockPos pos){
if(world.getBlockState(pos.offset(Direction.NORTH)).isOf(this))
state = state.with(WIRE_CONNECTION_NORTH, determineConnection(world, pos, Direction.NORTH));
if(world.getBlockState(pos.offset(Direction.SOUTH)).isOf(this))
state = state.with(WIRE_CONNECTION_SOUTH, determineConnection(world, pos, Direction.SOUTH));
if(world.getBlockState(pos.offset(Direction.EAST)).isOf(this))
state = state.with(WIRE_CONNECTION_EAST, determineConnection(world, pos, Direction.EAST));
if(world.getBlockState(pos.offset(Direction.WEST)).isOf(this))
state = state.with(WIRE_CONNECTION_WEST, determineConnection(world, pos, Direction.WEST));
System.out.println(state.toString());
return state;
}
// This decides what I should look like whens something around me happens
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState newState, WorldAccess world, BlockPos pos, BlockPos posFrom) {
System.out.println("state for neighbor update w: " + state.toString() + ", " + direction.toString() + ", " + pos.toString() + ", " + posFrom.toString());
return state;
private WireConnection determineConnection(WorldAccess world, BlockPos pos, Direction direction){
if(world.getBlockState(pos.offset(direction).up()).isOf(this))
return WireConnection.UP;
if(world.getBlockState(pos.offset(direction)).isOf(this) || world.getBlockState(pos.offset(direction).down()).isOf(this))
return WireConnection.SIDE;
return WireConnection.NONE;
}
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
return DOT_SHAPE;
}
public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
@ -109,9 +110,10 @@ public class GlowstoneWireBlock extends Block{
}
private boolean canRunOnTop(BlockView world, BlockPos pos, BlockState floor) {
return floor.isSideSolidFullSquare(world, pos, Direction.UP) || floor.isOf(Blocks.HOPPER);
return floor.isSideSolidFullSquare(world, pos, Direction.UP);
}
@Override
public BlockState rotate(BlockState state, BlockRotation rotation) {
switch(rotation) {
case CLOCKWISE_180:
@ -125,6 +127,7 @@ public class GlowstoneWireBlock extends Block{
}
}
@Override
public BlockState mirror(BlockState state, BlockMirror mirror) {
switch(mirror) {
case LEFT_RIGHT:
@ -136,49 +139,46 @@ public class GlowstoneWireBlock extends Block{
}
}
// This needs to be fixed, it should notify it's neighbors that it shouldn't be connected to me anymore
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit){
if(isNotConnected(state))
world.setBlockState(pos, FULL_STATE, 3);
else
world.setBlockState(pos, this.getDefaultState(), 3);
System.out.println("onUse");
return ActionResult.SUCCESS;
// Set to blank state
if(hasConnection(state)) {
world.setBlockState(pos, this.getDefaultState());
return ActionResult.SUCCESS;
}
// Check if we are updating the state to a new state
BlockState newState = determineState(world, state, pos);
if(state != newState) {
world.setBlockState(pos, newState);
return ActionResult.SUCCESS;
}
return ActionResult.PASS;
}
/* public void update(World world, BlockPos pos, BlockState state){
System.out.println("Update");
// This decides what I should look like whens something around me happens (use this for wire connection)
@Override
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState newState, WorldAccess world, BlockPos pos, BlockPos posFrom) {
if (direction != Direction.DOWN && direction != Direction.UP)
state = state.with(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(direction), determineConnection(world, pos, direction));
return state;
}
// Check if I'm this,
// then update all my neighbors
// then tell my neighbors to update all their neighbors (why?)
/* public void updateNeighbors(World world, BlockPos pos){
if (world.getBlockState(pos).isOf(this)) {
world.updateNeighborsAlways(pos, this);
//Direction[] d = Direction.values();
//int dl = d.length;
//for(int i = 0; i < dl; ++i) {
// Direction direction = d[i];
// world.updateNeighborsAlways(pos.offset(direction), this);
//}
}
} */
public void neighborUpdate(BlockState state, World world, BlockPos pos, Block block, BlockPos fromPos, boolean notify){
if (!world.isClient) {
if (state.canPlaceAt(world, pos)) {
return;
} else {
dropStacks(state, world, pos);
world.removeBlock(pos, false);
}
}
@Override
public BlockState getPlacementState(ItemPlacementContext ctx){
return determineState(ctx.getWorld(), this.getDefaultState(), ctx.getBlockPos());
}
@Override
public void neighborUpdate(BlockState state, World world, BlockPos pos, Block block, BlockPos fromPos, boolean notify) {
if (!world.isClient && !state.canPlaceAt(world, pos)) {
dropStacks(state, world, pos);
world.removeBlock(pos, false);
}
}
static {
WIRE_CONNECTION_NORTH = Properties.NORTH_WIRE_CONNECTION;
WIRE_CONNECTION_EAST = Properties.EAST_WIRE_CONNECTION;