Implemented transfering between two Solarpanels

This commit is contained in:
2026-02-07 01:58:54 +01:00
parent 486254b6dd
commit 75900d50f9
7 changed files with 58 additions and 30 deletions

View File

@@ -45,7 +45,7 @@ public class ExampleCommand extends CommandBase {
Vector3i playerPosition = Objects.requireNonNull(playerTransform).getPosition().toVector3i(); Vector3i playerPosition = Objects.requireNonNull(playerTransform).getPosition().toVector3i();
var size = sizeArg.get(ctx); var size = sizeArg.get(ctx);
if (size == null) size = 5; 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); // BlockType blockType = defaultWorld.getBlockType(x, y, z);
// if (blockType != null) { // if (blockType != null) {
// LOGGER.atInfo().log(blockType.getId() + " at " + x + "," + y + "," + z); // LOGGER.atInfo().log(blockType.getId() + " at " + x + "," + y + "," + z);

View File

@@ -9,17 +9,17 @@ import org.KaiFlo.SolarCell.Components.EnergySource.AbstractEnergySource;
import org.KaiFlo.SolarCell.SolarCellPlugin; import org.KaiFlo.SolarCell.SolarCellPlugin;
import org.checkerframework.checker.nullness.compatqual.NullableDecl; import org.checkerframework.checker.nullness.compatqual.NullableDecl;
public class SolarCellComponent extends AbstractEnergySource implements Component<ChunkStore> { public class EnergySourceComponent extends AbstractEnergySource implements Component<ChunkStore> {
public static final BuilderCodec<SolarCellComponent> CODEC = BuilderCodec.builder(SolarCellComponent.class, SolarCellComponent::new).build(); public static final BuilderCodec<EnergySourceComponent> CODEC = BuilderCodec.builder(EnergySourceComponent.class, EnergySourceComponent::new).build();
private final HytaleLogger Logger = HytaleLogger.getLogger(); private final HytaleLogger Logger = HytaleLogger.getLogger();
private long energyRatePerTick = 5; private long energyRatePerTick = 5;
public static ComponentType<ChunkStore, SolarCellComponent> getComponentType() { public static ComponentType<ChunkStore, EnergySourceComponent> getComponentType() {
return SolarCellPlugin.get().getSolarCellComponentType(); return SolarCellPlugin.get().getSolarCellComponentType();
} }
private SolarCellComponent copyFrom(SolarCellComponent other) { private EnergySourceComponent copyFrom(EnergySourceComponent other) {
this.energyRatePerTick = other.energyRatePerTick; this.energyRatePerTick = other.energyRatePerTick;
return this; return this;
} }
@@ -32,7 +32,7 @@ public class SolarCellComponent extends AbstractEnergySource implements Componen
} catch (CloneNotSupportedException e) { } catch (CloneNotSupportedException e) {
Logger.atWarning().log("Cloning of " + this.getClass().getName() + " failed."); Logger.atWarning().log("Cloning of " + this.getClass().getName() + " failed.");
} }
return new SolarCellComponent().copyFrom(this); return new EnergySourceComponent().copyFrom(this);
} }
@Override @Override

View File

@@ -2,12 +2,17 @@ package org.KaiFlo.SolarCell.Helpers;
public class BlockHelper { 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 x1 = x - size / 2; x1 < x + size / 2; x1++) {
for (int y1 = y - size / 2; y1 < y + size / 2; y1++) { for (int y1 = y - size / 2; y1 < y + size / 2; y1++) {
for (int z1 = z - size / 2; z1 < z + size / 2; z1++) { for (int z1 = z - size / 2; z1 < z + size / 2; z1++) {
if (!(x1 == x && y1 == y && z1 == z)) {
callback.accept(x1, y1, z1); callback.accept(x1, y1, z1);
} }
else if (own) {
callback.accept(x1, y1, z1);
}
}
} }
} }
} }

View File

@@ -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.plugin.JavaPluginInit;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore; import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import org.KaiFlo.SolarCell.Commands.ExampleCommand; import org.KaiFlo.SolarCell.Commands.ExampleCommand;
import org.KaiFlo.SolarCell.Components.EnergySource.Implementations.SolarCellComponent; import org.KaiFlo.SolarCell.Components.EnergySource.Implementations.EnergySourceComponent;
import org.KaiFlo.SolarCell.Systems.EnergySource.SolarCellInitializer; import org.KaiFlo.SolarCell.Systems.EnergySource.EnergySourceInitializerSystem;
import org.KaiFlo.SolarCell.Systems.EnergySource.SolarCellTickingSystem; import org.KaiFlo.SolarCell.Systems.EnergySource.EnergyProducerTickingSystem;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@@ -22,7 +22,7 @@ public class SolarCellPlugin extends JavaPlugin {
private static final HytaleLogger LOGGER = HytaleLogger.forEnclosingClass(); private static final HytaleLogger LOGGER = HytaleLogger.forEnclosingClass();
private ComponentType<ChunkStore, SolarCellComponent> solarCellComponentType; private ComponentType<ChunkStore, EnergySourceComponent> solarCellComponentType;
public SolarCellPlugin(@Nonnull JavaPluginInit init) { public SolarCellPlugin(@Nonnull JavaPluginInit init) {
super(init); super(init);
@@ -34,16 +34,16 @@ public class SolarCellPlugin extends JavaPlugin {
instance = this; instance = this;
LOGGER.atInfo().log("Setting up plugin " + this.getName()); 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.getCommandRegistry().registerCommand(new ExampleCommand(this.getName(), this.getManifest().getVersion().toString()));
this.getChunkStoreRegistry().registerSystem(new SolarCellInitializer()); this.getChunkStoreRegistry().registerSystem(new EnergySourceInitializerSystem());
this.getChunkStoreRegistry().registerSystem(new SolarCellTickingSystem()); this.getChunkStoreRegistry().registerSystem(new EnergyProducerTickingSystem());
} }
public ComponentType<ChunkStore, SolarCellComponent> getSolarCellComponentType() { public ComponentType<ChunkStore, EnergySourceComponent> getSolarCellComponentType() {
return solarCellComponentType; return solarCellComponentType;
} }
} }

View File

@@ -5,6 +5,7 @@ import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store; import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.query.Query; import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem; 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.math.util.ChunkUtil;
import com.hypixel.hytale.server.core.asset.type.blocktick.BlockTickStrategy; 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.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.BlockSection;
import com.hypixel.hytale.server.core.universe.world.chunk.section.ChunkSection; import com.hypixel.hytale.server.core.universe.world.chunk.section.ChunkSection;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore; 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.NonNullDecl;
import org.checkerframework.checker.nullness.compatqual.NullableDecl; import org.checkerframework.checker.nullness.compatqual.NullableDecl;
public class SolarCellTickingSystem extends EntityTickingSystem<ChunkStore> { public class EnergyProducerTickingSystem extends EntityTickingSystem<ChunkStore> {
private final HytaleLogger LOGGER = HytaleLogger.getLogger();
@Override @Override
public void tick(float v, int i, @NonNullDecl ArchetypeChunk<ChunkStore> archetypeChunk, @NonNullDecl Store<ChunkStore> store, @NonNullDecl CommandBuffer<ChunkStore> commandBuffer) { public void tick(float v, int i, @NonNullDecl ArchetypeChunk<ChunkStore> archetypeChunk, @NonNullDecl Store<ChunkStore> store, @NonNullDecl CommandBuffer<ChunkStore> commandBuffer) {
@@ -30,20 +34,31 @@ public class SolarCellTickingSystem extends EntityTickingSystem<ChunkStore> {
var worldChunk = commandBuffer.getComponent(chunkSection.getChunkColumnReference(), WorldChunk.getComponentType()); var worldChunk = commandBuffer.getComponent(chunkSection.getChunkColumnReference(), WorldChunk.getComponentType());
if (blockComponentChunk == null || worldChunk == null) return; if (blockComponentChunk == null || worldChunk == null) return;
var world = worldChunk.getWorld();
blockSection.forEachTicking(blockComponentChunk, commandBuffer, chunkSection.getY(), blockSection.forEachTicking(blockComponentChunk, commandBuffer, chunkSection.getY(),
(_, _, localX, localY, localZ, _) -> { (blockCompChunk, _, localX, localY, localZ, _) -> {
var blockRef = blockComponentChunk.getEntityReference(ChunkUtil.indexBlockInColumn(localX, localY, localZ)); var blockRef = blockCompChunk.getEntityReference(ChunkUtil.indexBlockInColumn(localX, localY, localZ));
if (blockRef == null) return BlockTickStrategy.IGNORED; if (blockRef == null) return BlockTickStrategy.IGNORED;
var solarCellComponent = commandBuffer.getComponent(blockRef, SolarCellComponent.getComponentType()); var thisEnergySourceComponent = commandBuffer.getComponent(blockRef, EnergySourceComponent.getComponentType());
if (solarCellComponent == null) return BlockTickStrategy.IGNORED; var thisEnergyStorageComponent = commandBuffer.getComponent(blockRef, EnergyStorageBlockComponent.getComponentType());
if (thisEnergySourceComponent == null || thisEnergyStorageComponent == null)
return BlockTickStrategy.IGNORED;
int globalX = localX + (worldChunk.getX() * 32); int globalX = localX + (worldChunk.getX() * 32);
int globalZ = localZ + (worldChunk.getZ() * 32); int globalZ = localZ + (worldChunk.getZ() * 32);
world.execute(() -> { BlockHelper.executeForCubeAround(globalX, localY, globalZ, 5, false, (x, y, z) -> {
world.setBlock(globalX + 1, localY, globalZ, "Rock_Ice"); 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; return BlockTickStrategy.CONTINUE;

View File

@@ -3,15 +3,16 @@ package org.KaiFlo.SolarCell.Systems.EnergySource;
import com.hypixel.hytale.component.*; import com.hypixel.hytale.component.*;
import com.hypixel.hytale.component.query.Query; import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.system.RefSystem; import com.hypixel.hytale.component.system.RefSystem;
import com.hypixel.hytale.logger.HytaleLogger;
import com.hypixel.hytale.math.util.ChunkUtil; import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.server.core.modules.block.BlockModule; 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.chunk.WorldChunk;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore; 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.NonNullDecl;
import org.checkerframework.checker.nullness.compatqual.NullableDecl; import org.checkerframework.checker.nullness.compatqual.NullableDecl;
public class SolarCellInitializer extends RefSystem<ChunkStore> { public class EnergySourceInitializerSystem extends RefSystem<ChunkStore> {
@Override @Override
public void onEntityAdded(@NonNullDecl Ref<ChunkStore> ref, @NonNullDecl AddReason addReason, @NonNullDecl Store<ChunkStore> store, @NonNullDecl CommandBuffer<ChunkStore> commandBuffer) { public void onEntityAdded(@NonNullDecl Ref<ChunkStore> ref, @NonNullDecl AddReason addReason, @NonNullDecl Store<ChunkStore> store, @NonNullDecl CommandBuffer<ChunkStore> commandBuffer) {
BlockModule.BlockStateInfo blockInfo = commandBuffer.getComponent(ref, BlockModule.BlockStateInfo.getComponentType()); BlockModule.BlockStateInfo blockInfo = commandBuffer.getComponent(ref, BlockModule.BlockStateInfo.getComponentType());
@@ -25,6 +26,7 @@ public class SolarCellInitializer extends RefSystem<ChunkStore> {
int z = ChunkUtil.zFromBlockInColumn(blockInfo.getIndex()); int z = ChunkUtil.zFromBlockInColumn(blockInfo.getIndex());
worldChunk.setTicking(x, y, z, true); 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<ChunkStore> {
@NullableDecl @NullableDecl
@Override @Override
public Query<ChunkStore> getQuery() { public Query<ChunkStore> getQuery() {
return Query.and(SolarCellComponent.getComponentType(), BlockModule.BlockStateInfo.getComponentType()); return Query.and(EnergySourceComponent.getComponentType(), BlockModule.BlockStateInfo.getComponentType());
} }
} }

View File

@@ -45,7 +45,13 @@
"BlockBreakingDecalId": "Breaking_Decals_Rock", "BlockBreakingDecalId": "Breaking_Decals_Rock",
"BlockEntity": { "BlockEntity": {
"Components": { "Components": {
"SolarCell": {} "SolarCell": {},
"EnergyStorageComponent": {
"EnergyStored": 5,
"MaxEnergy": 80000,
"MaxReceive": 1000,
"MaxExtract": 1000
}
} }
} }
}, },