Skip to content

Commit

Permalink
More work on multiblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Rearth committed Jan 28, 2024
1 parent c236462 commit 15dffed
Show file tree
Hide file tree
Showing 18 changed files with 319 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// 1.20.4 2024-01-28T18:29:04.8090958 Oritech/Model Definitions
// 1.20.4 2024-01-28T23:15:58.7343725 Oritech/Model Definitions
a5a8d2e834fc121777ed1a4c0df99e85a6f23613 assets\oritech\models\block\machine_core.json
3d6f377b938592ac22aca17bd64561a6579ad217 assets\oritech\blockstates\grinder_block.json
f29468ff950a191567ab35557334666099ccd7cc assets\oritech\blockstates\assembler_block.json
964457f56e25b66dfef2160c8618bcaba2b640aa assets\oritech\blockstates\pulverizer_block.json
5462a2453fcdd2f8b99129894aec60f282d29ca9 assets\oritech\models\item\pulverizer_block.json
eafe84c787830ab2a866e0878e9ed718d58659f5 assets\oritech\blockstates\banana_block.json
25864df3cd7786f2bf4fe6fa9855b3cf366c28ca assets\oritech\models\item\banana.json
339d54da2345c657c8ecff8e8dbda1734fd88d8d assets\oritech\models\block\assembler_block.json
469ea8a66c529fe26bc029c21092e7f062ed0e40 assets\oritech\models\block\banana_block.json
d35ecef5df9198b12dc04267780c727c144b1959 assets\oritech\models\item\assembler_block.json
3bd3ce424e506dbbd4a45a0081e0f04b914abb27 assets\oritech\models\item\machine_core.json
c48da59250e22665d33524132de2dfd27f0c574f assets\oritech\models\block\grinder_block.json
746b98d721e5ae0d96dd769e71b786afa8c2d142 assets\oritech\models\item\banana_block.json
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "oritech:block/assembler_block"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:block/cube_all",
"textures": {
"all": "oritech:block/assembler_block"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"parent": "oritech:block/assembler_block"
}
75 changes: 1 addition & 74 deletions src/main/java/rearth/oritech/block/base/MachineBlockEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,9 @@ public AnimatableInstanceCache getAnimatableInstanceCache() {
public void writeScreenOpeningData(ServerPlayerEntity player, PacketByteBuf buf) {
buf.writeBlockPos(this.getPos());
sendNetworkEntry();
initMultiblock();
}

private Direction getFacing() {
protected Direction getFacing() {
return Objects.requireNonNull(world).getBlockState(getPos()).get(Properties.HORIZONTAL_FACING);
}

Expand Down Expand Up @@ -477,76 +476,4 @@ public InventoryInputMode getInventoryInputMode() {

public abstract int getInventorySize();

// positive x = forward
// positive y = up
// positive z = right?
public List<Vec3i> getCorePositions() {
return List.of(
new Vec3i(1, 0,0),
new Vec3i(2, 0,0),
new Vec3i(0, 1, 0)
);
}

private boolean assembled = false;

public void initMultiblock() {

// check if multiblock is already created, if so cancel
// call method the get a list of relative positions
// check all positions if the blocks there extend MachineCoreBlock
// if so, add them to list of used blocks
// if not (e.g. block wrong type or air), draw a small particle to indicate the missing position
// when all blocks are valid, multiblock is active
// update all multiblocks state to USED=true, write controller position to block state

if (assembled) return;

var ownFacing = getFacing();

var targetPositions = getCorePositions();
var coreBlocks = new ArrayList<MultiBlockElement>(targetPositions.size());

for (var targetPosition : targetPositions) {
var rotatedPos = rotatePosition(targetPosition, ownFacing); //todo
var checkPos = pos.add(rotatedPos);
var checkState = Objects.requireNonNull(world).getBlockState(checkPos);

var blockType = checkState.getBlock();
if (blockType instanceof MachineCoreBlock coreBlock) {
coreBlocks.add(new MultiBlockElement(checkState, coreBlock, checkPos));
} else {
highlightBlock(checkPos);
}
}

if (targetPositions.size() == coreBlocks.size()) {
// valid
assembled = true;
for (var core : coreBlocks) {
world.setBlockState(core.pos, core.state.with(MachineCoreBlock.USED, true));
System.out.println("multiblock valid");
}
} else {
// invalid
System.out.println("multiblock invalid");
}

}

private void highlightBlock(BlockPos block) {
ParticleContent.HIGHLIGHT_BLOCK.spawn(world, Vec3d.of(block), null);
}

private Vec3i rotatePosition(Vec3i relativePos, Direction facing) {
return switch (facing) {
case NORTH -> new BlockPos(relativePos.getZ(), relativePos.getY(), relativePos.getX());
case EAST -> new BlockPos(-relativePos.getX(), relativePos.getY(), -relativePos.getZ());
case SOUTH -> new BlockPos(-relativePos.getZ(), relativePos.getY(), -relativePos.getX());
case WEST -> new BlockPos(relativePos.getX(), relativePos.getY(), relativePos.getZ());
default -> relativePos;
};
}

private static record MultiBlockElement(BlockState state, MachineCoreBlock coreBlock, BlockPos pos) {}
}
54 changes: 54 additions & 0 deletions src/main/java/rearth/oritech/block/base/MultiblockMachine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package rearth.oritech.block.base;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.IntProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;

public abstract class MultiblockMachine extends MachineBlock {

public static final BooleanProperty ASSEMBLED = BooleanProperty.of("machine_assembled");

public MultiblockMachine(Settings settings) {
super(settings);
setDefaultState(getDefaultState().with(ASSEMBLED, false));
}

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
super.appendProperties(builder);
builder.add(ASSEMBLED);
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {

if (!world.isClient) {

var entity = world.getBlockEntity(pos);
if (!(entity instanceof MultiblockMachineEntity machineEntity)) {
return ActionResult.FAIL;
}

var isAssembled = machineEntity.initMultiblock(state);

if (!isAssembled) {
player.sendMessage(Text.literal("Machine is not assembled"));
return ActionResult.FAIL;
}

}

return super.onUse(state, world, pos, player, hand, hit);
}
}
107 changes: 107 additions & 0 deletions src/main/java/rearth/oritech/block/base/MultiblockMachineEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package rearth.oritech.block.base;

import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.Vec3i;
import rearth.oritech.block.custom.MachineCoreBlock;
import rearth.oritech.client.init.ParticleContent;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public abstract class MultiblockMachineEntity extends MachineBlockEntity {

public MultiblockMachineEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
}


// positive x = forward
// positive y = up
// positive z = right?
public abstract List<Vec3i> getCorePositions();

public boolean initMultiblock(BlockState state) {

// check if multiblock is already created, if so cancel
// call method the get a list of relative positions
// check all positions if the blocks there extend MachineCoreBlock
// if so, add them to list of used blocks
// if not (e.g. block wrong type or air), draw a small particle to indicate the missing position
// when all blocks are valid, multiblock is active
// update all multiblocks state to USED=true, write controller position to block state

if (state.get(MultiblockMachine.ASSEMBLED)) return true;

var ownFacing = getFacing();

var targetPositions = getCorePositions();
var coreBlocks = new ArrayList<MultiBlockElement>(targetPositions.size());

for (var targetPosition : targetPositions) {
var rotatedPos = rotatePosition(targetPosition, ownFacing);
var checkPos = pos.add(rotatedPos);
var checkState = Objects.requireNonNull(world).getBlockState(checkPos);

var blockType = checkState.getBlock();
if (blockType instanceof MachineCoreBlock coreBlock) {
coreBlocks.add(new MultiBlockElement(checkState, coreBlock, checkPos));
} else {
highlightBlock(checkPos);
}
}

if (targetPositions.size() == coreBlocks.size()) {
// valid
for (var core : coreBlocks) {
var offset = pos.subtract(core.pos);
var newState = core.state
.with(MachineCoreBlock.USED, true)
.with(MachineCoreBlock.CONTROLLER_X, offset.getX() + 4)
.with(MachineCoreBlock.CONTROLLER_Y, offset.getY() + 4)
.with(MachineCoreBlock.CONTROLLER_Z, offset.getZ() + 4);
world.setBlockState(core.pos, newState);
}

Objects.requireNonNull(world).setBlockState(pos, state.with(MultiblockMachine.ASSEMBLED, true));
System.out.println("multiblock valid");
return true;
} else {
// invalid
System.out.println("multiblock invalid");
return false;
}

}

public void onCoreBroken(BlockPos corePos, BlockState coreState) {

System.out.println("registering broken core!");

// set assembled to false
// go through all existing cores
// set used to false

}

private void highlightBlock(BlockPos block) {
ParticleContent.HIGHLIGHT_BLOCK.spawn(world, Vec3d.of(block), null);
}

private Vec3i rotatePosition(Vec3i relativePos, Direction facing) {
return switch (facing) {
case NORTH -> new BlockPos(relativePos.getZ(), relativePos.getY(), relativePos.getX());
case EAST -> new BlockPos(-relativePos.getX(), relativePos.getY(), -relativePos.getZ());
case SOUTH -> new BlockPos(-relativePos.getZ(), relativePos.getY(), -relativePos.getX());
case WEST -> new BlockPos(relativePos.getX(), relativePos.getY(), relativePos.getZ());
default -> relativePos;
};
}

private record MultiBlockElement(BlockState state, MachineCoreBlock coreBlock, BlockPos pos) {
}
}
20 changes: 20 additions & 0 deletions src/main/java/rearth/oritech/block/custom/AssemblerBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package rearth.oritech.block.custom;

import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.entity.BlockEntity;
import org.jetbrains.annotations.NotNull;
import rearth.oritech.block.base.MachineBlock;
import rearth.oritech.block.base.MultiblockMachine;
import rearth.oritech.block.entity.AssemblerBlockEntity;

public class AssemblerBlock extends MultiblockMachine implements BlockEntityProvider {

public AssemblerBlock(Settings settings) {
super(settings);
}

@Override
public @NotNull Class<? extends BlockEntity> getBlockEntityType() {
return AssemblerBlockEntity.class;
}
}
31 changes: 29 additions & 2 deletions src/main/java/rearth/oritech/block/custom/MachineCoreBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,35 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.IntProperty;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3i;
import net.minecraft.world.World;
import rearth.oritech.block.base.MultiblockMachineEntity;

public class MachineCoreBlock extends Block {

public static final BooleanProperty USED = BooleanProperty.of("core_used");
public static final IntProperty CONTROLLER_X = IntProperty.of("linked_x", 0, 7);
public static final IntProperty CONTROLLER_Y = IntProperty.of("linked_y", 0, 7);
public static final IntProperty CONTROLLER_Z = IntProperty.of("linked_z", 0, 7);

public MachineCoreBlock(Settings settings) {
super(settings);
this.setDefaultState(getDefaultState().with(USED, false));
this.setDefaultState(getDefaultState()
.with(USED, false)
.with(CONTROLLER_X, 1)
.with(CONTROLLER_Y, 1)
.with(CONTROLLER_Z, 1)
);
}

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(USED);
builder.add(USED, CONTROLLER_X, CONTROLLER_Y, CONTROLLER_Z);
}

@Override
Expand All @@ -40,4 +51,20 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt

return ActionResult.SUCCESS;
}

@Override
public BlockState onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {

if (state.get(USED)) {
var offset = new Vec3i(state.get(CONTROLLER_X) - 4, state.get(CONTROLLER_Y) - 4, state.get(CONTROLLER_Z) - 4);
var controllerPos = pos.add(offset);
System.out.println("notifying machine controller that core has been removed");
var controllerEntity = world.getBlockEntity(controllerPos);
if (controllerEntity instanceof MultiblockMachineEntity machineEntity) {
machineEntity.onCoreBroken(pos, state);
}
}

return super.onBreak(world, pos, state, player);
}
}
Loading

0 comments on commit 15dffed

Please sign in to comment.