Files
src
build.gradle
settings.gradle
Code
5 482
src/main/java/com/kodari/scythe/ScythePlugin.java
package com.kodari.scythe;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XEnchantment;
import com.cryptomorin.xseries.XAttribute;
import com.cryptomorin.xseries.XSound;
import com.cryptomorin.xseries.particles.XParticle;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.util.Vector;
import org.bukkit.scheduler.BukkitTask;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
public final class ScythePlugin extends JavaPlugin implements Listener {
private static final String SCYTHE_NAME = "Scythe";
private static final double NORMAL_DAMAGE_MULTIPLIER = 3.2D;
private static final double EXECUTION_FALL_DISTANCE = 10.0D;
private static final double SCYTHE_OWNER_HEALTH = 60.0D;
private static final double DEFAULT_HEALTH = 20.0D;
private final Set<UUID> scytheKills = new HashSet<>();
private final Set<UUID> scytheOwners = new HashSet<>();
private BukkitTask scytheTask;
private Particle dustParticle;
private int particleFrame;
@Override
public void onEnable() {
saveDefaultConfig();
getServer().getPluginManager().registerEvents(this, this);
XParticle.of("DUST").ifPresent(particle -> dustParticle = particle.get());
for (Player player : Bukkit.getOnlinePlayers()) {
syncOwnerHealth(player);
}
scytheTask = Bukkit.getScheduler().runTaskTimer(this, this::updateScythePlayers, 1L, 2L);
if (getCommand("scythe") != null) {
getCommand("scythe").setExecutor(this);
}
}
@Override
public void onDisable() {
if (scytheTask != null) {
scytheTask.cancel();
}
for (Player player : Bukkit.getOnlinePlayers()) {
setMaximumHealth(player, DEFAULT_HEALTH);
}
scytheOwners.clear();
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onScytheAttack(EntityDamageByEntityEvent event) {
if (!(event.getDamager() instanceof Player attacker)
|| !(event.getEntity() instanceof Player victim)
|| !isScythe(attacker.getInventory().getItemInMainHand())
|| victim.isDead()) {
return;
}
debug("Attack detected: " + attacker.getName() + " -> " + victim.getName());
int armorPieces = countArmorPieces(victim);
debug("Target armor: " + armorPieces + "/4 pieces (" + victim.getName() + ")");
boolean execution = !attacker.isOnGround()
&& attacker.getFallDistance() >= EXECUTION_FALL_DISTANCE
&& attacker.getVelocity().getY() <= 0.0D;
if (execution) {
event.setCancelled(true);
victim.setLastDamageCause(event);
victim.setKiller(attacker);
debug("Execution triggered: " + attacker.getName()
+ " was falling " + attacker.getFallDistance() + " blocks");
killWithScythe(victim);
return;
}
double damage = event.getDamage() * NORMAL_DAMAGE_MULTIPLIER;
event.setDamage(damage);
debug("Normal damage applied: " + damage + " to " + victim.getName());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void trackScytheKill(EntityDamageByEntityEvent event) {
if (!(event.getDamager() instanceof Player attacker)
|| !(event.getEntity() instanceof Player victim)
|| !isScythe(attacker.getInventory().getItemInMainHand())
|| victim.isDead()) {
return;
}
double totalHealth = victim.getHealth() + victim.getAbsorptionAmount();
if (event.getFinalDamage() >= totalHealth) {
scytheKills.add(victim.getUniqueId());
}
}
@EventHandler
public void onScytheDeath(PlayerDeathEvent event) {
Player victim = event.getEntity();
if (!scytheKills.remove(victim.getUniqueId())) {
return;
}
Location location = victim.getLocation().clone();
World world = location.getWorld();
if (world == null) {
return;
}
world.strikeLightningEffect(location);
debug("Lightning spawned at " + location.getBlockX() + ", "
+ location.getBlockY() + ", " + location.getBlockZ());
XSound.matchXSound("ENTITY_LIGHTNING_BOLT_THUNDER")
.ifPresent(sound -> world.playSound(location, sound.get(), 1.0F, 0.8F));
}
@Override
public boolean onCommand(
CommandSender sender,
Command command,
String label,
String[] args
) {
if (!command.getName().equalsIgnoreCase("scythe")) {
return false;
}
if (!sender.hasPermission("scythe.give")) {
sender.sendMessage("You do not have permission to use this command.");
return true;
}
Player target;
if (args.length == 0) {
if (!(sender instanceof Player player)) {
sender.sendMessage("Usage: /scythe give <player>");
return true;
}
target = player;
} else if (args.length == 1 && args[0].equalsIgnoreCase("give")) {
if (!(sender instanceof Player player)) {
sender.sendMessage("Usage: /scythe give <player>");
return true;
}
target = player;
} else if (args.length == 2 && args[0].equalsIgnoreCase("give")) {
target = Bukkit.getPlayerExact(args[1]);
if (target == null) {
sender.sendMessage("That player is not online.");
return true;
}
} else {
sender.sendMessage("Usage: /scythe [give] [player]");
return true;
}