Problem by updating a sign

Discussion in 'Spigot Plugin Development' started by goebel123, May 4, 2015.

  1. Hello peeps,
    currently I'm trying to update a sign by a normal java method. The method gets fired by the SignChangeEvent.
    Here's the code:
    Code (Text):
    @EventHandler
        public void onSignChange(SignChangeEvent e) {
            Player p = e.getPlayer();
           
            if (e.getLine(0).equalsIgnoreCase("kostenlos")) {
                if (p.hasPermission(freesigns.getConfig().getString("freesigns.permissions.erstellen"))) {
                    int maxvalue = Integer.parseInt(freesigns.getConfig().getString("freesigns.signs.maxvalue"));
                   
                    maxvalue = maxvalue +1;
                    freesigns.getConfig().set("freesigns.signs.maxvalue", maxvalue);
                   
                    Double x = e.getBlock().getLocation().getX();
                    Double y = e.getBlock().getLocation().getY();
                    Double z = e.getBlock().getLocation().getZ();
                   
                    freesigns.getConfig().set("freesigns.signs." + maxvalue + ".locs.x", x);
                    freesigns.getConfig().set("freesigns.signs." + maxvalue + ".locs.y", y);
                    freesigns.getConfig().set("freesigns.signs." + maxvalue + ".locs.z", z);
                   
                    String id = e.getLine(1);
                   
                    freesigns.getConfig().set("freesigns.signs." + maxvalue + ".itemId", id);
                    freesigns.getConfig().set("freesigns.signs." + maxvalue + ".loaded", false);
                    freesigns.getConfig().set("freesigns.signs." + maxvalue + ".paused", false);
                   
                   
                    freesigns.saveConfig();
                    freesigns.reloadSignsList();
                }
            }
        }
    And the method:
    Code (Text):
    public void reloadSignsList() {
            System.out.println(1);
            int maxvalue = Integer.parseInt(this.getConfig().getString("freesigns.signs.maxvalue"));
            Sign sign = null;
           
            if (maxvalue != 0) {
                System.out.println(2);
                for (int i = 1; i <= maxvalue; i++) {
                    System.out.println(3);
                    if (!this.getConfig().getBoolean("freesigns.signs." + i + ".loaded")) {
                        System.out.println(4);
                        if (!this.getConfig().getBoolean("freesigns.signs." + i + ".paused")) {
                            System.out.println(5);
                            Double x = Double.parseDouble(this.getConfig().getString("freesigns.signs." + i + ".locs.x"));
                            Double y = Double.parseDouble(this.getConfig().getString("freesigns.signs." + i + ".locs.y"));
                            Double z = Double.parseDouble(this.getConfig().getString("freesigns.signs." + i + ".locs.z"));
                           
                            Location loc = new Location(Bukkit.getWorld(this.getConfig().getString("freesigns.defaultworld")), x, y, z);
                            Block b = loc.getBlock();
                           
                            if (b.getState() instanceof Sign) {
                                System.out.println(6);
                                sign = (Sign) loc.getBlock().getState();
                               
                                System.out.println(sign.getLocation());
                                   
                                sign.setLine(0, "§0[§cKostenlos§0]");
                                sign.setLine(1, ChatColor.RED + this.getConfig().getString("freesigns.signs." + i + ".itemId"));
                                sign.update();
                               
                                this.getConfig().set("freesigns.signs." + i + ".loaded", true);
                                this.saveConfig();
                               
                                System.out.println(sign.getLine(0));
                            }                      
                        }                                      
                    } else {
                        System.out.println("Schild " + i + "bereits geladen, daher übersprungen.");
                    }
                }
            } else {
                System.err.println("Keine Schilder vorhanden, daher wurde die SignsList nicht gereloaded.");
                return;
            }
        }
    I guess you already saw the debug code, so here is the consolelog:
    [​IMG]
    And finally the plugin config:
    [​IMG]
    The problem is that the sign doesn't update its text. I'm using the 1.8 Spigot version, however the version ain't the problem, cause another pl does update the signs well. I also tried to use sign.update(true); or b.getState().update(); etc. I hope you can help me solving this bug.
     
  2. Vik1395

    Supporter

    Code (Text):
    System.out.println(sign.getLine(0));
    Try using the command on top before and after you change the lines. What does it print?
     
  3. after changing it says the same stuff like above and before "kostenlos"
     
  4. have you tried doing something like:

    Code (Text):
    new BukkitRunnable() {
        public void run() {
            freesigns.reloadSignsList();
        }
    }.runTask(this.plugin);
    (ofc, you need to replace "this.plugin" with your plugin's instance)
    instead of just doing:

    Code (Text):
    freesigns.reloadSignsList();
    ??

    my guess is you tried to run
    Code (Text):
    freesigns.reloadSignsList();
    but after you invoke that, onSignChagne() method finishes and sign information that is held in SighChangeEvent (e) will be used to update the sign again... so you put the original sign back on the sign..

    So, what I put up there is to schedule
    Code (Text):
    freesigns.reloadSignsList();
    to be executed after onSignChange() method is completed.
     
  5. Vik1395

    Supporter

    If it is just one line that you need to change, you can try returning that particular string to the onSignChangeEvent and then just use
    Code (Text):
    e.setLine(1, newstring);
    Its also safer to use ChatColor.x instead of §x