Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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);
}
}

For death: LivingDeathEvent

 

For the respawn EntityJoinedWorldEvent (or something similar).

We all stuff up sometimes... But I seem to be at the bottom of that pot.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.