Compare commits

...

5 Commits

7 changed files with 154 additions and 94 deletions

View File

@ -1,29 +1,19 @@
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;
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
import net.minecraft.item.ItemGroup;
import net.minecraft.util.Identifier;
public class GlowstoneWire implements ModInitializer {
public static final String MODID = "GlowstoneWire";
public static final ItemGroup GLOWSTONEWIRE_MOD = FabricItemGroupBuilder.create(new Identifier(MODID, "general")).build();
@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
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

@ -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

@ -29,4 +29,8 @@ public enum GlowWireConnection implements StringIdentifiable{
public boolean isConnected() {
return this != NONE && this != BROKE;
}
public boolean isConnectedUp(){
return this == UP;
}
}

View File

@ -4,14 +4,20 @@ 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;
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.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.EnumProperty;
import net.minecraft.util.ActionResult;
@ -40,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
@ -56,20 +68,6 @@ public class GlowstoneWireBlock extends Block{
builder.add(WIRE_CONNECTION_NORTH, WIRE_CONNECTION_EAST, WIRE_CONNECTION_SOUTH, WIRE_CONNECTION_WEST);
}
private static boolean isFullyConnected(BlockState state) {
return ((GlowWireConnection)state.get(WIRE_CONNECTION_NORTH)).isConnected() &&
((GlowWireConnection)state.get(WIRE_CONNECTION_SOUTH)).isConnected() &&
((GlowWireConnection)state.get(WIRE_CONNECTION_EAST)).isConnected() &&
((GlowWireConnection)state.get(WIRE_CONNECTION_WEST)).isConnected();
}
private static boolean isNotConnected(BlockState state) {
return !((GlowWireConnection)state.get(WIRE_CONNECTION_NORTH)).isConnected() &&
!((GlowWireConnection)state.get(WIRE_CONNECTION_SOUTH)).isConnected() &&
!((GlowWireConnection)state.get(WIRE_CONNECTION_EAST)).isConnected() &&
!((GlowWireConnection)state.get(WIRE_CONNECTION_WEST)).isConnected();
}
private static boolean hasConnection(BlockState state) {
return ((GlowWireConnection)state.get(WIRE_CONNECTION_NORTH)).isConnected() ||
((GlowWireConnection)state.get(WIRE_CONNECTION_SOUTH)).isConnected() ||
@ -81,40 +79,52 @@ public class GlowstoneWireBlock extends Block{
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.get(dir)).isBroken())
state = state.with(DIRECTION_TO_WIRE_CONNECTION.get(dir), determineConnection(world, pos, dir));
state = state.with(DIRECTION_TO_WIRE_CONNECTION.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.get(direction.getOpposite())).isBroken())
return GlowWireConnection.UP;
if(world.getBlockState(pos.offset(direction).down()).isOf(this))
if(!world.getBlockState(pos.offset(direction).down()).get(DIRECTION_TO_WIRE_CONNECTION.get(direction.getOpposite())).isBroken())
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
return GlowWireConnection.SIDE;
if(world.getBlockState(pos.offset(direction)).isOf(this))
if(!world.getBlockState(pos.offset(direction)).get(DIRECTION_TO_WIRE_CONNECTION.get(direction.getOpposite())).isBroken()) {
if(world.getBlockState(pos.offset(direction)).isOf(this)) // if offset is me
if(!world.getBlockState(pos.offset(direction)).get(DIRECTION_TO_WIRE_CONNECTION.get(direction.getOpposite())).isBroken()) // if offset is not broken
return GlowWireConnection.SIDE;
}
return GlowWireConnection.NONE;
}
@Override
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
return STATE_TO_VOXELSHAPE.get(state);
}
public VoxelShape determineVoxelShape(BlockState state){
VoxelShape voxelShape = DOT_VOXELSHAPE;
Iterator var3 = Direction.Type.HORIZONTAL.iterator();
while(var3.hasNext()) {
Direction direction = (Direction)var3.next();
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));
}
}
return voxelShape;
}
@ -140,7 +150,7 @@ public class GlowstoneWireBlock extends Block{
default:
return state;
}
}
}
@Override
public BlockState mirror(BlockState state, BlockMirror mirror) {
@ -152,9 +162,10 @@ public class GlowstoneWireBlock extends Block{
default:
return super.mirror(state, mirror);
}
}
}
@Override
// Place block check disconnect (if block block connection)
@Override // For each link, set to break and update the blocks around me (above and below)
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit){
if(player.abilities.allowModifyWorld)
if(hasConnection(state) && !player.hasStackEquipped(EquipmentSlot.MAINHAND)) {
@ -162,34 +173,53 @@ public class GlowstoneWireBlock extends Block{
while(dirItr.hasNext()){
Direction dir = dirItr.next();
if(state.get(DIRECTION_TO_WIRE_CONNECTION.get(dir)).isConnected())
state = state.with(DIRECTION_TO_WIRE_CONNECTION.get(dir), GlowWireConnection.BROKE);
state = state.with(DIRECTION_TO_WIRE_CONNECTION.get(dir), GlowWireConnection.BROKE);
}
world.setBlockState(pos, state);
world.updateNeighborsAlways(pos, this);
// These call neighborUpdate not getStateForNeighborUpdate!!
//world.updateNeighborsAlways(pos, this); // Update those around 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
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState newState, WorldAccess world, BlockPos pos, BlockPos posFrom) {
if (direction != Direction.DOWN && direction != Direction.UP && !state.get(DIRECTION_TO_WIRE_CONNECTION.get(direction)).isBroken())
state = state.with(DIRECTION_TO_WIRE_CONNECTION.get(direction), determineConnection(world, pos, direction));
return state;
}
@Override
@Override // Get the state I should place in, and update those around me (above/below) as needed
public BlockState getPlacementState(ItemPlacementContext ctx){
return determineState(ctx.getWorld(), this.getDefaultState(), ctx.getBlockPos());
}
@Override
public void onPlaced(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack itemStack) {
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) {
if (!world.isClient && !state.canPlaceAt(world, pos)) {
dropStacks(state, world, pos);
world.removeBlock(pos, false);
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()){
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));
}
world.setBlockState(pos, state);
}
}
}
}
static {
WIRE_CONNECTION_NORTH = EnumProperty.of("north", GlowWireConnection.class);
@ -197,11 +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)))
);
}
}

View File

@ -1,5 +1,6 @@
package me.parsell.glowstonewire.core;
import me.parsell.glowstonewire.GlowstoneWire;
import me.parsell.glowstonewire.common.GlowstoneWireBlock;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.Block;
@ -11,7 +12,6 @@ public class glowBlocks {
public static final Block GLOWSTONEWIRE = new GlowstoneWireBlock(FabricBlockSettings.of(Material.SUPPORTED).breakInstantly().noCollision().luminance(8));
public static void init(){
Registry.register(Registry.BLOCK, new Identifier("glowstonewire", "glowstone_wire"), GLOWSTONEWIRE);
Registry.register(Registry.BLOCK, new Identifier(GlowstoneWire.MODID, "glowstone_wire"), GLOWSTONEWIRE);
}
}

View File

@ -1,13 +1,17 @@
package me.parsell.glowstonewire.core;
import me.parsell.glowstonewire.GlowstoneWire;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.Item;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
public class glowItems {
public static final Item Glowstone = new BlockItem(glowBlocks.GLOWSTONEWIRE, new FabricItemSettings().group(GlowstoneWire.GLOWSTONEWIRE_MOD));
public static void init(){
Registry.register(Registry.ITEM, new Identifier("glowstonewire", "glowstone_wire"), new BlockItem(glowBlocks.GLOWSTONEWIRE, new FabricItemSettings().group(ItemGroup.MISC)));
Registry.register(Registry.ITEM, new Identifier(GlowstoneWire.MODID, "glowstone"), Glowstone);
};
}

View File

@ -4,10 +4,10 @@
"when": {
"OR": [
{
"south": "none",
"north": "none",
"west": "none",
"east": "none"
"south": "none|broke",
"north": "none|broke",
"west": "none|broke",
"east": "none|broke"
},
{
"north": "side|up",
@ -39,9 +39,9 @@
},
{
"south": "side|up",
"north": "none",
"west": "none",
"east": "none"
"north": "none|broke",
"west": "none|broke",
"east": "none|broke"
}
]
},
@ -56,10 +56,16 @@
"south": "side|up"
},
{
"south": "none",
"south": "none|broke",
"north": "side|up",
"west": "none",
"east": "none"
"west": "none|broke",
"east": "none|broke"
},
{
"south": "broke",
"north": "side|up",
"west": "broke",
"east": "broke"
}
]
},
@ -74,10 +80,16 @@
"east": "side|up"
},
{
"south": "none",
"north": "none",
"south": "none|broke",
"north": "none|broke",
"west": "side|up",
"east": "none"
"east": "none|broke"
},
{
"south": "broke",
"north": "broke",
"west": "side|up",
"east": "broke"
}
]
},
@ -93,9 +105,15 @@
"west": "side|up"
},
{
"south": "none",
"north": "none",
"west": "none",
"south": "none|broke",
"north": "none|broke",
"west": "none|broke",
"east": "side|up"
},
{
"south": "broke",
"north": "broke",
"west": "broke",
"east": "side|up"
}
]