Plex-FAWE/worldedit-core/src/main/java/com/boydti/fawe/beta/IBlocks.java

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

133 lines
4.9 KiB
Java
Raw Normal View History

2019-04-28 15:44:59 +00:00
package com.boydti.fawe.beta;
2019-10-26 13:21:49 +00:00
import com.boydti.fawe.FaweCache;
import com.boydti.fawe.object.FaweOutputStream;
import com.boydti.fawe.object.io.FastByteArrayOutputStream;
2019-10-23 04:23:52 +00:00
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.platform.Capability;
2019-10-23 04:23:52 +00:00
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BlockID;
2019-10-23 04:23:52 +00:00
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.registry.BlockRegistry;
2020-07-14 02:50:59 +00:00
import org.jetbrains.annotations.Range;
import java.io.IOException;
2019-10-23 04:23:52 +00:00
import java.util.Map;
import java.util.Set;
import java.util.stream.IntStream;
2019-10-23 04:23:52 +00:00
2019-04-28 17:36:23 +00:00
/**
* A shared interface for IGetBlocks and ISetBlocks.
2019-04-28 17:36:23 +00:00
*/
2019-04-30 16:19:10 +00:00
public interface IBlocks extends Trimable {
2019-08-06 15:28:12 +00:00
2020-01-29 22:10:59 +00:00
boolean hasSection(@Range(from = 0, to = 15) int layer);
2019-04-28 15:44:59 +00:00
2019-11-07 10:28:17 +00:00
char[] load(int layer);
2019-10-23 04:23:52 +00:00
BlockState getBlock(int x, int y, int z);
2019-10-26 13:21:49 +00:00
Map<BlockVector3, CompoundTag> getTiles();
CompoundTag getTile(int x, int y, int z);
2019-10-26 13:21:49 +00:00
Set<CompoundTag> getEntities();
2019-12-19 16:19:46 +00:00
BiomeType getBiomeType(int x, int y, int z);
2019-10-26 13:21:49 +00:00
default int getBitMask() {
return IntStream.range(0, FaweCache.IMP.CHUNK_LAYERS).filter(this::hasSection)
.map(layer -> (1 << layer)).sum();
2019-10-26 13:21:49 +00:00
}
boolean trim(boolean aggressive, int layer);
2019-07-20 05:32:15 +00:00
IBlocks reset();
default byte[] toByteArray(boolean full, boolean stretched) {
return toByteArray(null, getBitMask(), full, stretched);
}
default byte[] toByteArray(byte[] buffer, int bitMask, boolean full, boolean stretched) {
if (buffer == null) {
buffer = new byte[1024];
}
BlockRegistry registry = WorldEdit.getInstance().getPlatformManager()
.queryCapability(Capability.GAME_HOOKS).getRegistries().getBlockRegistry();
FastByteArrayOutputStream sectionByteArray = new FastByteArrayOutputStream(buffer);
try (FaweOutputStream sectionWriter = new FaweOutputStream(sectionByteArray)) {
for (int layer = 0; layer < FaweCache.IMP.CHUNK_LAYERS; layer++) {
if (!this.hasSection(layer) || (bitMask & (1 << layer)) == 0) {
continue;
}
2019-11-17 17:22:21 +00:00
2019-11-07 10:28:17 +00:00
char[] ids = this.load(layer);
int nonEmpty = 0; // TODO optimize into same loop as toPalette
2019-11-04 07:47:31 +00:00
for (int i = 0; i < ids.length; i++) {
char ordinal = ids[i];
switch (ordinal) {
case BlockID.__RESERVED__:
case BlockID.CAVE_AIR:
case BlockID.VOID_AIR:
ids[i] = BlockID.AIR;
case BlockID.AIR:
continue;
default:
nonEmpty++;
}
}
sectionWriter.writeShort(nonEmpty); // non empty
FaweCache.Palette palette;
if (stretched) {
palette = FaweCache.IMP.toPalette(0, ids);
} else {
palette = FaweCache.IMP.toPaletteUnstretched(0, ids);
}
sectionWriter.writeByte(palette.bitsPerEntry); // bits per block
sectionWriter.writeVarInt(palette.paletteToBlockLength);
for (int i = 0; i < palette.paletteToBlockLength; i++) {
int ordinal = palette.paletteToBlock[i];
switch (ordinal) {
case BlockID.__RESERVED__:
case BlockID.CAVE_AIR:
case BlockID.VOID_AIR:
case BlockID.AIR:
sectionWriter.write(0);
break;
default:
BlockState state = BlockState.getFromOrdinal(ordinal);
int mcId = registry.getInternalBlockStateId(state).getAsInt();
sectionWriter.writeVarInt(mcId);
break;
}
}
sectionWriter.writeVarInt(palette.blockStatesLength);
for (int i = 0; i < palette.blockStatesLength; i++) {
sectionWriter.writeLong(palette.blockStates[i]);
}
}
2019-11-17 17:22:21 +00:00
if (full) {
2019-11-14 19:21:28 +00:00
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
2019-12-19 16:19:46 +00:00
BiomeType biome = getBiomeType(x, 0, z);
2019-11-14 19:21:28 +00:00
if (biome != null) {
sectionWriter.writeInt(biome.getLegacyId());
} else {
sectionWriter.writeInt(0);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return sectionByteArray.toByteArray();
}
2019-08-06 15:28:12 +00:00
}