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

com.parzivail.nem.NemManager Maven / Gradle / Ivy

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

import F;
import I;
import Z;
import com.google.common.collect.ImmutableList;
import com.parzivail.nem.mixin.ModelPartAccessor;
import com.parzivail.util.client.render.ModelAngleAnimator;
import com.parzivail.util.client.render.MutableAnimatedModel;
import com.parzivail.util.client.render.armor.BipedEntityArmorModel;
import com.parzivail.util.data.KeyedReloadableLoader;
import net.minecraft.class_1297;
import net.minecraft.class_1309;
import net.minecraft.class_2487;
import net.minecraft.class_2499;
import net.minecraft.class_2507;
import net.minecraft.class_2520;
import net.minecraft.class_2960;
import net.minecraft.class_3300;
import net.minecraft.class_3545;
import net.minecraft.class_3695;
import net.minecraft.class_5603;
import net.minecraft.class_5605;
import net.minecraft.class_5606;
import net.minecraft.class_5607;
import net.minecraft.class_5609;
import net.minecraft.class_5610;
import net.minecraft.class_572;
import net.minecraft.class_591;
import net.minecraft.class_6230;
import net.minecraft.class_630;
import net.minecraft.class_742;
import net.minecraft.client.model.*;
import org.jetbrains.annotations.NotNull;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class NemManager extends KeyedReloadableLoader
{
	private final class_2960 id;
	private final Map modelData;
	private final ArrayList>> models;
	private final HashMap> playerModels;
	private final HashMap> bipedModels;

	public NemManager(class_2960 id)
	{
		super("models", "nem");
		this.id = id;
		modelData = new HashMap<>();
		models = new ArrayList<>();
		playerModels = new HashMap<>();
		bipedModels = new HashMap<>();
	}

	@Override
	public class_5607 readResource(class_3300 resourceManager, class_3695 profiler, InputStream stream) throws IOException
	{
		return buildModel(class_2507.method_10627(new DataInputStream(stream)));
	}

	@NotNull
	public static class_5607 buildModel(class_2487 nbt)
	{
		var modelData = new class_5609();
		var root = modelData.method_32111();

		addChildren(root, nbt.method_10562("parts"));

		var texTag = nbt.method_10562("tex");
		return class_5607.method_32110(modelData, texTag.method_10550("w"), texTag.method_10550("h"));
	}

	private static void addChildren(class_5610 root, class_2487 parts)
	{
		for (var key : parts.method_10541())
			addChild(root, key, parts.method_10562(key));
	}

	private static void addChild(class_5610 parent, String partName, class_2487 part)
	{
		var tex = part.method_10562("tex");
		var partU = tex.method_10550("u");
		var partV = tex.method_10550("v");
		var mirrored = tex.method_10577("mirrored");

		var pos = part.method_10562("pos");
		var x = pos.method_10583("x");
		var y = pos.method_10583("y");
		var z = pos.method_10583("z");

		var rot = part.method_10562("rot");
		var pitch = rot.method_10583("pitch");
		var yaw = rot.method_10583("yaw");
		var roll = rot.method_10583("roll");

		var transform = class_5603.method_32091(x, y, z, pitch, yaw, roll);

		var partBuilder = class_5606.method_32108();

		var cuboids = part.method_10554("cuboids", class_2520.field_33260);
		for (var i = 0; i < cuboids.size(); i++)
		{
			var cuboid = cuboids.method_10602(i);

			var cPos = cuboid.method_10562("pos");
			var cX = cPos.method_10583("x");
			var cY = cPos.method_10583("y");
			var cZ = cPos.method_10583("z");

			var cSize = cuboid.method_10562("size");
			var cSX = cSize.method_10550("x");
			var cSY = cSize.method_10550("y");
			var cSZ = cSize.method_10550("z");

			var cExpand = cuboid.method_10562("expand");
			var cEX = cExpand.method_10583("x");
			var cEY = cExpand.method_10583("y");
			var cEZ = cExpand.method_10583("z");

			var cTex = cuboid.method_10562("tex");
			var cU = cTex.method_10550("u");
			var cV = cTex.method_10550("v");
			var cMirrored = cTex.method_10577("mirrored");

			partBuilder = partBuilder.method_32106(cMirrored ^ mirrored).method_32105(
					"",
					cX, cY, cZ,
					cSX, cSY, cSZ,
					new class_5605(cEX, cEY, cEZ),
					partU + cU, partV + cV
			);
		}

		var childPart = parent.method_32117(partName, partBuilder, transform);

		if (part.method_10545("children"))
			addChildren(childPart, part.method_10562("children"));
	}

	@Override
	protected void apply(Map prepared, class_3300 manager, class_3695 profiler)
	{
		modelData.clear();
		modelData.putAll(prepared);

		// The model loading sites return before the models are loaded from disk and before
		// they're actually rendered, so we use a callback system to set the model part
		// when the part becomes available
		for (var modelInfo : models)
		{
			var modelId = modelInfo.method_15442();
			var model = modelInfo.method_15441();

			if (modelData.containsKey(modelId))
				model.accept(modelData.get(modelId).method_32109());
		}
	}

	public  MutableAnimatedModel getModel(class_2960 modelId, ModelAngleAnimator angleAnimator)
	{
		var model = new MutableAnimatedModel<>(angleAnimator);
		models.add(new class_3545<>(modelId, model::setRoot));
		return model;
	}

	public Supplier> getPlayerModel(class_2960 modelId, boolean thinArms)
	{
		models.add(new class_3545<>(modelId, modelPart -> playerModels.put(modelId, new class_591<>(modelPart, thinArms))));
		return () -> playerModels.get(modelId);
	}

	public Supplier> getOverridingPlayerModel(class_2960 overrideModelId, class_2960 baseModelId, boolean thinArms)
	{
		models.add(new class_3545<>(overrideModelId, modelPart -> playerModels.put(overrideModelId, combinePlayerModels(baseModelId, modelPart, thinArms))));
		return () -> playerModels.get(overrideModelId);
	}

	private static ModelPartAccessor mpa(Object o)
	{
		return (ModelPartAccessor)o;
	}

	private  class_591 combinePlayerModels(class_2960 baseModelId, class_630 overrideModel, boolean thinArms)
	{
		var baseModelData = modelData.get(baseModelId);
		var baseModel = baseModelData.method_32109();

		var baseChildren = mpa(baseModel).getChildren();
		var overrideChildren = mpa(overrideModel).getChildren();

		for (var entry : overrideChildren.entrySet())
		{
			if (entry.getValue().method_32087() && mpa(entry.getValue()).getChildren().isEmpty())
				continue;

			baseChildren.put(entry.getKey(), entry.getValue());
		}

		return new class_591<>(baseModel, thinArms);
	}

	public Supplier> getBipedModel(class_2960 modelId)
	{
		models.add(new class_3545<>(modelId, modelPart -> bipedModels.put(modelId, new class_572<>(ensureBipedParts(modelPart)))));
		return () -> bipedModels.get(modelId);
	}

	public Supplier> getOverridingBipedModel(class_2960 overrideModelId, class_2960 baseModelId, boolean thinArms)
	{
		models.add(new class_3545<>(overrideModelId, modelPart -> bipedModels.put(overrideModelId, combinePlayerModels(baseModelId, modelPart, thinArms))));
		return () -> bipedModels.get(overrideModelId);
	}

	public Supplier> getBipedArmorModel(class_2960 modelId)
	{
		models.add(new class_3545<>(modelId, modelPart -> bipedModels.put(modelId, new BipedEntityArmorModel<>(ensureBipedParts(modelPart)))));
		return () -> (BipedEntityArmorModel)bipedModels.get(modelId);
	}

	private static class_630 ensureBipedParts(class_630 modelPart)
	{
		var mpa = (ModelPartAccessor)(Object)modelPart;

		if (!modelPart.method_41919(class_6230.field_32606))
			mpa.getChildren().put(class_6230.field_32606, createEmptyModelPart());
		if (!modelPart.method_41919(class_6230.field_32607))
			mpa.getChildren().put(class_6230.field_32607, createEmptyModelPart());
		if (!modelPart.method_41919(class_6230.field_32608))
			mpa.getChildren().put(class_6230.field_32608, createEmptyModelPart());
		if (!modelPart.method_41919(class_6230.field_32622))
			mpa.getChildren().put(class_6230.field_32622, createEmptyModelPart());
		if (!modelPart.method_41919(class_6230.field_32621))
			mpa.getChildren().put(class_6230.field_32621, createEmptyModelPart());
		if (!modelPart.method_41919(class_6230.field_32632))
			mpa.getChildren().put(class_6230.field_32632, createEmptyModelPart());
		if (!modelPart.method_41919(class_6230.field_32631))
			mpa.getChildren().put(class_6230.field_32631, createEmptyModelPart());

		return modelPart;
	}

	private static class_630 createEmptyModelPart()
	{
		return new class_630(ImmutableList.of(), Map.of());
	}

	@Override
	public class_2960 getFabricId()
	{
		return this.id;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy