Working wireconnection w/ expected functionality

This commit is contained in:
Justin Parsell 2021-04-30 19:17:05 -04:00
parent cbfce1a065
commit ead8d12c6e
3 changed files with 48 additions and 36 deletions

View File

@ -1,8 +1,11 @@
package me.parsell.glowstonewire;
import java.util.Iterator;
import me.parsell.glowstonewire.core.glowBlocks;
import me.parsell.glowstonewire.core.glowItems;
import net.fabricmc.api.ModInitializer;
import net.minecraft.util.math.Direction;
public class GlowstoneWire implements ModInitializer {
@Override
@ -14,5 +17,13 @@ public class GlowstoneWire implements ModInitializer {
System.out.println("Hello Fabric world!");
glowBlocks.init();
glowItems.init();
String[] states = {"none", "broke", "side", "up"};
//for(int i = 0; i < 4; i++)
// for (int j = 0; j < 4; j++)
// for (int k = 0; k < 4; k++)
// for (int l = 0; l < 4; l++)
// System.out.println("north: " + states[i] + " south: " + states[j] + " east: " + states[k] + " west: " + states[l]);
}
}

View File

@ -23,10 +23,10 @@ public enum GlowWireConnection implements StringIdentifiable{
}
public boolean isBroken() {
return this != BROKE;
return this == BROKE;
}
public boolean isConnected() {
return this != NONE;
return this != NONE && this != BROKE;
}
}

View File

@ -4,15 +4,16 @@ import java.util.Map;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import java.util.Iterator;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.ShapeContext;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.player.PlayerEntity;
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.util.ActionResult;
import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation;
@ -77,22 +78,26 @@ public class GlowstoneWireBlock extends Block{
}
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));
Iterator<Direction> dirItr = Direction.Type.HORIZONTAL.iterator();
while(dirItr.hasNext()){
Direction dir = dirItr.next();
if(world.getBlockState(pos.offset(dir)).isOf(this) && !state.get(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(dir)).isBroken())
state = state.with(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(dir), determineConnection(world, pos, dir));
}
return state;
}
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()).get(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(direction.getOpposite())).isBroken())
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).down()).isOf(this))
if(!world.getBlockState(pos.offset(direction).down()).get(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(direction.getOpposite())).isBroken())
return GlowWireConnection.SIDE;
if(world.getBlockState(pos.offset(direction)).isOf(this))
if(!world.getBlockState(pos.offset(direction)).get(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(direction.getOpposite())).isBroken()) {
return GlowWireConnection.SIDE;
}
return GlowWireConnection.NONE;
}
@ -139,28 +144,24 @@ 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){
// Set to blank state
if(hasConnection(state)) {
world.setBlockState(pos, this.getDefaultState());
if(player.abilities.allowModifyWorld)
if(hasConnection(state) && !player.hasStackEquipped(EquipmentSlot.MAINHAND)) {
Iterator<Direction> dirItr = Direction.Type.HORIZONTAL.iterator();
while(dirItr.hasNext()){
Direction dir = dirItr.next();
if(state.get(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(dir)).isConnected())
state = state.with(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(dir), GlowWireConnection.BROKE);
}
world.setBlockState(pos, state);
world.updateNeighborsAlways(pos, this);
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;
}
// 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) {
System.out.println(world.getBlockState(posFrom).toString());
if (direction != Direction.DOWN && direction != Direction.UP)
if (direction != Direction.DOWN && direction != Direction.UP && !state.get(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(direction)).isBroken())
state = state.with(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(direction), determineConnection(world, pos, direction));
return state;
}