From 39b4649a048583c6aea5bb4a91f88e8180af172b Mon Sep 17 00:00:00 2001 From: Daniel Mills Date: Tue, 27 Oct 2020 21:19:47 -0400 Subject: [PATCH] Parallax API --- .../v2/scaffold/parallax/ParallaxAccess.java | 54 +++++++ .../v2/scaffold/parallax/ParallaxWorld.java | 142 ++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 src/main/java/com/volmit/iris/gen/v2/scaffold/parallax/ParallaxAccess.java create mode 100644 src/main/java/com/volmit/iris/gen/v2/scaffold/parallax/ParallaxWorld.java diff --git a/src/main/java/com/volmit/iris/gen/v2/scaffold/parallax/ParallaxAccess.java b/src/main/java/com/volmit/iris/gen/v2/scaffold/parallax/ParallaxAccess.java new file mode 100644 index 000000000..1bd20bbc4 --- /dev/null +++ b/src/main/java/com/volmit/iris/gen/v2/scaffold/parallax/ParallaxAccess.java @@ -0,0 +1,54 @@ +package com.volmit.iris.gen.v2.scaffold.parallax; + +import com.volmit.iris.gen.v2.scaffold.hunk.Hunk; +import org.bukkit.block.data.BlockData; + +public interface ParallaxAccess +{ + default BlockData getBlock(int x, int y, int z) + { + return getBlocksR(x>>4,z>>4).get(x & 15,y,z & 15); + } + + default void setBlock(int x, int y, int z, BlockData d) + { + getBlocksRW(x>>4, z>>4).set(x&15, y, z&15, d); + } + + default String getObject(int x, int y, int z) + { + return getObjectsR(x>>4,z>>4).get(x & 15,y,z & 15); + } + + default void setObject(int x, int y, int z, String d) + { + getObjectsRW(x>>4, z>>4).set(x&15, y, z&15, d); + } + + default Boolean isUpdate(int x, int y, int z) + { + return getUpdatesR(x>>4,z>>4).get(x & 15,y,z & 15); + } + + default void updateBlock(int x, int y, int z) + { + setUpdate(x, y, z, true); + } + + default void setUpdate(int x, int y, int z, boolean d) + { + getUpdatesRW(x>>4, z>>4).set(x&15, y, z&15, d); + } + + public Hunk getBlocksR(int x, int z); + + public Hunk getBlocksRW(int x, int z); + + public Hunk getObjectsR(int x, int z); + + public Hunk getObjectsRW(int x, int z); + + public Hunk getUpdatesR(int x, int z); + + public Hunk getUpdatesRW(int x, int z); +} diff --git a/src/main/java/com/volmit/iris/gen/v2/scaffold/parallax/ParallaxWorld.java b/src/main/java/com/volmit/iris/gen/v2/scaffold/parallax/ParallaxWorld.java new file mode 100644 index 000000000..9a59a54d6 --- /dev/null +++ b/src/main/java/com/volmit/iris/gen/v2/scaffold/parallax/ParallaxWorld.java @@ -0,0 +1,142 @@ +package com.volmit.iris.gen.v2.scaffold.parallax; + +import com.volmit.iris.gen.v2.scaffold.hunk.Hunk; +import com.volmit.iris.gen.v2.scaffold.hunk.io.HunkCompoundRegion; +import com.volmit.iris.util.KList; +import com.volmit.iris.util.KMap; +import org.bukkit.block.data.BlockData; + +import java.io.File; +import java.io.IOException; + +public class ParallaxWorld implements ParallaxAccess { + private final KMap loadedRegions; + private final KList save; + private final File folder; + private final int height; + + public ParallaxWorld(int height, File folder) + { + this.height = height; + this.folder = folder; + save = new KList<>(); + loadedRegions = new KMap<>(); + folder.mkdirs(); + } + + public synchronized void close() + { + for(HunkCompoundRegion i : loadedRegions.v()) + { + unload(i.getX(), i.getZ()); + } + + save.clear(); + loadedRegions.clear(); + } + + public synchronized void save(HunkCompoundRegion region) + { + try { + region.save(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public boolean isLoaded(int x, int z) + { + return loadedRegions.containsKey(key(x,z)); + } + + public synchronized void save(int x, int z) + { + if(isLoaded(x,z)) + { + save(getR(x,z)); + } + } + + public synchronized void unload(int x, int z) + { + long key = key(x,z); + + if(isLoaded(x, z)) + { + if(save.contains(key)) + { + save(x,z); + save.remove(key); + } + + loadedRegions.remove(key); + } + } + + public synchronized HunkCompoundRegion load(int x, int z) + { + if(isLoaded(x,z)) + { + return loadedRegions.get(key(x,z)); + } + + HunkCompoundRegion v = new HunkCompoundRegion(height, folder, x, z); + loadedRegions.put(key(x,z), v); + return v; + } + + public HunkCompoundRegion getR(int x, int z) + { + long key = key(x,z); + + HunkCompoundRegion region = loadedRegions.get(key); + + if(region == null) + { + region = load(x, z); + } + + return region; + } + + public HunkCompoundRegion getRW(int x, int z) + { + save.addIfMissing(key(x,z)); + return getR(x,z); + } + + private long key(int x, int z) + { + return (((long)x) << 32) | (((long)z) & 0xffffffffL); + } + + @Override + public Hunk getBlocksR(int x, int z) { + return getR(x>>5, z>>5).getParallaxSlice().getR(x & 31,z & 31); + } + + @Override + public synchronized Hunk getBlocksRW(int x, int z) { + return getRW(x>>5, z>>5).getParallaxSlice().getRW(x & 31,z & 31); + } + + @Override + public Hunk getObjectsR(int x, int z) { + return getR(x>>5, z>>5).getObjectSlice().getR(x & 31,z & 31); + } + + @Override + public synchronized Hunk getObjectsRW(int x, int z) { + return getRW(x>>5, z>>5).getObjectSlice().getRW(x & 31,z & 31); + } + + @Override + public Hunk getUpdatesR(int x, int z) { + return getR(x>>5, z>>5).getUpdateSlice().getR(x & 31,z & 31); + } + + @Override + public synchronized Hunk getUpdatesRW(int x, int z) { + return getRW(x>>5, z>>5).getUpdateSlice().getRW(x & 31,z & 31); + } +}