Trouble with methods

Discussion in 'Plugin Development' started by pedrinholuizf, Sep 21, 2015.

Thread Status:
Not open for further replies.
  1. Offline


    I'm creating a plugin for my server. Basically, when someone types /eventospeed, it broadcasts a word from config and the first person who types it into the global chat wins a prize.

    Here's my code:
        boolean status = false;
        String palavra;
        FileConfiguration config = Main.plugin.getConfig();
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender.hasPermission("stormcraft.speed.start")) {
                status = true;
                List<String> palavras = config.getStringList("eventospeed.palavras");
                Random rnd = new Random();
                palavra = palavras.get(rnd.nextInt(palavras.toArray().length));
                Bukkit.broadcastMessage("§3§l(Speed) §bA primeira pessoa a digitar §b§l" + palavra + " §bno chat global ganha 10k!");
            } else {
                sender.sendMessage("§4§lERRO: §cVocê não pode fazer isso!");
            return true;
        public void onChat(ChatMessageEvent e) {
            Bukkit.broadcastMessage("§5Event ok! " + palavra);
            if (status) {
                Bukkit.broadcastMessage("§5Status ok");
                Channel c = Legendchat.getChannelManager().getChannelByName("global");
                if (e.getChannel() == c) {
                    if (e.getMessage().contains(palavra)) {
                        Bukkit.broadcastMessage("§5Word ok");
                        status = false;
                        Bukkit.broadcastMessage("§3§l(Speed) §b" + e.getSender().getName() + " ganhou!");
                        try {
                            Economy.add(e.getSender().getName(), BigDecimal.valueOf((double) 10000));
                        } catch (NoLoanPermittedException | ArithmeticException | UserDoesNotExistException e1) {
                            e.getSender().sendMessage("§3§l(Speed) §cNão foi possível dar seu prêmio. Por favor, contate um staffer!");
    EDIT: ChatMessageEvent is from LegendchatAPI.

    The problem is: The status variable (that tells if the minigame is happening or not), and the word variable, doesen't change through methods. When someone types the word in the chat, it stops on this line:
    if (status) {
  2. Offline


    So no matter what the command is ,if the player does not have the permission they will receive that message? Also, if they do have the permission, even /say will be treated as though they type /eventospeed.

    For your actual problem:
    Did you even try to debug? What does status equal? When are you ever setting status?

    [edit] Also, for your onCommand it will always return true. So no matter what command the player sends (/bhfdugjridfuska for example), it is a command that is already been handled.
    Last edited: Sep 21, 2015
  3. Offline


    @Zombie_Striker, the last part about the command stuff is false. Bukkit only allow you to handle commands you have specified in your plugin.yml, and you can return true or false depending on if it was handled or not.
  4. Offline


    Use the ChatColor enum instead of §
  5. Offline


    On the EventHandler, even if i setted status to true and palavra (word in portuguese) to something, they always return the initialization value. About debugging, i'm sending debugger messages after each if, but it stops after the first one, because status is false, as defined in initialization, and palavra is always null. (On the Event)

    And, this is a commandexecutor. I think the plugin only sends the command to this class is its the command that i defined in the main class.

    Whats the difference?
  6. Offline


    When you use §, if there is anytime a change in color codes, your plugin may break. I don't think they will ever change the enum.
  7. Offline


    And even if they change the enum, the only way they would change it is the code/return itself. The object itself would still remain, even if it would be deprecated.

    Lets stop talking about the chatcolor symbol. It has been talked about far to much for today (on other posts).
  8. Offline


    Regardless how many times it has been covered you shouldn't let it go again, he hasn't read those threads, he needs to know that the ChatColor Enum is a much better solution, even it it means he just changes it to ChatColor.translate...();

    also e.getMessage().contains(palavra) should be e.getMessage().toLowerCase().contains(palavra.toLowerCase()) incase someone forgets the capitals when typing it, unless you want your event to be super strict and palavras.toArray().length should just be palavras.size(). Apart from that, try debugging the status variable in the Event Ok debug message
  9. Offline


    The plugin is for my own server, if something breaks, i can change it, but i don't pretend to update my server that soon.

    Good idea about the lower case thing. About the size, if a List has 10 objects in it, and i do .size(), it will return 9 or 10?

    I already tried to debug status and palavra, and they still with the the value that i gave in initialization even after changing the values in onCommand
  10. Offline


    You should still use it, it's good programming practice, if you're too lazy to type out all the ChatColors that you want to use then use ChatColor.translate...(Character, String);
    It returns the same amount an array of the same size returns (it returns 10)
    Try adding debug after you set the values to see if they're updating
Thread Status:
Not open for further replies.

Share This Page