net.querz.nbt.tag.CompoundTag Java Examples

The following examples show how to use net.querz.nbt.tag.CompoundTag. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DeleteSectionsField.java    From mcaselector with MIT License 6 votes vote down vote up
@Override
public void change(CompoundTag root) {
	Tag<?> rawSections = root.getCompoundTag("Level").get("Sections");
	if (rawSections == null || rawSections.getID() == LongArrayTag.ID) {
		return;
	}
	ListTag<CompoundTag> sections = ((ListTag<?>) rawSections).asCompoundTagList();
	for (int i = 0; i < sections.size(); i++) {
		CompoundTag section = sections.get(i);
		for (Range range : getNewValue()) {
			if (range.contains(section.getByte("Y"))) {
				sections.remove(i);
				i--;
			}
		}
	}
}
 
Example #2
Source File: Chunk.java    From NBT with MIT License 6 votes vote down vote up
/**
 * Reads chunk data from a RandomAccessFile. The RandomAccessFile must already be at the correct position.
 * @param raf The RandomAccessFile to read the chunk data from.
 * @param loadFlags A logical or of {@link LoadFlags} constants indicating what data should be loaded
 * @throws IOException When something went wrong during reading.
 */
public void deserialize(RandomAccessFile raf, long loadFlags) throws IOException {
	byte compressionTypeByte = raf.readByte();
	CompressionType compressionType = CompressionType.getFromID(compressionTypeByte);
	if (compressionType == null) {
		throw new IOException("invalid compression type " + compressionTypeByte);
	}
	BufferedInputStream dis = new BufferedInputStream(compressionType.decompress(new FileInputStream(raf.getFD())));
	NamedTag tag = new NBTDeserializer(false).fromStream(dis);
	if (tag != null && tag.getTag() instanceof CompoundTag) {
		data = (CompoundTag) tag.getTag();
		initReferences(loadFlags);
	} else {
		throw new IOException("invalid data tag: " + (tag == null ? "null" : tag.getClass().getName()));
	}
}
 
Example #3
Source File: Anvil112ChunkDataProcessor.java    From mcaselector with MIT License 6 votes vote down vote up
public void mergeChunks(CompoundTag source, CompoundTag destination, List<Range> ranges) {
	mergeCompoundTagLists(source, destination, ranges, "Sections", c -> (int) c.getByte("Y"));
	mergeCompoundTagLists(source, destination, ranges, "Entities", c -> c.getListTag("Pos").asDoubleTagList().get(1).asInt() >> 4);
	mergeCompoundTagLists(source, destination, ranges, "TileEntities", c -> c.getInt("y") >> 4);
	mergeCompoundTagLists(source, destination, ranges, "TileTicks", c -> c.getInt("y") >> 4);
	mergeCompoundTagLists(source, destination, ranges, "LiquidTicks", c -> c.getInt("y") >> 4);
	mergeListTagLists(source, destination, ranges, "Lights");
	mergeListTagLists(source, destination, ranges, "LiquidsToBeTicked");
	mergeListTagLists(source, destination, ranges, "ToBeTicked");
	mergeListTagLists(source, destination, ranges, "PostProcessing");
	mergeStructures(source, destination, ranges);
	// do not merge biomes here, we will overwrite this function in a future version

	// we need to fix entity UUIDs, because Minecraft doesn't like duplicates
	fixEntityUUIDs(destination);
}
 
Example #4
Source File: Section.java    From NBT with MIT License 6 votes vote down vote up
void putValueIndexedPalette(CompoundTag data, int index) {
	PaletteIndex leaf = new PaletteIndex(data, index);
	String name = data.getString("Name");
	List<PaletteIndex> leaves = valueIndexedPalette.get(name);
	if (leaves == null) {
		leaves = new ArrayList<>(1);
		leaves.add(leaf);
		valueIndexedPalette.put(name, leaves);
	} else {
		for (PaletteIndex pal : leaves) {
			if (pal.data.equals(data)) {
				return;
			}
		}
		leaves.add(leaf);
	}
}
 
Example #5
Source File: Anvil113ChunkFilter.java    From mcaselector with MIT License 6 votes vote down vote up
@Override
public boolean matchBiomeIDs(CompoundTag data, int... ids) {
	if (!data.containsKey("Level") || withDefault(() -> data.getCompoundTag("Level").getIntArrayTag("Biomes"), null) == null) {
		return false;
	}

	filterLoop: for (int filterID : ids) {
		for (int dataID : data.getCompoundTag("Level").getIntArray("Biomes")) {
			if (filterID == dataID) {
				continue filterLoop;
			}
		}
		return false;
	}
	return true;
}
 
Example #6
Source File: SNBTParser.java    From NBT with MIT License 6 votes vote down vote up
private CompoundTag parseCompoundTag(int maxDepth) throws ParseException {
	ptr.expectChar('{');

	CompoundTag compoundTag = new CompoundTag();

	ptr.skipWhitespace();
	while (ptr.hasNext() && ptr.currentChar() != '}') {
		ptr.skipWhitespace();
		String key = ptr.currentChar() == '"' ? ptr.parseQuotedString() : ptr.parseSimpleString();
		if (key.isEmpty()) {
			throw new ParseException("empty keys are not allowed");
		}
		ptr.expectChar(':');

		compoundTag.put(key, parseAnything(decrementMaxDepth(maxDepth)));

		if (!ptr.nextArrayElement()) {
			break;
		}
	}
	ptr.expectChar('}');
	return compoundTag;
}
 
Example #7
Source File: EntityFilter.java    From mcaselector with MIT License 6 votes vote down vote up
@Override
public boolean contains(List<String> value, FilterData data) {
	Tag<?> rawEntities = data.getChunk().getCompoundTag("Level").get("Entities");
	if (rawEntities == null || rawEntities.getID() == LongArrayTag.ID) {
		return false;
	}
	ListTag<CompoundTag> entities = ((ListTag<?>) rawEntities).asCompoundTagList();
	nameLoop: for (String name : getFilterValue()) {
		for (CompoundTag entity : entities) {
			String id = entity.getString("id");
			if (name.equals(id)) {
				continue nameLoop;
			}
		}
		return false;
	}
	return true;
}
 
Example #8
Source File: NBTInputStream.java    From NBT with MIT License 5 votes vote down vote up
private static CompoundTag readCompound(NBTInputStream in, int maxDepth) throws IOException {
	CompoundTag comp = new CompoundTag();
	for (int id = in.readByte() & 0xFF; id != 0; id = in.readByte() & 0xFF) {
		String key = in.readUTF();
		Tag<?> element = in.readTag((byte) id, in.decrementMaxDepth(maxDepth));
		comp.put(key, element);
	}
	return comp;
}
 
Example #9
Source File: LevelDatNbt.java    From amidst with GNU General Public License v3.0 5 votes vote down vote up
private static String readGeneratorOptions(CompoundTag dataTag, WorldType worldType) {
	if (worldType == WorldType.CUSTOMIZED) {
		return readGeneratorOptions(dataTag);
	} else {
		return "";
	}
}
 
Example #10
Source File: LevelDatNbt.java    From amidst with GNU General Public License v3.0 5 votes vote down vote up
private static long readRandomSeed(CompoundTag dataTag) {
	Tag<?> randomSeed = dataTag.get(NBTTagKeys.TAG_KEY_RANDOM_SEED);
	if (randomSeed != null) {
		return NBTUtils.getLongValue(randomSeed);
	}
	// Minecraft 1.16 format
	CompoundTag worldGenSettings = dataTag.get(NBTTagKeys.TAG_KEY_WORLD_GEN_SETTINGS, CompoundTag.class);
	return NBTUtils.getLongValue(worldGenSettings.get(NBTTagKeys.TAG_KEY_SEED));
}
 
Example #11
Source File: MCAChunkData.java    From mcaselector with MIT License 5 votes vote down vote up
static MCAChunkData newEmptyLevelMCAChunkData(Point2i absoluteLocation, int dataVersion) {
	MCAChunkData mcaChunkData = new MCAChunkData(absoluteLocation, 0, 0, (byte) 1);
	CompoundTag root = new CompoundTag();
	CompoundTag level = new CompoundTag();
	level.putInt("xPos", absoluteLocation.getX());
	level.putInt("zPos", absoluteLocation.getY());
	level.putString("Status", "full");
	root.put("Level", level);
	root.putInt("DataVersion", dataVersion);
	mcaChunkData.data = root;
	mcaChunkData.compressionType = CompressionType.ZLIB;
	return mcaChunkData;
}
 
Example #12
Source File: Anvil113ColorMapping.java    From mcaselector with MIT License 5 votes vote down vote up
@Override
public int getRGB(Object o) {
	if (isWaterlogged((CompoundTag) o)) {
		return (int) mapping.get("minecraft:water");
	}
	Object value = mapping.get(withDefault(() -> ((CompoundTag) o).getString("Name"), ""));
	if (value instanceof Integer) {
		return (int) value;
	} else if (value instanceof BlockStateMapping) {
		return ((BlockStateMapping) value).getColor(withDefault(() -> ((CompoundTag) o).getCompoundTag("Properties"), null));
	}
	return 0x000000;
}
 
Example #13
Source File: Anvil113ChunkFilter.java    From mcaselector with MIT License 5 votes vote down vote up
@Override
public void forceBiome(CompoundTag data, int id) {
	if (data.containsKey("Level")) {
		int[] biomes = new int[256];
		Arrays.fill(biomes, id);
		data.getCompoundTag("Level").putIntArray("Biomes", biomes);
	}
}
 
Example #14
Source File: Anvil113ChunkFilter.java    From mcaselector with MIT License 5 votes vote down vote up
@Override
public void changeBiome(CompoundTag data, int id) {
	if (!data.containsKey("Level") || withDefault(() -> data.getCompoundTag("Level").getIntArrayTag("Biomes"), null) == null) {
		return;
	}
	Arrays.fill(data.getCompoundTag("Level").getIntArray("Biomes"), id);
}
 
Example #15
Source File: Chunk.java    From NBT with MIT License 5 votes vote down vote up
public CompoundTag getBlockStateAt(int blockX, int blockY, int blockZ) {
	Section section = sections[MCAUtil.blockToChunk(blockY)];
	if (section == null) {
		return null;
	}
	return section.getBlockStateAt(blockX, blockY, blockZ);
}
 
Example #16
Source File: Anvil113ChunkDataProcessor.java    From mcaselector with MIT License 5 votes vote down vote up
private boolean isEmpty(int paletteIndex, CompoundTag blockData) {
	if (paletteIndex == 0) {
		return true;
	}
	switch (withDefault(() -> blockData.getString("Name"), "")) {
		case "minecraft:air":
		case "minecraft:cave_air":
		case "minecraft:barrier":
		case "minecraft:structure_void":
			return blockData.size() == 1;
	}
	return false;
}
 
Example #17
Source File: MCAFile.java    From NBT with MIT License 5 votes vote down vote up
/**
 * Fetches a block state at a specific block location.
 * The block coordinates can be absolute coordinates or they can be relative to the region.
 * @param blockX The x-coordinate of the block.
 * @param blockY The y-coordinate of the block.
 * @param blockZ The z-coordinate of the block.
 * @return The block state or <code>null</code> if the chunk or the section do not exist.
 */
public CompoundTag getBlockStateAt(int blockX, int blockY, int blockZ) {
	int chunkX = MCAUtil.blockToChunk(blockX), chunkZ = MCAUtil.blockToChunk(blockZ);
	Chunk chunk = getChunk(chunkX, chunkZ);
	if (chunk == null) {
		return null;
	}
	return chunk.getBlockStateAt(blockX, blockY, blockZ);
}
 
Example #18
Source File: LevelDatNbt.java    From amidst with GNU General Public License v3.0 5 votes vote down vote up
private static WorldType readWorldType(CompoundTag dataTag) {
	// Minecraft 1.16: the world type doesn't exist in the nbt data anymore
	// so we'll always return Default
	// TODO: Fix this
	if (hasGeneratorName(dataTag)) {
		return WorldType.from(readGeneratorName(dataTag));
	} else {
		return WorldType.DEFAULT;
	}
}
 
Example #19
Source File: Section.java    From NBT with MIT License 5 votes vote down vote up
/**
 * Creates an empty Section with base values.
 * @return An empty Section
 */
public static Section newSection() {
	Section s = new Section();
	s.blockStates = new long[256];
	s.palette = new ListTag<>(CompoundTag.class);
	CompoundTag air = new CompoundTag();
	air.putString("Name", "minecraft:air");
	s.palette.add(air);
	s.data = new CompoundTag();
	return s;
}
 
Example #20
Source File: LevelDatNbt.java    From amidst with GNU General Public License v3.0 5 votes vote down vote up
private static long readRandomSeed(CompoundTag dataTag) {
	Tag<?> randomSeed = dataTag.get(NBTTagKeys.TAG_KEY_RANDOM_SEED);
	if (randomSeed != null) {
		return NBTUtils.getLongValue(randomSeed);
	}
	// Minecraft 1.16 format
	CompoundTag worldGenSettings = dataTag.get(NBTTagKeys.TAG_KEY_WORLD_GEN_SETTINGS, CompoundTag.class);
	return NBTUtils.getLongValue(worldGenSettings.get(NBTTagKeys.TAG_KEY_SEED));
}
 
Example #21
Source File: LastUpdateField.java    From mcaselector with MIT License 5 votes vote down vote up
@Override
public void change(CompoundTag root) {
	LongTag tag = root.getCompoundTag("Level").getLongTag("LastUpdate");
	if (tag != null) {
		tag.setValue(getNewValue());
	}
}
 
Example #22
Source File: Anvil112ChunkDataProcessor.java    From mcaselector with MIT License 5 votes vote down vote up
private CompoundTag initLevel(CompoundTag c) {
	CompoundTag level = c.getCompoundTag("Level");
	if (level == null) {
		c.put("Level", level = new CompoundTag());
	}
	return level;
}
 
Example #23
Source File: Anvil112ChunkFilter.java    From mcaselector with MIT License 5 votes vote down vote up
@Override
public boolean matchBlockNames(CompoundTag data, String... names) {
	ListTag<CompoundTag> sections = withDefault(() -> data.getCompoundTag("Level").getListTag("Sections").asCompoundTagList(), null);
	if (sections == null) {
		return false;
	}
	int c = 0;
	nameLoop:
	for (String name : names) {
		BlockData[] bd = mapping.get(name);
		if (bd == null) {
			Debug.dump("No mapping found for " + name);
			continue;
		}
		for (CompoundTag t : sections) {
			byte[] blocks = withDefault(() -> t.getByteArray("Blocks"), null);
			if (blocks == null) {
				continue;
			}
			byte[] blockData = withDefault(() -> t.getByteArray("Data"), null);
			if (blockData == null) {
				continue;
			}

			for (int i = 0; i < blocks.length; i++) {
				short b = (short) (blocks[i] & 0xFF);
				for (BlockData d : bd) {
					if (d.id == b) {
						byte dataByte = (byte) (i % 2 == 0 ? blockData[i / 2] & 0x0F : (blockData[i / 2] >> 4) & 0x0F);
						if (d.data.contains(dataByte)) {
							c++;
							continue nameLoop;
						}
					}
				}
			}
		}
	}
	return names.length == c;
}
 
Example #24
Source File: Anvil112ChunkFilter.java    From mcaselector with MIT License 5 votes vote down vote up
@Override
public boolean matchBiomeIDs(CompoundTag data, int... ids) {
	if (!data.containsKey("Level") || withDefault(() -> data.getCompoundTag("Level").getByteArray("Biomes"), null) == null) {
		return false;
	}
	filterLoop: for (int filterID : ids) {
		for (byte dataID : data.getCompoundTag("Level").getByteArray("Biomes")) {
			if (filterID == dataID) {
				continue filterLoop;
			}
		}
		return false;
	}
	return true;
}
 
Example #25
Source File: InhabitedTimeField.java    From mcaselector with MIT License 5 votes vote down vote up
@Override
public void change(CompoundTag root) {
	LongTag tag = root.getCompoundTag("Level").getLongTag("InhabitedTime");
	if (tag != null) {
		tag.setValue(getNewValue());
	}
}
 
Example #26
Source File: LevelDatNbt.java    From amidst with GNU General Public License v3.0 5 votes vote down vote up
private static String readGeneratorOptions(CompoundTag dataTag, WorldType worldType) {
	if (worldType == WorldType.CUSTOMIZED) {
		return readGeneratorOptions(dataTag);
	} else {
		return "";
	}
}
 
Example #27
Source File: Section.java    From NBT with MIT License 5 votes vote down vote up
PaletteIndex getValueIndexedPalette(CompoundTag data) {
	List<PaletteIndex> leaves = valueIndexedPalette.get(data.getString("Name"));
	if (leaves == null) {
		return null;
	}
	for (PaletteIndex leaf : leaves) {
		if (leaf.data.equals(data)) {
			return leaf;
		}
	}
	return null;
}
 
Example #28
Source File: LevelDatNbt.java    From amidst with GNU General Public License v3.0 5 votes vote down vote up
private static WorldType readWorldType(CompoundTag dataTag) {
	// Minecraft 1.16: the world type doesn't exist in the nbt data anymore
	// so we'll always return Default
	// TODO: Fix this
	if (hasGeneratorName(dataTag)) {
		return WorldType.from(readGeneratorName(dataTag));
	} else {
		return WorldType.DEFAULT;
	}
}
 
Example #29
Source File: Chunk.java    From NBT with MIT License 5 votes vote down vote up
public static Chunk newChunk() {
	Chunk c = new Chunk(0);
	c.dataVersion = DEFAULT_DATA_VERSION;
	c.data = new CompoundTag();
	c.data.put("Level", new CompoundTag());
	c.status = "mobs_spawned";
	return c;
}
 
Example #30
Source File: LevelDatNbt.java    From amidst with GNU General Public License v3.0 5 votes vote down vote up
public static LevelDatNbt from(Path path) throws IOException, FormatException {
	try {
		CompoundTag dataTag = readDataTag(NBTUtils.readTagFromFile(path));
		long seed = readRandomSeed(dataTag);
		CoordinatesInWorld worldSpawn = readWorldSpawn(dataTag);
		WorldType worldType = readWorldType(dataTag);
		String generatorOptions = readGeneratorOptions(dataTag, worldType);
		boolean hasPlayer = hasPlayerTag(dataTag);
		return new LevelDatNbt(seed, worldSpawn, worldType, generatorOptions, hasPlayer);
	} catch (NullPointerException e) {
		throw new FormatException("cannot read level.dat: " + path);
	}
}