diff --git a/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java b/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java index 92d2f49..f2bc662 100644 --- a/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java +++ b/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java @@ -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 getEnergySourceComponentType() { diff --git a/src/main/java/org/KaiFlo/SolarCell/StorageIntercationListener.java b/src/main/java/org/KaiFlo/SolarCell/StorageIntercationListener.java new file mode 100644 index 0000000..92f3759 --- /dev/null +++ b/src/main/java/org/KaiFlo/SolarCell/StorageIntercationListener.java @@ -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 CODEC = BuilderCodec.builder( + StorageIntercationListener.class, StorageIntercationListener::new, SimpleInstantInteraction.CODEC + ).build(); + + @Override + protected void firstRun(@Nonnull InteractionType interactionType, @Nonnull InteractionContext interactionContext, @Nonnull CooldownHandler cooldownHandler) { + CommandBuffer commandBuffer = interactionContext.getCommandBuffer(); + Ref 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 + )); + }); + } + } +} diff --git a/src/main/resources/Server/Item/Items/EnergyStorage.json b/src/main/resources/Server/Item/Items/EnergyStorage.json index a1805d5..8895f9a 100644 --- a/src/main/resources/Server/Item/Items/EnergyStorage.json +++ b/src/main/resources/Server/Item/Items/EnergyStorage.json @@ -41,7 +41,13 @@ } }, "Interactions": { - "Primary": "Break_Container" + "Use": { + "Interactions": [ + { + "Type": "StorageUseInteraction" + } + ] + } }, "BlockSoundSetId": "Wood", "BlockParticleSetId": "Wood", diff --git a/src/main/resources/Server/Item/Items/SolarCell.json b/src/main/resources/Server/Item/Items/SolarCell.json index b09f411..2b92f31 100644 --- a/src/main/resources/Server/Item/Items/SolarCell.json +++ b/src/main/resources/Server/Item/Items/SolarCell.json @@ -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": [