When executing the command "/group" and "/group example_player", I get a NullPointerException. It doesn't give me any more information, of what (or where) the problem is. I hope, that StackOverflow can help.
Stym.java:
import org.bukkit.plugin.java.JavaPlugin;
public class Stym extends JavaPlugin {
@Override
public void onEnable() {
getConfig().options().copyDefaults(true);
saveConfig();
registerCommands();
registerListeners();
}
public void registerCommands() {
GroupCommand groupCommandClass = new GroupCommand(this);
getCommand("GROUP").setExecutor(groupCommandClass);
}
public void registerListeners() {
}
public boolean isLeather(String playerName) {
for(String ironPlayerName : getConfig().getStringList("groups.iron")) {
if(!ironPlayerName.equals(playerName)) {
for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
return !diamondPlayerName.equals(playerName);
}
} else {
return false;
}
}
return false;
}
public boolean isIron(String playerName) {
for(String ironPlayerName : getConfig().getStringList("groups.iron")) {
if(ironPlayerName.equals(playerName)) {
for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
return !diamondPlayerName.equals(playerName);
}
} else {
return false;
}
}
return false;
}
public boolean isDiamond(String playerName) {
for(String ironPlayerName : getConfig().getStringList("groups.iron")) {
if(!ironPlayerName.equals(playerName)) {
for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
return diamondPlayerName.equals(playerName);
}
} else {
return false;
}
}
return false;
}
}
GroupCommand.java:
import org.bukkit.entity.Player;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.CommandExecutor;
public class GroupCommand implements CommandExecutor {
Stym stym;
GroupCommand(Stym stymClass) {
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] arguments) {
try {
if(arguments.length == 0) {
if(sender instanceof Player) {
if(!sender.isOp()) {
if(stym.isLeather(sender.getName())) {
sender.sendMessage("You are made out of leather!");
} else if(stym.isIron(sender.getName())) {
sender.sendMessage("You are made out of iron!");
} else {
sender.sendMessage("You are made out of diamond!");
}
} else {
sender.sendMessage("You are an operator!");
}
} else {
sender.sendMessage("You are a god!");
}
} else if(arguments.length == 1) {
if(!arguments[0].equals("Krischon")) {
if(stym.isLeather(sender.getName())) {
sender.sendMessage(arguments[0] + " " + "is made out of leather!");
} else if(stym.isIron(sender.getName())) {
sender.sendMessage(arguments[0] + " " + "is made out of iron!");
} else {
sender.sendMessage(arguments[0] + " " + "is made out of diamond!");
}
} else {
sender.sendMessage("Krischon is an operator!");
}
} else {
sender.sendMessage("Not supported yet!");
}
} catch(Exception exception) {
System.out.println(exception);
}
return true;
}
}
plugin.yml:
name: Stym
main: Stym
version: 1.3.3.7
commands:
group:
config.yml:
groups:
iron:
- Player
- Another_Player
diamond:
- Rich_Player
Stym stym;
GroupCommand(Stym stymClass) {
}
You never initialized Stym stym
in the constructor. Therefore, when you try to use it in your methods, it is null
.
Stym stym;
GroupCommand(Stym stymClass) {
stym = stymClass;
}
For loop
for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
return !diamondPlayerName.equals(playerName);
}
The way you have these loops set up, they will only evaluate the first name in the list and return the corresponding boolean value. The moment you return
a value, the method will terminate. You'd be better off using something like this:
public boolean isLeather(String playerName) {
for(String ironPlayerName : getConfig().getStringList("groups.iron")) {
if(ironPlayerName.equals(playerName)) {
return false;
}
}
for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
if(diamondPlayerName.equals(playerName)) {
return false;
}
}
return true;
}
This way it will run through the iron and diamond lists, and if the player name is found in one of those lists, it will instantly return false. If it makes it through both lists without finding the player name, it will return true. You will need to do something similar to this for isIron
and isDiamond
as well.
If you also have a "groups.leather"
then this can be done even more simply.
public boolean isLeather(String playerName) {
for(String leatherPlayerName : getConfig().getStringList("groups.leather")) {
if(leatherPlayerName.equals(playerName)) {
return true;
}
}
return false;
}
public boolean isIron(String playerName) {
for(String ironPlayerName : getConfig().getStringList("groups.iron")) {
if(ironPlayerName.equals(playerName)) {
return true;
}
}
return false;
}
public boolean isDiamond(String playerName) {
for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
if(diamondPlayerName.equals(playerName)) {
return true;
}
}
return false;
}
This method would only search the respective list for leather/iron/diamond. Since you're calling all 3 methods in Stym
anyway, this would be ideal.