Solved SignChangeEvent Error

Discussion in 'Plugin Development' started by BrickBoy55, Apr 26, 2015.

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

    BrickBoy55

    I'm having trouble with my event. It's all going fine, but it is still sending a message, even if the sign has no text. (It is mainly sending the message on line 55)

    Code:
    Code:
    package com.mayhem.protect.events;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    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.SignChangeEvent;
    
    import com.mayhem.protect.Protect;
    import com.mayhem.protect.api.ProtectAPI;
    
    public class SignChange implements Listener {
        private ProtectAPI protect = new ProtectAPI();
      
        @EventHandler
        public void onSignChange(SignChangeEvent event) {
            String prefix = ChatColor.translateAlternateColorCodes('&', Protect.plugin.getConfig().getString("prefix"));
            Player player = event.getPlayer();
            int radius = 50;
            Location location = player.getLocation();
          
            if (event.getLine(1).equals("[PROTECT]")) {
                if (protect.getProtectFolder(event.getLine(2)).exists()) {
                    player.sendMessage(prefix + "A protected area with that id already exists!");
                    return;
                } else {
                    event.setLine(1, "§8[§9Protected§8]");
                    event.setLine(2, "§4" + event.getLine(2));
                  
                    protect.addClaim(player, ChatColor.stripColor(event.getLine(2)));
                  
                    player.sendMessage(prefix + "Area protected!");
                    return;
                }
            }
    
            for (int x = (int) (location.getX() - radius); x < location.getX() + radius; x++) {
                for (int y = (int) (location.getY() - radius); y < location.getY() + radius; y++) {
                    for (int z = (int) (location.getZ() - radius); z < location.getZ() + radius; z++) {
    
                        Block block = player.getWorld().getBlockAt(x, y, z);
    
                        if (block.getState() instanceof Sign) {
                            Sign sign = (Sign) block.getState();
    
                            if (sign.getLine(1).equals("§8[§9Protected§8]")) {
                                FileConfiguration config = YamlConfiguration.loadConfiguration(protect.getProtectFolder(sign.getLine(2)));
                                if (!player.getUniqueId().toString().equals(config.getString(sign.getLine(2) + ".owner"))) {
                                    player.sendMessage(prefix + "You are too close to " + config.getString(ChatColor.stripColor(sign.getLine(2)) + ".name") + "'s land to protect!");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    The event is registered, and there is no stack trace. Help appreciated!
     
  2. Offline

    Zombie_Striker

    Why are you getting a chatcolor from the config?

    Divide the string you want into sections and test them for null errors separately.
     
  3. Offline

    meguy26

    @BrickBoy55
    Im not sure if this will fix your problem, but signs have lines like an array has values, the first line of a sign is actually 0:
    Code:
    //gets the FIRST line on the sign
    Sign.getLine(0);
    
    //gets the SECOND line on the sign
    Sign.getLine(1);
    
    //gets the THIRD line on the sign
    Sign.getLine(2);
    
    //gets the FOURTH line on the sign
    Sign.getLine(3);
    give me a second to look over the rest of your code and ill edit in a solution if i can come up with one.

    EDIT:
    @BrickBoy55
    it looks like your code would work, however you check if a sign is too close to protect on every sign change event, put the for loops within the if statement:
    Example Code (please try to figure out what Im saying on your own before opening this) (open)

    Code:
        package com.mayhem.protect.events;
       
        import org.bukkit.ChatColor;
        import org.bukkit.Location;
        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.SignChangeEvent;
       
        import com.mayhem.protect.Protect;
        import com.mayhem.protect.api.ProtectAPI;
        public class SignChange implements Listener {
            private ProtectAPI protect = new ProtectAPI();
       
            @EventHandler
            public void onSignChange(SignChangeEvent event) {
                String prefix = ChatColor.translateAlternateColorCodes('&', Protect.plugin.getConfig().getString("prefix"));
                Player player = event.getPlayer();
                int radius = 50;
                Location location = player.getLocation();
           
                if (event.getLine(0).equals("[PROTECT]")) {
                    boolean notProtectable = false;
                    for (int x = (int) (location.getX() - radius); x < location.getX() + radius; x++) {
                        for (int y = (int) (location.getY() - radius); y < location.getY() + radius; y++) {
                            for (int z = (int) (location.getZ() - radius); z < location.getZ() + radius; z++) {
           
                                Block block = player.getWorld().getBlockAt(x, y, z);
           
                                if (block.getState() instanceof Sign) {
                                    Sign sign = (Sign) block.getState();
           
                                    if (sign.getLine(1).equals("§8[§9Protected§8]")) {
                                        FileConfiguration config = YamlConfiguration.loadConfiguration(protect.getProtectFolder(sign.getLine(2)));
                                        if (!player.getUniqueId().toString().equals(config.getString(sign.getLine(2) + ".owner"))) {
                                            player.sendMessage(prefix + "You are too close to " + config.getString(ChatColor.stripColor(sign.getLine(2)) + ".name") + "'s land to protect!");
                                            notProtectable = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if(!notProtectable){
                        if (protect.getProtectFolder(event.getLine(1)).exists()) {
                            player.sendMessage(prefix + "A protected area with that id already exists!");
                            return;
                        } else {
                            event.setLine(0, "§8[§9Protected§8]");
                            event.setLine(1, "§4" + event.getLine(1));
                       
                            protect.addClaim(player, ChatColor.stripColor(event.getLine(1)));
                       
                            player.sendMessage(prefix + "Area protected!");
                            return;
                        }
                    }
                }
            }
        }
     
    Last edited: Apr 26, 2015
  4. Offline

    BrickBoy55

    @meguy26

    I understand that you think that the arrays are messed up, but they aren't. I purposely have them in the middle lines of the sign.
     
  5. @BrickBoy55 As @meguy26 says, this is expected behaviour. You code sends that message to anyone placing any sort of sign (regardless of text) if they're within a 50 block radius of a 'protected' sign. Indentation is there to help you, not just to look nice.
     
  6. Offline

    BrickBoy55

    @AdamQpzm

    Yes, I understand, but how do I make it stop messaging people?

    I managed to get my own answer, it's not ideal, but it works. (I put the for loop in the check statement.)

    Solved.

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

Share This Page