I have a glitch with my mod that alarms me when I am looking at a slime.

Started by PythonHunter on

Topic category: Help with modding (Java Edition)

Last seen on 23:45, 8. Nov 2020
Joined Feb 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I have a glitch with my mod that alarms me when I am looking at a slime.
Mon, 10/19/2020 - 05:47 (edited)

 Basically, the mod runs perfectly EXCEPT that sometimes, i'm pretty sure always, if I look at a grass block from about 2 blocks or closer, the mod plays the sound even if I am not looking at a slime.

package net.mcreator.aspmod.procedures;

import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.common.MinecraftForge;

import net.minecraft.world.World;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.ResourceLocation;
import net.minecraft.entity.monster.EntitySlime;

import net.minecraft.entity.Entity;

import net.mcreator.aspmod.ElementsASPMod;

import java.util.Map;
import java.util.HashMap;
import java.util.Comparator;
import java.util.List;

@ElementsASPMod.ModElement.Tag
public class ProcedureSlimeAlert extends ElementsASPMod.ModElement {
	public ProcedureSlimeAlert(ElementsASPMod instance) {
		super(instance, 3);
	}
	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("entity") == null) {
			System.err.println("Failed to load dependency entity for procedure InfestorAlert!");
			return;
		}
		if (dependencies.get("x") == null) {
			System.err.println("Failed to load dependency x for procedure InfestorAlert!");
			return;
		}
		if (dependencies.get("y") == null) {
			System.err.println("Failed to load dependency y for procedure InfestorAlert!");
			return;
		}
		if (dependencies.get("z") == null) {
			System.err.println("Failed to load dependency z for procedure InfestorAlert!");
			return;
		}
		if (dependencies.get("world") == null) {
			System.err.println("Failed to load dependency world for procedure InfestorAlert!");
			return;
		}
		Entity entity = (Entity) dependencies.get("entity");
		double x = dependencies.get("x") instanceof Integer ? (int) dependencies.get("x") : (double) dependencies.get("x");
		double y = dependencies.get("y") instanceof Integer ? (int) dependencies.get("y") : (double) dependencies.get("y");
		double z = dependencies.get("z") instanceof Integer ? (int) dependencies.get("z") : (double) dependencies.get("z");
		World world = (World) dependencies.get("world");
		List entities = (world.getEntitiesWithinAABB(EntitySlime.class, new AxisAlignedBB(
			(entity.world.rayTraceBlocks(entity.getPositionEyes(1f),
				entity.getPositionEyes(1f).addVector(entity.getLook(1f).x * 100, entity.getLook(1f).y * 100, entity.getLook(1f).z * 100), false,
				false, true).getBlockPos().getX()) - 1.0,
			(entity.world.rayTraceBlocks(entity.getPositionEyes(1f),
				entity.getPositionEyes(1f).addVector(entity.getLook(1f).x * 100, entity.getLook(1f).y * 100, entity.getLook(1f).z * 100), false,
				false, true).getBlockPos().getX()) - 1.0,
			(entity.world.rayTraceBlocks(entity.getPositionEyes(1f),
				entity.getPositionEyes(1f).addVector(entity.getLook(1f).x * 100, entity.getLook(1f).y * 100, entity.getLook(1f).z * 100), false,
				false, true).getBlockPos().getX()) - 1.0,
			(entity.world.rayTraceBlocks(entity.getPositionEyes(1f),
				entity.getPositionEyes(1f).addVector(entity.getLook(1f).x * 100, entity.getLook(1f).y * 100, entity.getLook(1f).z * 100), false,
				false, true).getBlockPos().getX()) - 1.0,
			(entity.world.rayTraceBlocks(entity.getPositionEyes(1f),
				entity.getPositionEyes(1f).addVector(entity.getLook(1f).x * 100, entity.getLook(1f).y * 100, entity.getLook(1f).z * 100), false,
				false, true).getBlockPos().getX()) - 1.0,
			(entity.world.rayTraceBlocks(entity.getPositionEyes(1f),
				entity.getPositionEyes(1f).addVector(entity.getLook(1f).x * 100, entity.getLook(1f).y * 100, entity.getLook(1f).z * 100), false,
				false, true).getBlockPos().getX()) - 1.0), null));
	// If the list of entities is not empty / contains at least one Slime do ...
		if (entities != null) {
			// Play a sound
		}
	}

	@SubscribeEvent
	public void onPlayerTick(TickEvent.PlayerTickEvent event) {
		if (event.phase == TickEvent.Phase.END) {
			Entity entity = event.player;
			World world = entity.world;
			double i = entity.posX;
			double j = entity.posY;
			double k = entity.posZ;
			Map<String, Object> dependencies = new HashMap<>();
			dependencies.put("x", i);
			dependencies.put("y", j);
			dependencies.put("z", k);
			dependencies.put("world", world);
			dependencies.put("entity", entity);
			dependencies.put("event", event);
			this.executeProcedure(dependencies);
		}
	}
	
	@Override
	public void preInit(FMLPreInitializationEvent event) {
		MinecraftForge.EVENT_BUS.register(this);
	}
}

 

Edited by PythonHunter on Mon, 10/19/2020 - 05:47