Incorrect Event Activation

Discussion in 'Plugin Development' started by cococow123, Dec 2, 2015.

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

    mcdorli

    Yup, the colors need to match too. Do a ChatColot.stripColors, to remove them
     
    cococow123 likes this.
  2. Offline

    cococow123

    hmm.. this is odd.

    (Its a debug test)

    When I click the sign with
    Code:
    [mVote]
    ID: <ID>
    it works..

    When I click a sign with
    Code:
    [mVote]
    ID; <ID>
    it doesn't work.

    Good things:
    -Clicking 1 type of sign works
    -No errors/stacktraces when clicking sign
    -Gets the correct ID and info when sending message

    Bad Things:
    -Clicking the other type of sign doesn't work

    I don't know if this has to do with colons or semicolons. It might be a character that I cannot
    check.. (probably not the case)

    Picture:
    http://imgur.com/VR1qD5t

    Left Sign: Sign clicking doesn't work.
    Right Sign: Sign clicking does work.

    Hopefully someone can help!

    EDIT: How could I forget to add the code *facepalm*

    Code:
    Code:
       
        @EventHandler
        public void onSignClick(PlayerInteractEvent e) {
            Player player = e.getPlayer();
            Block block = e.getClickedBlock();
            Action action = e.getAction();
    
            if (e.getClickedBlock() == null) {
                return;
        }
    
            if (action == Action.RIGHT_CLICK_BLOCK
                && block != null
                && (block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN)) {
            Sign sign = (Sign) block.getState();
               
               
                String line1 = ChatColor.stripColor(sign.getLine(0));
                if (line1.equals("[mVote]") && sign.getLine(1).contains(ChatColor.GRAY + "ID;")) {
                       
                final String pre = ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "mVote" + ChatColor.DARK_GRAY + "] ";
                           
                            String[] ids = sign.getLine(1).split("; ");
                            String id = ids[1];
                           
                            File idss = new File("plugins" + File.separator + "mVote" + File.separator + "votes" + File.separator + id + File.separator +  id + ".yml");
                        FileConfiguration idd = YamlConfiguration.loadConfiguration(idss);
    
                            player.sendMessage(pre + ChatColor.YELLOW + "Hosted by1: " + ChatColor.AQUA + idd.getString("host"));
                        player.sendMessage(pre + ChatColor.YELLOW + "Vote Poll Ends: " + ChatColor.AQUA + idd.getString("end-date"));
                           
               
               
                }    
           
               
                    if (line1.equals("[mVote]") && sign.getLine(1).contains(ChatColor.GRAY + "ID:")) {
                                   
                                    String[] ids = sign.getLine(1).split(": ");
                                    String id = ids[1];
                                   
                                    File idss = new File("plugins" + File.separator + "mVote" + File.separator + "votes" + File.separator + id + File.separator +  id + ".yml");
                                FileConfiguration idd = YamlConfiguration.loadConfiguration(idss);
    
                                    player.sendMessage(pre + ChatColor.YELLOW + "Hosted by2: " + ChatColor.AQUA + idd.getString("host") );
                                    player.sendMessage(pre + ChatColor.YELLOW + "Vote Poll Ends: " + ChatColor.AQUA + idd.getString("end-date") );
                           
            }
           
        }
            }
            }
    
            
     
  3. Offline

    Zombie_Striker

    @cococow123
    You see, those are two different characters, meaning they are treated like to different characters. You wouldn't accept when looking for "Cat" getting "Ckt", so why would you expect testing for ":" would also bring up ";"?
     
  4. Offline

    cococow123

  5. Offline

    cococow123

    new code:

    Code:
    package me.cococow123.vote;
    
    import java.io.File;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    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.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    public class SignClickE implements Listener {
       
        final String pre = ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "mVote" + ChatColor.DARK_GRAY + "] ";
       
        @EventHandler
        public void onSignClick(PlayerInteractEvent e)
        {
            Player player = e.getPlayer();
            Block block = e.getClickedBlock();
            Action action = e.getAction();
    
            if (e.getClickedBlock() == null)
            {
                return;
            }
    
            if (action == Action.RIGHT_CLICK_BLOCK
                && block != null
                && (block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN))
            {
                Sign sign = (Sign) block.getState();
               
                String line1 = ChatColor.stripColor(sign.getLine(0));
                String line2 = ChatColor.stripColor(sign.getLine(1));
               
                if (line1.equals("[mVote]"))
                {
                    if (line2.contains("ID:"))
                    {
                        String[] ids = sign.getLine(1).split(": ");
                        String id = ids[1];
                       
                       
                        File idFilePath = new File("plugins" + File.separator + "mVote" + File.separator + "votes" + File.separator + id + File.separator +  id + ".yml");
                        FileConfiguration idFile  = YamlConfiguration.loadConfiguration(idFilePath);
                       
                    player.sendMessage(pre + ChatColor.YELLOW + "Hosted by1: " + ChatColor.AQUA + idFile.getString("host"));
                    player.sendMessage(pre + ChatColor.YELLOW + "Vote Poll Ends: " + ChatColor.AQUA + idFile.getString("end-date"));
                       
                    }
                    else if (line2.contains("ID;"))
                    {
                        player.sendMessage("Success!");
                            String[] ids = sign.getLine(1).split("; ");
                            String id = ids[1];
                       
                        File idFilePath = new File("plugins" + File.separator + "mVote" + File.separator + "votes" + File.separator + id + File.separator +  id + ".yml");
                        FileConfiguration idFile  = YamlConfiguration.loadConfiguration(idFilePath);
                       
                        player.sendMessage(pre + ChatColor.YELLOW + "Hosted by2: " + ChatColor.AQUA + idFile.getString("host"));
                        player.sendMessage(pre + ChatColor.YELLOW + "Vote Poll Ends: " + ChatColor.AQUA + idFile.getString("end-date"));
                       
                    }
                }
            }
        }
    }
    For some reason, its's not working when I click a sign with 'ID;' but it does work with 'ID:'
    just like before.

    I really need this to work for my plugin

    Any help is much appreciated!
     
  6. Offline

    Zombie_Striker

    @cococow123
    I hate posting code but this is too simple

    Code:
    if(line2.toUpperCase().contains("ID"){
    // IMPORTANT: I added "to upper case". This means that, no matter the case, it will check if it contains "ID" or "iD" or "id"
    if(line2.contains(":")){//Checks if it has the ":"
    //Dostuff
    if(line2.contains(";")){//checks if it has the ";"
     
  7. Offline

    WolfMage1

    Code:
    if(e.getClickedBlock() == null)
    
    why not just use your variable block? and just do block == null

    Code:
    if(action == Action.RIGHT_CLICK_BLOCK && block != null
    
    you already checked if its null? why check again

    Code:
    split(": ");
    I'd honestly just do .split(":") instead of adding the space after the colon.

    I had to write this bit out because it kept changing it to a google query :mad:

    "plugins"+File.separator+"mVote"+File.separator+"votes"+File.separator

    With the Java libraries for dealing with files, you can safely use / (slash, not backslash) on all platforms. The library code handles translating things into platform-specific paths internally.

    Code:
    FileConfiguration idFile  = YamlConfiguration.loadConfiguration(idFilePath);
    
    What if your file doesn't exist? It's going to throw a FileNotFoundException, you need to check if it does/doesn't exist. then load it.

    You do get +1 for being the first person I've seen all day who formats their code properly though :D

    EDIT:

    I hate my life ¬_¬, bukkit forums kept changing stuff in my post to google querys
     
    Last edited: Dec 23, 2015
    cococow123 and Zombie_Striker like this.
  8. Offline

    cococow123

    Thanks so much for the quick replies!

    @Zombie_Striker

    That code looks right, I'll test it tomorrow :)

    @WolfMage1

    1) I'll fix the null checks
    2) The space before the colon in the split is there because it would return " <ID>" (with a space before the id)
    3) I haven't added file checks yet. I did realize that it wasn't there though!

    4) Thanks for liking my format :)
     
  9. Offline

    WolfMage1

    I don't see why it would return a space after the id, (probably being an idiot)

    and you're welcome :p
     
  10. Offline

    cococow123

    @WolfMage1 When it made the file, it would be " <ID>" and it would create a whole other folder called " <ID>"
     
  11. Offline

    WolfMage1

    hmmm, weird.

    EDIT:

    "votes"+File.separator+ id +File.separator+ id +".yml"
    think this guys your problem for that :p

    I think you want it to be "votes/" + id + ".yml" instead of "votes/" + id + ".yml"

    your one creates a folder called <id> and in that folder it puts an id.yml with (their vote im guessing)
     
  12. Offline

    cococow123

    @WolfMage1
    I want it to make a folder. Also, I have many lines of commented code below it from previous attempts.

    Also, It's on github:
    https://github.com/cococow123/mVote

    Incase you want to see the code.

    I would like someone I could just message for a quick response instead of making threads,
    Maybe you could do that. (if you arent busy or working on something else)
    Just message me if you want to ;)
     
  13. Offline

    DoggyCode™

    I believe it was sarcasm, a darn good one!
     
  14. Offline

    Zombie_Striker

    @DoggyCode™
    That was from 21 DAYS ago! I think he figured that out by now.
     
    cococow123 and DoggyCode™ like this.
  15. Offline

    DoggyCode™

    Whoops.. stupid Tapatalk. I didn't realise.
     
  16. Offline

    cococow123

    ">
    Yup, I've realized it was sarcasm!

    @Zombie_Striker

    Your code didn't work.. :/

    I really don't know why it isn't working.
    Like before, only clicking on a sign with ":" works

    Code:
    Code:
    package me.cococow123.vote;
    
    import java.io.File;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    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.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    public class SignClick implements Listener {
      
        final String pre = ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "mVote" + ChatColor.DARK_GRAY + "] ";
      
        @EventHandler
        public void onSignClick(PlayerInteractEvent e)
        {
            Player player = e.getPlayer();
            Block block = e.getClickedBlock();
            Action action = e.getAction();
    
            if (e.getClickedBlock() == null)
            {
                return;
            }
    
            if (action == Action.RIGHT_CLICK_BLOCK
                && block != null
                && (block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN))
            {
                Sign sign = (Sign) block.getState();
              
                String line1 = ChatColor.stripColor(sign.getLine(0));
                String line2 = ChatColor.stripColor(sign.getLine(1));
              
                if (line1.equals("[mVote]"))
                {
                    if(line2.toUpperCase().contains("ID"))
                    {
                        if(line2.contains(":"))
                        {
                            String[] getId = line2.split(": ");
                            String id = getId[1];
                          
                            File idFilePath = new File("plugins" + File.separator + "mVote" + File.separator + "votes" + File.separator + id + File.separator +  id + ".yml");
                            FileConfiguration idFile  = YamlConfiguration.loadConfiguration(idFilePath);
                          
                        player.sendMessage(pre + ChatColor.YELLOW + "Hosted by1: " + ChatColor.AQUA + idFile.getString("host"));
                        player.sendMessage(pre + ChatColor.YELLOW + "Vote Poll Ends: " + ChatColor.AQUA + idFile.getString("end-date"));
                        }  
                            if(line2.contains(";"))
                            {
                                String[] getId2 = line2.split("; ");
                                String id2 = getId2[1];
                              
                                File idFilePath2 = new File("plugins" + File.separator + "mVote" + File.separator + "votes" + File.separator + id2 + File.separator +  id2 + ".yml");
                                FileConfiguration idFile2  = YamlConfiguration.loadConfiguration(idFilePath2);
                              
                            player.sendMessage(pre + ChatColor.YELLOW + "Hosted by1: " + ChatColor.AQUA + idFile2.getString("host"));
                            player.sendMessage(pre + ChatColor.YELLOW + "Vote Poll Ends: " + ChatColor.AQUA + idFile2.getString("end-date"));
                              
                          
                            }
                      
                    }
                }
            }
        }
    }
    
    Here is a snippet from creating the option sign (sign with ';' ):
    Code:
               
                   
                 else if (line2.equalsIgnoreCase("option") && !e.getLine(2).isEmpty()) {
    
                        e.setLine(0, pre); 
                        e.setLine(1, ChatColor.GRAY + "ID; " + e.getLine(2));
                        e.setLine(2,  "");
                        e.setLine(3, ChatColor.DARK_PURPLE +  e.getLine(3).toUpperCase());
    }        
    Urgh!!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 24, 2015
Thread Status:
Not open for further replies.

Share This Page