All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.parzivail.util.data.DataReader Maven / Gradle / Ivy

There is a newer version: 0.0.114+1.20.2
Show newest version
package com.parzivail.util.data;

import ;
import B;
import I;
import Z;
import com.parzivail.util.ParziUtil;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.minecraft.class_2248;
import net.minecraft.class_2487;
import net.minecraft.class_2507;
import net.minecraft.class_2680;
import net.minecraft.class_2689;
import net.minecraft.class_2769;
import net.minecraft.class_2960;
import net.minecraft.class_7922;
import net.minecraft.class_7923;
import org.joml.Matrix4f;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Optional;

public class DataReader
{
	public static String readNullTerminatedString(DataInput s) throws IOException
	{
		var str = new StringBuilder();
		while (true)
		{
			var b = s.readByte();
			if (b == 0)
				return str.toString();
			str.append((char)b);
		}
	}

	public static int read7BitEncodedInt(InputStream is) throws IOException
	{
		var count = 0;
		var shift = 0;
		var more = true;
		while (more)
		{
			var b = (byte)is.read();
			count |= (b & 0x7F) << shift;
			shift += 7;
			if ((b & 0x80) == 0)
			{
				more = false;
			}
		}
		return count;
	}

	public static class_2487 readUncompressedNbt(DataInput s, int len) throws IOException
	{
		var bytesNbt = new byte[len];
		s.readFully(bytesNbt);

		var stream = new DataInputStream(new ByteArrayInputStream(bytesNbt));
		var tag = class_2507.method_10627(stream);
		stream.close();
		return tag;
	}

	public static FileChannel getFile(String domain, class_2960 resourceLocation)
	{
		try
		{
			var container = FabricLoader.getInstance().getModContainer(resourceLocation.method_12836()).orElseThrow(IllegalStateException::new);
			var path = container.findPath(domain + "/" + resourceLocation.method_12836() + "/" + resourceLocation.method_12832()).orElseThrow(IllegalStateException::new);

			return FileChannel.open(path, StandardOpenOption.READ);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

		return null;
	}

	public static class_2680 readBlockState(DataInput stream) throws IOException
	{
		var name = readNullTerminatedString(stream);
		var hasProperties = stream.readByte() == 1;

		var blockRegistry = class_7923.field_41175;
		var block = blockRegistry.method_10223(new class_2960(name));
		var blockState = block.method_9564();

		if (hasProperties)
		{
			var stateManager = block.method_9595();

			var tagLen = stream.readInt();
			var props = readUncompressedNbt(stream, tagLen);

			for (var key : props.method_10541())
			{
				var property = stateManager.method_11663(key);
				if (property != null)
					blockState = withProperty(blockState, property, key, props, blockState.toString());
			}
		}

		return blockState;
	}

	private static > class_2680 withProperty(class_2680 state, class_2769 property, String key, class_2487 propertiesTag, String context)
	{
		var optional = property.method_11900(propertiesTag.method_10558(key));
		if (optional.isPresent())
			return state.method_11657(property, optional.get());
		else
		{
			ParziUtil.LOG.warn("Unable to read property: %s with value: %s for blockstate: %s", key, propertiesTag.method_10558(key), context);
			return state;
		}
	}

	public static Matrix4f readMatrix4f(DataInput s) throws IOException
	{
		var dat = new float[16];

		for (int i = 0; i < 16; i++)
			dat[i] = s.readFloat();

		return new Matrix4f().setTransposed(dat);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy