implement ChunkAccess

This commit is contained in:
dfsek 2021-01-24 01:58:51 -07:00
parent da5d3fe0ce
commit ce62f1d7ac
7 changed files with 77 additions and 27 deletions

View File

@ -1,6 +1,31 @@
package com.dfsek.terra.api.platform.world;
import com.dfsek.terra.api.platform.Handle;
import com.dfsek.terra.api.platform.block.BlockData;
import org.jetbrains.annotations.NotNull;
public interface ChunkAccess extends Handle {
/**
* Set the block at x,y,z in the chunk data to material.
* <p>
* Setting blocks outside the chunk's bounds does nothing.
*
* @param x the x location in the chunk from 0-15 inclusive
* @param y the y location in the chunk from 0 (inclusive) - maxHeight (exclusive)
* @param z the z location in the chunk from 0-15 inclusive
* @param blockData the type to set the block to
*/
void setBlock(int x, int y, int z, @NotNull BlockData blockData);
/**
* Get the type and data of the block at x, y, z.
* <p>
* Getting blocks outside the chunk's bounds returns air.
*
* @param x the x location in the chunk from 0-15 inclusive
* @param y the y location in the chunk from 0 (inclusive) - maxHeight (exclusive)
* @param z the z location in the chunk from 0-15 inclusive
* @return the data of the block or the BlockData for air if x, y or z are outside the chunk's bounds
*/
@NotNull BlockData getBlockData(int x, int y, int z);
}

View File

@ -1,7 +1,6 @@
package com.dfsek.terra.api.platform.world.generator;
import com.dfsek.terra.api.platform.Handle;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.platform.world.BiomeGrid;
import com.dfsek.terra.api.platform.world.ChunkAccess;
import com.dfsek.terra.api.platform.world.World;
@ -40,31 +39,5 @@ public interface ChunkGenerator extends Handle {
* @return the maximum height
*/
int getMaxHeight();
/**
* Set the block at x,y,z in the chunk data to material.
* <p>
* Setting blocks outside the chunk's bounds does nothing.
*
* @param x the x location in the chunk from 0-15 inclusive
* @param y the y location in the chunk from 0 (inclusive) - maxHeight (exclusive)
* @param z the z location in the chunk from 0-15 inclusive
* @param blockData the type to set the block to
*/
void setBlock(int x, int y, int z, @NotNull BlockData blockData);
/**
* Get the type and data of the block at x, y, z.
* <p>
* Getting blocks outside the chunk's bounds returns air.
*
* @param x the x location in the chunk from 0-15 inclusive
* @param y the y location in the chunk from 0 (inclusive) - maxHeight (exclusive)
* @param z the z location in the chunk from 0-15 inclusive
* @return the data of the block or the BlockData for air if x, y or z are outside the chunk's bounds
*/
@NotNull BlockData getBlockData(int x, int y, int z);
}
}

View File

@ -1,4 +1,7 @@
package com.dfsek.terra.carving;
import com.dfsek.terra.api.platform.world.ChunkAccess;
public interface Carver {
void carve(int chunkX, int chunkZ, ChunkAccess chunk);
}

View File

@ -4,6 +4,7 @@ package com.dfsek.terra.bukkit.world;
import com.dfsek.terra.api.math.vector.Vector3;
import com.dfsek.terra.api.platform.CommandSender;
import com.dfsek.terra.api.platform.block.Axis;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.platform.block.BlockFace;
import com.dfsek.terra.api.platform.block.data.Bisected;
import com.dfsek.terra.api.platform.block.data.Rail;
@ -16,6 +17,7 @@ import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.api.transform.MapTransform;
import com.dfsek.terra.api.transform.Transformer;
import com.dfsek.terra.bukkit.BukkitCommandSender;
import com.dfsek.terra.bukkit.world.block.data.BukkitBlockData;
import com.dfsek.terra.bukkit.world.inventory.meta.BukkitEnchantment;
import org.bukkit.Location;
import org.bukkit.TreeType;
@ -55,6 +57,14 @@ public final class BukkitAdapter {
}
}
public static BlockData adapt(org.bukkit.block.data.BlockData data) {
return BukkitBlockData.newInstance(data);
}
public static org.bukkit.block.data.BlockData adapt(BlockData data) {
return ((BukkitBlockData) data).getHandle();
}
public static Axis adapt(org.bukkit.Axis axis) {
switch(axis) {
case X:

View File

@ -1,9 +1,11 @@
package com.dfsek.terra.bukkit.world;
import com.dfsek.terra.api.platform.block.Block;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.platform.world.Chunk;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.bukkit.world.block.BukkitBlock;
import org.jetbrains.annotations.NotNull;
public class BukkitChunk implements Chunk {
private final org.bukkit.Chunk delegate;
@ -36,4 +38,14 @@ public class BukkitChunk implements Chunk {
public org.bukkit.Chunk getHandle() {
return delegate;
}
@Override
public void setBlock(int x, int y, int z, @NotNull BlockData blockData) {
delegate.getBlock(x, y, z).setBlockData(BukkitAdapter.adapt(blockData));
}
@Override
public @NotNull BlockData getBlockData(int x, int y, int z) {
return getBlock(x, y, z).getBlockData();
}
}

View File

@ -1,8 +1,12 @@
package com.dfsek.terra.fabric.world.handles.chunk;
import com.dfsek.terra.api.platform.block.Block;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.platform.world.Chunk;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.fabric.world.block.FabricBlockData;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.NotNull;
public class FabricChunk implements Chunk {
private final net.minecraft.world.chunk.Chunk chunk;
@ -35,4 +39,14 @@ public class FabricChunk implements Chunk {
public net.minecraft.world.chunk.Chunk getHandle() {
return chunk;
}
@Override
public void setBlock(int x, int y, int z, @NotNull BlockData blockData) {
chunk.setBlockState(new BlockPos(x, y, z), ((FabricBlockData) blockData).getHandle(), false);
}
@Override
public @NotNull BlockData getBlockData(int x, int y, int z) {
return getBlock(x, y, z).getBlockData();
}
}

View File

@ -1,12 +1,15 @@
package com.dfsek.terra.fabric.world.handles.chunk;
import com.dfsek.terra.api.platform.block.Block;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.platform.world.Chunk;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.fabric.world.block.FabricBlock;
import com.dfsek.terra.fabric.world.block.FabricBlockData;
import com.dfsek.terra.fabric.world.handles.world.FabricWorldAccess;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.WorldAccess;
import org.jetbrains.annotations.NotNull;
public class FabricChunkWorldAccess implements Chunk {
private final WorldAccess chunkRegion;
@ -44,4 +47,14 @@ public class FabricChunkWorldAccess implements Chunk {
public WorldAccess getHandle() {
return chunkRegion;
}
@Override
public void setBlock(int x, int y, int z, @NotNull BlockData blockData) {
chunkRegion.setBlockState(new BlockPos(x + this.x, y, z + this.z), ((FabricBlockData) blockData).getHandle(), 0);
}
@Override
public @NotNull BlockData getBlockData(int x, int y, int z) {
return getBlock(x, y, z).getBlockData();
}
}