Queue system - teleporting issue

Discussion in 'Plugin Development' started by xDeeKay, Apr 25, 2015.

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

    xDeeKay

    I have a queue system based on a command which should store a players location, inventory, and a bunch of other stuff including teleporting up to 4 players to 4 different locations.

    My issue is that the for loop is teleporting all the players before it stores some of their locations, therefor storing their locations as the new location.

    I need it to, if possible, store all required information before teleporting all the players. Using delayed tasks are an option, but I'd like to use that as a last resort.

    Here's the runnable:
    Code:
        @Override
        public void run() {
            p.sendMessage(ChatColor.GREEN + "Spleef game is starting.");
    
            //store players locations
            plugin.location.put(p.getName(), p.getLocation());
            System.out.println("Saved " + p.getName() + "'s location: " + p.getLocation());
    
            //store players inventory
            plugin.inventory.put(p.getName(), p.getInventory().getContents());
            System.out.println("Saved " + p.getName() + "'s inventory");
    
            //move players to plugin.spleefGame
            plugin.spleefGame.add(p.getName());
            System.out.println("Added " + p.getName() + " to spleefGame");
    
            //clear inventory
            p.getInventory().clear();
            System.out.println("Cleared " + p.getName() + "'s inventory");
    
            //set gamemode to survival
            p.setGameMode(GameMode.SURVIVAL);
            System.out.println("Set " + p.getName() + "'s gamemode to survival");
    
            //teleport players to arena
            World world = Bukkit.getWorld(plugin.getConfig().getString("spleef.world"));
    
            //player 1 location
            double player1x = plugin.getConfig().getDouble("spleef.players.player1.x");
            double player1y = plugin.getConfig().getDouble("spleef.players.player1.y");
            double player1z = plugin.getConfig().getDouble("spleef.players.player1.z");
            float player1yaw = (float) plugin.getConfig().getDouble("spleef.players.player1.yaw");
            float player1pitch = (float) plugin.getConfig().getDouble("spleef.players.player1.pitch");
            Location player1loc = new Location(world, player1x, player1y, player1z, player1yaw, player1pitch);
    
            //player 2 location
            double player2x = plugin.getConfig().getDouble("spleef.players.player2.x");
            double player2y = plugin.getConfig().getDouble("spleef.players.player2.y");
            double player2z = plugin.getConfig().getDouble("spleef.players.player2.z");
            float player2yaw = (float) plugin.getConfig().getDouble("spleef.players.player2.yaw");
            float player2pitch = (float) plugin.getConfig().getDouble("spleef.players.player2.pitch");
            Location player2loc = new Location(world, player2x, player2y, player2z, player2yaw, player2pitch);
    
            //player 3 location
            double player3x = plugin.getConfig().getDouble("spleef.players.player3.x");
            double player3y = plugin.getConfig().getDouble("spleef.players.player3.y");
            double player3z = plugin.getConfig().getDouble("spleef.players.player3.z");
            float player3yaw = (float) plugin.getConfig().getDouble("spleef.players.player3.yaw");
            float player3pitch = (float) plugin.getConfig().getDouble("spleef.players.player3.pitch");
            Location player3loc = new Location(world, player3x, player3y, player3z, player3yaw, player3pitch);
    
            //player 4 location
            double player4x = plugin.getConfig().getDouble("spleef.players.player4.x");
            double player4y = plugin.getConfig().getDouble("spleef.players.player4.y");
            double player4z = plugin.getConfig().getDouble("spleef.players.player4.z");
            float player4yaw = (float) plugin.getConfig().getDouble("spleef.players.player4.yaw");
            float player4pitch = (float) plugin.getConfig().getDouble("spleef.players.player4.pitch");
            Location player4loc = new Location(world, player4x, player4y, player4z, player4yaw, player4pitch);
    
            Location[] locations = new Location[4];
            locations[0] = player1loc;
            locations[1] = player2loc;
            locations[2] = player3loc;
            locations[3] = player4loc;
    
            int i = 0;
            for (Player player : Bukkit.getOnlinePlayers()) {
                if (plugin.spleefQueue.contains(player.getName())) {
                    if (i <= 4) {
                        player.teleport(locations[i]);
                        System.out.println("Teleported " + player.getName() + " to the arena");
                        i++;
                    }
                }
            }
    
            //remove players from plugin.spleefQueue
            plugin.spleefQueue.remove(p.getName());
            System.out.println("Removed " + p.getName() + " from spleefQueue");
        }
    Here's the part of the command where the runnable is being run:
    Code:
                        } else if (plugin.spleefQueue.size() == 1) {
                            plugin.spleefQueue.add(playerName);
                            cs.sendMessage(ChatColor.GREEN + "You have been added to the spleef queue.");
                            cs.sendMessage(ChatColor.GREEN + "Queue size: " + plugin.spleefQueue.size() + "/4");
                            for (final Player p : Bukkit.getOnlinePlayers()) {
                                if (plugin.spleefQueue.contains(p.getName())) {
                                    if (plugin.spleefGame.size() >= 1) {
                                        p.sendMessage(ChatColor.GREEN + "A Spleef game is in progress. Your game will start soon.");
                                    } else {
                                        p.sendMessage(ChatColor.GREEN + "Spleef game will start in " + timeToStart + " seconds.");
                                      
                                        //start SpleefRunnable
                                        new SpleefRunnable(this.plugin, p).runTaskLater(this.plugin, timeToStart * 20);
                                    }
                                }
                            }
    
                        }
     
  2. Oh god, don't store locations like that, there are so many things to fix, i dont know where i should start.
     
  3. Offline

    xDeeKay

    @MaTaMoR_ That wasn't a very useful reply then was it? Can you explain?
     
  4. Offline

    mythbusterma

    @xDeeKay

    1. Create a ConfigurationSerializable for your Locations
    2. Use loops to avoid repeating code
     
  5. The queue system can be done checking the amount of players in the arena when a player joins the queue, say bb to 1 runnable.
     
  6. Offline

    xDeeKay

    @mythbusterma @MaTaMoR_ Excuse my ignorance, but I'm only using a runnable because I have 2 instances where I need to run that code, so it was for convenience sake. Is this not recommended? That's why the code simply isn't run right after the arena size check.
     
  7. Show me the code, and ill help you .
     
  8. Offline

    mythbusterma

    @xDeeKay

    I wasn't objecting to using a runnable, only to the two items I mentioned in my post.
     
Thread Status:
Not open for further replies.

Share This Page