Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.8 kB
3
Indexable
Never
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ToddsConversations extends JavaPlugin implements Listener {

    private static NPCRegistry npcRegistry;
    private static Map<Player, String> playerWorlds;
    private static Map<Player, ConversationData> conversationParticipants;
    private static FileConfiguration messagesConfig;
    private static FileConfiguration config;
    private static File messagesFile;
    private static File configFile;

    @Override
    public void onEnable() {
        npcRegistry = CitizensAPI.getNPCRegistry();
        playerWorlds = new HashMap<>();
        conversationParticipants = new HashMap<>();
        // Dentro del método onEnable
        config = getConfig(); // Inicializar la variable config con getConfig()
        config.options().copyDefaults(true); // Opcional: copiar las configuraciones predeterminadas si no existen

        loadMessagesConfig();
        loadConfig();

        saveConfig(); // Guardar la configuración en disco

        Bukkit.getPluginManager().registerEvents(this, this);

        Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Plugin 'Todd's Conversations' has been enabled.");
    }

    @Override
    public void onDisable() {
        saveConfigurations();

        Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Plugin 'Todd's Conversations' has been disabled.");
    }

    private void loadMessagesConfig() {
        messagesFile = new File(getDataFolder(), "messages.yml");

        if (!messagesFile.exists()) {
            messagesFile.getParentFile().mkdirs();
            saveResource("messages.yml", false);
        }

        messagesConfig = YamlConfiguration.loadConfiguration(messagesFile);

        // Load default messages from the resource file
        Reader defaultMessagesReader = new InputStreamReader(getResource("messages.yml"), StandardCharsets.UTF_8);
        FileConfiguration defaultMessagesConfig = YamlConfiguration.loadConfiguration(defaultMessagesReader);
        messagesConfig.setDefaults(defaultMessagesConfig);
        messagesConfig.options().copyDefaults(true);
    }

    private void saveMessagesConfig() {
        try {
            messagesConfig.save(messagesFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void loadConfig() {
        configFile = new File(getDataFolder(), "config.yml");

        if (!configFile.exists()) {
            configFile.getParentFile().mkdirs();
            saveResource("config.yml", false);
        }

        config = YamlConfiguration.loadConfiguration(configFile);
    }

    public void saveConfig() {
        try {
            config.save(configFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void saveConfigurations() {
        saveMessagesConfig();
        saveConfig();
    }

    private void startConversation(Player player, ConversationData conversationData) {
        conversationParticipants.put(player, conversationData);
        conversationData.sendMessage(player);
    }

    private static void endConversation(Player player) {
        conversationParticipants.remove(player);
        player.sendMessage(getFormattedMessage("conversation-ended"));
    }

    private static boolean hasPermission(Player player, String permission) {
        if (player.isOp()) {
            return true;
        }
        return player.hasPermission(permission);
    }

    @EventHandler
    public void onChat(AsyncPlayerChatEvent event) {
        Player player = event.getPlayer();
        if (conversationParticipants.containsKey(player)) {
            event.setCancelled(true);
            ConversationData conversationData = conversationParticipants.get(player);
            conversationData.handleInput(player, event.getMessage());
        }
    }

    private static String getFormattedMessage(String key) {
        return ChatColor.translateAlternateColorCodes('&', messagesConfig.getString(key));
    }

    public static class ConversationData {
        private List<String> questions;
        private List<String> answers;
        private int currentQuestionIndex;

        public ConversationData(List<String> questions, List<String> answers) {
            this.questions = questions;
            this.answers = answers;
            this.currentQuestionIndex = 0;
        }

        public void sendMessage(Player player) {
            if (currentQuestionIndex < questions.size()) {
                String question = questions.get(currentQuestionIndex);
                player.sendMessage(question);
            } else {
                endConversation(player);
            }
        }

        public void handleInput(Player player, String input) {
            String expectedAnswer = answers.get(currentQuestionIndex);
            if (input.equalsIgnoreCase(expectedAnswer)) {
                currentQuestionIndex++;
                sendMessage(player);
            } else {
                player.sendMessage(getFormattedMessage("wrong-answer"));
            }
        }
    }
}