sign.setLine() Issue

Discussion in 'Spigot Plugin Development' started by bman7842, May 31, 2015.

  1. In the code below I am trying to make a Casino on a sign. In the runCasino method, it is suppose to spin through random colors and if all 3 colors are the same then you win a prize. My issue is the sign.setLine method is not working within DelayedTasks.

    Sign.setLine() works everywhere else, in fact the first few lines of the runCasino method have sign.setLine and they work just fine so I'm a little confused on why it is not working.

    There are no errors in console, in fact everything runs smoothly. Sometimes I will even be alerted in chat that I have won which is interesting and shows me that it is working.

    This is not the main class but it is the only class needed:
    Code (Text):
    package me.bman7842.eclipsecasino;

    import net.milkbowl.vault.economy.EconomyResponse;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Sign;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;

    import java.util.Random;

    /**
    * Created by brand_000 on 5/24/2015.
    */
    public class CasinoSignManager implements Listener {

        Main main;

        public CasinoSignManager(Main main)
        {
            this.main = main;
        }

        @EventHandler
        public void onSignChange(SignChangeEvent event) {
            Player p = event.getPlayer();
            String line = event.getLine(0);
            String numline = event.getLine(1);
            if (!line.contains("[casino]")) return;

            try {
                Integer.valueOf(numline);
            } catch (Exception e) {
                Utils.sendErrorMessage(p.getUniqueId(), "Invalid number, the second line of your sign should contain the price amount to play!");
                return;
            }

            if (!p.hasPermission("casino.create")) {
                Utils.sendErrorMessage(p.getUniqueId(), "You don't have permission to create a casino!");
                return;
            }
            event.setLine(0, Utils.returnCasinoSignMessage());
            Utils.sendAlertMessage(p.getUniqueId(), "The casino was successfully setup!");
        }

        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e){
            Player p = e.getPlayer();
            //Should work now go try it
            if(e.getClickedBlock().getType().equals(Material.SIGN) || e.getClickedBlock().getType().equals(Material.SIGN_POST) || e.getClickedBlock().getType().equals(Material.WALL_SIGN)){
                Sign s = (Sign) e.getClickedBlock().getState();

                if (s.getLine(0).equals(Utils.returnCasinoSignMessage())) {
                    e.setCancelled(true);
                    System.out.println("Interacted");
                    if (Utils.returnActiveCasino().contains(e.getClickedBlock()))
                    {
                        Utils.sendErrorMessage(p.getUniqueId(), "Someone is already using the casino, please wait or find another casino!");
                        return;
                    }

    //                try {
    //                    EconomyResponse r = Main.econ.withdrawPlayer(p.getName(), Integer.parseInt(s.getLine(1)));
    //                    if (r.transactionSuccess())
    //                    {
    //                        Utils.sendAlertMessage(p.getUniqueId(), "Your transaction was successful, beginning to spin the slot machine!");
    //                        Utils.addActiveCasino(e.getClickedBlock());
    //                        runCasino(s, e.getPlayer());
    //                    } else {
    //                        Utils.sendErrorMessage(p.getUniqueId(), "You have insufficient funds!");
    //                    }
    //                } catch (Exception exception) {
    //                    Utils.sendErrorMessage(p.getUniqueId(), "Errrmm, it appears this sign somehow was incorrectly setup, contact an administrator!");
    //                }
                    runCasino(s, e.getPlayer());
                }
            }
        }

        public void runCasino(Sign sign, Player p) {
            final Sign nSign = sign;
            final Player pp = p;
            nSign.setLine(2, ChatColor.RED + "" + ChatColor.BOLD + "ACTIVE:");
            nSign.setLine(3, ChatColor.WHITE + "? ? ?");
            nSign.update();
            Random rn = new Random();
            int numberone = rn.nextInt(7) + 1;
            int waitTime = 20;
            for (int i = 1; i == numberone*3; i++) {
                final int num = i;
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
                    public void run() {
                        nSign.setLine(3, numberToColor(num, "?") + ChatColor.WHITE + " ? " + ChatColor.WHITE + "?");
                        pp.sendMessage(numberToColor(num, "?") + ChatColor.WHITE + " ? " + ChatColor.WHITE + "?");
                        nSign.update();
                    }
                }, waitTime*i);
            }
            Random rn2 = new Random();
            int numbertwo = rn2.nextInt(7) + 1;
            for (int i = 1; i == numbertwo*3; i++) {
                final int num = i;
                final int firstnum = numberone;
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
                    public void run() {
                        nSign.setLine(3, numberToColor(firstnum, "?") + numberToColor(num, "?") + ChatColor.WHITE + " ?");
                        pp.sendMessage(numberToColor(firstnum, "?") + numberToColor(num, "?") + ChatColor.WHITE + " ?");
                        nSign.update();
                    }
                }, waitTime*i);
            }
            Random rn3 = new Random();
            int numberthree = rn3.nextInt(7) + 1;
            for (int i = 1; i == numberthree*3; i++) {
                final int num = i;
                final int firstnum = numberone;
                final int secondnum = numbertwo;
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
                    public void run() {
                        nSign.setLine(3, numberToColor(firstnum, "?") + numberToColor(secondnum, "?") + numberToColor(num, "?"));
                        pp.sendMessage(numberToColor(firstnum, "?") + numberToColor(secondnum, "?") + numberToColor(num, "?"));
                        nSign.update();
                    }
                }, waitTime*i);
            }

            if (numberone == numbertwo && numbertwo == numberthree)
            {
                Utils.sendAlertMessage(p.getUniqueId(),"You win!!!");
                //Add rewards
            }
            //Reset Sign
        }

        public static String numberToColor(Integer num, String string)
        {
            if (num == 1) {
                return (ChatColor.RED + string);
            } else if (num == 2) {
                return (ChatColor.YELLOW + string);
            } else if (num == 3) {
                return (ChatColor.GREEN + string);
            } else if (num == 4) {
                return (ChatColor.BLUE + string);
            } else if (num == 5) {
                return (ChatColor.DARK_PURPLE + string);
            } else if (num == 6) {
                return (ChatColor.BLACK + string);
            } else if (num == 7) {
                return (ChatColor.AQUA + string);
            } else if (num == 8) {
                return (ChatColor.WHITE + string);
            }
            return (ChatColor.WHITE + string);
        }
    }
     
     
  2. Trying removing final from nSign then in the run declare a variable which is = sign (Don't make it final) then edit it from there
     
  3. When I do this and set the new Variable equal to sign it says that sign needs to be final for it to be used.
    Code (Text):
    for (int i = 1; i == numberone*3; i++) {
                final int num = i;
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
                    public void run() {
                        Sign nsign = sign;
                        nsign.setLine(3, numberToColor(num, "?") + ChatColor.WHITE + " ? " + ChatColor.WHITE + "?");
                        //pp.sendMessage(numberToColor(num, "?") + ChatColor.WHITE + " ? " + ChatColor.WHITE + "?");
                        nsign.update();
                    }
                }, waitTime*i);
            }
     
  4. Instead of using a sign object you might want to try passing the block object instead and doing the conversion every time you want to update the sign.
     
  5. Here is the new code but it still is not working.
    Code (Text):
    package me.bman7842.eclipsecasino;

    import net.milkbowl.vault.economy.EconomyResponse;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;

    import java.util.Random;

    /**
    * Created by brand_000 on 5/24/2015.
    */
    public class CasinoSignManager implements Listener {

        Main main;

        public CasinoSignManager(Main main)
        {
            this.main = main;
        }

        @EventHandler
        public void onSignChange(SignChangeEvent event) {
            Player p = event.getPlayer();
            String line = event.getLine(0);
            String numline = event.getLine(1);
            if (!line.contains("[casino]")) return;

            try {
                Integer.valueOf(numline);
            } catch (Exception e) {
                Utils.sendErrorMessage(p.getUniqueId(), "Invalid number, the second line of your sign should contain the price amount to play!");
                return;
            }

            if (!p.hasPermission("casino.create")) {
                Utils.sendErrorMessage(p.getUniqueId(), "You don't have permission to create a casino!");
                return;
            }
            event.setLine(0, Utils.returnCasinoSignMessage());
            Utils.sendAlertMessage(p.getUniqueId(), "The casino was successfully setup!");
        }

        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e){
            Player p = e.getPlayer();
            //Should work now go try it
            if(e.getClickedBlock().getType().equals(Material.SIGN) || e.getClickedBlock().getType().equals(Material.SIGN_POST) || e.getClickedBlock().getType().equals(Material.WALL_SIGN)){
                Sign s = (Sign) e.getClickedBlock().getState();

                if (s.getLine(0).equals(Utils.returnCasinoSignMessage())) {
                    e.setCancelled(true);
                    System.out.println("Interacted");
                    if (Utils.returnActiveCasino().contains(e.getClickedBlock()))
                    {
                        Utils.sendErrorMessage(p.getUniqueId(), "Someone is already using the casino, please wait or find another casino!");
                        return;
                    }

    //                try {
    //                    EconomyResponse r = Main.econ.withdrawPlayer(p.getName(), Integer.parseInt(s.getLine(1)));
    //                    if (r.transactionSuccess())
    //                    {
    //                        Utils.sendAlertMessage(p.getUniqueId(), "Your transaction was successful, beginning to spin the slot machine!");
    //                        Utils.addActiveCasino(e.getClickedBlock());
    //                        runCasino(s, e.getPlayer());
    //                    } else {
    //                        Utils.sendErrorMessage(p.getUniqueId(), "You have insufficient funds!");
    //                    }
    //                } catch (Exception exception) {
    //                    Utils.sendErrorMessage(p.getUniqueId(), "Errrmm, it appears this sign somehow was incorrectly setup, contact an administrator!");
    //                }
                    runCasino(e.getClickedBlock(), e.getPlayer());
                }
            }
        }

        public void runCasino(Block block, Player p) {
            Sign sign = (Sign) block.getState();
            sign.setLine(2, ChatColor.RED + "" + ChatColor.BOLD + "ACTIVE:");
            sign.setLine(3, ChatColor.WHITE + "? ? ?");
            sign.update();
            Random rn = new Random();
            int numberone = rn.nextInt(7) + 1;
            int waitTime = 20;
            for (int i = 1; i == numberone*3; i++) {
                final int num = i;
                final Block nBlock = block;
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
                    public void run() {
                        Sign nsign = (Sign)nBlock.getState();
                        nsign.setLine(3, numberToColor(num, "?") + ChatColor.WHITE + " ? " + ChatColor.WHITE + "?");
                        //pp.sendMessage(numberToColor(num, "?") + ChatColor.WHITE + " ? " + ChatColor.WHITE + "?");
                        nsign.update();
                    }
                }, waitTime*i);
            }
            Random rn2 = new Random();
            int numbertwo = rn2.nextInt(7) + 1;
            for (int i = 1; i == numbertwo*3; i++) {
                final int num = i;
                final int firstnum = numberone;
                final Block nBlock = block;
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
                    public void run() {
                        Sign nsign = (Sign)nBlock.getState();
                        nsign.setLine(3, numberToColor(firstnum, "?") + numberToColor(num, "?") + ChatColor.WHITE + " ?");
                        //pp.sendMessage(numberToColor(firstnum, "?") + numberToColor(num, "?") + ChatColor.WHITE + " ?");
                        nsign.update();
                    }
                }, waitTime*i);
            }
            Random rn3 = new Random();
            int numberthree = rn3.nextInt(7) + 1;
            for (int i = 1; i == numberthree*3; i++) {
                final int num = i;
                final int firstnum = numberone;
                final int secondnum = numbertwo;
                final Block nBlock = block;
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
                    public void run() {
                        Sign nsign = (Sign)nBlock.getState();
                        nsign.setLine(3, numberToColor(firstnum, "?") + numberToColor(secondnum, "?") + numberToColor(num, "?"));
                        //pp.sendMessage(numberToColor(firstnum, "?") + numberToColor(secondnum, "?") + numberToColor(num, "?"));
                        nsign.update();
                    }
                }, waitTime*i);
            }

            if (numberone == numbertwo && numbertwo == numberthree)
            {
                Utils.sendAlertMessage(p.getUniqueId(),"You win!!!");
                //Add rewards
            }
            //Reset Sign
        }

        public static String numberToColor(Integer num, String string)
        {
            if (num == 1) {
                return (ChatColor.RED + string);
            } else if (num == 2) {
                return (ChatColor.YELLOW + string);
            } else if (num == 3) {
                return (ChatColor.GREEN + string);
            } else if (num == 4) {
                return (ChatColor.BLUE + string);
            } else if (num == 5) {
                return (ChatColor.DARK_PURPLE + string);
            } else if (num == 6) {
                return (ChatColor.BLACK + string);
            } else if (num == 7) {
                return (ChatColor.AQUA + string);
            } else if (num == 8) {
                return (ChatColor.WHITE + string);
            }
            return (ChatColor.WHITE + string);
        }
    }