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; package me.parsell.glowstonewire.common;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps; 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.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
@ -19,7 +15,6 @@ import net.minecraft.item.ItemPlacementContext;
import net.minecraft.state.StateManager; import net.minecraft.state.StateManager;
import net.minecraft.state.property.EnumProperty; import net.minecraft.state.property.EnumProperty;
import net.minecraft.state.property.Properties; import net.minecraft.state.property.Properties;
import net.minecraft.state.property.Property;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
import net.minecraft.util.BlockMirror; import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation; 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 // Make sure out block has the properties for us to manage
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) { protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(WIRE_CONNECTION_NORTH, WIRE_CONNECTION_EAST, WIRE_CONNECTION_SOUTH, WIRE_CONNECTION_WEST); 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(); !((WireConnection)state.get(WIRE_CONNECTION_WEST)).isConnected();
} }
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) { private static boolean hasConnection(BlockState state) {
return DOT_SHAPE; 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 private BlockState determineState(WorldAccess world, BlockState state, BlockPos pos){
public BlockState getPlacecmentState(ItemPlacementContext ctx){ if(world.getBlockState(pos.offset(Direction.NORTH)).isOf(this))
World world = ctx.getWorld(); state = state.with(WIRE_CONNECTION_NORTH, determineConnection(world, pos, Direction.NORTH));
BlockPos placePos = ctx.getBlockPos(); if(world.getBlockState(pos.offset(Direction.SOUTH)).isOf(this))
BlockState state = this.getDefaultState(); state = state.with(WIRE_CONNECTION_SOUTH, determineConnection(world, pos, Direction.SOUTH));
if(world.getBlockState(placePos.offset(Direction.NORTH)).isOf(this)) if(world.getBlockState(pos.offset(Direction.EAST)).isOf(this))
state = state.with(WIRE_CONNECTION_NORTH, WireConnection.SIDE); state = state.with(WIRE_CONNECTION_EAST, determineConnection(world, pos, Direction.EAST));
if(world.getBlockState(placePos.offset(Direction.SOUTH)).isOf(this)) if(world.getBlockState(pos.offset(Direction.WEST)).isOf(this))
state = state.with(WIRE_CONNECTION_SOUTH, WireConnection.SIDE); state = state.with(WIRE_CONNECTION_WEST, determineConnection(world, pos, Direction.WEST));
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);
System.out.println(state.toString()); System.out.println(state.toString());
return state; return state;
} }
// This decides what I should look like whens something around me happens private WireConnection determineConnection(WorldAccess world, BlockPos pos, Direction direction){
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState newState, WorldAccess world, BlockPos pos, BlockPos posFrom) { if(world.getBlockState(pos.offset(direction).up()).isOf(this))
System.out.println("state for neighbor update w: " + state.toString() + ", " + direction.toString() + ", " + pos.toString() + ", " + posFrom.toString()); return WireConnection.UP;
return state; 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) { 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) { 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) { public BlockState rotate(BlockState state, BlockRotation rotation) {
switch(rotation) { switch(rotation) {
case CLOCKWISE_180: case CLOCKWISE_180:
@ -125,6 +127,7 @@ public class GlowstoneWireBlock extends Block{
} }
} }
@Override
public BlockState mirror(BlockState state, BlockMirror mirror) { public BlockState mirror(BlockState state, BlockMirror mirror) {
switch(mirror) { switch(mirror) {
case LEFT_RIGHT: case LEFT_RIGHT:
@ -136,47 +139,44 @@ 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){ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit){
if(isNotConnected(state)) // Set to blank state
world.setBlockState(pos, FULL_STATE, 3); if(hasConnection(state)) {
else world.setBlockState(pos, this.getDefaultState());
world.setBlockState(pos, this.getDefaultState(), 3);
System.out.println("onUse");
return ActionResult.SUCCESS; return ActionResult.SUCCESS;
} }
/* public void update(World world, BlockPos pos, BlockState state){ // Check if we are updating the state to a new state
System.out.println("Update"); BlockState newState = determineState(world, state, pos);
if(state != newState) {
world.setBlockState(pos, newState);
return ActionResult.SUCCESS;
} }
// Check if I'm this, return ActionResult.PASS;
// 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){ // This decides what I should look like whens something around me happens (use this for wire connection)
if (!world.isClient) { @Override
if (state.canPlaceAt(world, pos)) { public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState newState, WorldAccess world, BlockPos pos, BlockPos posFrom) {
return; if (direction != Direction.DOWN && direction != Direction.UP)
} else { state = state.with(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(direction), determineConnection(world, pos, direction));
return state;
}
@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); dropStacks(state, world, pos);
world.removeBlock(pos, false); world.removeBlock(pos, false);
} }
}
} }
static { static {