Newer
Older
ChallengeSystem / src / main / java / de / fanta / challenges / commands / VillageCommand.java
@fanta fanta on 4 Jul 2022 3 KB Fix Village Command
package de.fanta.challenges.commands;

import de.fanta.challenges.Challenges;
import de.fanta.challenges.utils.ChatUtil;
import de.iani.cubesideutils.bukkit.commands.SubCommand;
import de.iani.cubesideutils.commands.ArgsParser;
import org.bukkit.Bukkit;
import org.bukkit.HeightMap;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.generator.structure.Structure;
import org.bukkit.util.StructureSearchResult;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public class VillageCommand extends SubCommand {

    private final Challenges plugin;

    public VillageCommand(Challenges plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) {
        if (!(sender instanceof Player player)) {
            ChatUtil.sendErrorMessage(sender, "You are not a Player :>");
            return true;
        }

        if (plugin.isEditor(player) || player.hasPermission("Challenges.editor.override")) {
            if (args.hasNext()) {
                String structureString = args.getNext();
                Structure structure;

                try {
                    structure = VillageTypes.valueOf(structureString.toUpperCase()).getStructure();
                } catch (IllegalArgumentException ex) {
                    ChatUtil.sendErrorMessage(player, "Das von dir gesuchte Villager Dorf (" + structureString + ") gibt es nicht.");
                    return true;
                }

                StructureSearchResult villageResult = player.getWorld().locateNearestStructure(player.getLocation(), structure, 10000, true);

                if (villageResult == null) {
                    ChatUtil.sendErrorMessage(player, "Kein Villager Dorf gefunden.");
                    return true;
                }

                Location village = villageResult.getLocation();
                int x = village.getBlockX();
                int z = village.getBlockZ();
                Location villagetop = village.getWorld().getHighestBlockAt(x, z, HeightMap.MOTION_BLOCKING).getLocation().add(0, 1, 0);
                int y = villagetop.getBlockY();
                Bukkit.getWorlds().get(0).setSpawnLocation(villagetop);
                ChatUtil.sendNormalMessage(player, "Dorf: " + ChatUtil.BLUE + x + " " + y + " " + z);
                for (Player pp : Bukkit.getOnlinePlayers()) {
                    pp.teleport(villagetop);
                    ChatUtil.sendNormalMessage(pp, "Dorfspawn gesetzt!");
                }
            } else {
                ChatUtil.sendWarningMessage(player, "/village <Villager Dorf Biom>");
            }
            return true;
        } else if (plugin.getCurrentEditor() != null) {
            ChatUtil.sendErrorMessage(player, "Du bist kein Editor! nur" + plugin.getCurrentEditor().getName() + " kann nach Dörfern suchen");
            return true;
        } else {
            ChatUtil.sendErrorMessage(player, "Aktuell gibt es keinen Editor!");
            ChatUtil.sendErrorMessage(player, "Um selbst Editor zu werden musst du dir im Freebuild VIP Kaufen!");
            return true;
        }
    }

    @Override
    public Collection<String> onTabComplete(CommandSender sender, Command command, String alias, ArgsParser args) {
        String[] villages = new String[] { "Plains", "Desert", "Savanna", "Snowy", "Taiga" };
        return new ArrayList<>(Arrays.asList(villages));
    }

    public enum VillageTypes {
        PLAINS(Structure.VILLAGE_PLAINS),
        DESERT(Structure.VILLAGE_DESERT),
        SAVANNA(Structure.VILLAGE_SAVANNA),
        SNOWY(Structure.VILLAGE_SNOWY),
        TAIGA(Structure.VILLAGE_TAIGA);

        private final Structure structure;

        VillageTypes(Structure structure) {
            this.structure = structure;
        }

        public Structure getStructure() {
            return structure;
        }
    }
}