Added Interaction for EnergyStorages

This commit is contained in:
Flotschispielt
2026-02-08 16:08:29 +01:00
parent b085d8b6ad
commit ba5f5e11b4
4 changed files with 97 additions and 7 deletions

View File

@@ -2,6 +2,7 @@ package org.KaiFlo.SolarCell;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.logger.HytaleLogger;
import com.hypixel.hytale.server.core.modules.interaction.interaction.config.Interaction;
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;
@@ -56,6 +57,8 @@ public class SolarCellPlugin extends JavaPlugin {
this.getChunkStoreRegistry().registerSystem(new EnergyStorageInitializerSystem());
this.getChunkStoreRegistry().registerSystem(energyTickingSystem);
this.getCodecRegistry(Interaction.CODEC).register("StorageUseInteraction", StorageIntercationListener.class, StorageIntercationListener.CODEC);
}
public ComponentType<ChunkStore, EnergySourceComponent> getEnergySourceComponentType() {

View File

@@ -0,0 +1,75 @@
package org.KaiFlo.SolarCell;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.protocol.InteractionType;
import com.hypixel.hytale.protocol.SimpleBlockInteraction;
import com.hypixel.hytale.server.core.entity.InteractionContext;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.modules.entity.component.DisplayNameComponent;
import com.hypixel.hytale.server.core.modules.interaction.interaction.CooldownHandler;
import com.hypixel.hytale.server.core.modules.interaction.interaction.config.SimpleInstantInteraction;
import com.hypixel.hytale.server.core.modules.interaction.interaction.config.SimpleInteraction;
import com.hypixel.hytale.server.core.modules.interaction.interaction.config.client.UseBlockInteraction;
import com.hypixel.hytale.server.core.universe.world.World;
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.storage.EntityStore;
import com.hypixel.hytale.server.core.util.NotificationUtil;
import com.hypixel.hytale.protocol.BlockPosition;
import org.KaiFlo.SolarCell.Components.EnergyStorage.Implementations.EnergyStorageComponent;
import javax.annotation.Nonnull;
public class StorageIntercationListener extends SimpleInstantInteraction {
public static final BuilderCodec<StorageIntercationListener> CODEC = BuilderCodec.builder(
StorageIntercationListener.class, StorageIntercationListener::new, SimpleInstantInteraction.CODEC
).build();
@Override
protected void firstRun(@Nonnull InteractionType interactionType, @Nonnull InteractionContext interactionContext, @Nonnull CooldownHandler cooldownHandler) {
CommandBuffer<EntityStore> commandBuffer = interactionContext.getCommandBuffer();
Ref<EntityStore> ref = interactionContext.getEntity();
BlockPosition pos = interactionContext.getTargetBlock();
Player player = commandBuffer.getComponent(ref, Player.getComponentType());
World world = commandBuffer.getExternalData().getWorld();
if (interactionType.getValue() == InteractionType.Use.getValue()) {
int chunkX = Math.floorDiv(pos.x, 32);
int chunkZ = Math.floorDiv(pos.z, 32);
int localX = Math.floorMod(pos.x, 32);
int localZ = Math.floorMod(pos.z, 32);
world.execute(() -> {
var chunkStore = world.getChunkStore().getStore();
var targetChunk = world.getChunk(ChunkUtil.indexChunk(chunkX, chunkZ));
if (targetChunk == null) return;
var blockComponentChunk = chunkStore.getComponent(
targetChunk.getReference(),
BlockComponentChunk.getComponentType()
);
if (blockComponentChunk == null) return;
int index = ChunkUtil.indexBlockInColumn(localX, pos.y, localZ);
var targetRef = blockComponentChunk.getEntityReference(index);
if (targetRef == null) return;
var energyStorage = chunkStore.getComponent(targetRef, EnergyStorageComponent.getComponentType());
if (energyStorage == null) return;
NotificationUtil.sendNotificationToUniverse(String.format("%d/%d (%.2f%%)",
energyStorage.getCurrentEnergyAmount(),
energyStorage.getMaxCapacity(),
energyStorage.getCurrentEnergyToCapacityRatio() * 100
));
});
}
}
}

View File

@@ -41,7 +41,13 @@
}
},
"Interactions": {
"Primary": "Break_Container"
"Use": {
"Interactions": [
{
"Type": "StorageUseInteraction"
}
]
}
},
"BlockSoundSetId": "Wood",
"BlockParticleSetId": "Wood",

View File

@@ -19,14 +19,14 @@
"BlockEntity": {
"Components": {
"EnergyStorage": {
"MaxCapacity": 10000,
"MaxCapacity": 1000,
"ExtractEnergyPerTick": 100,
"ReceiveEnergyPerTick": 1000,
"CurrentEnergyAmount": 0
},
"EnergySource": {
"EnergyCapacity": -1,
"GeneratesPerTick": 100
"GeneratesPerTick": 1
}
}
},
@@ -51,14 +51,20 @@
}
]
},
"Interactions": {
"Primary": "Break_Container"
},
"State": {},
"BlockSoundSetId": "Wood",
"BlockParticleSetId": "Wood",
"VariantRotation": "NESW",
"ParticleColor": "#3e352a"
"ParticleColor": "#3e352a",
"Interactions": {
"Use": {
"Interactions": [
{
"Type": "StorageUseInteraction"
}
]
}
}
},
"Scale": 1,
"ResourceTypes": [