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

com.parzivail.util.entity.EntityUtil Maven / Gradle / Ivy

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

import D;
import com.parzivail.util.math.EntityHitResult;
import com.parzivail.util.math.MathUtil;
import com.parzivail.util.math.QuatUtil;
import org.joml.Quaternionf;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import net.minecraft.class_1297;
import net.minecraft.class_1299;
import net.minecraft.class_1301;
import net.minecraft.class_1309;
import net.minecraft.class_2379;
import net.minecraft.class_238;
import net.minecraft.class_243;
import net.minecraft.class_2487;
import net.minecraft.class_3959;
import net.minecraft.class_3965;

public class EntityUtil
{
	public static class_2487 serializeEntity(class_1297 e)
	{
		var nbtCompound = new class_2487();
		nbtCompound.method_10582("id", class_1299.method_5890(e.method_5864()).toString());
		e.method_5647(nbtCompound);
		return nbtCompound;
	}

	public static EntityHitResult raycastEntities(Class clazz, class_243 startPos, class_243 fromDir, double distance, class_1297 fromEntity, class_1297[] exclude)
	{
		class_1297 pointedEntity = null;
		class_243 hitLocation = null;

		var blacklist = Arrays.asList(exclude);

		fromDir = fromDir.method_1029();

		var endPos = startPos.method_1019(fromDir.method_1021(distance));
		var list = fromEntity.method_37908().method_8390(clazz, fromEntity.method_5829().method_1012(fromDir.field_1352 * distance, fromDir.field_1351 * distance, fromDir.field_1350 * distance).method_1009(1, 1, 1), class_1301.field_6155);

		for (var entity : list)
		{
			if (blacklist.contains(entity))
				continue;

			if (entity.method_5863())
			{
				var box = entity.method_5829();
				var hitvec = box.method_992(startPos, endPos);

				if (hitvec.isPresent())
				{
					var distanceTo = startPos.method_1022(hitvec.get());

					if (distanceTo < distance)
					{
						pointedEntity = entity;
						distance = distanceTo;
						hitLocation = hitvec.get();
					}
				}
			}
		}

		if (pointedEntity != null)
			return new EntityHitResult(pointedEntity, hitLocation);

		return null;
	}

	public static ArrayList raycastEntitiesCone(class_243 startPos, class_243 fromDir, double maxAngleRad, double distance, class_1297 fromEntity, class_1297[] exclude)
	{
		var blacklist = Arrays.asList(exclude);

		fromDir = fromDir.method_1029();

		var list = fromEntity.method_37908().method_8390(class_1309.class, fromEntity.method_5829().method_1012(fromDir.field_1352 * distance, fromDir.field_1351 * distance, fromDir.field_1350 * distance).method_1009(1, 1, 1), class_1301.field_6155);

		var hit = new ArrayList();

		for (var entity : list)
		{
			if (blacklist.contains(entity))
				continue;

			if (entity.method_5863())
			{
				var entityDirVec = entity.method_19538().method_1020(startPos).method_1029();
				if (Math.acos(entityDirVec.method_1026(fromDir)) > maxAngleRad)
					continue;

				hit.add(entity);
			}
		}

		return hit;
	}

	public static class_3965 raycastBlocks(class_243 startPos, class_243 fromDir, double distance, class_1297 fromEntity, class_3959.class_3960 shapeType, class_3959.class_242 fluidHandling)
	{
		var end = startPos.method_1019(fromDir.method_1021(distance));
		return fromEntity.method_37908().method_17742(new class_3959(startPos, end, shapeType, fluidHandling, fromEntity));
	}

	public static void setVelocityFromAngles(class_1297 entity, float pitch, float yaw, float scalar)
	{
		var look = MathUtil.anglesToLook(pitch, yaw);
		entity.method_18800(scalar * look.field_1352, scalar * look.field_1351, scalar * look.field_1350);
	}

	public static void updateEulerRotation(class_1297 entity, Quaternionf rotation)
	{
		entity.field_6004 = entity.method_36455();
		entity.field_5982 = entity.method_36454();

		var eulerAngle = QuatUtil.toEulerAngles(rotation);
		entity.method_36456(eulerAngle.method_10257());
		entity.method_36457(eulerAngle.method_10256());

		while (entity.method_36455() - entity.field_6004 >= 180.0F)
		{
			entity.field_6004 += 360.0F;
		}

		while (entity.method_36454() - entity.field_5982 < -180.0F)
		{
			entity.field_5982 -= 360.0F;
		}

		while (entity.method_36454() - entity.field_5982 >= 180.0F)
		{
			entity.field_5982 += 360.0F;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy