Solved Unsquashable bug

Discussion in 'Plugin Development' started by xTrollxDudex, Nov 11, 2013.

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

    xTrollxDudex

    Sorry for the formatting, the tabs and spaces mix together...

    Alright, here's the prob:
    Whenever Arena#game() is executed by the command handler, the messages all show TWICE. I'll try to show some pics tomorrow, but for now it's like:

    [BattleDome][BattleDome]starts in 10 seconds, prepare for battle!
    [BattleDome][BattleDome]starts in 10 seconds, prepare for battle!
    [BattleDome][BattleDome]starts in 9 seconds, prepare for battle!
    [BattleDome][BattleDome]starts in 9 seconds, prepare for battle!

    Here's all the code:
    PHP:
    //Command
            
    if(cmd.equalsIgnoreCase("start")){
                if(
    ErrorHandler.getEh().validateCmd(pargs.length1"bd.start")){
                    
    int i 0;
                    try{
                        
    Integer.parseInt(args[0]);
                    }catch (
    NumberFormatException e){
                        
    p.sendMessage(ErrorHandler.getEh().error "Invalid arena ID");
                    }
                    
    Arena a BattleManager.getBM().getArena(i);

                    if(
    != null){
                        if(
    a.getState().equals(Arena.Game.LOBBY)){
                            
    a.game();
                            
    p.sendMessage(BattleManager.getBM().bd "Starting Arena #" a.getId());
                        }else{
                            
    p.sendMessage(ErrorHandler.getEh().error "This arena has already started!");
                        }
                    }else{
                        
    p.sendMessage(ErrorHandler.getEh().error "Invalid arena!");
                    }
                }
                return;
            }
    PHP:
    //Arena#game() / start() / broadcast()
        
    public void game() {
            for(
    String s players){
                
    Player p Bukkit.getPlayer(s);
                if(
    == null){
                    return;
                }
                
    MoveListener.getSet().add(s);
            }

            
    Count c = new Count(10thisfalse);

            
    c.setId(Bukkit.getScheduler().scheduleSyncRepeatingTask(pluginc0L20L));
        }


        public 
    void lobby(int i) {
            
    Count c = new Count(ithistrue);

            
    c.setId(Bukkit.getScheduler().scheduleSyncRepeatingTask(pluginc0L5*60L*20L));

        }

        public 
    void start(){
            
    rb.saveWorld(redSpawn.getWorld());
            for(
    String s players){
                
    Player p Bukkit.getPlayer(s);
                if(
    == null){
                    return;
                }
                
    MoveListener.getSet().remove(s);
            }

            
    setState(Game.BUILD);
            
    Countdown c = new Countdown(45this);

            
    Bukkit.getScheduler().scheduleSyncRepeatingTask(pluginc0L5L*60L*20L);
        }

        public 
    void broadcast(String s){
            for(
    String st players){
                
    Player p Bukkit.getPlayer(st);
                            if(
    != null) {
                                
    p.sendMessage(/*BattleManager.getBM().bd oops, I'll fix that tomorrow. 1 problem fixed :D*/ s);
                            }
                    }
            }
    PHP:
    //Count
    package com.gmail.woodyc40.battledome.runnables;

    import com.gmail.woodyc40.battledome.Arena;
    import com.gmail.woodyc40.battledome.BattleManager;
    import com.gmail.woodyc40.battledome.handlers.SignHandler;
    import org.bukkit.Bukkit;

    public class 
    Count implements Runnable{

        
    int count 0;
        
    int id 0;
        
    Arena a;
        private 
    boolean isLobby;

        public 
    Count(int iArena aboolean b) {
            
    this.count i;
            
    this.a;
            
    this.isLobby b;

            
    RunnableQueue.queue(thisa);
        }

        public 
    void setId(int id) {
            
    this.id id;
        }

        public 
    int getId() {
            return 
    id;
        }

        
    void stop(){
            
    Bukkit.getScheduler().cancelTask(id);
        }

        @
    Override
        
    public void run() {
            if(
    this.isLobby){
                
    SignHandler.getSh().update(aArena.Game.LOBBY);
                
    a.broadcast(BattleManager.getBM().bd "Lobby ends in " count " minutes! Please vote to start quicker!");
                if(
    count == 0){
                    if(
    a.getPlayerCount() == 0){
                        
    a.lobby(5);
                        return;
                    }
                    
    a.teleportToGame();
                    
    stop();
                    
    a.game();
                    
    RunnableQueue.removeFromQueue(this);
                }
                
    count--;
            }else {
                
    SignHandler.getSh().update(aArena.Game.STARTING);
                
    a.broadcast(BattleManager.getBM().bd "starting in " count " seconds, prepare for battle!");
                if(
    count == 0){
                    
    a.broadcast(BattleManager.getBM().bd "has started! Best of luck!");
                    
    a.start();
                    
    stop();
                    
    RunnableQueue.removeFromQueue(this);
                }
                
    count--;
            }
        }
    }
    PHP:
    //RunnableQueue
    package com.gmail.woodyc40.battledome.runnables;

    import com.gmail.woodyc40.battledome.Arena;
    import org.bukkit.Bukkit;

    import java.util.*;

    public class 
    RunnableQueue {

        private static 
    Map<IntegerCountrunnableQueue = new HashMap<>();

        public static 
    void queue(Count cArena a) {
            
    runnableQueue.put(a.getId(), c);
        }

        public static 
    void cancelAllTasks(Arena a) {
            for(
    Map.Entry<IntegerCountrunnableQueue.entrySet()) {
                if(
    a.getId() == e.getKey()) {
                    
    Bukkit.getScheduler().cancelTask(e.getValue().getId());
                }
            }
        }

        public static 
    void removeFromQueue(Count c) {
            if(!
    runnableQueue.containsValue(c)) {
                return;
            }

            for(
    Count count loopThroughValues()) {
                if(
    count.getId() == c.getId()) {
                    
    Bukkit.getScheduler().cancelTask(c.getId());
                    break;
                }
            }
        }

        public static 
    boolean containsInQueue(Count c) {
            return 
    runnableQueue.containsValue(c);
        }

        private static List<
    CountloopThroughValues() {
            List<
    Count> list = new ArrayList<>();
            for(
    Map.Entry<IntegerCountrunnableQueue.entrySet()) {
                list.
    add(e.getValue());
            }
            return list;
        }

    }
    Edit: I'll try to use a HashSet tomorrow to see if there are 2 of me in the ArrayList :p
     
  2. Offline

    Xacero

    xTrollxDudex
    Code:java
    1. public void broadcast(String s){
    2. System.out.println("broadcasted : " + s);
    3. for(String st : players){
    4. Player p = Bukkit.getPlayer(st);
    5. if(p != null) {
    6. p.sendMessage(/*BattleManager.getBM().bd oops, I'll fix that tomorrow. 1 problem fixed :D*/ + s);
    7. }
    8. }
    9. }


    Does it print to the console more than once?
     
  3. Offline

    xTrollxDudex

    Xacero
    I'm on an iPad right now, but when I did that earlier it shows only once. I'll try tomorrow.
     
  4. Offline

    Xacero

    xTrollxDudex Something wrong with the variable "players" then.
    I dont think you included where that's coming from , and if you did I missed it.

    If you're adding to that list when the player joins the arena make sure you aren't calling the join method 2x.

    Some good debug code would be

    Code:java
    1. for(String st : players){
    2. Player p = Bukkit.getPlayer(st);
    3. if(p != null) {
    4. System.out.println(p.getName());
    5. p.sendMessage(/*BattleManager.getBM().bd oops, I'll fix that tomorrow. 1 problem fixed :D*/ + s);
    6. }
    7. }
    8. }
     
  5. Offline

    Blah1

    This is what I used in my 1v1 plugin. Give it a try:
    Code:
            new BukkitRunnable() {
     
                int i = 3;
                @Override
                public void run() {
                    String ll1 = (ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("messages.count-down-1v1-1")));
                    String ll2 = (ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("messages.count-down-1v1-2")));
                    String ll3 = (ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("messages.count-down-1v1-3")));
                    switch (i){
                        case 3:
                            player.sendMessage(ll3);
                            target.sendMessage(ll3);
                            break;
                        case 2:
                            player.sendMessage(ll2);
                            target.sendMessage(ll2);
                            break;
                        case 1:
                            player.sendMessage(ll1);
                            target.sendMessage(ll1);
                            break;
                        case 0:
                            player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("messages.1v1-go")));
                            target.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("messages.1v1-go")));
                            freeze.remove(player.getName());
                            freeze.remove(target.getName());
                            cancel();
                    }
                    i--;
                }
     
            }.runTaskTimer(plugin, 20, 20);
     
  6. Offline

    xTrollxDudex

    players is an ArrayList as mentioned.
     
  7. Offline

    Xacero

    xTrollxDudex
    I know it's an ArrayList, I just didn't see any methods adding / removing things from it. only methods getting information from it.

    I'd like to see the methods setting the data in the list. :) Sorry for the confusion
     
    xTrollxDudex likes this.
  8. Offline

    xTrollxDudex

    Xacero
    Here:
    PHP:
    //Arena#addPlayer(Player)
    public void addPlayer(Player p) {
            
    p.teleport(FileHandler.deserializeLoc(FileHandler.getFh().getSpawns().getString("Arenas." getId() + ".lobby")));
            
    p.setGameMode(GameMode.SURVIVAL);
            
    p.setHealth(20);
            
    p.setFoodLevel(20);
            
    p.setSaturation(20);
            
    players.add(p.getName());
            
    this.numOfPlayers++;
        }
    PHP:
        //BattleManager#addPlayer()
    public void addPlayer(Player pint id) {
            
    Arena a getArena(id);
            if(
    == null){
                
    p.sendMessage(ErrorHandler.getEh().error "This arena does not exist...");
                return;
            }
            
    save(p);
            
    exp.put(p.getName(), p.getTotalExperience());

            
    a.addPlayer(p);
        }
    PHP:
    //command. I swear I didn't type it in twice!
            
    if (cmd.equalsIgnoreCase("join")) {
                if (
    ErrorHandler.getEh().validateCmd(pargs.length1,
                        
    "bd.join")) {
                    
    int i 0;
                    try {
                        
    Integer.parseInt(args[0]);
                    } catch (
    NumberFormatException e) {
                        
    p.sendMessage(ErrorHandler.getEh().error
                                
    "Invalid arena ID!");
                    }
                    
    BattleManager.getBM().addPlayer(pi);
                    
    Arena a BattleManager.getBM().getArena(i);
                    if(
    == null){
                        
    p.sendMessage(ErrorHandler.getEh().error "Invalid arena!");
                        return;
                    }

                    if(
    a.getState().equals(Arena.Game.LOBBY)){BattleManager.getBM().addPlayer(pi);p.sendMessage(BattleManager.getBM().bd "Added you to Arena #" i);}
                    else {
                        if(
    a.getState().equals(Arena.Game.BUILD) || a.getState().equals(Arena.Game.DEATHMATCH) || a.getState().equals(Arena.Game.FIGHT))    a.setSpectator(p);
                        else if(
    a.getState().equals(Arena.Game.DISABLED) || a.getState().equals(Arena.Game.NOT_JOINABLE) || a.getState().equals(Arena.Game.UNKNOWN) || a.getState().equals(Arena.Game.STARTING) || a.getState().equals(Arena.Game.ENDGAME)){    p.sendMessage(ErrorHandler.getEh().error "This arena is not joinable at the current time");    return;}
                    }

                    if (
    a.getPlayerCount() == FileHandler.getFh().getConfig().getInt("Arena.max-players")) {
                        
    a.lobby(10);
                    } else if (
    a.getPlayerCount() == 1) {
                        
    BattleManager.getBM().countdown(3a);
                    }
                }else if(
    args.length == 0){
                    
    p.teleport(BattleManager.deadSpawn);
                }
                return;
            }
     
  9. Offline

    Xacero

    xTrollxDudex

    Code:java
    1.  
    2. BattleManager.getBM().addPlayer(p, i); // added 1x
    3. Arena a = BattleManager.getBM().getArena(i);
    4. if(a == null){
    5. p.sendMessage(ErrorHandler.getEh().error + "Invalid arena!");
    6. return;
    7. }


    Code:java
    1. if(a.getState().equals(Arena.Game.LOBBY)){
    2.  
    3. BattleManager.getBM().addPlayer(p, i); // added 2x


    Is that your problem ?
     
    xTrollxDudex likes this.
  10. Offline

    xTrollxDudex

    What!?

    .....

    What's wrong with me D:

    Thanks. I was banging my head against the wall for a few days over stupid mistakes such as this. If you want to see another idiotic mistake a while back, feel free to ask :p thanks again.
     
  11. Offline

    Xacero

    Code:
    //command. I swear I didn't type it in twice!
    Mhhm. ;)

    Don't worry about it, it happens to all of us :) Remember to mark as [solved] unless there's anything else you need assistance with :)
     
  12. Offline

    xTrollxDudex

  13. Offline

    Xacero

    xTrollxDudex
    Bracket errors can be tricky to spot. ;) Don't even worry about it
     
  14. Offline

    mattrick

    xTrollxDudex
    xTrollxDudex asking for help? No it's... impossible. Also just a question. I'm following your tutorial on minigame plugin and am using GameStates and join signs. How should I run a countdown for when the game starts?
     
  15. Offline

    xTrollxDudex

    mattrick16
    Hmmm.
    Eh?
     
  16. Offline

    mattrick

    xTrollxDudex
    Ok thanks, I wasn't really paying attention :p
     
Thread Status:
Not open for further replies.

Share This Page