Solved Timers causing thread issues

Discussion in 'Plugin Development' started by soulofw0lf, May 1, 2013.

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

    soulofw0lf

    it's my first time messing around with a timed event at all and i'm getting some massive issues from it, it counts down fine, but then completely errors out at the end.
    here's the error:
    Code:
    30.04 23:22:02 [Server] SEVERE at java.lang.Thread.run(Thread.java:722)
    30.04 23:22:02 [Server] SEVERE at com.vartala.soulofw0lf.rpglobby.LobbyTimer.run(LobbyTimer.java:46)
    30.04 23:22:02 [Server] SEVERE at com.vartala.soulofw0lf.rpglobby.enterLobby.<init>(enterLobby.java:16)
    30.04 23:22:02 [Server] SEVERE java.lang.NullPointerException
    30.04 23:22:02 [Server] SEVERE Exception in thread "Thread-22" 

    Here's the timer
    Code:
    public class LobbyTimer implements Runnable {
        Player player;
        Location loc;
        String lobbyname;
        RpgLobby Rpgl;
        public LobbyTimer(RpgLobby rpgl){
            this.Rpgl = rpgl;
        }
       
        public LobbyTimer(Player p, Location loc, String lobbyname){
            this.player = p;
            this.loc = loc;
            this.lobbyname = lobbyname;
        }
     
        @Override
        public void run(){
            try{
                int count = 8;
                while( count > 0 ){
                    player.sendMessage( "Wait " + count + " Seconds." );
     
                    if (player.getLocation().getX() != this.loc.getX()){
                        player.sendMessage("Cancelled entering lobby, don't move!");
                        return;
                    }
                    if (player.getLocation().getZ() != this.loc.getZ()){
                        player.sendMessage("Cancelled entering lobby, don't move!");
                        return;
                    }
     
                    Thread.sleep( 1000 );
                    count--;
                }
                String playername = player.getName();
                new enterLobby(playername, lobbyname);
            } catch (InterruptedException e) {
            }
        }
    here's how i'm starting it
    Code:
    LobbyTimer lt = new LobbyTimer(player, newloc, args[1]);
                new Thread(lt).start();
    and here's the line it's bugging out on
    Code:
    this.Rpgl.getConfig().set(p.getName() + ".X", p.getLocation().getX());
    i don't think i like timers very much~
     
  2. Offline

    CubieX

    Please use a synchronized Bukkit scheduler.
    Otherwise you are accessing the Bukkit API from a not-synchronized task, which will cause errors.

    Bukkit.getServer().getScheduler().runTaskTimer(...). --> repeating task
    Bukkit.getServer().getScheduler().runTask(...). --> 1-shot task on next tick
    Bukkit.getServer().getScheduler().runTaskLater(...). --> delayed task, 1-shot
     
  3. Offline

    soulofw0lf

    CubieX is there a good guide or tutorial out there for learning how to use the scheduler?
     
  4. Offline

    TheE

    The wiki contains some examples that should help you. It is pretty straight forward.
     
  5. Offline

    soulofw0lf

    Yeah I use the wiki alot, i've just been trying to make it work from what i've read on that page all morning with no avail so was hoping there was a tut out there somewhere. thank you anyway :)

    ok i'm guessing i'm making some horrendously simplistic error.. i can't even get it to start the scheduled event, no matter what i try is either setting the plugin as null or giving me a null pointer exception.
    here's what i have for my EnterLobby class no matter how i try setting up the plugin in the line.
    Code:
    Bukkit.getServer().getScheduler().runTaskTimer(Rpgl.getPlugin(), new Thread(new Runnable(){
    i get a plugin cannot be null or a null pointer exception. Exactly how dumb am i being right now?

    Code:
    public class EnterLobby{
        RpgLobby Rpgl;
       
        public EnterLobby(RpgLobby rpgl){
            this.Rpgl = rpgl;
       
        }
     
        public EnterLobby(Player p, Location loc1, String lobbyname){
            final Player player = p;
            final Location loc = loc1;
            final String name = lobbyname;
            Bukkit.getServer().getScheduler().runTaskTimer(Rpgl.getPlugin(), new Thread(new Runnable(){
                @Override
                public void run(){
                    int count = 8;
                    player.sendMessage( "Wait " + count + " Seconds." );
                    if (player.getLocation().getX() != loc.getX()){
                        player.sendMessage("Cancelled entering lobby, don't move!");
                        return;
                    }
                    if (player.getLocation().getZ() != loc.getZ()){
                        player.sendMessage("Cancelled entering lobby, don't move!");
                        return;
                    }
                    count--;
                    if (count == 0){
                        Player p = player;
                        Rpgl.getConfig().set(p.getName() + ".X", p.getLocation().getX());
                        Rpgl.getConfig().set(p.getName() + ".Y", p.getLocation().getY());
                        Rpgl.getConfig().set(p.getName() + ".Z", p.getLocation().getZ());
                        Rpgl.getConfig().set(p.getName() + ".Pitch", p.getLocation().getPitch());
                        Rpgl.getConfig().set(p.getName() + ".Yaw", p.getLocation().getYaw());
                        Rpgl.getConfig().set(p.getName() + ".World", p.getLocation().getWorld().getName());
                        Rpgl.getConfig().set(p.getName() + ".Health", p.getHealth());
                        PlayerInventory inventory = p.getInventory();
                        PlayerInventory armorinv = p.getInventory();
                        InventoryUtil.getContentInventory(inventory);
                        InventoryUtil.getArmorInventory(armorinv);
                        Rpgl.saveInv(inventory, p);
                        p.getInventory().clear();
                        p.getInventory().setHelmet(null);
                        p.getInventory().setChestplate(null);
                        p.getInventory().setLeggings(null);
                        p.getInventory().setBoots(null);
                        Rpgl.saveConfig();
                        World Lobby = Bukkit.getServer().getWorld(Rpgl.getConfig().getString("Lobby." + name + ".World"));
                        String pitch = Rpgl.getConfig().getString("Lobby." + name + ".Pitch");
                        String yaw = Rpgl.getConfig().getString("Lobby." + name + ".Yaw");
                        Float newpitch = Float.parseFloat(pitch);
                        Float newyaw = Float.parseFloat(yaw);
                        Location Spawn = new Location(Lobby, Rpgl.getConfig().getDouble("Lobby." + name + ".X"), Rpgl.getConfig().getDouble("Lobby." + name + ".Y"), Rpgl.getConfig().getDouble("Lobby." + name + ".Z"), newyaw, newpitch);
                        p.teleport(Spawn);
                        Integer healling = p.getMaxHealth();
                        p.setHealth(healling);
                        return;
                    }
                }
            }), (20), 8*20);
        }
     
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  6. soulofw0lf
    Rpgl is null.

    Because you're using the other constructor to asign stuff and in that instance the Rpgl field is not asigned.
     
  7. Offline

    Comphenix

    I recommend you set every field in your class to final unless you absolutely must change them later. That way, the compiler will complain if you forget to assign to them in the constructor.
     
  8. Offline

    soulofw0lf

    thanks digi i actually recently figured that out, and have it running in game, only thing i can't get working now is cancelling it, i can't seem to get the task id? here's the new code after all my fiddling around (no current attempt to cancel in it)
    Code:
    public void enterLobby(Player p, Location loc1, String lobbyname){
            final Player player = p;
            final Location loc = loc1;
            final String name = lobbyname;
           
            Bukkit.getServer().getScheduler().runTaskTimer(plugin, new Thread(new Runnable(){
                   
               
                int count = 8;
               
                @Override
                public void run(){
                   
                   
                       
                    player.sendMessage( "Wait " + count + " Seconds." );
                    count--;
                    if (player.getLocation().getX() != loc.getX()){
                        player.sendMessage("Cancelled entering lobby, don't move!");
                       
                        return;
                    }
                    if (player.getLocation().getZ() != loc.getZ()){
                        player.sendMessage("Cancelled entering lobby, don't move!");
                       
                        return;
                       
                        }
                    if (count == 0){
                        Player p = player;
                        getConfig().set(p.getName() + ".X", p.getLocation().getX());
                        getConfig().set(p.getName() + ".Y", p.getLocation().getY());
                        getConfig().set(p.getName() + ".Z", p.getLocation().getZ());
                        getConfig().set(p.getName() + ".Pitch", p.getLocation().getPitch());
                        getConfig().set(p.getName() + ".Yaw", p.getLocation().getYaw());
                        getConfig().set(p.getName() + ".World", p.getLocation().getWorld().getName());
                        getConfig().set(p.getName() + ".Health", p.getHealth());
                        PlayerInventory inventory = p.getInventory();
                        PlayerInventory armorinv = p.getInventory();
                        InventoryUtil.getContentInventory(inventory);
                        InventoryUtil.getArmorInventory(armorinv);
                        saveInv(inventory, p);
                        p.getInventory().clear();
                        p.getInventory().setHelmet(null);
                        p.getInventory().setChestplate(null);
                        p.getInventory().setLeggings(null);
                        p.getInventory().setBoots(null);
                        saveConfig();
                        World Lobby = Bukkit.getServer().getWorld(getConfig().getString("Lobby." + name + ".World"));
                        String pitch = getConfig().getString("Lobby." + name + ".Pitch");
                        String yaw = getConfig().getString("Lobby." + name + ".Yaw");
                        Float newpitch = Float.parseFloat(pitch);
                        Float newyaw = Float.parseFloat(yaw);
                        Location Spawn = new Location(Lobby, getConfig().getDouble("Lobby." + name + ".X"), getConfig().getDouble("Lobby." + name + ".Y"), getConfig().getDouble("Lobby." + name + ".Z"), newyaw, newpitch);
                        p.teleport(Spawn);
                        Integer healling = p.getMaxHealth();
                        p.setHealth(healling);
                       
                        return;
                    }
                }
            }), (20), 20);
        }
    i changed it to a BukkitRunnable and got it working, thanks for the input everyone!

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

Share This Page