Handling a "Round" for a plugin

Discussion in 'Plugin Development' started by Jombi, Dec 12, 2012.

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

    Jombi

    We've seen a couple PVP/Hungergames/etc plugins out there that are automated, but how do the rounds work? I'm trying to figure out if they're based off of custom events that are triggered, or a global boolean is simply switched...

    Seems like the easiest way would be to create an event and trigger it every time you want the round to change from start/stop and vis versa. I've read the API on custom event and am a little lost on what to do for this particular situation.

    How would I create the event so its callable from anywhere in my plugin? How would I trigger the event so the listener can catch it?

    Any help is appreciated =/
     
  2. Offline

    Rprrr

    What exactly do you mean by the rounds? Like how the game starts and stops?
     
  3. Offline

    Jombi

    Ah, sorry didn't explain it enough :p

    Yes, how the specific game starts and stops.
     
  4. Offline

    Rprrr

    Jombi
    Well, for example, a Hunger Games plugins starts when an admin executes a certain command or when there are enough votes to start a game. You could also start a timer when someone executes a game start command, and when the timer's over, you use the code that starts the game.

    A game like the Hunger Games ends when there's only one tribute left, or for example when there's no time left. You could add people to HashMap's when the game starts, and remove them when they die. Everytime someone dies, you just check if there's only one key remaining in that HashMap.

    That's kinda how you could get it working, but it can be different for other types of games.
     
  5. Offline

    Jombi

    Hmm, I'll try it and report back.
     
  6. Offline

    Rprrr

    Jombi
    Okay. :)

    I suggest making a stopGame() and startGame() function, and then just think about when it has to be used. :) For my Crown Conquest plugin, I stop the game everytime a player gets to the end block with a crown, or when there's only one team left.

    Good luck, I hope I was of any help.
     
  7. Offline

    Jombi

    On a similar note:

    I am getting an NPE for this code:
    Code:
    package me.jimbo.plugin;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    
    public class PlayerJoinListener implements Listener {
    
        @SuppressWarnings("unused")
        private CTF plugin;
        private Countdown countdown;
        
        public PlayerJoinListener (CTF plugin) {
            this.plugin = plugin;
        }
        
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            player.sendMessage("Joined");
            if (!CTF.AllPlayers.contains(player)){
                CTF.AllPlayers.add(player);
            }
            while (!countdown.getProgress()) {
                countdown.setProgress(true);
                countdown.startTimer();
            }
        }
    
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerQuit(PlayerQuitEvent e) {
            //CTF.AllPlayers.remove(CTF.AllPlayers.indexOf(e.getPlayer()));
        }
    }
    
    The NPE is thrown at "while (!countdown.getProgress()) {". Here is the class it is attempting to call:
    Code:
    package me.jimbo.plugin;
    
    public class Countdown {
    
        private boolean inProgress = false;
        
        private CTF plugin;
        
        public Countdown (CTF plugin) {
            this.plugin = plugin;
        }
        
        public void setProgress(boolean bln) {
            inProgress = bln;
        }
        
        public boolean getProgress() {
            return inProgress;
        }    
        
        public void startTimer() {
            int taskID = plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() {
                public void run() {
                    plugin.getServer().broadcastMessage("____________________________________________________");
                }
            }, 20L, 500L);
            
            if (!inProgress) {
                //cancel the countdown task
                plugin.getServer().getScheduler().cancelTask(taskID);
            }
        }
    }
    
    I'm not really sure why its throwing that.
     
  8. Offline

    fireblast709

    countdown is undefined
     
  9. Offline

    Jombi

    Thanks, fixed and that particular part works now!

    As for the countdown. Which would be better, an async repeating task that counts down from 25 and cancels when it hits 0, or a for loop to count down? If I use a for loop, I can't put the thread to sleep because then nobody can join the server during the countdown. Unless there is another way I'm not aware of.
     
  10. Offline

    MisterErwin

    Don't use while ... use if and a thread or the server will freeze!
     
  11. Offline

    fireblast709

    Jombi Just use a sync delayed task
    Code:java
    1. new BukkitRunnable()
    2. {
    3. @Override
    4. public void run()
    5. {
    6. // Do whatever
    7. }
    8. }.runTaskLater(<plugin instance>, 20L*delay_in_seconds);
     
    Jombi likes this.
Thread Status:
Not open for further replies.

Share This Page