Java: Can't make a static reference?

Za
- in Plugins
27

I started writing Minecraft Bukkit Plugins (1.15.2) again today. Now I've written a little plugin, but since I'm still a beginner at writing Minecraft plugins, I now have a bug in the code.

Can't make a static reference to the non-static method sendMessage (String) from the Type CommandSender

I would like to know what's wrong.

Thanks in advance! The code:

package main.Bukkit.plugin.mcp;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import com.mojang.brigadier.Command;

public class plugin extends JavaPlugin {
public String PluginName = this.getDescription (). GetName ();
public String PluginVers = this.getDescription (). GetVersion ();

public void onEnable (String [] args) {

System.out.println ("& f [& 5" + PluginName + "& f] & aPlugin started! Version:" + PluginVers);
}

public void onDisable (String [] args) {
System.out.println ("& f [& 5" + PluginName + "& f] & aPlugin switched off!");
}

@SuppressWarnings ("rawtypes")
public boolean onCommand (CommandSender sender, Command command, String label, String [] args) {

Player player = null;
if (sender instanceof Player) {
player = (player) transmitter;
}

if CommandSender) command) .getName (). EqualsIgnoreCase ("health")) {
if (player == null) {
return true;
} else {
Player.sendMessage (getDescription (). GetAuthors (). Get (0));
return true;
}
}

}
}

Ma

What does the console say?

Ka

You try to call the sendMessage method using the Player class. However, the method is not static.

Za

I can't say that at the moment because my Minecraft server doesn't want to start. But when it comes back I'll say it.

Za

So what do I have to do about it?

Ka

Call the method using an object of the Player class.

And maybe first Java or learn the basics of object-oriented programming before you deal with any library.

Su

How about "Learn the basics of Java first"? -.

Za

So I've mastered the basics for a long time.

Za

I already can do it.

Su

Obviously not…

Ka

Do you know what static methods are? (honestly without googling now)

Au

You have created a variable above which is called player with a small "p" at the beginning and which comes from the class player with a large "p" at the beginning.

If you now want to access the functions of the Player class, you have to take the variable and not the class. If you access the class directly, the function you access of the "static" type must be static, then you can access it directly via the class (player).

The sendMessage function is probably not a static function, which is why you can only access it via a constructor.

So not "Player.sendMessage" but "player.sendMessage". The "p" from player at the beginning must be in lower case.

Maybe next time you shouldn't call the variable player but player or p1 then you can't get through another one so quickly.

Za

But. But I have not dealt with this topic before.

Za

Functions that can't be changed

Su

Then you do not master the basics contrary to your claim.

Static methods and the like Because they are part of the basics.

Ka

Not correct. Is not meant badly, but obviously you really can't do the basics and you should learn them.

Za

Thank you!

Za

But I've been developing in Java for a long time. Ok, you don't notice that now because unfortunately it's a thing that I don't know but I've really been dealing with programming for a long time.

Ka

Static means that a variable or method is not bound to the instance of a class, so it applies to all objects.

Ka

How did you learn? The term "static" is not exactly a niche term now 😅

Sk

That the error is caused by:

Player.sendMessage (getDescription (). GetAuthors (). Get (0));

player have capitalized, others have already explained to you.

A few more comments on the rest:

public class plugin extends JavaPlugin {

I doubt that's the right name. Better find a more descriptive name for the class.

public String PluginName = this.getDescription (). GetName ();
public String PluginVers = this.getDescription (). GetVersion ();

That should be private. Variable names please small, conventions are annoying at the beginning, but very important.

@SuppressWarnings ("rawtypes")

Only ignore warnings if you know what you are doing. They already make sense.

Player player = null;
if (sender instanceof Player) {
player = (player) transmitter;
}

If sender is not an instance of Player here, player = (Player) sender is not executed, but the code is still processed. There you have a crash very quickly due to a zero pointer. Wouldn't you rather put a return in there?

if CommandSender) command) .getName (). EqualsIgnoreCase ("health")) {

I doubt that a Command can be cast into a CommandSender. That looks like the completely wrong start.

Player.sendMessage (getDescription (). GetAuthors (). Get (0));

Here, as I said, the error that you have capitalized player, and thus does not address the instance but the class itself. But what I'm also wondering is how do you know that getAuthors () has at least one element? What if it is empty? Don't you want to check that out?

You should really look at the basics because they are missing. Even if you say that you have been developing in Java for a long time. This is in no way meant to be evil or anything, it's just good advice. If you invest in the basics, you will have to spend a little time on it, but in the end it will be worth it. If you go on like this you may get a little further, but eventually you will get stuck and not be able to move forward because you simply lack the basics. Then you have to go back and learn what you have already learned.

Za

I taught myself. But you can't say that I can't Java because I have already written a compiler and a console program and many other things with Java. And I enjoy programming. It is actually not important to me whether I know a little thing, but that I can deal with it.

Za

Thank you for your answer!

You should really look at the basics because they are missing. Even if you say that you have been developing in Java for a long time.

Maybe I should really do that again. But my real problem is to familiarize myself with completely new projects in Java. I have done this again for a long time today. That's why it's difficult for me.

Su

So you claim that you programmed a compiler (for which programming language?) In Java? OK

I very much doubt that.

Za

So that was just a project. I stopped it a few days ago. Maybe I'll keep going at some point. I invented the language myself.

Ka

It sounds very questionable how you invented your own programming language and wrote a compiler with Java without knowing the definition of static…

Za

I know

Za

It's not a big compiler either.