So I wanted to make to make a chat message appear if the player is near my mob. There is no error with the code but it crashes when I create or join a world. When the player is created so is the tick so then my code crashes the game. The code that is mess up is checking if the player is near the entity and when it starts the player tick.
Crash report
---- Minecraft Crash Report ----
// Surprise! Haha. Well, this is awkward.
Time: 9/2/13 10:03 PM
Description: Ticking player
java.lang.NullPointerException
at TFU_Mod.ServerTickHandler.onPlayerTick(ServerTickHandler.java:48)
at TFU_Mod.ServerTickHandler.tickStart(ServerTickHandler.java:58)
at cpw.mods.fml.common.SingleIntervalHandler.tickStart(SingleIntervalHandler.java:28)
at cpw.mods.fml.common.FMLCommonHandler.tickStart(FMLCommonHandler.java:122)
at cpw.mods.fml.common.FMLCommonHandler.onPlayerPreTick(FMLCommonHandler.java:383)
at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:281)
at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:324)
at net.minecraft.network.NetServerHandler.handleFlying(NetServerHandler.java:304)
at net.minecraft.network.packet.Packet10Flying.processPacket(Packet10Flying.java:51)
at net.minecraft.network.MemoryConnection.processReadPackets(MemoryConnection.java:89)
at net.minecraft.network.NetServerHandler.networkTick(NetServerHandler.java:141)
at net.minecraft.network.NetworkListenThread.networkTick(NetworkListenThread.java:54)
at net.minecraft.server.integrated.IntegratedServerListenThread.networkTick(IntegratedServerListenThread.java:109)
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:689)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:585)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:129)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:482)
at net.minecraft.server.ThreadMinecraftServer.run(ThreadMinecraftServer.java:16)
A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------
-- Head --
Stacktrace:
at TFU_Mod.ServerTickHandler.onPlayerTick(ServerTickHandler.java:48)
at TFU_Mod.ServerTickHandler.tickStart(ServerTickHandler.java:58)
at cpw.mods.fml.common.SingleIntervalHandler.tickStart(SingleIntervalHandler.java:28)
at cpw.mods.fml.common.FMLCommonHandler.tickStart(FMLCommonHandler.java:122)
at cpw.mods.fml.common.FMLCommonHandler.onPlayerPreTick(FMLCommonHandler.java:383)
at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:281)
-- Player being ticked --
Details:
Entity Type: null (net.minecraft.entity.player.EntityPlayerMP)
Entity ID: 174
Entity Name: Player90
Entity's Exact location: 105.50, 70.00, -150.50
Entity's Block location: World: (105,70,-151), Chunk: (at 9,4,9 in 6,-10; contains blocks 96,0,-160 to 111,255,-145), Region: (0,-1; contains chunks 0,-32 to 31,-1, blocks 0,0,-512 to 511,255,-1)
Entity's Momentum: 0.00, 0.00, 0.00
Stacktrace:
at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:324)
at net.minecraft.network.NetServerHandler.handleFlying(NetServerHandler.java:304)
at net.minecraft.network.packet.Packet10Flying.processPacket(Packet10Flying.java:51)
at net.minecraft.network.MemoryConnection.processReadPackets(MemoryConnection.java:89)
at net.minecraft.network.NetServerHandler.networkTick(NetServerHandler.java:141)
-- Ticking connection --
Details:
Connection: net.minecraft.network.NetServerHandler@1079622
Stacktrace:
at net.minecraft.network.NetworkListenThread.networkTick(NetworkListenThread.java:54)
at net.minecraft.server.integrated.IntegratedServerListenThread.networkTick(IntegratedServerListenThread.java:109)
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:689)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:585)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:129)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:482)
at net.minecraft.server.ThreadMinecraftServer.run(ThreadMinecraftServer.java:16)
-- System Details --
Details:
Minecraft Version: 1.6.2
Operating System: Windows 7 (x86) version 6.1
Java Version: 1.7.0_25, Oracle Corporation
Java VM Version: Java HotSpot(TM) Client VM (mixed mode), Oracle Corporation
Memory: 924735248 bytes (881 MB) / 1037959168 bytes (989 MB) up to 1037959168 bytes (989 MB)
JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M
AABB Pool Size: 2180 (122080 bytes; 0 MB) allocated, 2180 (122080 bytes; 0 MB) used
Suspicious classes: FML and Forge are installed
IntCache: cache: 0, tcache: 0, allocated: 1, tallocated: 63
FML: MCP v8.04 FML v6.2.43.819 Minecraft Forge 9.10.0.819 4 mods loaded, 4 mods active
mcp{8.04} [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
FML{6.2.43.819} [Forge Mod Loader] (coremods) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
Forge{9.10.0.819} [Minecraft Forge] (coremods) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
TFU_Mod{v1.0.0} [TFU_Mod] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
Profiler Position: N/A (disabled)
Vec3 Pool Size: 3362 (188272 bytes; 0 MB) allocated, 3350 (187600 bytes; 0 MB) used
Player Count: 1 / 8; [EntityPlayerMP['Player90'/174, l='New World', x=105.50, y=70.00, z=-150.50]]
Type: Integrated Server (map_client.txt)
Is Modded: Definitely; Client brand changed to 'fml,forge'
Java File
package TFU_Mod;
import java.util.EnumSet;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.server.MinecraftServer;
import cpw.mods.fml.common.ITickHandler;
import cpw.mods.fml.common.TickType;
import net.minecraft.entity.player.EntityPlayerMP;
public class ServerTickHandler implements ITickHandler {
private int rand;
public MinecraftServer mcServer;
public Minecraft mc;
public EntityTFU_Trainer bat;
public void onPlayerTick(EntityPlayer player) {
if (player.getCurrentItemOrArmor(4) != null) {
ItemStack helmet = player.getCurrentItemOrArmor(4);
if (helmet.getItem() == TFU.TFU_Leader) {
player.addPotionEffect(new PotionEffect(Potion.nightVision.getId(), 40, 0));
rand = (int) (Math.random() * 10000);
if(rand == 25) {
player.addChatMessage("#TFU");
}
}
}
if (player.getCurrentItemOrArmor(3) != null) {
ItemStack chest = player.getCurrentItemOrArmor(3);
if (chest.getItem() == TFU.Cloaking) {
player.addPotionEffect(new PotionEffect(Potion.invisibility.getId(), 40, 0));
}
}
int distance = 16;
if (Math.sqrt(Math.pow((bat.posX - this.mc.thePlayer.posX), 2) + Math.pow((bat.posY - this.mc.thePlayer.posY), 2) + Math.pow((bat.posZ - this.mc.thePlayer.posZ), 2)) < distance) {
this.mc.thePlayer.addChatMessage("Message");
System.out.print("dfgdfgdfgb");
}
}
@Override
public void tickStart(EnumSet<TickType> type, Object... tickData) {
if (type.equals(EnumSet.of(TickType.PLAYER))) {
onPlayerTick((EntityPlayer) tickData[0]);
}
}
@Override
public void tickEnd(EnumSet<TickType> type, Object... tickData) {
// TODO Auto-generated method stub
}
@Override
public EnumSet<TickType> ticks() {
return EnumSet.of(TickType.PLAYER, TickType.SERVER);
}
@Override
public String getLabel() {
// TODO Auto-generated method stub
return null;
}
}
Crash report
Java File