View difference between Paste ID: bmXivD7X and wuGPdkp6
SHOW: | | - or go back to the newest paste.
1
package test;
2
3
import org.bukkit.Bukkit;
4
import org.bukkit.ChatColor;
5
import org.bukkit.OfflinePlayer;
6
import org.bukkit.command.Command;
7
import org.bukkit.command.CommandSender;
8
import org.bukkit.command.ConsoleCommandSender;
9
import org.bukkit.configuration.Configuration;
10
import org.bukkit.configuration.ConfigurationSection;
11
import org.bukkit.entity.Player;
12
import org.bukkit.event.EventHandler;
13
import org.bukkit.event.Listener;
14
import org.bukkit.event.player.PlayerJoinEvent;
15
import org.bukkit.plugin.java.JavaPlugin;
16
17
import com.gmail.nossr50.datatypes.skills.SkillType;
18
import com.gmail.nossr50.events.experience.McMMOPlayerXpGainEvent;
19
20
public class TestPlugin extends JavaPlugin implements Listener {
21
    boolean configProblems = false;
22
23
    @Override
24-
        validateConfig(getConfig());
24+
25
        saveDefaultConfig();
26
        getServer().getPluginManager().registerEvents(this, this);
27-
    private void validateConfig(ConfigurationSection config) {
27+
28
        if (validateConfig(getConfig())) {
29
            getLogger().warning("### Some mistakes were found in the configuration file. ###");
30
            getLogger().warning("###  Those players' perks may not function correctly.   ###");
31
            configProblems = true;
32
        }
33
    }
34
35
    private boolean validateConfig(ConfigurationSection config) {
36
        configProblems = false;
37
        ConfigurationSection playersSection = config.getConfigurationSection("players");
38
        for (String plName : playersSection.getKeys(false)) {
39
            OfflinePlayer op = getServer().getOfflinePlayer(plName);
40
            if (!op.hasPlayedBefore()) {
41
                getLogger().warning(plName + " has not played on this server, but they are in the config file!");
42
                configProblems = true;
43
            }
44
            ConfigurationSection subSection = playersSection.getConfigurationSection(plName);
45
            for (String skName : subSection.getKeys(false)) {
46
                SkillType skilltype = SkillType.valueOf(skName);
47
                if (skilltype == null) {
48-
        sender.sendMessage(ChatColor.AQUA + "Reloaded mcMMO XP Perks config");
48+
49
                    configProblems = true;
50
                } else if (!skilltype.name().toLowerCase().equals(skName)) {
51
                    getLogger().warning("Please use '" + skilltype.name().toLowerCase() + "' instead of '" + skName + "'.");
52
                    configProblems = true;
53
                }
54
            }
55
        }
56
        return configProblems;
57
    }
58
59
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
60
        this.reloadConfig();
61
        if (validateConfig(getConfig())) {
62
            String message = ChatColor.AQUA + "Reloaded mcMMO XP Perks config, " + ChatColor.RED + "but some problems were found.";
63
            if (!(sender instanceof ConsoleCommandSender)) {
64
                sender.sendMessage(message);
65
                sender.sendMessage(ChatColor.RED + "Please review the issues printed in the server console.");
66
                Bukkit.getConsoleSender().sendMessage(message);
67
            } else {
68
                sender.sendMessage(message);
69
                sender.sendMessage(ChatColor.RED + "Please review the issues printed above.");
70
            }
71
        } else {
72
            String message = ChatColor.AQUA + "Reloaded mcMMO XP Perks config. " + ChatColor.GREEN + "No issues found.";
73
            if (!(sender instanceof ConsoleCommandSender)) {
74
                sender.sendMessage(message);
75
            }
76
            Bukkit.getConsoleSender().sendMessage(message);
77
        }
78
        return true;
79
    }
80
81
    @EventHandler
82
    public void onJoin(PlayerJoinEvent event) {
83
        if (configProblems && event.getPlayer().isOp()) {
84
            event.getPlayer().sendMessage(ChatColor.RED + "Some problems were found in the " + ChatColor.DARK_AQUA + "CustomMcmmoXpPerks" + ChatColor.RED + " configuration file.");
85
            event.getPlayer().sendMessage(ChatColor.RED + "Please resolve the issues and run /mcxpperks reload to reload the configuration.");
86
        }
87
    }
88
89
    @EventHandler
90
    public void onXp(McMMOPlayerXpGainEvent event) {
91
        event.setRawXpGained((float) (event.getRawXpGained() * getMultiplier(event.getPlayer(), event.getSkill())));
92
    }
93
94
    private double getMultiplier(Player player, SkillType skill) {
95
        return getConfig().getDouble("players." + player.getName() + "." + skill.name().toLowerCase(), 1D);
96
    }
97
}