Skip to content

Commit

Permalink
Begin adding energy storage blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Rearth committed Apr 1, 2024
1 parent 1ee0b21 commit c8c37bd
Show file tree
Hide file tree
Showing 24 changed files with 529 additions and 29 deletions.
18 changes: 10 additions & 8 deletions src/main/generated/.cache/19c08d24c255c2719fbb8ac01f9dff290b763461
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// 1.20.4 2024-03-29T22:10:13.6086234 Oritech/Model Definitions
// 1.20.4 2024-04-01T19:45:11.2977587 Oritech/Model Definitions
0ea8315aed10abcf90ec950fee0062aed26075cb assets\oritech\models\item\energy_pipe.json
8ede0997449fd1c3f65a07df184768a843b473ed assets\oritech\models\item\centrifuge_block.json
90e28b721796ff7ee21c8eefe4525f03a251d2fb assets\oritech\models\block\machine_extender.json
3fcaea2f05319816f7904137763182c4106c91a9 assets\oritech\blockstates\destroyer_block.json
e6f197ee5ac22cd68bc502a86d40b65bfe40884a assets\oritech\models\item\target_designator.json
7eb395cd65df72caf43bedc54067cbf53940fd6c assets\oritech\blockstates\centrifuge_block.json
8ef5ff056a3d2fa26393ec9ccb4b6f3b410f8be8 assets\oritech\models\item\item_pipe.json
7eb395cd65df72caf43bedc54067cbf53940fd6c assets\oritech\blockstates\fragment_forge_block.json
b91305cbccecb97f9d74e2b92787ae9fa6387786 assets\oritech\blockstates\fluid_pipe.json
7eb395cd65df72caf43bedc54067cbf53940fd6c assets\oritech\blockstates\big_solar_panel_block.json
7eb395cd65df72caf43bedc54067cbf53940fd6c assets\oritech\blockstates\fragment_forge_block.json
98d62a615b70fca694eb00c6b8aa9297c4a92b62 assets\oritech\blockstates\energy_pipe_connection.json
7eb395cd65df72caf43bedc54067cbf53940fd6c assets\oritech\blockstates\big_solar_panel_block.json
3ceff0ef4325bb269831205626285bc857c60de8 assets\oritech\models\item\destroyer_block.json
829faf2dfb04c9aea2dd6c971037fbfdaa9aa389 assets\oritech\models\item\pump_block.json
7eb395cd65df72caf43bedc54067cbf53940fd6c assets\oritech\blockstates\laser_arm_block.json
7eb395cd65df72caf43bedc54067cbf53940fd6c assets\oritech\blockstates\pulverizer_block.json
7eb395cd65df72caf43bedc54067cbf53940fd6c assets\oritech\blockstates\laser_arm_block.json
e4497bad8afbd4f4b4e830d892284a05718b06dd assets\oritech\models\block\machine_core_basic.json
505d30e3b61ee0ba1f2616e1226689e2cdb7ab58 assets\oritech\models\block\addon_indicator_block.json
a5651b8d4a27308b40c404f7cd3abcf9769b64b6 assets\oritech\models\item\machine_core_basic.json
Expand All @@ -24,13 +24,14 @@ ee822d455f99c8c53c789fdb744979f678ca2729 assets\oritech\blockstates\fertilizer_b
46af77abe62b29d232feadccde429fd59d5603fc assets\oritech\blockstates\placer_block.json
3d37038977380bd685f913e722cc3b1c3a25c35d assets\oritech\models\item\atomic_forge_block.json
7eb395cd65df72caf43bedc54067cbf53940fd6c assets\oritech\blockstates\test_generator_block.json
eafe84c787830ab2a866e0878e9ed718d58659f5 assets\oritech\blockstates\banana_block.json
b5ee97616ae0d2da17dfaecf45aa42f2cf19f691 assets\oritech\blockstates\machine_extender.json
eafe84c787830ab2a866e0878e9ed718d58659f5 assets\oritech\blockstates\banana_block.json
25864df3cd7786f2bf4fe6fa9855b3cf366c28ca assets\oritech\models\item\banana.json
1fdbeb99862a58436d745edb768b503539ecb295 assets\oritech\models\item\placer_block.json
58b38bcc6c404953490330d4cc797464b6874d88 assets\oritech\models\item\test_generator_block.json
9cd64a2fe3b9a0ee479646d36cc0302bcd7cab4f assets\oritech\models\block\destroyer_block.json
b8bc03092cb1eb686ede01daffc7421253af860c assets\oritech\models\item\machine_extender.json
9cd64a2fe3b9a0ee479646d36cc0302bcd7cab4f assets\oritech\models\block\destroyer_block.json
45fbece69084d6df6eaa4ba344d25eef914c63c3 assets\oritech\models\item\small_storage_block.json
63f14855c688ce7ec236097ce232dd381cbc1538 assets\oritech\blockstates\addon_indicator_block.json
7ee14de0149c70d1145dbf338518690b536cc02e assets\oritech\models\item\powered_furnace_block.json
746b98d721e5ae0d96dd769e71b786afa8c2d142 assets\oritech\models\item\banana_block.json
Expand All @@ -45,8 +46,8 @@ c641d411ad252487b93d779cee9028b9ee920281 assets\oritech\blockstates\machine_core
a5afa0ba8780ec08bd0a9284bc08e0c681fe7158 assets\oritech\blockstates\block_placer_head.json
7eb395cd65df72caf43bedc54067cbf53940fd6c assets\oritech\blockstates\assembler_block.json
5054284e6d5fe0785ac93bdda5101c47337953cf assets\oritech\models\item\machine_frame_block.json
56c7ceb5cc86d67913ad95cb4ae5ec192aed926f assets\oritech\models\block\pump_block.json
7b3ae67ec28f9b36b96539cd48b92096861d0853 assets\oritech\models\item\oil_bucket.json
56c7ceb5cc86d67913ad95cb4ae5ec192aed926f assets\oritech\models\block\pump_block.json
7eb395cd65df72caf43bedc54067cbf53940fd6c assets\oritech\blockstates\powered_furnace_block.json
1b9cd3719105dc1e03398de41f2cab37337816fe assets\oritech\blockstates\machine_core_good.json
ccd7e87c2ba5a5062bb88aa6e103d49d47cd56ff assets\oritech\blockstates\energy_pipe.json
Expand All @@ -56,6 +57,7 @@ a3f9b72f6f36d42af4e5c3a87d7e180b2e36a2ed assets\oritech\models\item\machine_core
97794db126be871e2cb68297abbe8917c343e705 assets\oritech\models\item\fluid_pipe.json
469ea8a66c529fe26bc029c21092e7f062ed0e40 assets\oritech\models\block\banana_block.json
d35ecef5df9198b12dc04267780c727c144b1959 assets\oritech\models\item\assembler_block.json
f337c5c80895d0c133e8218a41a0915b307cce18 assets\oritech\blockstates\small_storage_block.json
d39d7a8eaa54d12c155fb1acd1722e322c0aeac3 assets\oritech\models\block\block_destroyer_head.json
7eb395cd65df72caf43bedc54067cbf53940fd6c assets\oritech\blockstates\foundry_block.json
30525b49546801c90bc77faa6eab332ba288eca5 assets\oritech\models\item\fertilizer_block.json
Expand All @@ -66,6 +68,6 @@ f53b229ded70f8ae0bff50e829d62326601b0fe8 assets\oritech\models\block\block_place
b7e73ae6ffd983038af7540301bf488242562790 assets\oritech\models\block\fertilizer_block.json
83b81f38fd21e58e98aab409d36368b3a78b675e assets\oritech\blockstates\item_pipe.json
66c23bb916995ef1664b6119475d183a66f32624 assets\oritech\blockstates\block_fertilizer_head.json
42580a93e058faa5381bfac746ed2a200ec5f323 assets\oritech\models\item\laser_arm_block.json
3d6d17333aeaef12a7bd95f18b169ee69a0bab5f assets\oritech\blockstates\fluid_pipe_connection.json
42580a93e058faa5381bfac746ed2a200ec5f323 assets\oritech\models\item\laser_arm_block.json
3166a5959c87c4465c5eed82034d3178c4b8b518 assets\oritech\models\block\machine_core_good.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "oritech:block/small_storage_block"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"parent": "oritech:block/small_storage_block"
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package rearth.oritech.block.base.block;

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.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.world.World;
import rearth.oritech.block.base.entity.MultiblockMachineEntity;
import rearth.oritech.block.base.entity.UpgradableMachineBlockEntity;
import rearth.oritech.util.MachineAddonController;

public abstract class UpgradableMachineBlock extends MachineBlock {
Expand All @@ -27,7 +21,7 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
if (!world.isClient) {

var entity = world.getBlockEntity(pos);
if (!(entity instanceof UpgradableMachineBlockEntity machineEntity)) {
if (!(entity instanceof MachineAddonController machineEntity)) {
return ActionResult.SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package rearth.oritech.block.base.entity;

import net.fabricmc.fabric.api.lookup.v1.block.BlockApiCache;
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;
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.inventory.SimpleInventory;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.world.ServerWorld;
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 net.minecraft.world.World;
import rearth.oritech.block.blocks.machines.storage.SmallStorageBlock;
import rearth.oritech.client.init.ParticleContent;
import rearth.oritech.util.*;
import team.reborn.energy.api.EnergyStorage;
import team.reborn.energy.api.base.DelegatingEnergyStorage;

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

public abstract class ExpandableEnergyStorageBlockEntity extends BlockEntity implements EnergyProvider, MachineAddonController, BlockEntityTicker<ExpandableEnergyStorageBlockEntity> {

private final List<BlockPos> connectedAddons = new ArrayList<>();
private final List<BlockPos> openSlots = new ArrayList<>();
private BaseAddonData addonData = MachineAddonController.DEFAULT_ADDON_DATA;
private HashMap<Direction, BlockApiCache<EnergyStorage, Direction>> directionCaches;


//own storage
protected final DynamicEnergyStorage energyStorage = new DynamicEnergyStorage(getDefaultCapacity(), getDefaultInsertRate(), getDefaultExtractionRate()) {
@Override
public void onFinalCommit() {
super.onFinalCommit();
ExpandableEnergyStorageBlockEntity.this.markDirty();
}
};

private final EnergyStorage outputStorage = new DelegatingEnergyStorage(energyStorage, null) {
@Override
public boolean supportsInsertion() {
return false;
}
};

private final EnergyStorage inputStorage = new DelegatingEnergyStorage(energyStorage, null) {
@Override
public boolean supportsExtraction() {
return false;
}
};

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

@Override
public void tick(World world, BlockPos pos, BlockState state, ExpandableEnergyStorageBlockEntity blockEntity) {
if (world.isClient) return;

outputEnergy();
}

private void outputEnergy() {
if (energyStorage.amount <= 0) return;
var availableOutput = Math.min(energyStorage.amount, energyStorage.maxExtract);
var totalInserted = 0L;

if (directionCaches == null) directionCaches = getNeighborCaches(pos, world);

try (var tx = Transaction.openOuter()) {
for (var entry : directionCaches.entrySet()) {
var insertDirection = entry.getKey().getOpposite();
var targetCandidate = entry.getValue().find(insertDirection);
if (targetCandidate == null) continue;
var inserted = targetCandidate.insert(availableOutput, tx);
availableOutput -= inserted;
totalInserted += inserted;
if (availableOutput <= 0) break;
}
energyStorage.extract(totalInserted, tx);
tx.commit();
}

this.markDirty();
}

// defaults only to front block
protected HashMap<Direction, BlockApiCache<EnergyStorage, Direction>> getNeighborCaches(BlockPos pos, World world) {

var res = new HashMap<Direction, BlockApiCache<EnergyStorage, Direction>>(6);
var facing = getFacing();
var blockInFront = (BlockPos) Geometry.offsetToWorldPosition(facing, new Vec3i(-1, 0, 0), pos);

System.out.println(blockInFront.toCenterPos());
ParticleContent.HIGHLIGHT_BLOCK.spawn(world, Vec3d.of(blockInFront));

var frontCache = BlockApiCache.create(EnergyStorage.SIDED, (ServerWorld) world, blockInFront);
res.put(Direction.DOWN, frontCache);

return res;
}

@Override
public void writeNbt(NbtCompound nbt) {
super.writeNbt(nbt);
writeAddonToNbt(nbt);
nbt.putLong("energy_stored", energyStorage.amount);
}

@Override
public void readNbt(NbtCompound nbt) {
super.readNbt(nbt);
loadAddonNbtData(nbt);
updateEnergyContainer();
energyStorage.amount = nbt.getLong("energy_stored");
}

public Direction getFacing() {
return getCachedState().get(SmallStorageBlock.TARGET_DIR);
}


@Override
public EnergyStorage getStorage(Direction direction) {

if (direction == null)
return energyStorage;

if (direction.equals(getFacing())) {
return outputStorage;
} else {
return inputStorage;
}
}

@Override
public List<BlockPos> getConnectedAddons() {
return connectedAddons;
}

@Override
public List<BlockPos> getOpenSlots() {
return openSlots;
}

@Override
public Direction getFacingForAddon() {
var facing = Objects.requireNonNull(world).getBlockState(getPos()).get(SmallStorageBlock.TARGET_DIR);

if (facing.equals(Direction.UP) || facing.equals(Direction.DOWN))
return Direction.NORTH;

return facing;
}

@Override
public DynamicEnergyStorage getStorageForAddon() {
return energyStorage;
}

@Override
public SimpleInventory getInventoryForAddon() {
return null;
}

@Override
public ScreenProvider getScreenProvider() {
return null;
}

@Override
public BaseAddonData getBaseAddonData() {
return addonData;
}

@Override
public void setBaseAddonData(BaseAddonData data) {
this.addonData = data;
}

public abstract long getDefaultExtractionRate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public InventoryStorage getInventory(Direction direction) {
}

@Override
public EnergyStorage getStorage() {
public EnergyStorage getStorage(Direction direction) {
return energyStorage;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ public ScreenHandler createMenu(int syncId, PlayerInventory playerInventory, Pla
}

@Override
public EnergyStorage getStorage() {
public EnergyStorage getStorage(Direction direction) {
return energyStorage;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected void writeNbt(NbtCompound nbt) {
}

@Override
public EnergyStorage getStorage() {
public EnergyStorage getStorage(Direction direction) {
return energyStorage;
}

Expand Down
Loading

0 comments on commit c8c37bd

Please sign in to comment.