9
0
mirror of https://github.com/VolmitSoftware/Iris.git synced 2025-12-29 12:09:07 +00:00
This commit is contained in:
cyberpwn
2021-08-24 08:24:36 -04:00
parent 6dc3e74607
commit 7b6405fba7
11 changed files with 189 additions and 139 deletions

View File

@@ -28,21 +28,49 @@ import com.volmit.iris.util.math.M;
import com.volmit.iris.util.nbt.tag.CompoundTag;
import com.volmit.iris.util.nbt.tag.StringTag;
import com.volmit.iris.util.parallel.HyperLock;
import com.volmit.iris.util.scheduling.IrisLock;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Biome;
import org.bukkit.block.data.BlockData;
import javax.management.RuntimeErrorException;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
public class NBTWorld {
private static final BlockData AIR = B.get("AIR");
private static final Map<String, CompoundTag> blockDataCache = new KMap<>();
private static final Map<BlockData, CompoundTag> blockDataCache = new KMap<>();
private static final Function<BlockData, CompoundTag> BLOCK_DATA_COMPUTE = (blockData) -> {
CompoundTag s = new CompoundTag();
String data = blockData.getAsString(true);
NamespacedKey key = blockData.getMaterial().getKey();
s.putString("Name", key.getNamespace() + ":" + key.getKey());
if (data.contains("[")) {
String raw = data.split("\\Q[\\E")[1].replaceAll("\\Q]\\E", "");
CompoundTag props = new CompoundTag();
if (raw.contains(",")) {
for (String i : raw.split("\\Q,\\E")) {
String[] m = i.split("\\Q=\\E");
String k = m[0];
String v = m[1];
props.put(k, new StringTag(v));
}
} else {
String[] m = raw.split("\\Q=\\E");
String k = m[0];
String v = m[1];
props.put(k, new StringTag(v));
}
s.put("Properties", props);
}
return s;
};
private static final Map<Biome, Integer> biomeIds = computeBiomeIDs();
private final KMap<Long, MCAFile> loadedRegions;
private final HyperLock hyperLock = new HyperLock();
@@ -184,38 +212,8 @@ public class NBTWorld {
return b;
}
public static CompoundTag getCompound(BlockData blockData) {
String data = blockData.getAsString(true);
if (blockDataCache.containsKey(data)) {
return blockDataCache.get(data).clone();
}
CompoundTag s = new CompoundTag();
NamespacedKey key = blockData.getMaterial().getKey();
s.putString("Name", key.getNamespace() + ":" + key.getKey());
if (data.contains("[")) {
String raw = data.split("\\Q[\\E")[1].replaceAll("\\Q]\\E", "");
CompoundTag props = new CompoundTag();
if (raw.contains(",")) {
for (String i : raw.split("\\Q,\\E")) {
String[] m = i.split("\\Q=\\E");
String k = m[0];
String v = m[1];
props.put(k, new StringTag(v));
}
} else {
String[] m = raw.split("\\Q=\\E");
String k = m[0];
String v = m[1];
props.put(k, new StringTag(v));
}
s.put("Properties", props);
}
blockDataCache.put(data, s.clone());
return s;
public static CompoundTag getCompound(BlockData bd) {
return blockDataCache.computeIfAbsent(bd, BLOCK_DATA_COMPUTE).clone();
}
public BlockData getBlockData(int x, int y, int z) {
@@ -275,6 +273,13 @@ public class NBTWorld {
return c;
}
public Chunk getNewChunk(MCAFile mca, int x, int z) {
Chunk c = Chunk.newChunk();
mca.setChunk(x & 31, z & 31, c);
return c;
}
public long getIdleDuration(int x, int z) {
return hyperLock.withResult(x, z, () -> {
Long l = lastUse.get(Cache.key(x, z));

View File

@@ -25,6 +25,7 @@ import com.volmit.iris.util.nbt.tag.CompoundTag;
import com.volmit.iris.util.nbt.tag.ListTag;
import com.volmit.iris.util.nbt.tag.LongArrayTag;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import net.minecraft.world.level.chunk.DataPaletteGlobal;
import java.util.ArrayList;
import java.util.HashMap;
@@ -148,7 +149,7 @@ public class Section {
* @param blockZ The z-coordinate of the block in this Section
* @return The block state data of this block.
*/
public CompoundTag getBlockStateAt(int blockX, int blockY, int blockZ) {
public synchronized CompoundTag getBlockStateAt(int blockX, int blockY, int blockZ) {
try {
int index = getBlockIndex(blockX, blockY, blockZ);
int paletteIndex = getPaletteIndex(index);
@@ -172,7 +173,7 @@ public class Section {
* This option should only be used moderately to avoid unnecessary recalculation of the palette indices.
* Recalculating the Palette should only be executed once right before saving the Section to file.
*/
public void setBlockStateAt(int blockX, int blockY, int blockZ, CompoundTag state, boolean cleanup) {
public synchronized void setBlockStateAt(int blockX, int blockY, int blockZ, CompoundTag state, boolean cleanup) {
int paletteIndex = addToPalette(state);
int paletteSizeBefore = palette.size();
//power of 2 --> bits must increase, but only if the palette size changed
@@ -184,7 +185,6 @@ public class Section {
}
setPaletteIndex(getBlockIndex(blockX, blockY, blockZ), paletteIndex, blockStates);
if (cleanup) {
cleanupPaletteAndBlockStates();
}
@@ -196,7 +196,7 @@ public class Section {
* @param blockStateIndex The index of the block in this section, ranging from 0-4095.
* @return The index of the block data in the palette.
*/
public int getPaletteIndex(int blockStateIndex) {
public synchronized int getPaletteIndex(int blockStateIndex) {
int bits = blockStates.length() >> 6;
if (dataVersion < 2527) {
@@ -251,7 +251,7 @@ public class Section {
*
* @return The palette of this Section.
*/
public ListTag<CompoundTag> getPalette() {
public synchronized ListTag<CompoundTag> getPalette() {
return palette;
}
@@ -367,7 +367,7 @@ public class Section {
/**
* @return The indices of the block states of this Section.
*/
public AtomicLongArray getBlockStates() {
public synchronized AtomicLongArray getBlockStates() {
return blockStates;
}
@@ -431,7 +431,7 @@ public class Section {
* @param y The Y-value of this Section
* @return A reference to the raw CompoundTag this Section is based on
*/
public CompoundTag updateHandle(int y) {
public synchronized CompoundTag updateHandle(int y) {
data.putByte("Y", (byte) y);
if (palette != null) {
data.put("Palette", palette);