Jump to content

Living spawn events. (1.12.2)


vova7865

Recommended Posts

I have this code:

package com.vova7865.essencecraft;

import java.util.UUID;

import com.mojang.authlib.GameProfile;

import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;



@EventBusSubscriber
public class EventHandler {
	@SubscribeEvent
	public void livingSpawn(LivingSpawnEvent event) {
		if(event.getEntityLiving().toString().contains("Guardian")) {
			EntityLiving living = (EntityLiving) event.getEntityLiving();
			World world = event.getWorld();
			WorldServer serverWorld = DimensionManager.getWorld(0);
			GameProfile gameProfile = new GameProfile(UUID.randomUUID(), "FakePlayer");
			FakePlayer fakePlayer = new FakePlayer(serverWorld, gameProfile);
			MinecraftServer minecraftServer = fakePlayer.mcServer;
			String playerNames[] = minecraftServer.getOnlinePlayerNames();
			String firstPlayerName = playerNames[0];
			EntityPlayer player = world.getPlayerEntityByName(firstPlayerName);
			double x = player.posX;
			double y = player.posY;
			double z = player.posZ;
			living.attemptTeleport(x, y, z)
		}
	}
}

So when the guardians spawn, they teleport to me. They do, but the same guardians are teleported to me multiple times, so I think that LivingSpawnEvent is fired not only when they spawn.

Anyone knows how can I do some code only when the mob spawns?

Link to comment
Share on other sites

The LivingSpawnEvent has sub-events IIRC.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

livingSpawn(LivingSpawnEvent.NameOfSubEvent event)

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Mobs spawned by the world & mobs spawned by spawners eventually pass through LivingSpawnEvent#checkSpawn before actually getting spawned.
And yes, all children of LivingSpawnEvent will be given to you unless a specific one is stated.

 

Btw, your whole "create a fake-player just to get the server" is quite redundant. Just call World::getMinecraftServer and be done with it.
Creating FakePlayers is dangerous if you don't also make sure to remove them when the world needs to unload, or it's not gonna get unloaded, because the "player" is still in there.

Edited by Matryoshika

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.