Sabotage XX

Compile it

Kodari AI
KodariMon at 8:28 am 1
  1. On desktop, click Compile in the top bar. On phone, tap ⋮ → Compile Project.

  2. Click Compile in the window.

  3. When it succeeds, click Download on the build or Download .jar in the chat.

If it fails, click Auto-fix in the chat or Fix in the build window.

Kodari can make mistakes. Review important code before using.
Files
src
pom.xml
Code
3 190
src/main/java/com/deathback/DeathBack.java
package com.deathback;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
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.PlayerDeathEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class DeathBack extends JavaPlugin implements Listener, CommandExecutor {

    private record DeathPoint(String world, double x, double y, double z, float yaw, float pitch) {}

    private final Map<UUID, DeathPoint> deaths = new HashMap<>();
    private File dataFile;

    @Override
    public void onEnable() {
        getDataFolder().mkdirs();
        dataFile = new File(getDataFolder(), "deaths.yml");
        loadData();

        getServer().getPluginManager().registerEvents(this, this);
        if (getCommand("deathback") != null) {
            getCommand("deathback").setExecutor(this);
        }
    }

    @Override
    public void onDisable() {
        saveData();
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onDeath(PlayerDeathEvent event) {
        Player player = event.getPlayer();
        Location loc = player.getLocation();
        if (loc.getWorld() == null) return;

        deaths.put(player.getUniqueId(), new DeathPoint(
                loc.getWorld().getName(),
                loc.getX(), loc.getY(), loc.getZ(),
                loc.getYaw(), loc.getPitch()));
        saveData();
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player player)) {
            sender.sendMessage(Component.text("Only players can use this command.", NamedTextColor.RED));
            return true;
        }

        DeathPoint point = deaths.get(player.getUniqueId());
        if (point == null) {
            player.sendMessage(Component.text("You don't have a saved death location.", NamedTextColor.RED));
            return true;
        }

        World world = Bukkit.getWorld(point.world());
        if (world == null) {
            player.sendMessage(Component.text("The world of your death location isn't loaded.", NamedTextColor.RED));
            return true;
        }

        double y = point.y();
        // If the player died in the void, don't send them back into it.
        if (y < world.getMinHeight()) {
            y = world.getHighestBlockYAt((int) Math.floor(point.x()), (int) Math.floor(point.z())) + 1;
        }

        Location target = new Location(world, point.x(), y, point.z(), point.yaw(), point.pitch());
        player.teleportAsync(target).thenAccept(success -> {
            if (success) {
                player.sendMessage(Component.text("Teleported back to your death location.", NamedTextColor.GREEN));
            } else {
                player.sendMessage(Component.text("Teleport failed.", NamedTextColor.RED));
            }
        });
        return true;
    }

    private void loadData() {
        if (!dataFile.exists()) return;
        YamlConfiguration yaml = YamlConfiguration.loadConfiguration(dataFile);
        for (String key : yaml.getKeys(false)) {
            ConfigurationSection sec = yaml.getConfigurationSection(key);
            if (sec == null) continue;
            try {
                deaths.put(UUID.fromString(key), new DeathPoint(
                        sec.getString("world", ""),
                        sec.getDouble("x"), sec.getDouble("y"), sec.getDouble("z"),
                        (float) sec.getDouble("yaw"), (float) sec.getDouble("pitch")));
            } catch (IllegalArgumentException ignored) {
                // skip invalid entries
            }
        }
    }

    private void saveData() {
        YamlConfiguration yaml = new YamlConfiguration();
        for (Map.Entry<UUID, DeathPoint> e : deaths.entrySet()) {
            String base = e.getKey().toString();
            DeathPoint p = e.getValue();
            yaml.set(base + ".world", p.world());
            yaml.set(base + ".x", p.x());
            yaml.set(base + ".y", p.y());
            yaml.set(base + ".z", p.z());
            yaml.set(base + ".yaw", p.yaw());
            yaml.set(base + ".pitch", p.pitch());
        }
        try {
            yaml.save(dataFile);
        } catch (IOException ex) {
            getLogger().warning("Could not save deaths.yml: " + ex.getMessage());
        }
    }
}