Trying to activate game in my onEnable method

Discussion in 'Plugin Development' started by Creeoer, Aug 26, 2014.

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

    Creeoer

    I am trying to execute my game, meaning give the players all the equitmenet, and tp them to the arena every 280 seconds as specificed in my plugin. The only problem I'm having is defining p, as I am trying to get players from both of my teams, how would I define Player p so the game can get all of the players?

    My method:
    Code:java
    1.  
    2. public void activateGames(final Player p, int i){
    3.  
    4. Bukkit.getScheduler().scheduleSyncDelayedTask(DeathMongol.getInstance(), new Runnable(){
    5. public void run(){
    6. Random rand = new Random();
    7. rand.nextInt();
    8. if (rand.nextInt() % 2 == 0){
    9. Hunters.addHunter(p);
    10. p.sendMessage(ChatColor.RED + "You are now a hunter");
    11. } else if (rand.nextInt() % 3 == 0){
    12. Runners.addRunner(p);
    13. p.sendMessage(ChatColor.GREEN + "You are now a runner!");
    14. }
    15. }
    16. },20 * 70);
    17.  
    18. Arena a = ArenaManager.getManager().getArena(i);
    19.  
    20.  
    21. p.sendMessage(ChatColor.BLACK + "[DeathCavallo]" + ChatColor.AQUA + "You will be summoned into the arena in 50 seconds..");
    22. if(Hunters.isPlayerHunting(p) && !Runners.getRunners().isEmpty()){
    23. ItemStack[] items = {new ItemStack(Material.COOKED_BEEF, 16), new ItemStack(Material.IRON_SWORD), new ItemStack(Material.BOW)};
    24. p.getInventory().addItem(items);
    25. p.getInventory().setHelmet( new ItemStack (Material.DIAMOND_HELMET));
    26. p.getInventory().setChestplate( new ItemStack (Material.DIAMOND_CHESTPLATE));
    27. p.getInventory().setLeggings( new ItemStack (Material.DIAMOND_HELMET));
    28. p.getInventory().setBoots( new ItemStack (Material.DIAMOND_BOOTS));
    29. p.updateInventory();
    30. p.teleport(a.spawn);
    31. Horse horse = (Horse) p.getWorld().spawnCreature(p.getLocation(), EntityType.HORSE); // Spawns the horse
    32. horse.getInventory().setSaddle(new ItemStack(Material.SADDLE, 1)); // Gives horse saddle
    33. horse.setTamed(true); // Sets horse to tamed
    34. horse.setOwner(p);// Makes the horse the players
    35. } else if (Runners.getRunners().isEmpty()){
    36. ArenaManager.getManager().removePlayer(p);
    37. p.sendMessage(ChatColor.RED + "You've been removed from the game since no players are available");
    38. return;
    39.  
    40. } if (Runners.isPlayerRunning(p) && !Hunters.getHunters().isEmpty()){
    41. ItemStack[] items = {new ItemStack(Material.COOKED_BEEF, 16), new ItemStack(Material.DIAMOND_SWORD), new ItemStack(Material.BOW)};
    42. p.getInventory().addItem(items);
    43. p.getInventory().setHelmet( new ItemStack (Material.LEATHER_HELMET));
    44. p.getInventory().setChestplate( new ItemStack (Material.LEATHER_CHESTPLATE));
    45. p.getInventory().setLeggings( new ItemStack (Material.LEATHER_LEGGINGS));
    46. p.getInventory().setBoots( new ItemStack (Material.LEATHER_BOOTS));
    47.  
    48. p.updateInventory();
    49. p.teleport(a.spawn);
    50. Bukkit.getScheduler().scheduleSyncDelayedTask(DeathMongol.getInstance(), new Runnable(){
    51. public void run(){
    52. if(!(Runners.getRunners().isEmpty())){
    53. String name = p.getName();
    54. Bukkit.getServer().broadcastMessage(ChatColor.BLACK + "[DeathCavallo]" + ChatColor.GOLD + name + ChatColor.AQUA + "Has made it out on time!");
    55. ArenaManager.getManager().removePlayer(p);
    56. }
    57. }
    58. }, 20 * 180);
    59.  
    60.  
    61.  
    62.  
    63. } else if (Hunters.getHunters().isEmpty()){
    64. ArenaManager.getManager().removePlayer(p);
    65. p.sendMessage(ChatColor.RED + "You've been removed from the game since no players are available");
    66. return;


    Where it is executed
    Code:
    public void onEnable(){
           
            instance = this;
            //Listener Registry
            getServer().getPluginManager().registerEvents(new GameListener(), getInstance());
              new ArenaManager(this);
              ArenaManager.getManager().loadGames();
            //Load Config
            if(!getDataFolder().exists())
                    getDataFolder().mkdir();
                if(getConfig() == null)
                    saveDefaultConfig();
            getLogger().info("Enabling DeathCavallo");
           
           
           
            Bukkit.getScheduler().scheduleSyncRepeatingTask(DeathMongol.getInstance(), new Runnable(){
                public void run(){
    Player p = ??
            ArenaManager.getManager().activateGames(p, 1);
                }
        }, 20 * 200, 0); //What does the third number reperesent? The time you wait before it is repeated?
        
    My hunter class (Runner class pretty much the same):
    Code:java
    1. package me.creeoer.teams;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import java.util.UUID;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.entity.Player;
    9.  
    10. public class Hunters {
    11.  
    12.  
    13.  
    14.  
    15. private static List<UUID> hunters = new ArrayList<UUID>();
    16.  
    17. public static void addHunter(Player player){
    18. if(hunters.contains(player)){
    19. return;
    20. }
    21. hunters.add(player.getUniqueId());
    22. player.setDisplayName(ChatColor.RED + player.getName());
    23.  
    24.  
    25. }
    26. public static List<UUID> getHunters(){
    27. return hunters;
    28. }
    29. public void removeHunter(Player player){
    30. if(hunters.contains(player)){
    31. hunters.remove(player);
    32. }
    33. }
    34.  
    35. public static boolean isPlayerHunting(Player player){
    36. if (hunters.contains(player)){
    37. return true;
    38. } else
    39. return false;
    40. }
    41.  
    42.  
    43. }
    44.  
     
  2. Offline

    SantaClawz69

    Is there any specific place we should be looking? It would help if you had a stack trace or something.
     
  3. Offline

    fireblast709

    Creeoer
    • Why are you using nextInt() and % 2, just use nextInt(2)
    • No need for using static plugin instances
    • saveDefaultConfig() already checks for the existance of a config.yml file. That aside, the null check is completely unrelated to the default config.
    • No need for enable/disable messages, there's a thing called Bukkit doing that for you ;)
    • A static List... please stay away from static until you properly know the impacts. Even if we leave that aside, it's bad design to use static.
    • The third parameter in a timer task is the interval (indeed, the time between task #n and task #n+1)
     
    mine-care likes this.
  4. Offline

    stormneo7

    Code:java
    1. public static boolean isPlayerHunting(Player player){
    2. if (hunters.contains(player)){
    3. return true;
    4. } else
    5. return false;
    6. }

    just do
    Code:
    public static boolean isPlayerHunting(Player player){
      return hunters.contains(player);
    }
     
    Garris0n likes this.
  5. Offline

    fireblast709

    inb4 List<UUID>
     
  6. Offline

    stormneo7

    Creeoer
    Code:java
    1. }, 20 * 200, 0);

    First long represents how many ticks before starting the runnable.
    Second long represents how many ticks before rerunning the runnable.
    For instance, if I did 20, 10 and ran it onEnable, it would wait 1 second before running the runnable then run it half a second repetitively thereafter.

    The fact you did 0, I think you meant to do
    Code:java
    1. }, 20 * 200, 20 * 200);
     
  7. Offline

    mine-care

    fireblast709 oh my good catches! Yet another like for you!
     
  8. Offline

    Creeoer

    stormneo7 fireblast709 Thank you for the lesson on static, I've created instnaces of both of my classes . And I appreciate all of your help,but I still don't know what to define player as in my onEnable? I just started java itself 3 weeks ago, I hope I'm not missing sometihng here.
     
  9. Offline

    Necrodoom

    Creeoer why would you need to access a player on onEnable?
     
  10. Offline

    Creeoer

    Necrodoom so I can give a player object to my activateGame method to allow it to execute, I need to define what player is.
     
  11. Offline

    Necrodoom

    Creeoer you won't have any player object on onEnable. Don't you need to wait for a certain amount of players to start a game?
     
  12. Offline

    Creeoer

    Necrodoom No, I just need 1 player on the hunter team and 1 player on the runner. I made a method to tp all the players from both of those teams into the arena spawn, and I want to start the game every 200 seconds inside my onEnable method. My method needs a player object isnce it is a paramater in my method, I just need to define that player object so I can get my players from both teams
     
  13. Offline

    Necrodoom

    Creeoer again, you won't have 2 players on onEnable, the server just started.
    Your activeGames currently only sets up a single player - can you paste the entire project please? Preferably using github - I don't think you are understanding what you are trying to do.
     
  14. Offline

    Creeoer

    Necrodoom Alrigth, I'll edit this post with a link once I have one, btw you I've noticed you always are offline, but you still manage to reply..
     
  15. Offline

    Necrodoom

    Creeoer my online status is hidden, to reduce random PMs.
     
  16. Offline

    Creeoer

Thread Status:
Not open for further replies.

Share This Page