Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Getting EntityPlayer in a TickEvent to call another method.


Geometrically
 Share

Recommended Posts

I want to call a method that has a parameter, EntityPlayer player to a TickEvent. I did this with null, and I get a NullPointerExeption. How do I get the entity player there and put it there? My code is attached.

package com.geometrically.SolarApoc.util;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.WorldTickEvent;


public class WorldTickHandler {
	
	public static int ticker;
	@SubscribeEvent
	public void onWorldTick(WorldTickEvent handler){
		ticker++;
		
		//Tick Checkers
		if (ticker > SAConfiguration.dayoneStart){
			SAMethods.beginDayOne();
			System.out.println("Day One has started");
		}
		if (ticker > SAConfiguration.daytwoStart){
			SAMethods.beginDayTwo();
		}
		if (ticker > SAConfiguration.daythreeStart){
			SAMethods.beginDayThree();
		}
		if (ticker > SAConfiguration.dayfourStart){
			SAMethods.beginDayFour();
		}
		if (ticker > SAConfiguration.dayfiveStart){
			SAMethods.beginDayFive();
		}
		
		printMessages(null);
		
	}
	
	public void printMessages(EntityPlayer player){

		//Message Printers
		if (ticker == SAConfiguration.dayoneStart){
			SAMethods.beginDayOne();
			
			player.addChatComponentMessage(new TextComponentString(TextFormatting.RED + "The Solar Apocalypse has started. Day One will now upbring its wrath."));
			System.out.println("!!!!!!!!!!!!!!!!!!!!Day One has started");
		}
		if (ticker == SAConfiguration.daytwoStart){
			SAMethods.beginDayTwo();
			
			
			player.addChatComponentMessage(new TextComponentString(TextFormatting.RED + "The Solar Apocalypse's second wave has arrived. Day Two will now upbring its wrath."));
		}
		if (ticker == SAConfiguration.daythreeStart){
			SAMethods.beginDayThree();
			
			player.addChatComponentMessage(new TextComponentString(TextFormatting.RED + "The Solar Apocalypse's third wave has arrived. Day Three will now upbring its wrath."));
		}
		if (ticker == SAConfiguration.dayfourStart){
			SAMethods.beginDayFour();
			
			player.addChatComponentMessage(new TextComponentString(TextFormatting.RED + "The Solar Apocalypse's fourth wave has arrived. Day Four will now upbring its wrath."));
		}
		if (ticker == SAConfiguration.dayfiveStart){
			SAMethods.beginDayFive();
			
			player.addChatComponentMessage(new TextComponentString(TextFormatting.RED + "The Solar Apocalypse's fifth and final wave has arrived. Let's see how long you can survive. "));
			player.addChatComponentMessage(new TextComponentString(TextFormatting.GOLD + "Escape if you can."));
		}
	}
}

 

Link to comment
Share on other sites

Which player?

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

Draco isn't asking where you want to use the player, he's asking which player you're actually trying to use. If there are 20 players on a server, which one do you want to call the method for?

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Geometrically, I posted an answer in what appears to be an accidental duplicate of this thread. Here

To add more information to it, the handler variable in your method has the information you want and you get in the form of a List<EntityPlayer> through the world. From what I understand, you want to broadcast the message to all players, so you would iterate through the list like this

 

World worldIn = handler.world;
List<EntityPlayer> playerList = worldIn.playerEntities;

for (EntityPlayer player : playerList) {
  printMessages(player);
}

 

I find it odd that the code I posted in the other thread has EntityPlayers. With an 's' at the end. It is supposed to be EntityPlayer. Just to clarify.

 

I hope it helps.

Edited by ctbe
Typo
  • Like 1
Link to comment
Share on other sites

23 hours ago, Choonster said:

Draco isn't asking where you want to use the player, he's asking which player you're actually trying to use. If there are 20 players on a server, which one do you want to call the method for?

I'm trying to use all of them. The method called prints out messages to the whole world

Link to comment
Share on other sites

3 hours ago, Geometrically said:

I'm trying to use all of them. The method called prints out messages to the whole world

 

Then you need to get the players from the World, as ctbe demonstrated.

 

It's not directly related to your problem, but it should be noted that WorldTickEvent fires twice per tick per loaded dimension, so ticker will be incremented at least twice per tick with your current code. You need to check the event's Phase and the dimension being ticked to avoid this.

 

You should avoid storing things related to game state in your event handler classes as these are shared between both logical sides (so you could potentially be accessing the fields from two threads at once) and aren't persisted in any way (so the data won't be saved when the client/server is stopped). Use data storage methods like World Saved Data or Capabilities to safely store per-dimension or per-map data that will be persisted with the save.

 

You also need to consider which dimensions are affected by this "Solar Apocalypse", is it all dimensions or only the Overworld? Does each dimension have its own timer, or do they all use the same timer?

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

On 4/14/2017 at 0:38 AM, Choonster said:

 

Then you need to get the players from the World, as ctbe demonstrated.

 

It's not directly related to your problem, but it should be noted that WorldTickEvent fires twice per tick per loaded dimension, so ticker will be incremented at least twice per tick with your current code. You need to check the event's Phase and the dimension being ticked to avoid this. So The World Tick Event is called every 1/2 a tick?

 

You should avoid storing things related to game state in your event handler classes as these are shared between both logical sides (so you could potentially be accessing the fields from two threads at once) and aren't persisted in any way (so the data won't be saved when the client/server is stopped). Use data storage methods like World Saved Data or Capabilities to safely store per-dimension or per-map data that will be persisted with the save. I'll try this.

 

You also need to consider which dimensions are affected by this "Solar Apocalypse", is it all dimensions or only the Overworld? Does each dimension have its own timer, or do they all use the same timer? It's only in the overworld

My answers are in bold.

Link to comment
Share on other sites

1 minute ago, Geometrically said:

So The World Tick Event is called every 1/2 a tick?

No.  It's called twice per tick. Once before any vanilla logic occurs and once after.

Phase.Start effectively happens absolfreakinglutelyimmediately after Phase.End as far as game-time is concerned, just not computation-wise as there are multiple dimensions to process.

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

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

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.