Skip to content

Commit fbf1f47

Browse files
committed
Add Contactor and Screwdriver
1 parent 9624e3b commit fbf1f47

21 files changed

Lines changed: 210 additions & 90 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ yarn_mappings=1.21.1+build.3
77
loader_version=0.16.10
88
fabric_version=0.115.1+1.21.1
99

10-
mod_version=1.0.0-alpha.20
10+
mod_version=1.0.0-alpha.22
1111
maven_group=falseresync.vivatech
1212
archives_base_name=vivatech
1313

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"variants": {
3+
"facing=east": {
4+
"model": "vivatech:block/contactor",
5+
"y": 90
6+
},
7+
"facing=north": {
8+
"model": "vivatech:block/contactor"
9+
},
10+
"facing=south": {
11+
"model": "vivatech:block/contactor",
12+
"y": 180
13+
},
14+
"facing=west": {
15+
"model": "vivatech:block/contactor",
16+
"y": 270
17+
}
18+
}
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"parent": "vivatech:block/cube_column_horizontal",
3+
"textures": {
4+
"end": "vivatech:block/contactor_top",
5+
"side": "vivatech:block/contactor_side"
6+
}
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"parent": "vivatech:block/contactor"
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"parent": "minecraft:item/generated",
3+
"textures": {
4+
"layer0": "vivatech:item/screwdriver"
5+
}
6+
}

src/main/java/falseresync/vivatech/common/Vivatech.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import falseresync.vivatech.common.data.VivatechComponents;
1010
import falseresync.vivatech.common.entity.VivatechEntities;
1111
import falseresync.vivatech.common.item.VivatechItemGroups;
12+
import falseresync.vivatech.common.item.VivatechItemTags;
1213
import falseresync.vivatech.common.item.VivatechItems;
1314
import falseresync.vivatech.common.item.focus.TransmutationFocusBehavior;
1415
import falseresync.vivatech.common.power.PowerSystem;
@@ -59,6 +60,7 @@ public void onInitialize() {
5960

6061
VivatechBlocks.registerAll();
6162
VivatechItems.registerAll();
63+
VivatechItemTags.init();
6264
new AutoRegistry(MOD_ID, LOGGER.getDelegate())
6365
.link(Registries.BLOCK_ENTITY_TYPE, VivatechBlockEntities.class)
6466
.link(Registries.ITEM_GROUP, VivatechItemGroups.class)
Lines changed: 112 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,131 @@
11
package falseresync.vivatech.common.block;
22

3-
import com.mojang.serialization.MapCodec;
4-
import falseresync.vivatech.common.blockentity.ContactorBlockEntity;
5-
import net.minecraft.block.BlockRenderType;
3+
import falseresync.vivatech.common.Vivatech;
4+
import falseresync.vivatech.common.item.VivatechItems;
5+
import falseresync.vivatech.common.power.GridEdge;
6+
import falseresync.vivatech.common.power.Wire;
7+
import net.minecraft.block.Block;
68
import net.minecraft.block.BlockState;
7-
import net.minecraft.block.BlockWithEntity;
8-
import net.minecraft.block.entity.BlockEntity;
9+
import net.minecraft.entity.player.PlayerEntity;
10+
import net.minecraft.item.ItemPlacementContext;
11+
import net.minecraft.item.ItemStack;
12+
import net.minecraft.server.world.ServerWorld;
13+
import net.minecraft.state.StateManager;
14+
import net.minecraft.state.property.BooleanProperty;
15+
import net.minecraft.state.property.DirectionProperty;
16+
import net.minecraft.state.property.Properties;
17+
import net.minecraft.util.*;
18+
import net.minecraft.util.hit.BlockHitResult;
919
import net.minecraft.util.math.BlockPos;
10-
import org.jetbrains.annotations.Nullable;
20+
import net.minecraft.util.math.Direction;
21+
import net.minecraft.util.math.random.Random;
22+
import net.minecraft.world.World;
1123

12-
public class ContactorBlock extends BlockWithEntity {
13-
public static final MapCodec<ContactorBlock> CODEC = createCodec(ContactorBlock::new);
24+
public class ContactorBlock extends Block {
25+
public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING;
26+
public static final BooleanProperty POWERED = Properties.POWERED;
27+
public static final BooleanProperty NORMALLY_OPEN = BooleanProperty.of("normally_open");
1428

1529
protected ContactorBlock(Settings settings) {
1630
super(settings);
31+
setDefaultState(stateManager.getDefaultState()
32+
.with(FACING, Direction.NORTH)
33+
.with(POWERED, false)
34+
.with(NORMALLY_OPEN, true));
1735
}
1836

1937
@Override
20-
protected MapCodec<ContactorBlock> getCodec() {
21-
return CODEC;
38+
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
39+
builder.add(FACING, POWERED, NORMALLY_OPEN);
2240
}
2341

24-
@Nullable
2542
@Override
26-
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
27-
return new ContactorBlockEntity(pos, state);
43+
public BlockState getPlacementState(ItemPlacementContext ctx) {
44+
return getDefaultState().with(FACING, ctx.getHorizontalPlayerFacing());
2845
}
2946

3047
@Override
31-
protected BlockRenderType getRenderType(BlockState state) {
32-
return BlockRenderType.MODEL;
48+
protected BlockState rotate(BlockState state, BlockRotation rotation) {
49+
return state.with(FACING, rotation.rotate(state.get(FACING)));
50+
}
51+
52+
@Override
53+
protected BlockState mirror(BlockState state, BlockMirror mirror) {
54+
return state.rotate(mirror.getRotation(state.get(FACING)));
55+
}
56+
57+
@Override
58+
protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
59+
if (stack.isOf(VivatechItems.SCREWDRIVER)) {
60+
if (!world.isClient) {
61+
world.setBlockState(pos, state.cycle(NORMALLY_OPEN), Block.NOTIFY_LISTENERS);
62+
}
63+
64+
return ItemActionResult.SUCCESS;
65+
}
66+
67+
return super.onUseWithItem(stack, state, world, pos, player, hand, hit);
68+
}
69+
70+
@Override
71+
protected void neighborUpdate(BlockState state, World world, BlockPos pos, Block sourceBlock, BlockPos sourcePos, boolean notify) {
72+
if (!world.isClient) {
73+
var powered = state.get(POWERED);
74+
if (powered != world.isReceivingRedstonePower(pos)) {
75+
if (powered) {
76+
world.scheduleBlockTick(pos, this, 4);
77+
} else {
78+
world.setBlockState(pos, state.cycle(POWERED), Block.NOTIFY_LISTENERS);
79+
}
80+
}
81+
}
82+
super.neighborUpdate(state, world, pos, sourceBlock, sourcePos, notify);
83+
}
84+
85+
@Override
86+
protected void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
87+
if (state.isOf(newState.getBlock())) {
88+
manageGridConnection(state, world, pos);
89+
}
90+
super.onStateReplaced(state, world, pos, newState, moved);
91+
}
92+
93+
private static void manageGridConnection(BlockState state, World world, BlockPos pos) {
94+
var facing = state.get(FACING);
95+
var gridsLookup = Vivatech.getServerGridsLoader().getGridsManager(world).getGridLookup();
96+
97+
var posU = pos.offset(facing.rotateYClockwise());
98+
var gridU = gridsLookup.get(posU);
99+
if (gridU == null) {
100+
return;
101+
}
102+
103+
var posV = pos.offset(facing.rotateYCounterclockwise());
104+
var gridV = gridsLookup.get(posV);
105+
if (gridV == null) {
106+
return;
107+
}
108+
109+
var powered = state.get(POWERED);
110+
if (!state.get(NORMALLY_OPEN)) { // I have no idea why this needs to be inverted... I am probably just dumb
111+
if (powered && gridU != gridV) {
112+
gridU.connect(new GridEdge(posU, posV), true);
113+
} else if (!powered && gridU == gridV) {
114+
gridU.disconnect(new GridEdge(posU, posV), Wire.DropRule.NO_DROP);
115+
}
116+
} else {
117+
if (powered && gridU == gridV) {
118+
gridU.disconnect(new GridEdge(posU, posV), Wire.DropRule.NO_DROP);
119+
} else if (!powered && gridU != gridV) {
120+
gridU.connect(new GridEdge(posU, posV), true);
121+
}
122+
}
123+
}
124+
125+
@Override
126+
protected void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
127+
if (state.get(POWERED) && !world.isReceivingRedstonePower(pos)) {
128+
world.setBlockState(pos, state.cycle(POWERED), Block.NOTIFY_LISTENERS);
129+
}
33130
}
34131
}

src/main/java/falseresync/vivatech/common/block/VivatechBlocks.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.minecraft.block.AbstractBlock;
44
import net.minecraft.block.Block;
55
import net.minecraft.block.Blocks;
6+
import net.minecraft.block.piston.PistonBehavior;
67
import net.minecraft.registry.Registries;
78
import net.minecraft.registry.Registry;
89

@@ -19,7 +20,7 @@ public class VivatechBlocks {
1920
public static final ChargerBlock CHARGER = r("charger", ChargerBlock::new, AbstractBlock.Settings.copy(Blocks.COPPER_BLOCK));
2021

2122
public static final StaticCompensatorBlock STATIC_COMPENSATOR = r("static_compensator", StaticCompensatorBlock::new, AbstractBlock.Settings.copy(Blocks.COPPER_BLOCK));
22-
public static final StaticCompensatorBlock CONTACTOR = r("contactor", StaticCompensatorBlock::new, AbstractBlock.Settings.copy(Blocks.COPPER_BLOCK));
23+
public static final ContactorBlock CONTACTOR = r("contactor", ContactorBlock::new, AbstractBlock.Settings.copy(Blocks.COPPER_BLOCK).pistonBehavior(PistonBehavior.BLOCK));
2324

2425
public static final WirePostBlock WIRE_POST = r("wire_post", WirePostBlock::new, AbstractBlock.Settings.copy(Blocks.LIGHTNING_ROD));
2526

src/main/java/falseresync/vivatech/common/block/WirePostBlock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected BlockState mirror(BlockState state, BlockMirror mirror) {
8383
@Override
8484
public GridVertex getGridVertex(World world, BlockPos pos, BlockState state) {
8585
var facing = state.get(FACING);
86-
return new GridVertex(pos, PowerSystem.APPLIANCE.find(world, pos.offset(facing), facing));
86+
return new GridVertex(pos, facing, PowerSystem.APPLIANCE.find(world, pos.offset(facing), facing));
8787
}
8888

8989
@Override

src/main/java/falseresync/vivatech/common/blockentity/ContactorBlockEntity.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)