diff --git a/src/main/java/org/KaiFlo/SolarCell/Commands/ExampleCommand.java b/src/main/java/org/KaiFlo/SolarCell/Commands/ExampleCommand.java index 52ec703..2bf37f3 100644 --- a/src/main/java/org/KaiFlo/SolarCell/Commands/ExampleCommand.java +++ b/src/main/java/org/KaiFlo/SolarCell/Commands/ExampleCommand.java @@ -13,7 +13,7 @@ import com.hypixel.hytale.server.core.modules.entity.component.TransformComponen import com.hypixel.hytale.server.core.universe.Universe; import com.hypixel.hytale.server.core.universe.world.World; import com.hypixel.hytale.server.core.universe.world.storage.EntityStore; -import it.unimi.dsi.fastutil.longs.LongSet; +import org.KaiFlo.SolarCell.Helpers.BlockHelper; import javax.annotation.Nonnull; import java.util.Objects; @@ -45,7 +45,7 @@ public class ExampleCommand extends CommandBase { Vector3i playerPosition = Objects.requireNonNull(playerTransform).getPosition().toVector3i(); var size = sizeArg.get(ctx); if (size == null) size = 5; - executeForCubeAround(playerPosition.x, playerPosition.y, playerPosition.z, size, (x, y, z) -> { + BlockHelper.executeForCubeAround(playerPosition.x, playerPosition.y, playerPosition.z, size, (x, y, z) -> { // BlockType blockType = defaultWorld.getBlockType(x, y, z); // if (blockType != null) { // LOGGER.atInfo().log(blockType.getId() + " at " + x + "," + y + "," + z); @@ -63,17 +63,4 @@ public class ExampleCommand extends CommandBase { }); } - void executeForCubeAround(int x, int y, int z, int size, Callback callback) { - for (int x1 = x - size / 2; x1 < x + size / 2; x1++) { - for (int y1 = y - size / 2; y1 < y + size / 2; y1++) { - for (int z1 = z - size / 2; z1 < z + size / 2; z1++) { - callback.onBlockPosition(x1, y1, z1); - } - } - } - } } - -interface Callback { - void onBlockPosition(int x, int y, int z); -} \ No newline at end of file diff --git a/src/main/java/org/KaiFlo/SolarCell/Helpers/BlockHelper.java b/src/main/java/org/KaiFlo/SolarCell/Helpers/BlockHelper.java new file mode 100644 index 0000000..c107577 --- /dev/null +++ b/src/main/java/org/KaiFlo/SolarCell/Helpers/BlockHelper.java @@ -0,0 +1,19 @@ +package org.KaiFlo.SolarCell.Helpers; + +public class BlockHelper { + + public static void executeForCubeAround(int x, int y, int z, int size, Callback callback) { + for (int x1 = x - size / 2; x1 < x + size / 2; x1++) { + for (int y1 = y - size / 2; y1 < y + size / 2; y1++) { + for (int z1 = z - size / 2; z1 < z + size / 2; z1++) { + callback.accept(x1, y1, z1); + } + } + } + } + public interface Callback { + void accept(int x, int y, int z); + } +} + + diff --git a/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java b/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java index 2d332cf..c88f6b1 100644 --- a/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java +++ b/src/main/java/org/KaiFlo/SolarCell/SolarCellPlugin.java @@ -8,6 +8,7 @@ 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 org.KaiFlo.SolarCell.Systems.EnergySource.SolarCellTickingSystem; import javax.annotation.Nonnull; @@ -38,6 +39,7 @@ public class SolarCellPlugin extends JavaPlugin { this.getCommandRegistry().registerCommand(new ExampleCommand(this.getName(), this.getManifest().getVersion().toString())); this.getChunkStoreRegistry().registerSystem(new SolarCellInitializer()); + this.getChunkStoreRegistry().registerSystem(new SolarCellTickingSystem()); } diff --git a/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/SolarCellTickingSystem.java b/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/SolarCellTickingSystem.java new file mode 100644 index 0000000..0cdaa10 --- /dev/null +++ b/src/main/java/org/KaiFlo/SolarCell/Systems/EnergySource/SolarCellTickingSystem.java @@ -0,0 +1,59 @@ +package org.KaiFlo.SolarCell.Systems.EnergySource; + +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.math.util.ChunkUtil; +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 org.KaiFlo.SolarCell.Components.EnergySource.Implementations.SolarCellComponent; +import org.checkerframework.checker.nullness.compatqual.NonNullDecl; +import org.checkerframework.checker.nullness.compatqual.NullableDecl; + +public class SolarCellTickingSystem extends EntityTickingSystem { + + @Override + public void tick(float v, int i, @NonNullDecl ArchetypeChunk archetypeChunk, @NonNullDecl Store store, @NonNullDecl CommandBuffer commandBuffer) { + var blockSection = archetypeChunk.getComponent(i, BlockSection.getComponentType()); + if (blockSection == null || blockSection.getTickingBlocksCount() != 0) return; + + var chunkSection = archetypeChunk.getComponent(i, ChunkSection.getComponentType()); + if (chunkSection == null) return; + + var blockComponentChunk = commandBuffer.getComponent(chunkSection.getChunkColumnReference(), BlockComponentChunk.getComponentType()); + var worldChunk = commandBuffer.getComponent(chunkSection.getChunkColumnReference(), WorldChunk.getComponentType()); + if (blockComponentChunk == null || worldChunk == null) return; + + var world = worldChunk.getWorld(); + + blockSection.forEachTicking(blockComponentChunk, commandBuffer, chunkSection.getY(), + (_, _, localX, localY, localZ, _) -> { + var blockRef = blockComponentChunk.getEntityReference(ChunkUtil.indexBlockInColumn(localX, localY, localZ)); + if (blockRef == null) return BlockTickStrategy.IGNORED; + var solarCellComponent = commandBuffer.getComponent(blockRef, SolarCellComponent.getComponentType()); + if (solarCellComponent == null) return BlockTickStrategy.IGNORED; + + int globalX = localX + (worldChunk.getX() * 32); + int globalZ = localZ + (worldChunk.getZ() * 32); + + world.execute(() -> { + world.setBlock(globalX + 1, localY, globalZ, "Rock_Ice"); + }); + + return BlockTickStrategy.CONTINUE; + }); + + } + + @NullableDecl + @Override + public Query getQuery() { + return Query.and(BlockSection.getComponentType(), ChunkSection.getComponentType()); + } +} diff --git a/src/main/resources/Server/Item/Items/SolarCell.json b/src/main/resources/Server/Item/Items/SolarCell.json new file mode 100644 index 0000000..5c94050 --- /dev/null +++ b/src/main/resources/Server/Item/Items/SolarCell.json @@ -0,0 +1,66 @@ +{ + "TranslationProperties": { + "Name": "server.items.Rock_Stone.name" + }, + "ItemLevel": 10, + "MaxStack": 100, + "Icon": "Icons/ItemsGenerated/Rock_Stone_Stalactite_Small.png", + "Categories": [ + "Blocks.Rocks" + ], + "PlayerAnimationsId": "Block", + "Set": "Rock_Stone", + "BlockType": { + "Material": "Solid", + "DrawType": "Cube", + "Group": "Stone", + "Flags": {}, + "Gathering": { + "Breaking": { + "GatherType": "Rocks", + "ItemId": "Rock_Stone_Cobble" + } + }, + "BlockParticleSetId": "Stone", + "Textures": [ + { + "All": "BlockTextures/Rock_Stone.png", + "Weight": 2 + }, + { + "All": "BlockTextures/Rock_Stone_2.png", + "Weight": 1 + }, + { + "All": "BlockTextures/Rock_Stone_3.png", + "Weight": 1 + } + ], + "ParticleColor": "#737055", + "BlockSoundSetId": "Stone", + "Aliases": [ + "stone", + "stone00" + ], + "BlockBreakingDecalId": "Breaking_Decals_Rock", + "BlockEntity": { + "Components": { + "SolarCell": {} + } + } + }, + "ResourceTypes": [ + { + "Id": "Rock" + }, + { + "Id": "Rock_Stone" + } + ], + "Tags": { + "Type": [ + "Rock" + ] + }, + "ItemSoundSetId": "ISS_Blocks_Stone" +}