TF-EssentialsX/Essentials/src/com/earth2me/essentials/commands/Commandsuicide.java
pop4959 07e2fe3af1
Improve suicide and kill commands (#3388)
Fixes #3189 

This PR makes improvements to the suicide and kill commands, namely:
1) Call `EntityDamageEvent` with damage equal to `Float.MAX_VALUE`, which is the amount that is inflicted by `/minecraft:kill`.
2) Removes the calls to `Damageable::damage` which ends up damaging the player with `DamageSource.GENERIC`, and later causing another `EntityDamageEvent` where as a result the last damage cause gets set to `EntityDamageEvent.DamageCause.CUSTOM`. Thus, the `EntityDamageEvent` that Essentials calls gets rendered useless when someone tries to get the damage cause of the player in `PlayerDeathEvent`. Setting health to zero forcibly kills the player without causing damage.
3) Add `getTabCompleteOptions` to the suicide command, as it currently incorrectly suggests players.
2020-07-01 22:04:46 +01:00

34 lines
1.1 KiB
Java

package com.earth2me.essentials.commands;
import com.earth2me.essentials.User;
import org.bukkit.Server;
import org.bukkit.event.entity.EntityDamageEvent;
import java.util.Collections;
import java.util.List;
import static com.earth2me.essentials.I18n.tl;
public class Commandsuicide extends EssentialsCommand {
public Commandsuicide() {
super("suicide");
}
@Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
EntityDamageEvent ede = new EntityDamageEvent(user.getBase(), EntityDamageEvent.DamageCause.SUICIDE, Float.MAX_VALUE);
server.getPluginManager().callEvent(ede);
ede.getEntity().setLastDamageCause(ede);
user.getBase().setHealth(0);
user.sendMessage(tl("suicideMessage"));
user.setDisplayNick();
ess.broadcastMessage(user, tl("suicideSuccess", user.getDisplayName()));
}
@Override
protected List<String> getTabCompleteOptions(Server server, User user, String commandLabel, String[] args) {
return Collections.emptyList();
}
}