From 75900d50f9c77b1c16c87f9e780037eade030235 Mon Sep 17 00:00:00 2001 From: Tim Kainz Date: Sat, 7 Feb 2026 01:58:54 +0100 Subject: [PATCH] Implemented transfering between two Solarpanels --- .../SolarCell/Commands/ExampleCommand.java | 2 +- ...ponent.java => EnergySourceComponent.java} | 10 +++--- .../KaiFlo/SolarCell/Helpers/BlockHelper.java | 9 +++-- .../org/KaiFlo/SolarCell/SolarCellPlugin.java | 16 ++++----- ....java => EnergyProducerTickingSystem.java} | 35 +++++++++++++------ ...ava => EnergySourceInitializerSystem.java} | 8 +++-- .../Server/Item/Items/SolarCell.json | 8 ++++- 7 files changed, 58 insertions(+), 30 deletions(-) rename src/main/java/org/KaiFlo/SolarCell/Components/EnergySource/Implementations/{SolarCellComponent.java => EnergySourceComponent.java} (73%) rename src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/{SolarCellTickingSystem.java => EnergyProducerTickingSystem.java} (52%) rename src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/{SolarCellInitializer.java => EnergySourceInitializerSystem.java} (82%) diff --git a/src/main/java/org/KaiFlo/SolarCell/Commands/ExampleCommand.java b/src/main/java/org/KaiFlo/SolarCell/Commands/ExampleCommand.java index 2bf37f3..73b4ebc 100644 --- a/src/main/java/org/KaiFlo/SolarCell/Commands/ExampleCommand.java +++ b/src/main/java/org/KaiFlo/SolarCell/Commands/ExampleCommand.java @@ -45,7 +45,7 @@ public class ExampleCommand extends CommandBase { Vector3i playerPosition = Objects.requireNonNull(playerTransform).getPosition().toVector3i(); var size = sizeArg.get(ctx); if (size == null) size = 5; - BlockHelper.executeForCubeAround(playerPosition.x, playerPosition.y, playerPosition.z, size, (x, y, z) -> { + BlockHelper.executeForCubeAround(playerPosition.x, playerPosition.y, playerPosition.z, size,true, (x, y, z) -> { // BlockType blockType = defaultWorld.getBlockType(x, y, z); // if (blockType != null) { // LOGGER.atInfo().log(blockType.getId() + " at " + x + "," + y + "," + z); diff --git a/src/main/java/org/KaiFlo/SolarCell/Components/EnergySource/Implementations/SolarCellComponent.java b/src/main/java/org/KaiFlo/SolarCell/Components/EnergySource/Implementations/EnergySourceComponent.java similarity index 73% rename from src/main/java/org/KaiFlo/SolarCell/Components/EnergySource/Implementations/SolarCellComponent.java rename to src/main/java/org/KaiFlo/SolarCell/Components/EnergySource/Implementations/EnergySourceComponent.java index eccc36b..1a0fcd2 100644 --- a/src/main/java/org/KaiFlo/SolarCell/Components/EnergySource/Implementations/SolarCellComponent.java +++ b/src/main/java/org/KaiFlo/SolarCell/Components/EnergySource/Implementations/EnergySourceComponent.java @@ -9,17 +9,17 @@ import org.KaiFlo.SolarCell.Components.EnergySource.AbstractEnergySource; import org.KaiFlo.SolarCell.SolarCellPlugin; import org.checkerframework.checker.nullness.compatqual.NullableDecl; -public class SolarCellComponent extends AbstractEnergySource implements Component { - public static final BuilderCodec CODEC = BuilderCodec.builder(SolarCellComponent.class, SolarCellComponent::new).build(); +public class EnergySourceComponent extends AbstractEnergySource implements Component { + public static final BuilderCodec CODEC = BuilderCodec.builder(EnergySourceComponent.class, EnergySourceComponent::new).build(); private final HytaleLogger Logger = HytaleLogger.getLogger(); private long energyRatePerTick = 5; - public static ComponentType getComponentType() { + public static ComponentType getComponentType() { return SolarCellPlugin.get().getSolarCellComponentType(); } - private SolarCellComponent copyFrom(SolarCellComponent other) { + private EnergySourceComponent copyFrom(EnergySourceComponent other) { this.energyRatePerTick = other.energyRatePerTick; return this; } @@ -32,7 +32,7 @@ public class SolarCellComponent extends AbstractEnergySource implements Componen } catch (CloneNotSupportedException e) { Logger.atWarning().log("Cloning of " + this.getClass().getName() + " failed."); } - return new SolarCellComponent().copyFrom(this); + return new EnergySourceComponent().copyFrom(this); } @Override diff --git a/src/main/java/org/KaiFlo/SolarCell/Helpers/BlockHelper.java b/src/main/java/org/KaiFlo/SolarCell/Helpers/BlockHelper.java index c107577..83f60f2 100644 --- a/src/main/java/org/KaiFlo/SolarCell/Helpers/BlockHelper.java +++ b/src/main/java/org/KaiFlo/SolarCell/Helpers/BlockHelper.java @@ -2,11 +2,16 @@ package org.KaiFlo.SolarCell.Helpers; public class BlockHelper { - public static void executeForCubeAround(int x, int y, int z, int size, Callback callback) { + public static void executeForCubeAround(int x, int y, int z, int size, boolean own, Callback callback) { for (int x1 = x - size / 2; x1 < x + size / 2; x1++) { for (int y1 = y - size / 2; y1 < y + size / 2; y1++) { for (int z1 = z - size / 2; z1 < z + size / 2; z1++) { - callback.accept(x1, y1, z1); + if (!(x1 == x && y1 == y && z1 == z)) { + callback.accept(x1, y1, z1); + } + else if (own) { + callback.accept(x1, y1, z1); + } } } } diff --git a/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java b/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java index c88f6b1..8bfa2ed 100644 --- a/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java +++ b/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java @@ -6,9 +6,9 @@ import com.hypixel.hytale.server.core.plugin.JavaPlugin; import com.hypixel.hytale.server.core.plugin.JavaPluginInit; import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore; import org.KaiFlo.SolarCell.Commands.ExampleCommand; -import org.KaiFlo.SolarCell.Components.EnergySource.Implementations.SolarCellComponent; -import org.KaiFlo.SolarCell.Systems.EnergySource.SolarCellInitializer; -import org.KaiFlo.SolarCell.Systems.EnergySource.SolarCellTickingSystem; +import org.KaiFlo.SolarCell.Components.EnergySource.Implementations.EnergySourceComponent; +import org.KaiFlo.SolarCell.Systems.EnergySource.EnergySourceInitializerSystem; +import org.KaiFlo.SolarCell.Systems.EnergySource.EnergyProducerTickingSystem; import javax.annotation.Nonnull; @@ -22,7 +22,7 @@ public class SolarCellPlugin extends JavaPlugin { private static final HytaleLogger LOGGER = HytaleLogger.forEnclosingClass(); - private ComponentType solarCellComponentType; + private ComponentType solarCellComponentType; public SolarCellPlugin(@Nonnull JavaPluginInit init) { super(init); @@ -34,16 +34,16 @@ public class SolarCellPlugin extends JavaPlugin { instance = this; LOGGER.atInfo().log("Setting up plugin " + this.getName()); - solarCellComponentType = this.getChunkStoreRegistry().registerComponent(SolarCellComponent.class, "SolarCell", SolarCellComponent.CODEC); + solarCellComponentType = this.getChunkStoreRegistry().registerComponent(EnergySourceComponent.class, "SolarCell", EnergySourceComponent.CODEC); this.getCommandRegistry().registerCommand(new ExampleCommand(this.getName(), this.getManifest().getVersion().toString())); - this.getChunkStoreRegistry().registerSystem(new SolarCellInitializer()); - this.getChunkStoreRegistry().registerSystem(new SolarCellTickingSystem()); + this.getChunkStoreRegistry().registerSystem(new EnergySourceInitializerSystem()); + this.getChunkStoreRegistry().registerSystem(new EnergyProducerTickingSystem()); } - public ComponentType getSolarCellComponentType() { + public ComponentType getSolarCellComponentType() { return solarCellComponentType; } } \ No newline at end of file diff --git a/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/SolarCellTickingSystem.java b/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/EnergyProducerTickingSystem.java similarity index 52% rename from src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/SolarCellTickingSystem.java rename to src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/EnergyProducerTickingSystem.java index 0cdaa10..21b6106 100644 --- a/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/SolarCellTickingSystem.java +++ b/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/EnergyProducerTickingSystem.java @@ -5,6 +5,7 @@ import com.hypixel.hytale.component.CommandBuffer; import com.hypixel.hytale.component.Store; import com.hypixel.hytale.component.query.Query; import com.hypixel.hytale.component.system.tick.EntityTickingSystem; +import com.hypixel.hytale.logger.HytaleLogger; import com.hypixel.hytale.math.util.ChunkUtil; import com.hypixel.hytale.server.core.asset.type.blocktick.BlockTickStrategy; import com.hypixel.hytale.server.core.universe.world.chunk.BlockComponentChunk; @@ -12,11 +13,14 @@ import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk; import com.hypixel.hytale.server.core.universe.world.chunk.section.BlockSection; import com.hypixel.hytale.server.core.universe.world.chunk.section.ChunkSection; import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore; -import org.KaiFlo.SolarCell.Components.EnergySource.Implementations.SolarCellComponent; +import dev.zkiller.energystorage.components.EnergyStorageBlockComponent; +import org.KaiFlo.SolarCell.Components.EnergySource.Implementations.EnergySourceComponent; +import org.KaiFlo.SolarCell.Helpers.BlockHelper; import org.checkerframework.checker.nullness.compatqual.NonNullDecl; import org.checkerframework.checker.nullness.compatqual.NullableDecl; -public class SolarCellTickingSystem extends EntityTickingSystem { +public class EnergyProducerTickingSystem extends EntityTickingSystem { + private final HytaleLogger LOGGER = HytaleLogger.getLogger(); @Override public void tick(float v, int i, @NonNullDecl ArchetypeChunk archetypeChunk, @NonNullDecl Store store, @NonNullDecl CommandBuffer commandBuffer) { @@ -30,20 +34,31 @@ public class SolarCellTickingSystem extends EntityTickingSystem { var worldChunk = commandBuffer.getComponent(chunkSection.getChunkColumnReference(), WorldChunk.getComponentType()); if (blockComponentChunk == null || worldChunk == null) return; - var world = worldChunk.getWorld(); - blockSection.forEachTicking(blockComponentChunk, commandBuffer, chunkSection.getY(), - (_, _, localX, localY, localZ, _) -> { - var blockRef = blockComponentChunk.getEntityReference(ChunkUtil.indexBlockInColumn(localX, localY, localZ)); + (blockCompChunk, _, localX, localY, localZ, _) -> { + var blockRef = blockCompChunk.getEntityReference(ChunkUtil.indexBlockInColumn(localX, localY, localZ)); if (blockRef == null) return BlockTickStrategy.IGNORED; - var solarCellComponent = commandBuffer.getComponent(blockRef, SolarCellComponent.getComponentType()); - if (solarCellComponent == null) return BlockTickStrategy.IGNORED; + var thisEnergySourceComponent = commandBuffer.getComponent(blockRef, EnergySourceComponent.getComponentType()); + var thisEnergyStorageComponent = commandBuffer.getComponent(blockRef, EnergyStorageBlockComponent.getComponentType()); + if (thisEnergySourceComponent == null || thisEnergyStorageComponent == null) + return BlockTickStrategy.IGNORED; int globalX = localX + (worldChunk.getX() * 32); int globalZ = localZ + (worldChunk.getZ() * 32); - world.execute(() -> { - world.setBlock(globalX + 1, localY, globalZ, "Rock_Ice"); + BlockHelper.executeForCubeAround(globalX, localY, globalZ, 5, false, (x, y, z) -> { + var index = ChunkUtil.indexBlockInColumn(x, y, z); + var targetRef = blockCompChunk.getEntityReference(index); + if (targetRef == null) return; + var targetEnergySource = commandBuffer.getComponent(targetRef, EnergySourceComponent.getComponentType()); + var targetEnergyStorage = commandBuffer.getComponent(targetRef, EnergyStorageBlockComponent.getComponentType()); + if (targetEnergySource == null || targetEnergyStorage == null) return; + + var energy = targetEnergyStorage.extractEnergy(targetEnergySource.getEnergyRatePerTick(), false); + var inserted = thisEnergyStorageComponent.receiveEnergy(energy, false); + LOGGER.atInfo().log("Inserted " + inserted + "/" + energy + " |" + targetEnergyStorage.getEnergyStored() + "| into storage" + + " at Block " + globalX + ", " + localY + ", " + globalZ + ", " + + thisEnergyStorageComponent.getEnergyStored() + "/" + thisEnergyStorageComponent.getMaxEnergyStored()); }); return BlockTickStrategy.CONTINUE; diff --git a/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/SolarCellInitializer.java b/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/EnergySourceInitializerSystem.java similarity index 82% rename from src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/SolarCellInitializer.java rename to src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/EnergySourceInitializerSystem.java index b331272..1a5a24e 100644 --- a/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/SolarCellInitializer.java +++ b/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/EnergySourceInitializerSystem.java @@ -3,15 +3,16 @@ package org.KaiFlo.SolarCell.Systems.EnergySource; import com.hypixel.hytale.component.*; import com.hypixel.hytale.component.query.Query; import com.hypixel.hytale.component.system.RefSystem; +import com.hypixel.hytale.logger.HytaleLogger; import com.hypixel.hytale.math.util.ChunkUtil; import com.hypixel.hytale.server.core.modules.block.BlockModule; import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk; import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore; -import org.KaiFlo.SolarCell.Components.EnergySource.Implementations.SolarCellComponent; +import org.KaiFlo.SolarCell.Components.EnergySource.Implementations.EnergySourceComponent; import org.checkerframework.checker.nullness.compatqual.NonNullDecl; import org.checkerframework.checker.nullness.compatqual.NullableDecl; -public class SolarCellInitializer extends RefSystem { +public class EnergySourceInitializerSystem extends RefSystem { @Override public void onEntityAdded(@NonNullDecl Ref ref, @NonNullDecl AddReason addReason, @NonNullDecl Store store, @NonNullDecl CommandBuffer commandBuffer) { BlockModule.BlockStateInfo blockInfo = commandBuffer.getComponent(ref, BlockModule.BlockStateInfo.getComponentType()); @@ -25,6 +26,7 @@ public class SolarCellInitializer extends RefSystem { int z = ChunkUtil.zFromBlockInColumn(blockInfo.getIndex()); worldChunk.setTicking(x, y, z, true); + HytaleLogger.getLogger().atInfo().log(String.valueOf(worldChunk.isTicking(x, y, z))); } @@ -36,6 +38,6 @@ public class SolarCellInitializer extends RefSystem { @NullableDecl @Override public Query getQuery() { - return Query.and(SolarCellComponent.getComponentType(), BlockModule.BlockStateInfo.getComponentType()); + return Query.and(EnergySourceComponent.getComponentType(), BlockModule.BlockStateInfo.getComponentType()); } } diff --git a/src/main/resources/Server/Item/Items/SolarCell.json b/src/main/resources/Server/Item/Items/SolarCell.json index 5c94050..1eead07 100644 --- a/src/main/resources/Server/Item/Items/SolarCell.json +++ b/src/main/resources/Server/Item/Items/SolarCell.json @@ -45,7 +45,13 @@ "BlockBreakingDecalId": "Breaking_Decals_Rock", "BlockEntity": { "Components": { - "SolarCell": {} + "SolarCell": {}, + "EnergyStorageComponent": { + "EnergyStored": 5, + "MaxEnergy": 80000, + "MaxReceive": 1000, + "MaxExtract": 1000 + } } } },