'I am trying to make a minecraft plugin with java 8 where if you get killed by an player, you drop your loot but if you get killed by a mob you keep it [closed]

enter image description here

theres the code, but the problem is that line 14 does not work and i cannot seem to find anyone else done this



Solution 1:[1]

Most likely, this is a problem with the types: If you are familiar with Java, you should know that MCEntity (example of its usage found with Tabnine) is not comparable to type EntityType.

Therefore, you will have to use a method to get the Type of the MCEntity (the Type I'm referring to in this context is the MC type, e.g., PLAYER, not Java-type).

This method, that exists on the MCEntity, is MCEntity.getType() that will return to you the MC-Type of the Killer. So you need the code:

public void onPlayerDead(PlayerDeathEvent event) {
    Player playerDead = event.getEntity();

    // Now check for the Type of the Killer with the getType()-method
    if (playerDead.getKiller().getType() == EntityType.PLAYER) {
        event.setKeepInventory(false);
    }
}

If you have further questions, feel free to ask, but this question is rather hard to answer because you just provided us one image of your code. Please take a look on how to format code with markdown.

Furthermore, the line in the editor has a red line. That indicates an error in your code. Asking the question, it would be great if you could give us the exact error message that is shown, when you hover over the red line. That helps a lot while debugging (especially other people's) code!

Solution 2:[2]

Firstly, I'm going to assume you are making a spigot plugin.

Instead of playerDead.getKiller(), instead use playerDead.getKiller().getType().

To avoid simple problems like this, learn how types work in java, and read javadocs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 STh
Solution 2 seriouslyForWhat