diff --git a/src/main/java/org/KaiFlo/SolarCell/Components/EnergySource/Implementations/SolarCellComponent.java b/src/main/java/org/KaiFlo/SolarCell/Components/EnergySource/Implementations/SolarCellComponent.java index 44207f4..eccc36b 100644 --- a/src/main/java/org/KaiFlo/SolarCell/Components/EnergySource/Implementations/SolarCellComponent.java +++ b/src/main/java/org/KaiFlo/SolarCell/Components/EnergySource/Implementations/SolarCellComponent.java @@ -2,9 +2,11 @@ package org.KaiFlo.SolarCell.Components.EnergySource.Implementations; import com.hypixel.hytale.codec.builder.BuilderCodec; 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.AbstractEnergySource; +import org.KaiFlo.SolarCell.SolarCellPlugin; import org.checkerframework.checker.nullness.compatqual.NullableDecl; public class SolarCellComponent extends AbstractEnergySource implements Component { @@ -13,6 +15,10 @@ public class SolarCellComponent extends AbstractEnergySource implements Componen private final HytaleLogger Logger = HytaleLogger.getLogger(); private long energyRatePerTick = 5; + public static ComponentType getComponentType() { + return SolarCellPlugin.get().getSolarCellComponentType(); + } + private SolarCellComponent copyFrom(SolarCellComponent other) { this.energyRatePerTick = other.energyRatePerTick; return this; diff --git a/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java b/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java index 9cf65aa..2d332cf 100644 --- a/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java +++ b/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java @@ -1,23 +1,28 @@ package org.KaiFlo.SolarCell; +import com.hypixel.hytale.component.ComponentType; import com.hypixel.hytale.logger.HytaleLogger; 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 javax.annotation.Nonnull; -/** - * This class serves as the entrypoint for your plugin. Use the setup method to register into game registries or add - * event listeners. - */ - -@SuppressWarnings("unused") public class SolarCellPlugin extends JavaPlugin { + protected static SolarCellPlugin instance; + + public static SolarCellPlugin get() { + return instance; + } + private static final HytaleLogger LOGGER = HytaleLogger.forEnclosingClass(); + private ComponentType solarCellComponentType; + public SolarCellPlugin(@Nonnull JavaPluginInit init) { super(init); LOGGER.atInfo().log("Hello from " + this.getName() + " version " + this.getManifest().getVersion().toString()); @@ -25,8 +30,18 @@ public class SolarCellPlugin extends JavaPlugin { @Override protected void setup() { + instance = this; LOGGER.atInfo().log("Setting up plugin " + this.getName()); + + solarCellComponentType = this.getChunkStoreRegistry().registerComponent(SolarCellComponent.class, "SolarCell", SolarCellComponent.CODEC); + this.getCommandRegistry().registerCommand(new ExampleCommand(this.getName(), this.getManifest().getVersion().toString())); - this.getChunkStoreRegistry().registerComponent(SolarCellComponent.class,"SolarCell",SolarCellComponent.CODEC); + + this.getChunkStoreRegistry().registerSystem(new SolarCellInitializer()); + + } + + public ComponentType getSolarCellComponentType() { + return solarCellComponentType; } } \ No newline at end of file diff --git a/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/SolarCellInitializer.java b/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/SolarCellInitializer.java new file mode 100644 index 0000000..b331272 --- /dev/null +++ b/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/SolarCellInitializer.java @@ -0,0 +1,41 @@ +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.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.checkerframework.checker.nullness.compatqual.NonNullDecl; +import org.checkerframework.checker.nullness.compatqual.NullableDecl; + +public class SolarCellInitializer 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); + } + + + @Override + public void onEntityRemove(@NonNullDecl Ref ref, @NonNullDecl RemoveReason removeReason, @NonNullDecl Store store, @NonNullDecl CommandBuffer commandBuffer) { + //Nothing to do yet + } + + @NullableDecl + @Override + public Query getQuery() { + return Query.and(SolarCellComponent.getComponentType(), BlockModule.BlockStateInfo.getComponentType()); + } +}