Geometrically Posted April 12, 2017 Posted April 12, 2017 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.")); } } } Quote
Geometrically Posted April 13, 2017 Author Posted April 13, 2017 So what do I put to get the player? Quote
Draco18s Posted April 13, 2017 Posted April 13, 2017 Which player? Quote 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.
Geometrically Posted April 13, 2017 Author Posted April 13, 2017 printMessages(null); } public void printMessages(EntityPlayer player){ That little snippit of code. Look at the first post. Quote
Choonster Posted April 13, 2017 Posted April 13, 2017 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? Quote 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.
ctbe Posted April 13, 2017 Posted April 13, 2017 (edited) 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 April 13, 2017 by ctbe Typo 1 Quote
Geometrically Posted April 14, 2017 Author Posted April 14, 2017 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 Quote
Choonster Posted April 14, 2017 Posted April 14, 2017 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? Quote 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.
Geometrically Posted April 17, 2017 Author Posted April 17, 2017 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. Quote
Draco18s Posted April 17, 2017 Posted April 17, 2017 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. Quote 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.
Geometrically Posted April 17, 2017 Author Posted April 17, 2017 So I have 2 variables, and increment a for loop where every two ticker1++'s ticker2++ Quote
Jay Avery Posted April 17, 2017 Posted April 17, 2017 No, you just need to check the event's phase before doing your own code - pick either Phase.START or Phase.END (often it doesn't matter which), and compare it to event#phase. Quote
Recommended Posts
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.