Skip to content

Commit

Permalink
Add energy / fluid pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rearth committed Mar 13, 2024
1 parent 3df8ec4 commit 0fd220c
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 63 deletions.
4 changes: 3 additions & 1 deletion src/main/java/rearth/oritech/Oritech.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public void onInitialize() {
private void onServerStarted(MinecraftServer minecraftServer) {
minecraftServer.getWorlds().forEach(world -> {
if (world.isClient) return;

var regKey = world.getRegistryKey().getValue();

var dataId = "energy_" + regKey.getNamespace() + "_" + regKey.getPath();
var result = world.getPersistentStateManager().getOrCreate(GenericPipeInterfaceEntity.PipeNetworkData.TYPE, dataId);
if (result.hashCode() != 0) {
Expand All @@ -49,7 +51,7 @@ private void onServerStarted(MinecraftServer minecraftServer) {
}


var fluidDataId = "energy_" + regKey.getNamespace() + "_" + regKey.getPath();
var fluidDataId = "fluid_" + regKey.getNamespace() + "_" + regKey.getPath();
var fluidResult = world.getPersistentStateManager().getOrCreate(GenericPipeInterfaceEntity.PipeNetworkData.TYPE, fluidDataId);
if (fluidResult.hashCode() != 0) {
FluidPipeBlock.FLUID_PIPE_DATA = fluidResult;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
package rearth.oritech.block.base.block;

import com.mojang.serialization.MapCodec;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory;
import net.minecraft.block.Block;
import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.BlockState;
import net.minecraft.block.HorizontalFacingBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityTicker;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
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;
import org.jetbrains.annotations.Nullable;
import rearth.oritech.block.base.entity.FrameInteractionBlockEntity;
import rearth.oritech.block.base.entity.MultiblockMachineEntity;
import rearth.oritech.network.NetworkContent;
import rearth.oritech.util.MachineAddonController;
import rearth.oritech.util.MultiblockMachineController;

import java.util.Objects;
import static rearth.oritech.block.base.block.MultiblockMachine.ASSEMBLED;

public abstract class MultiblockFrameInteractionBlock extends FrameInteractionBlock {

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

public MultiblockFrameInteractionBlock(Settings settings) {
super(settings);
setDefaultState(getDefaultState().with(ASSEMBLED, false));
Expand All @@ -56,9 +38,12 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
}

var isAssembled = machineEntity.initMultiblock(state);
if (!isAssembled)
if (!isAssembled) {
player.sendMessage(Text.literal("Machine is not assembled. Please add missing core blocks"));
return ActionResult.SUCCESS;
}

state = state.with(ASSEMBLED, true);
}

return super.onUse(state, world, pos, player, hand, hit);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
package rearth.oritech.block.blocks.machines.interaction;

import com.mojang.serialization.MapCodec;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory;
import net.minecraft.block.*;
import net.minecraft.block.AbstractBlock.Settings;
import net.minecraft.block.Block;
import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.BlockRenderType;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityTicker;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
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;
import org.jetbrains.annotations.Nullable;
import rearth.oritech.block.base.entity.FrameInteractionBlockEntity;
import rearth.oritech.block.base.entity.UpgradableMachineBlockEntity;
import rearth.oritech.block.entity.machines.interaction.DestroyerBlockEntity;
import rearth.oritech.block.entity.machines.interaction.LaserArmBlockEntity;
import rearth.oritech.network.NetworkContent;
import rearth.oritech.util.MachineAddonController;
import rearth.oritech.util.MultiblockMachineController;

import java.util.Objects;
import static rearth.oritech.block.base.block.MultiblockMachine.ASSEMBLED;

public class LaserArmBlock extends Block implements BlockEntityProvider {

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

public LaserArmBlock(Settings settings) {
super(settings);
setDefaultState(getDefaultState().with(ASSEMBLED, false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
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;
import org.jetbrains.annotations.Nullable;
import rearth.oritech.block.entity.pipes.FluidPipeInterfaceEntity;
import rearth.oritech.block.entity.pipes.GenericPipeInterfaceEntity;
Expand All @@ -16,8 +23,29 @@

public class FluidPipeConnectionBlock extends GenericPipeConnectionBlock {

public static final BooleanProperty EXTRACT = BooleanProperty.of("extract");

public FluidPipeConnectionBlock(Settings settings) {
super(settings);
this.setDefaultState(this.getDefaultState().with(EXTRACT, false));
}

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

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

if (world.isClient) return ActionResult.SUCCESS;

var oldExtract = state.get(EXTRACT);
world.setBlockState(pos, state.with(EXTRACT, !oldExtract));
System.out.println("changed extract to: " + !oldExtract);

return ActionResult.SUCCESS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,13 @@ public BlockState getStateForNeighborUpdate(BlockState state, Direction directio
var lookup = pipeBlock.getSidesLookup();

// transform to interface when machine is placed as neighbor
if (hasMachineInDirection(Direction.NORTH, world, pos, lookup)
if (!(state.getBlock() instanceof GenericPipeConnectionBlock) &&
(hasMachineInDirection(Direction.NORTH, world, pos, lookup)
|| hasMachineInDirection(Direction.EAST, world, pos, lookup)
|| hasMachineInDirection(Direction.SOUTH, world, pos, lookup)
|| hasMachineInDirection(Direction.WEST, world, pos, lookup)
|| hasMachineInDirection(Direction.UP, world, pos, lookup)
|| hasMachineInDirection(Direction.DOWN, world, pos, lookup)) {
|| hasMachineInDirection(Direction.DOWN, world, pos, lookup))) {

var stateBase = getConnectionBlock();
return GenericPipeConnectionBlock.addInterfaceState(stateBase, world, pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityTicker;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
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;
Expand Down Expand Up @@ -96,22 +92,9 @@ public BlockState getStateForNeighborUpdate(BlockState state, Direction directio
return normalPipeState;
}


return interfaceState;
}

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

if (world.isClient) return ActionResult.SUCCESS;

if (world.getBlockEntity(pos) instanceof GenericPipeInterfaceEntity interfaceEntity) {
interfaceEntity.reloadNetworkForInterface();
}

return ActionResult.SUCCESS;
}

@SuppressWarnings("rawtypes")
@Nullable
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import net.minecraft.world.World;
import rearth.oritech.block.base.entity.MachineBlockEntity;
import rearth.oritech.block.blocks.MachineCoreBlock;
import rearth.oritech.block.blocks.machines.interaction.LaserArmBlock;
import rearth.oritech.block.entity.machines.MachineCoreEntity;
import rearth.oritech.init.BlockContent;
import rearth.oritech.init.BlockEntitiesContent;
Expand All @@ -38,6 +37,8 @@
import java.util.List;
import java.util.Objects;

import static rearth.oritech.block.base.block.MultiblockMachine.ASSEMBLED;

public class LaserArmBlockEntity extends BlockEntity implements GeoBlockEntity, BlockEntityTicker<LaserArmBlockEntity>, EnergyProvider, MultiblockMachineController, MachineAddonController, InventoryProvider {

private static final int BLOCK_BREAK_ENERGY = 2000;
Expand Down Expand Up @@ -424,7 +425,7 @@ public void playSetupAnimation() {
}

public boolean isActive(BlockState state) {
return state.get(LaserArmBlock.ASSEMBLED);
return state.get(ASSEMBLED);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ protected void onFinalCommit() {
PumpBlockEntity.this.markDirty();
}
};

private final SimpleEnergyStorage energyStorage = new SimpleEnergyStorage(20000, 1000, 0);
private boolean initialized = false;
private boolean toolheadLowered = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public void tick(World world, BlockPos pos, BlockState state, GenericPipeInterfa
tx.commit();
}

markDirty();

}

private EnergyStorage findFromCache(World world, BlockPos pos, Direction direction) {
Expand Down
Loading

0 comments on commit 0fd220c

Please sign in to comment.