Skip to content
Advertisement

Why does this code only output “Test” when I have the boots on but, it never outputs “no” when I take the boots off?

Here is the code:

@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
    Player p = event.getPlayer();
    ItemStack boots = p.getInventory().getBoots();
    if (!boots.getItemMeta().getDisplayName().equals("§3Elemental Boots")) {
        p.sendMessage("no");
        p.setAllowFlight(false);
    }
    if (boots.getItemMeta().getDisplayName().equals("§3Elemental Boots")) {
        p.setAllowFlight(true);
        p.sendMessage("Test");
    }

}

To specify, when I put the boots on, and move, then it outputs Test. When I take them off, it outputs nothing at all. Please tell me what I am doing wrong, Thanks!

Advertisement

Answer

Because you don’t have any boots on. I’ll break it down line for line 🙂

The first three lines are obvious

@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
    Player p = event.getPlayer();

Here you’re creating a variable boots and giving it the value of inventory.getBoots(); which will return an ItemStack if the player is wearing them. You are probably getting a NullPointerException if you look in your servers console because if the player isn’t wearing any boots, the boots variable will be null.

    ItemStack boots = p.getInventory().getBoots();

Now you check if the boots displayname doesn’t equal “§3Elemental Boots”, this is all well and good if the player is wearing boots. In your case when the boots are off and the boot variable exuals null, you are trying to access the displayname of an item that doesn’t exist, so nothing will happen (Except an NPE error). (There is another issue with this line that I’ll explain in the bottom of the reply)

    if (!boots.getItemMeta().getDisplayName().equals("§3Elemental Boots")) {
        p.sendMessage("no");
        p.setAllowFlight(false);
    }

The same issue is happening here

    if (boots.getItemMeta().getDisplayName().equals("§3Elemental Boots")) {
        p.setAllowFlight(true);
        p.sendMessage("Test");
    }
}

The way around this is just adding another if statement to make sure the player is wearing boots. You can do this like so:

if(p.getInventory().getBoots().equals(null)) {
    //Gets called when the player isn't wearing boots
    p.sendMessage("You aren't wearing any boots!")
} else {
    //Gets called when the player is wearing boots
    ItemStack boots = p.getInventory().getBoots();
    //Rest of your code
}

The second issue above is that colour in text can be weird with Java, you could run into problems using .equals("§3Elemental Boots") You have two options here,

  • Option 1, Use: .equals(ChatColor.translateAlternateColorCodes('§', "§3Elemental Boots")) You should also use that ChatColor method when making any strings with colour in them.
  • Option 2, Use: ChatColor.stripColors(boots.getItemMeta().getDisplayName()).equals("Elemental Boots")

I would personally go for option 2

I hope this helped!

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement