diff --git a/src/main/java/com/hechu/mindustry/distribution/Conveyor.java b/src/main/java/com/hechu/mindustry/distribution/Conveyor.java new file mode 100644 index 0000000..70d8bc7 --- /dev/null +++ b/src/main/java/com/hechu/mindustry/distribution/Conveyor.java @@ -0,0 +1,31 @@ +package com.hechu.mindustry.distribution; + +import com.hechu.mindustry.MindustryConstants; +import net.minecraft.network.chat.Component; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.event.level.BlockEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; + +@Mod.EventBusSubscriber(modid = MindustryConstants.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE) +public class Conveyor { + @SubscribeEvent + public static void onConveyorEvent(TickEvent.ServerTickEvent event) { + if (event.phase == TickEvent.Phase.START && event.getServer().getTickCount() % 20 == 0) { +// event.getServer().getPlayerList().getPlayers().forEach(p -> p.displayClientMessage(Component.literal("Conveyor event fired!"), true)); +// event.getServer().sendSystemMessage(Component.literal("Conveyor event fired!")); + } + } + + @SubscribeEvent + public static void onBreakEvent(BlockEvent.BreakEvent event) { + event.getPlayer().displayClientMessage(Component.literal("Block broken!"), true); + } + + @SubscribeEvent + public static void onPlaceEvent(BlockEvent.EntityPlaceEvent event) { + if (event.getEntity() != null && event.getEntity() instanceof net.minecraft.world.entity.player.Player player) + player.displayClientMessage(Component.literal("Block placed!"), true); + } +} diff --git a/src/main/java/com/hechu/mindustry/world/level/block/distribution/ConveyorBlock.java b/src/main/java/com/hechu/mindustry/world/level/block/distribution/ConveyorBlock.java index da2ee40..0d35d4c 100644 --- a/src/main/java/com/hechu/mindustry/world/level/block/distribution/ConveyorBlock.java +++ b/src/main/java/com/hechu/mindustry/world/level/block/distribution/ConveyorBlock.java @@ -1,7 +1,6 @@ package com.hechu.mindustry.world.level.block.distribution; import com.hechu.mindustry.world.level.block.entity.distribution.ConveyorBlockEntity; -import com.hechu.mindustry.world.level.block.entity.turrets.TurretBlockEntityBase; import com.hechu.mindustry.world.level.block.state.properties.ConveyorShape; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; @@ -18,14 +17,17 @@ import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.level.block.state.properties.BooleanProperty; import net.minecraft.world.level.block.state.properties.EnumProperty; -import net.minecraft.world.level.block.state.properties.RailShape; -import net.minecraft.world.level.material.FluidState; import net.minecraft.world.level.material.Fluids; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.VoxelShape; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Arrays; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + public class ConveyorBlock extends BaseEntityBlock { public static final EnumProperty SHAPE = EnumProperty.create("shape", ConveyorShape.class); public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED; @@ -45,8 +47,8 @@ protected void createBlockStateDefinition(StateDefinition.Builder - HALF_BLOCK_AABB; + case DESCENDING_EAST, DESCENDING_NORTH, DESCENDING_SOUTH, DESCENDING_WEST, ASCENDING_EAST, ASCENDING_NORTH, + ASCENDING_SOUTH, ASCENDING_WEST -> HALF_BLOCK_AABB; default -> FLAT_AABB; }; } @@ -65,20 +67,25 @@ public BlockEntityTicker getTicker(@NotNull Level lev }; } - protected void updateState(BlockState pState, Level pLevel, BlockPos pPos, Block pBlock) { -// if (pBlock.defaultBlockState().isSignalSource() && (new RailState(pLevel, pPos, pState)).getConnections() == 3) { -// this.updateDir(pLevel, pPos, pState, false); -// } + protected void updateState(BlockState state, Level level, BlockPos pos, Block block) { + ConveyorShape shape = state.getValue(SHAPE); + BlockState blockState = this.updateDir(level, pos, state, false); + if (blockState.getValue(SHAPE) != shape) { + level.setBlock(pos, blockState, 3); + level.neighborChanged(pos, block, pos); + } } -// protected BlockState updateDir(Level pLevel, BlockPos pPos, BlockState pState, boolean pAlwaysPlace) { -// if (pLevel.isClientSide) { -// return pState; -// } else { -// RailShape railshape = pState.getValue(SHAPE); -// return (new RailState(pLevel, pPos, pState)).place(pLevel.hasNeighborSignal(pPos), pAlwaysPlace, railshape).getState(); -// } -// } + protected BlockState updateDir(Level level, BlockPos pos, BlockState state, boolean alwaysplace) { + if (level.isClientSide) { + return state; + } else { + BlockState blockstate = super.defaultBlockState(); + ConveyorShape shape = getConveyorShape(level, state.getValue(SHAPE).getOutputDirection(), pos); + blockstate = blockstate.setValue(SHAPE, shape); + return blockstate; + } + } public void neighborChanged(BlockState pState, Level pLevel, BlockPos pPos, Block pBlock, BlockPos pFromPos, boolean pIsMoving) { if (!pLevel.isClientSide && pLevel.getBlockState(pPos).is(this)) { @@ -125,13 +132,136 @@ public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState } } - public BlockState getStateForPlacement(BlockPlaceContext pContext) { - FluidState fluidstate = pContext.getLevel().getFluidState(pContext.getClickedPos()); - boolean flag = fluidstate.getType() == Fluids.WATER; + public BlockState getStateForPlacement(BlockPlaceContext context) { BlockState blockstate = super.defaultBlockState(); - Direction direction = pContext.getHorizontalDirection(); - boolean flag1 = direction == Direction.EAST || direction == Direction.WEST; - return blockstate.setValue(SHAPE, flag1 ? ConveyorShape.EAST_WEST : ConveyorShape.NORTH_SOUTH).setValue(WATERLOGGED, Boolean.valueOf(flag)); + ConveyorShape shape = getConveyorShape(context.getLevel(), context.getHorizontalDirection(), context.getClickedPos()); + blockstate = blockstate.setValue(SHAPE, shape); + return blockstate; + } + + private static @NotNull ConveyorShape getConveyorShape(Level level, Direction outputDirection, BlockPos pos) { + BlockPos outputPos = pos.relative(Direction.UP); + ConveyorShape shape; + boolean output = Stream.of(level.getBlockState(outputPos)) + .filter(s -> s.getValues().containsKey(SHAPE)) + .flatMap(s -> Arrays.stream(s.getValue(SHAPE).getInputBlockPos(outputPos))) + .anyMatch(pos::equals); + boolean outputUp = Stream.of(level.getBlockState(outputPos.above())) + .filter(s -> s.getValues().containsKey(SHAPE)) + .map(s -> Arrays.stream(s.getValue(SHAPE).getInputBlockPos(outputPos.above()))) + .anyMatch(pos.above()::equals); + if (!output && outputUp) { + shape = switch (outputDirection) { + case NORTH -> ConveyorShape.ASCENDING_NORTH; + case SOUTH -> ConveyorShape.ASCENDING_SOUTH; + case WEST -> ConveyorShape.ASCENDING_WEST; + case EAST -> ConveyorShape.ASCENDING_EAST; + default -> ConveyorShape.NORTH_SOUTH; + }; + } else { + Direction inputDirection = outputDirection.getOpposite(); + boolean input = Stream.of(level.getBlockState(pos.relative(inputDirection))) + .filter(s -> s.getValues().containsKey(SHAPE)) + .map(s -> s.getValue(SHAPE).getOutputBlockPos(pos.relative(inputDirection))) + .anyMatch(pos::equals); + boolean inputUp = Stream.of(level.getBlockState(pos.relative(inputDirection).above())) + .filter(s -> s.getValues().containsKey(SHAPE)) + .map(s -> s.getValue(SHAPE).getOutputBlockPos(pos.relative(inputDirection).above())) + .anyMatch(pos.above()::equals); + if (!input && inputUp) { + shape = switch (outputDirection) { + case NORTH -> ConveyorShape.DESCENDING_NORTH; + case SOUTH -> ConveyorShape.DESCENDING_SOUTH; + case WEST -> ConveyorShape.DESCENDING_WEST; + case EAST -> ConveyorShape.DESCENDING_EAST; + default -> ConveyorShape.NORTH_SOUTH; + }; + } else { + Set inputDirections = Arrays.stream(Direction.values()) + .filter(d -> d != outputDirection && d != Direction.UP && d != Direction.DOWN) + .filter(d -> Stream.of(pos.relative(d)) + .filter(p -> level.getBlockState(p).getValues().containsKey(SHAPE)) + .anyMatch(p -> level.getBlockState(p).getValue(SHAPE).getOutputBlockPos(p).equals(pos)) + ) + .collect(Collectors.toSet()); + switch (outputDirection) { + case NORTH -> { + if (inputDirections.containsAll(Set.of(Direction.SOUTH, Direction.WEST, Direction.EAST))) + shape = ConveyorShape.NORTH_ALL; + else if (inputDirections.containsAll(Set.of(Direction.SOUTH, Direction.WEST))) + shape = ConveyorShape.NORTH_WEST_SOUTH; + else if (inputDirections.containsAll(Set.of(Direction.SOUTH, Direction.EAST))) + shape = ConveyorShape.NORTH_EAST_SOUTH; + else if (inputDirections.containsAll(Set.of(Direction.WEST, Direction.EAST))) + shape = ConveyorShape.NORTH_WEST_EAST; + else if (inputDirections.contains(Direction.SOUTH)) + shape = ConveyorShape.NORTH_SOUTH; + else if (inputDirections.contains(Direction.WEST)) + shape = ConveyorShape.NORTH_WEST; + else if (inputDirections.contains(Direction.EAST)) + shape = ConveyorShape.NORTH_EAST; + else + shape = ConveyorShape.NORTH_SOUTH; + } + case SOUTH -> { + if (inputDirections.containsAll(Set.of(Direction.NORTH, Direction.WEST, Direction.EAST))) + shape = ConveyorShape.SOUTH_ALL; + else if (inputDirections.containsAll(Set.of(Direction.NORTH, Direction.WEST))) + shape = ConveyorShape.SOUTH_WEST_NORTH; + else if (inputDirections.containsAll(Set.of(Direction.NORTH, Direction.EAST))) + shape = ConveyorShape.SOUTH_EAST_NORTH; + else if (inputDirections.containsAll(Set.of(Direction.WEST, Direction.EAST))) + shape = ConveyorShape.SOUTH_WEST_EAST; + else if (inputDirections.contains(Direction.NORTH)) + shape = ConveyorShape.SOUTH_NORTH; + else if (inputDirections.contains(Direction.WEST)) + shape = ConveyorShape.SOUTH_WEST; + else if (inputDirections.contains(Direction.EAST)) + shape = ConveyorShape.SOUTH_EAST; + else + shape = ConveyorShape.SOUTH_NORTH; + } + case WEST -> { + if (inputDirections.containsAll(Set.of(Direction.NORTH, Direction.SOUTH, Direction.EAST))) + shape = ConveyorShape.WEST_ALL; + else if (inputDirections.containsAll(Set.of(Direction.NORTH, Direction.SOUTH))) + shape = ConveyorShape.WEST_NORTH_SOUTH; + else if (inputDirections.containsAll(Set.of(Direction.NORTH, Direction.EAST))) + shape = ConveyorShape.WEST_NORTH_EAST; + else if (inputDirections.containsAll(Set.of(Direction.SOUTH, Direction.EAST))) + shape = ConveyorShape.WEST_SOUTH_EAST; + else if (inputDirections.contains(Direction.NORTH)) + shape = ConveyorShape.WEST_NORTH; + else if (inputDirections.contains(Direction.SOUTH)) + shape = ConveyorShape.WEST_SOUTH; + else if (inputDirections.contains(Direction.EAST)) + shape = ConveyorShape.WEST_EAST; + else + shape = ConveyorShape.WEST_EAST; + } + case EAST -> { + if (inputDirections.containsAll(Set.of(Direction.NORTH, Direction.SOUTH, Direction.WEST))) + shape = ConveyorShape.EAST_ALL; + else if (inputDirections.containsAll(Set.of(Direction.NORTH, Direction.SOUTH))) + shape = ConveyorShape.EAST_NORTH_SOUTH; + else if (inputDirections.containsAll(Set.of(Direction.NORTH, Direction.WEST))) + shape = ConveyorShape.EAST_NORTH_WEST; + else if (inputDirections.containsAll(Set.of(Direction.SOUTH, Direction.WEST))) + shape = ConveyorShape.EAST_SOUTH_WEST; + else if (inputDirections.contains(Direction.NORTH)) + shape = ConveyorShape.EAST_NORTH; + else if (inputDirections.contains(Direction.SOUTH)) + shape = ConveyorShape.EAST_SOUTH; + else if (inputDirections.contains(Direction.WEST)) + shape = ConveyorShape.EAST_WEST; + else + shape = ConveyorShape.EAST_WEST; + } + default -> shape = ConveyorShape.NORTH_SOUTH; + } + } + } + return shape; } /** diff --git a/src/main/java/com/hechu/mindustry/world/level/block/state/properties/ConveyorShape.java b/src/main/java/com/hechu/mindustry/world/level/block/state/properties/ConveyorShape.java index adb3429..a032d3e 100644 --- a/src/main/java/com/hechu/mindustry/world/level/block/state/properties/ConveyorShape.java +++ b/src/main/java/com/hechu/mindustry/world/level/block/state/properties/ConveyorShape.java @@ -1,5 +1,7 @@ package com.hechu.mindustry.world.level.block.state.properties; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.util.StringRepresentable; import org.jetbrains.annotations.NotNull; @@ -59,6 +61,149 @@ public String toString() { return this.name; } + public Direction getOutputDirection() { + switch (this) { + case NORTH_SOUTH, NORTH_WEST, NORTH_ALL, NORTH_WEST_EAST, NORTH_EAST_SOUTH, NORTH_WEST_SOUTH, NORTH_EAST, + ASCENDING_NORTH, DESCENDING_NORTH -> { + return Direction.NORTH; + } + case SOUTH_NORTH, SOUTH_ALL, SOUTH_WEST_EAST, SOUTH_EAST_NORTH, SOUTH_WEST_NORTH, SOUTH_EAST, SOUTH_WEST, + ASCENDING_SOUTH, DESCENDING_SOUTH -> { + return Direction.SOUTH; + } + case WEST_EAST, WEST_ALL, WEST_NORTH_SOUTH, WEST_SOUTH_EAST, WEST_NORTH_EAST, WEST_SOUTH, WEST_NORTH, + ASCENDING_WEST, DESCENDING_WEST -> { + return Direction.WEST; + } + case EAST_WEST, EAST_ALL, EAST_NORTH_SOUTH, EAST_SOUTH_WEST, EAST_NORTH_WEST, EAST_SOUTH, EAST_NORTH, + DESCENDING_EAST, ASCENDING_EAST -> { + return Direction.EAST; + } + } + return Direction.NORTH; + } + + public BlockPos getOutputBlockPos(BlockPos origin){ + switch (this) { + case NORTH_SOUTH, NORTH_WEST, NORTH_ALL, NORTH_WEST_EAST, NORTH_EAST_SOUTH, NORTH_WEST_SOUTH, NORTH_EAST, + DESCENDING_NORTH -> { + return origin.north(); + } + case SOUTH_NORTH, SOUTH_ALL, SOUTH_WEST_EAST, SOUTH_EAST_NORTH, SOUTH_WEST_NORTH, SOUTH_EAST, SOUTH_WEST, + DESCENDING_SOUTH -> { + return origin.south(); + } + case WEST_EAST, WEST_ALL, WEST_NORTH_SOUTH, WEST_SOUTH_EAST, WEST_NORTH_EAST, WEST_SOUTH, WEST_NORTH, + DESCENDING_WEST -> { + return origin.west(); + } + case EAST_WEST, EAST_ALL, EAST_NORTH_SOUTH, EAST_SOUTH_WEST, EAST_NORTH_WEST, EAST_SOUTH, EAST_NORTH, + DESCENDING_EAST -> { + return origin.east(); + } + case ASCENDING_NORTH -> { + return origin.north().above(); + } + case ASCENDING_SOUTH -> { + return origin.south().above(); + } + case ASCENDING_WEST -> { + return origin.west().above(); + } + case ASCENDING_EAST -> { + return origin.east().above(); + } + } + return origin; + } + + public BlockPos[] getInputBlockPos(BlockPos origin){ + switch (this) { + case NORTH_SOUTH, EAST_SOUTH, WEST_SOUTH -> { + return new BlockPos[]{origin.south()}; + } + case SOUTH_NORTH, EAST_NORTH, WEST_NORTH -> { + return new BlockPos[]{origin.north()}; + } + case WEST_EAST, SOUTH_EAST, NORTH_EAST -> { + return new BlockPos[]{origin.east()}; + } + case EAST_WEST, SOUTH_WEST, NORTH_WEST -> { + return new BlockPos[]{origin.west()}; + } + case NORTH_WEST_SOUTH -> { + return new BlockPos[]{origin.west(), origin.south()}; + } + case NORTH_EAST_SOUTH -> { + return new BlockPos[]{origin.east(), origin.south()}; + } + case NORTH_WEST_EAST, SOUTH_WEST_EAST -> { + return new BlockPos[]{origin.west(), origin.east()}; + } + case SOUTH_WEST_NORTH -> { + return new BlockPos[]{origin.west(), origin.north()}; + } + case SOUTH_EAST_NORTH -> { + return new BlockPos[]{origin.east(), origin.north()}; + } + case WEST_NORTH_EAST -> { + return new BlockPos[]{origin.north(), origin.east()}; + } + case WEST_SOUTH_EAST -> { + return new BlockPos[]{origin.south(), origin.east()}; + } + case WEST_NORTH_SOUTH -> { + return new BlockPos[]{origin.north(), origin.south()}; + } + case EAST_NORTH_WEST -> { + return new BlockPos[]{origin.north(), origin.west()}; + } + case EAST_SOUTH_WEST -> { + return new BlockPos[]{origin.south(), origin.west()}; + } + case EAST_NORTH_SOUTH -> { + return new BlockPos[]{origin.north(), origin.south()}; + } + case NORTH_ALL -> { + return new BlockPos[]{origin.south(), origin.west(), origin.east()}; + } + case SOUTH_ALL -> { + return new BlockPos[]{origin.north(), origin.west(), origin.east()}; + } + case WEST_ALL -> { + return new BlockPos[]{origin.north(), origin.south(), origin.east()}; + } + case EAST_ALL -> { + return new BlockPos[]{origin.north(), origin.south(), origin.west()}; + } + case ASCENDING_NORTH -> { + return new BlockPos[]{origin.south(),origin.south().below()}; + } + case ASCENDING_SOUTH -> { + return new BlockPos[]{origin.north(),origin.north().below()}; + } + case ASCENDING_WEST -> { + return new BlockPos[]{origin.east(),origin.east().below()}; + } + case ASCENDING_EAST -> { + return new BlockPos[]{origin.west(),origin.west().below()}; + } + case DESCENDING_NORTH -> { + return new BlockPos[]{origin.south(),origin.south().above()}; + } + case DESCENDING_SOUTH -> { + return new BlockPos[]{origin.north(),origin.north().above()}; + } + case DESCENDING_WEST -> { + return new BlockPos[]{origin.east(),origin.east().above()}; + } + case DESCENDING_EAST -> { + return new BlockPos[]{origin.west(),origin.west().above()}; + } + } + return new BlockPos[]{}; + } + @Override public @NotNull String getSerializedName() { return this.name; diff --git a/src/main/resources/assets/mindustry/blockstates/conveyor.json b/src/main/resources/assets/mindustry/blockstates/conveyor.json index a68287c..d13321c 100644 --- a/src/main/resources/assets/mindustry/blockstates/conveyor.json +++ b/src/main/resources/assets/mindustry/blockstates/conveyor.json @@ -5,7 +5,7 @@ }, "shape=north_south": { "model": "mindustry:block/conveyor_0", - "y": 90 + "y": 270 }, "shape=west_east": { "model": "mindustry:block/conveyor_0", @@ -13,14 +13,14 @@ }, "shape=south_north": { "model": "mindustry:block/conveyor_0", - "y": 270 + "y": 90 }, "shape=east_north": { "model": "mindustry:block/conveyor_1" }, "shape=north_west": { "model": "mindustry:block/conveyor_1", - "y": 90 + "y": 270 }, "shape=west_south": { "model": "mindustry:block/conveyor_1", @@ -28,14 +28,14 @@ }, "shape=south_east": { "model": "mindustry:block/conveyor_1", - "y": 270 + "y": 90 }, "shape=west_north": { "model": "mindustry:block/conveyor_1_m" }, "shape=south_west": { "model": "mindustry:block/conveyor_1_m", - "y": 90 + "y": 270 }, "shape=east_south": { "model": "mindustry:block/conveyor_1_m", @@ -43,14 +43,14 @@ }, "shape=north_east": { "model": "mindustry:block/conveyor_1_m", - "y": 270 + "y": 90 }, "shape=east_south_west": { "model": "mindustry:block/conveyor_2" }, "shape=north_east_south": { "model": "mindustry:block/conveyor_2", - "y": 90 + "y": 270 }, "shape=west_north_east": { "model": "mindustry:block/conveyor_2", @@ -58,14 +58,14 @@ }, "shape=south_west_north": { "model": "mindustry:block/conveyor_2", - "y": 270 + "y": 90 }, "shape=west_south_east": { "model": "mindustry:block/conveyor_2_m" }, "shape=south_east_north": { "model": "mindustry:block/conveyor_2_m", - "y": 90 + "y": 270 }, "shape=east_north_west": { "model": "mindustry:block/conveyor_2_m", @@ -73,14 +73,14 @@ }, "shape=north_west_south": { "model": "mindustry:block/conveyor_2_m", - "y": 270 + "y": 90 }, "shape=east_all": { "model": "mindustry:block/conveyor_3" }, "shape=north_all": { "model": "mindustry:block/conveyor_3", - "y": 90 + "y":270 }, "shape=west_all": { "model": "mindustry:block/conveyor_3", @@ -88,14 +88,14 @@ }, "shape=south_all": { "model": "mindustry:block/conveyor_3", - "y": 270 + "y": 90 }, "shape=east_north_south": { "model": "mindustry:block/conveyor_4" }, "shape=north_west_east": { "model": "mindustry:block/conveyor_4", - "y": 90 + "y": 270 }, "shape=west_north_south": { "model": "mindustry:block/conveyor_4", @@ -103,7 +103,7 @@ }, "shape=south_west_east": { "model": "mindustry:block/conveyor_4", - "y": 270 + "y": 90 }, "shape=ascending_east": { "model": "mindustry:block/conveyor_5" @@ -125,7 +125,7 @@ }, "shape=descending_south": { "model": "mindustry:block/conveyor_5_m", - "y": 90 + "y": 270 }, "shape=descending_east": { "model": "mindustry:block/conveyor_5_m", @@ -133,7 +133,7 @@ }, "shape=descending_north": { "model": "mindustry:block/conveyor_5_m", - "y": 270 + "y": 90 } } } \ No newline at end of file