Jump to content

Getting EntityPlayer in a TickEvent to call another method.


Geometrically

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Your drivers are in a broken state, follow the guide in the FAQ: https://forums.minecraftforge.net/topic/125488-rules-and-frequently-asked-questions-faq/#:~:text=How do I update my drivers%3F
    • A fatal error has been detected by the Java Runtime Environment: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffc3fe22b60, pid=15228, tid=5140 # # JRE version: OpenJDK Runtime Environment Microsoft-8035246 (17.0.8+7) (build 17.0.8+7-LTS) # Java VM: OpenJDK 64-Bit Server VM Microsoft-8035246 (17.0.8+7-LTS, mixed mode, tiered, compressed oops, compressed class ptrs, g1 gc, windows-amd64) # Problematic frame: # C [atio6axx.dll+0x192b60] # # No core dump will be written. Minidumps are not enabled by default on client versions of Windows # # If you would like to submit a bug report, please visit: # https://aka.ms/minecraftjavacrashes # The crash happened outside the Java Virtual Machine in native code. # See problematic frame for where to report the bug i have no idea why please help
    • Quick-Books is great with regards to coordinating your monetary data. You can decide to live talk with a specialist at Quick-Books to get the answer for your questions. You will actually want to get to the talk going to the landing page and call us +1855-210-1428.
    • Update: I managed to make the item not disappear after crafting, but it doesn't remove any durability. import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.enchantment.Enchantments; import net.minecraft.enchantment.UnbreakingEnchantment; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; public class NuggetHammer extends Item { private boolean damage; public NuggetHammer(Properties p_i48487_1_) { super(p_i48487_1_); } @Override public int getMaxDamage(ItemStack stack) { return 54 - 1; } public boolean isBarVisible(ItemStack stack) { return false; } @Override public ItemStack getContainerItem(ItemStack stack) { ItemStack copy = stack.copy(); copy.setCount(1); if (!this.damage) return copy; int unbreaking = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.UNBREAKING, stack); for (int i = 0; i < unbreaking; i++) { if (UnbreakingEnchantment.shouldIgnoreDurabilityDrop(stack, unbreaking, random)) return copy; } copy.setDamageValue(stack.getDamageValue() + 1); if (copy.getDamageValue() > stack.getMaxDamage()) return ItemStack.EMPTY; return copy; } }  
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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