From 5380b275a89a918ce27a304cd5d8dc2b4052f620 Mon Sep 17 00:00:00 2001 From: Tim Kainz Date: Sat, 7 Feb 2026 18:55:14 +0100 Subject: [PATCH] Added Dispatcher for SourceTicking --- .../EnergyConsumerComponent.java | 7 ++- .../EnergySourceComponent.java | 6 +-- .../EnergyStorageComponent.java | 6 +-- .../KaiFlo/SolarCell/Helpers/BlockHelper.java | 1 + .../org/KaiFlo/SolarCell/SolarCellPlugin.java | 4 +- .../EnergySourceInitializerSystem.java | 18 ++------ .../EnergySource/IEnergySourceTicking.java | 12 +++++ .../EnergySource/SolarCellSourceTicking.java | 33 ++++++++++++++ ...ngSystem.java => EnergyTickingSystem.java} | 44 +++++++++---------- 9 files changed, 80 insertions(+), 51 deletions(-) create mode 100644 src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/IEnergySourceTicking.java create mode 100644 src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/SolarCellSourceTicking.java rename src/main/java/org/KaiFlo/SolarCell/Systems/{EnergySource/EnergyProducerTickingSystem.java => EnergyTickingSystem.java} (54%) diff --git a/src/main/java/org/KaiFlo/SolarCell/Components/EnergyConsumer/Implementations/EnergyConsumerComponent.java b/src/main/java/org/KaiFlo/SolarCell/Components/EnergyConsumer/Implementations/EnergyConsumerComponent.java index d784558..69a2f51 100644 --- a/src/main/java/org/KaiFlo/SolarCell/Components/EnergyConsumer/Implementations/EnergyConsumerComponent.java +++ b/src/main/java/org/KaiFlo/SolarCell/Components/EnergyConsumer/Implementations/EnergyConsumerComponent.java @@ -6,12 +6,13 @@ import com.hypixel.hytale.codec.builder.BuilderCodec; import com.hypixel.hytale.codec.validation.Validators; import com.hypixel.hytale.component.Component; import com.hypixel.hytale.component.ComponentType; -import com.hypixel.hytale.logger.HytaleLogger; import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore; import org.KaiFlo.SolarCell.Components.EnergyConsumer.IEnergyConsumer; import org.KaiFlo.SolarCell.SolarCellPlugin; import org.checkerframework.checker.nullness.compatqual.NullableDecl; +import static org.KaiFlo.SolarCell.Helpers.BlockHelper.LOGGER; + public class EnergyConsumerComponent implements Component, IEnergyConsumer { public static final BuilderCodec CODEC = BuilderCodec.builder(EnergyConsumerComponent.class, EnergyConsumerComponent::new) .append(new KeyedCodec<>("ConsumptionPerTick", Codec.LONG), @@ -26,8 +27,6 @@ public class EnergyConsumerComponent implements Component, IEnergyCo private long consumptionPerTick; private float workingCapabilityRatio; - private final HytaleLogger Logger = HytaleLogger.getLogger(); - @Override public float getWorkingCapabilityRatio() { return workingCapabilityRatio; @@ -60,7 +59,7 @@ public class EnergyConsumerComponent implements Component, IEnergyCo try { super.clone(); } catch (CloneNotSupportedException e) { - Logger.atWarning().log("Cloning of " + this.getClass().getName() + " failed."); + LOGGER.atWarning().log("Cloning of " + this.getClass().getName() + " failed."); } return new EnergyConsumerComponent().copyFrom(this); } diff --git a/src/main/java/org/KaiFlo/SolarCell/Components/EnergySource/Implementations/EnergySourceComponent.java b/src/main/java/org/KaiFlo/SolarCell/Components/EnergySource/Implementations/EnergySourceComponent.java index fb13485..4a7712d 100644 --- a/src/main/java/org/KaiFlo/SolarCell/Components/EnergySource/Implementations/EnergySourceComponent.java +++ b/src/main/java/org/KaiFlo/SolarCell/Components/EnergySource/Implementations/EnergySourceComponent.java @@ -6,12 +6,13 @@ import com.hypixel.hytale.codec.builder.BuilderCodec; import com.hypixel.hytale.codec.validation.Validators; import com.hypixel.hytale.component.Component; import com.hypixel.hytale.component.ComponentType; -import com.hypixel.hytale.logger.HytaleLogger; import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore; import org.KaiFlo.SolarCell.Components.EnergySource.IEnergySource; import org.KaiFlo.SolarCell.SolarCellPlugin; import org.checkerframework.checker.nullness.compatqual.NullableDecl; +import static org.KaiFlo.SolarCell.Helpers.BlockHelper.LOGGER; + public class EnergySourceComponent implements Component, IEnergySource { @SuppressWarnings("unchecked") public static final BuilderCodec CODEC = BuilderCodec.builder(EnergySourceComponent.class, EnergySourceComponent::new) @@ -31,7 +32,6 @@ public class EnergySourceComponent implements Component, IEnergySour .add() .build(); - private final HytaleLogger Logger = HytaleLogger.getLogger(); private long generatesPerTick = 5; private boolean isEndless = true; private long energyCapacity = -1; @@ -51,7 +51,7 @@ public class EnergySourceComponent implements Component, IEnergySour try { super.clone(); } catch (CloneNotSupportedException e) { - Logger.atWarning().log("Cloning of " + this.getClass().getName() + " failed."); + LOGGER.atWarning().log("Cloning of " + this.getClass().getName() + " failed."); } return new EnergySourceComponent().copyFrom(this); } diff --git a/src/main/java/org/KaiFlo/SolarCell/Components/EnergyStorage/Implementations/EnergyStorageComponent.java b/src/main/java/org/KaiFlo/SolarCell/Components/EnergyStorage/Implementations/EnergyStorageComponent.java index ba7cdd3..23ef505 100644 --- a/src/main/java/org/KaiFlo/SolarCell/Components/EnergyStorage/Implementations/EnergyStorageComponent.java +++ b/src/main/java/org/KaiFlo/SolarCell/Components/EnergyStorage/Implementations/EnergyStorageComponent.java @@ -6,12 +6,13 @@ import com.hypixel.hytale.codec.builder.BuilderCodec; import com.hypixel.hytale.codec.validation.Validators; import com.hypixel.hytale.component.Component; import com.hypixel.hytale.component.ComponentType; -import com.hypixel.hytale.logger.HytaleLogger; import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore; import org.KaiFlo.SolarCell.Components.EnergyStorage.IEnergyStorage; import org.KaiFlo.SolarCell.SolarCellPlugin; import org.checkerframework.checker.nullness.compatqual.NullableDecl; +import static org.KaiFlo.SolarCell.Helpers.BlockHelper.LOGGER; + public class EnergyStorageComponent implements Component, IEnergyStorage { public static final BuilderCodec CODEC = BuilderCodec.builder(EnergyStorageComponent.class, EnergyStorageComponent::new) .append(new KeyedCodec<>("MaxCapacity", Codec.LONG), @@ -44,7 +45,6 @@ public class EnergyStorageComponent implements Component, IEnergySto .add() .build(); - private final HytaleLogger Logger = HytaleLogger.getLogger(); private long maxCapacity; private long extractEnergyPerTick; @@ -124,7 +124,7 @@ public class EnergyStorageComponent implements Component, IEnergySto try { super.clone(); } catch (CloneNotSupportedException e) { - Logger.atWarning().log("Cloning of " + this.getClass().getName() + " failed."); + LOGGER.atWarning().log("Cloning of " + this.getClass().getName() + " failed."); } return new EnergyStorageComponent().copyFrom(this); } diff --git a/src/main/java/org/KaiFlo/SolarCell/Helpers/BlockHelper.java b/src/main/java/org/KaiFlo/SolarCell/Helpers/BlockHelper.java index 759d50e..1b53d95 100644 --- a/src/main/java/org/KaiFlo/SolarCell/Helpers/BlockHelper.java +++ b/src/main/java/org/KaiFlo/SolarCell/Helpers/BlockHelper.java @@ -10,6 +10,7 @@ import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore; import org.checkerframework.checker.nullness.compatqual.NonNullDecl; public class BlockHelper { + public static final HytaleLogger LOGGER = HytaleLogger.getLogger(); public static void executeForCubeAround(int x, int y, int z, int size, boolean own, Callback callback) { for (int xOffset = 0; xOffset < size; xOffset++) { diff --git a/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java b/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java index a96e8cd..bd45766 100644 --- a/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java +++ b/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java @@ -10,7 +10,7 @@ import org.KaiFlo.SolarCell.Components.EnergyConsumer.Implementations.EnergyCons import org.KaiFlo.SolarCell.Components.EnergySource.Implementations.EnergySourceComponent; import org.KaiFlo.SolarCell.Components.EnergyStorage.Implementations.EnergyStorageComponent; import org.KaiFlo.SolarCell.Systems.EnergySource.EnergySourceInitializerSystem; -import org.KaiFlo.SolarCell.Systems.EnergySource.EnergyProducerTickingSystem; +import org.KaiFlo.SolarCell.Systems.EnergyTickingSystem; import javax.annotation.Nonnull; @@ -45,7 +45,7 @@ public class SolarCellPlugin extends JavaPlugin { this.getCommandRegistry().registerCommand(new ExampleCommand(this.getName(), this.getManifest().getVersion().toString())); this.getChunkStoreRegistry().registerSystem(new EnergySourceInitializerSystem()); - this.getChunkStoreRegistry().registerSystem(new EnergyProducerTickingSystem()); + this.getChunkStoreRegistry().registerSystem(new EnergyTickingSystem()); } diff --git a/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/EnergySourceInitializerSystem.java b/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/EnergySourceInitializerSystem.java index 1a5a24e..fef122b 100644 --- a/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/EnergySourceInitializerSystem.java +++ b/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/EnergySourceInitializerSystem.java @@ -3,30 +3,18 @@ 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.EnergySourceComponent; import org.checkerframework.checker.nullness.compatqual.NonNullDecl; import org.checkerframework.checker.nullness.compatqual.NullableDecl; +import static org.KaiFlo.SolarCell.Helpers.BlockHelper.setBlockRefTicking; + 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()); - if(blockInfo == null) return; - - WorldChunk worldChunk = commandBuffer.getComponent(blockInfo.getChunkRef(), WorldChunk.getComponentType()); - if(worldChunk == null) return; - - int x = ChunkUtil.xFromBlockInColumn(blockInfo.getIndex()); - int y = ChunkUtil.yFromBlockInColumn(blockInfo.getIndex()); - int z = ChunkUtil.zFromBlockInColumn(blockInfo.getIndex()); - - worldChunk.setTicking(x, y, z, true); - HytaleLogger.getLogger().atInfo().log(String.valueOf(worldChunk.isTicking(x, y, z))); + setBlockRefTicking(ref, commandBuffer); } diff --git a/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/IEnergySourceTicking.java b/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/IEnergySourceTicking.java new file mode 100644 index 0000000..7ca7ba9 --- /dev/null +++ b/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/IEnergySourceTicking.java @@ -0,0 +1,12 @@ +package org.KaiFlo.SolarCell.Systems.EnergySource; + +import com.hypixel.hytale.component.CommandBuffer; +import com.hypixel.hytale.protocol.Vector3i; +import com.hypixel.hytale.server.core.universe.world.chunk.BlockComponentChunk; +import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore; +import org.KaiFlo.SolarCell.Components.EnergySource.Implementations.EnergySourceComponent; +import org.KaiFlo.SolarCell.Components.EnergyStorage.Implementations.EnergyStorageComponent; + +public interface IEnergySourceTicking{ + void accept (EnergySourceComponent thisEnergySource, EnergyStorageComponent thisEnergyStorage, Vector3i globalPosition, BlockComponentChunk blockComponentChunk, CommandBuffer commandBuffer); +} diff --git a/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/SolarCellSourceTicking.java b/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/SolarCellSourceTicking.java new file mode 100644 index 0000000..9aad8ae --- /dev/null +++ b/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/SolarCellSourceTicking.java @@ -0,0 +1,33 @@ +package org.KaiFlo.SolarCell.Systems.EnergySource; + +import com.hypixel.hytale.component.CommandBuffer; +import com.hypixel.hytale.math.util.ChunkUtil; +import com.hypixel.hytale.protocol.Vector3i; +import com.hypixel.hytale.server.core.universe.world.chunk.BlockComponentChunk; +import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore; +import org.KaiFlo.SolarCell.Components.EnergySource.Implementations.EnergySourceComponent; +import org.KaiFlo.SolarCell.Components.EnergyStorage.Implementations.EnergyStorageComponent; +import org.KaiFlo.SolarCell.Helpers.BlockHelper; + +import static org.KaiFlo.SolarCell.Helpers.BlockHelper.LOGGER; + +public class SolarCellSourceTicking implements IEnergySourceTicking{ + @Override + public void accept(EnergySourceComponent thisEnergySource, EnergyStorageComponent thisEnergyStorage, Vector3i globalPosition, BlockComponentChunk blockComponentChunk, CommandBuffer commandBuffer){ + + BlockHelper.executeForCubeAround(globalPosition.x, globalPosition.y, globalPosition.z, 5, false, (x, y, z) -> { + var index = ChunkUtil.indexBlockInColumn(x, y, z); + var targetRef = blockComponentChunk.getEntityReference(index); + if (targetRef == null) return; + var targetEnergySource = commandBuffer.getComponent(targetRef, EnergySourceComponent.getComponentType()); + var targetEnergyStorage = commandBuffer.getComponent(targetRef, EnergyStorageComponent.getComponentType()); + if (targetEnergySource == null || targetEnergyStorage == null) return; + + var energy = targetEnergyStorage.extractEnergy(targetEnergySource.getGeneratesPerTick()); + var inserted = thisEnergyStorage.receiveEnergy(energy); + LOGGER.atInfo().log("Inserted " + inserted + "/" + energy + " |" + targetEnergyStorage.getCurrentEnergyAmount() + "| into storage" + + " at Block " + globalPosition.x + ", " + globalPosition.y + ", " + globalPosition.z + ", " + + thisEnergyStorage.getCurrentEnergyAmount() + "/" + thisEnergyStorage.getMaxCapacity()); + }); + } +} diff --git a/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/EnergyProducerTickingSystem.java b/src/main/java/org/KaiFlo/SolarCell/Systems/EnergyTickingSystem.java similarity index 54% rename from src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/EnergyProducerTickingSystem.java rename to src/main/java/org/KaiFlo/SolarCell/Systems/EnergyTickingSystem.java index 5943e25..9176f38 100644 --- a/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/EnergyProducerTickingSystem.java +++ b/src/main/java/org/KaiFlo/SolarCell/Systems/EnergyTickingSystem.java @@ -1,26 +1,30 @@ -package org.KaiFlo.SolarCell.Systems.EnergySource; +package org.KaiFlo.SolarCell.Systems; import com.hypixel.hytale.component.ArchetypeChunk; 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.protocol.Vector3i; import com.hypixel.hytale.server.core.asset.type.blocktick.BlockTickStrategy; import com.hypixel.hytale.server.core.universe.world.chunk.BlockComponentChunk; 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 dev.zkiller.energystorage.components.EnergyStorageBlockComponent; +import org.KaiFlo.SolarCell.Components.EnergyConsumer.Implementations.EnergyConsumerComponent; import org.KaiFlo.SolarCell.Components.EnergySource.Implementations.EnergySourceComponent; -import org.KaiFlo.SolarCell.Helpers.BlockHelper; +import org.KaiFlo.SolarCell.Components.EnergyStorage.Implementations.EnergyStorageComponent; +import org.KaiFlo.SolarCell.Systems.EnergySource.IEnergySourceTicking; +import org.KaiFlo.SolarCell.Systems.EnergySource.SolarCellSourceTicking; import org.checkerframework.checker.nullness.compatqual.NonNullDecl; import org.checkerframework.checker.nullness.compatqual.NullableDecl; -public class EnergyProducerTickingSystem extends EntityTickingSystem { - private final HytaleLogger LOGGER = HytaleLogger.getLogger(); +import java.util.List; + +public class EnergyTickingSystem extends EntityTickingSystem { + private final List energySourceTicking = List.of(new SolarCellSourceTicking()); @Override public void tick(float v, int i, @NonNullDecl ArchetypeChunk archetypeChunk, @NonNullDecl Store store, @NonNullDecl CommandBuffer commandBuffer) { @@ -38,30 +42,22 @@ public class EnergyProducerTickingSystem extends EntityTickingSystem (blockCompChunk, _, localX, localY, localZ, _) -> { var blockRef = blockCompChunk.getEntityReference(ChunkUtil.indexBlockInColumn(localX, localY, localZ)); if (blockRef == 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); + var globalPosition = new Vector3i(globalX, localY, globalZ); - 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 energySourceComponent = commandBuffer.getComponent(blockRef, EnergySourceComponent.getComponentType()); + var energyConsumerComponent = commandBuffer.getComponent(blockRef, EnergyConsumerComponent.getComponentType()); + var energyStorageComponent = commandBuffer.getComponent(blockRef, EnergyStorageComponent.getComponentType()); - var energy = targetEnergyStorage.extractEnergy(targetEnergySource.getGeneratesPerTick(), 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()); - }); + if (energySourceComponent != null && energyStorageComponent != null){ + energySourceTicking.forEach(energySourceTicking -> energySourceTicking.accept(energySourceComponent, energyStorageComponent,globalPosition,blockCompChunk,commandBuffer)); + return BlockTickStrategy.CONTINUE; + } - return BlockTickStrategy.CONTINUE; + + return BlockTickStrategy.IGNORED; }); }