Jump to content

[Solved] Minecraft.getMinecraft().thePlayer "cannot be resolved or is not a field"


Recommended Posts

Posted (edited)

Hi,

I am new to Forge.

I am trying to make a mod that will (1) monitor the chat and (2) print messages (only to me) to the chat. I have #1 down, but I am having trouble with #2.

My research shows that I should be using

Minecraft.getMinecraft().thePlayer.addChatMessage("Hello World");

to do it. My issue is that "thePlayer" is underlined in red with "thePlayer cannot be resolved or is not a field". "Minecraft.getMinecraft()" is perfectly fine.

 

Context:

package com.solitary_knight.turbokartracersstatistics;

import net.minecraft.client.Minecraft;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.event.ServerChatEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class ChatEventHandler {
	@SubscribeEvent
	public void onServerChat(ClientChatReceivedEvent event)
	{
		String message = event.getMessage().getUnformattedText();  // get the message
		Minecraft.getMinecraft().thePlayer.addChatMessage(message);  // repeat the message (as a test)
	}
}

I'm sure that I am doing something wrong, I just have no idea what. Thanks in advance.

Edited by Solitary_Knight
Posted

getPlayer()

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.

Posted
3 minutes ago, Draco18s said:

getPlayer()

I'm sorry, I don't understand what I am supposed to do with this. getPlayer() does not seem to be an attribute of Minecraft or Minecraft.getMinecraft() or "event". Could you please elaborate?

Posted

Sorry, I should have booted up Eclipse.

I thought they wrapped that field in a getter.

Nope, it just got renamed.

Minecraft.getMinecraft().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.

Posted
Just now, Draco18s said:

Sorry, I should have booted up Eclipse.

I thought they wrapped that field in a getter.

Nope, it just got renamed.

Minecraft.getMinecraft().player

The problem with "player" is that it does not have the "addChatMessage()" function like "thePlayer" is supposed to. It does have "sendChatMessage()", but my understanding is that that will send the message to all players on the server, which is not what I want.

Posted
2 hours ago, diesieben07 said:

Where did you get the information that it "is supposed to" have it?

EntityPlayerSP#sendMessage will simply add it to the chat GUI. Please look at the code yourself in the future.

Hereherehere, etc. It definitely, at one point, was "supposed to" have it. I guess that must be for older versions, but I was having difficulty finding anything about a change occurring. 

I had been looking at the code myself, but I was searching for "chat" so "sendMessage()" was not showing up. That does appear to be what I am looking for, so thank you for the response and hopefully I can actually start this mod now :)

Posted

Also, you can simply look at the Type Hierarchy (in Eclipse or Structure in IDEA) of the Minecraft class and it will list all the available fields and methods. Even if it is a long list it doesn't take much time to scroll through and look for things that look correct. In that you'll see that the player field is public and of type EntityPlayerSP. If you then look at the Type Hierarchy for EntityPlayerSP, I see that i have a public method called sendChatMessage() that takes a string parameter.

 

So definitely player (not thePlayer which doesn't exist) is the field you want. And for me, in 1.12.2 it has the right method available.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.

Announcements



×
×
  • Create New...

Important Information

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