Jump to content

[Solved][1.7.2] LivingSpawnEvent not working


Einhaender

Recommended Posts

I'm trying to use events to detect when a player dies and then respawns:

 

relevant parts of main mod class:

package einhaender.modEin;

//all the necessary imports

@Mod(modid = ModEin.MODID, version = ModEin.VERSION)
public class ModEin
{
        @EventHandler
public void postInit(FMLPostInitializationEvent event)
{
	MinecraftForge.EVENT_BUS.register (new ListenerDeath ());
	FMLCommonHandler.instance ().bus ().register (new ListenerDeath ());
}
}

 

the class with the listeners:

package einhaender.modEin;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ChatComponentText;
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
import net.minecraftforge.event.entity.living.LivingSpawnEvent.CheckSpawn;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent;


public class ListenerDeath
{
@SubscribeEvent
public void onLogin(PlayerLoggedInEvent event)
{
	event.player.addChatComponentMessage (new ChatComponentText ("you just logged in!!!!!"));
}

@SubscribeEvent
public void onSpawn(CheckSpawn event)
{
	System.out.println ("Something just tried to spawn!!!");
	if (event.entity instanceof EntityPlayer)
		((EntityPlayer) event.entity).addChatComponentMessage (new ChatComponentText ("you just tried to spawn"));
}

@SubscribeEvent
public void onLivingSpawn(LivingSpawnEvent event)
{
	System.out.println ("A living just spawned.");
	if (!event.world.isRemote)
	{
		if (event.entityLiving instanceof EntityPlayer)
		{
			((EntityPlayer) event.entityLiving).addChatMessage (new ChatComponentText ("you just spawned!!!"));
		}
	}
}
}

 

When I enter a new world, a chat message "you just logged in" pops up, so I'm registering the listener properly. I just can't find any events that are executed when a player respawns. And I can't get the CheckSpawn or LivingSpawnEvent events to run at all.

 

So, is LivingSpawnEvent just not run when a player/mob spawns, or am I doing something wrong in my code?

 

Thanks for taking the time to read this!

 

 

SOLUTION

package einhaender.modEin;

import java.util.ArrayList;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ChatComponentText;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
import net.minecraftforge.event.entity.living.LivingSpawnEvent.CheckSpawn;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent;


public class ListenerDeath
{
ArrayList<EntityPlayer>	deadPlayers	= new ArrayList<EntityPlayer> ();

@SubscribeEvent
public void death(LivingDeathEvent event)
{
	if (!event.entity.worldObj.isRemote)
	{
		if (event.entity instanceof EntityPlayer)
		{
			deadPlayers.add ((EntityPlayer) event.entity);
		}
	}
}

@SubscribeEvent
public void entitySpawned (EntityJoinWorldEvent event)
{
	if (!event.world.isRemote)
	{
		if (event.entity instanceof EntityPlayer)
		{
			if (deadPlayers.contains ((EntityPlayer) event.entity))
			{
				EntityPlayer player = (EntityPlayer) event.entity;
				doStuffWithRespawnedPlayer (player);
				deadPlayers.remove (player);
			}
		}
	}
}

private void doStuffWithRespawnedPlayer(EntityPlayer player)
{
	player.setPositionAndUpdate (8.5, 16, 8.5);
}
}

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.