Simple spleef plugin issue.

Discussion in 'Plugin Development' started by TheAnswerIsMinecraft, Jan 20, 2014.

Thread Status:
Not open for further replies.
  1. I am coding a simple spleef plugin but for some reason when the spleef is supposed to start it says the arraylist is empty even though 2 players have joined it. Help please!
    Code:java
    1. ArrayList<String> usedkit = Main.usedkit;
    2. ArrayList<String> spleefjoin = new ArrayList<String>();
    3. ArrayList<String> spleef = new ArrayList<String>();
    4. String auk = Main.auk;
    5. String noperm = Main.noperm;
    6. String prefix = Main.prefix;
    7.  
    8. World world = Bukkit.getWorld("world");
    9. private Location loc1 = new Location(world, 152, 68, 1234555);
    10. private Location loc2 = new Location(world, 108, 68, 1234580);
    11. private Location loc3 = new Location(world, 131, 69, 1234567);
    12.  
    13. static Plugin plug = Bukkit.getPluginManager().getPlugin("W1kkedKits");
    14.  
    15. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    16. if(!(sender instanceof Player)) {
    17. sender.sendMessage("§cOnly players can execute this command!");
    18. return true;
    19. }
    20. Player p = (Player) sender;
    21. if (cmd.getName().equalsIgnoreCase("startspleef")){
    22. if (p.isOp() || p.hasPermission("w1kkedkits.startspleef")){
    23. if (spleef.isEmpty()){
    24. Bukkit.getServer().broadcastMessage(prefix + ChatColor.AQUA + "Spleef is starting in 1 minute type /joinspleef to join!");
    25. spleef.add("spl");
    26. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plug, new Runnable(){
    27. @Override
    28. public void run() {
    29. if (spleefjoin.size() > 1){
    30. int minX = Math.min(loc1.getBlockX(), loc2.getBlockX());
    31. int minY = Math.min(loc1.getBlockY(), loc2.getBlockY());
    32. int minZ = Math.min(loc1.getBlockZ(), loc2.getBlockZ());
    33. int maxX = Math.max(loc1.getBlockX(), loc2.getBlockX());
    34. int maxY = Math.max(loc1.getBlockY(), loc2.getBlockY());
    35. int maxZ = Math.max(loc1.getBlockZ(), loc2.getBlockZ());
    36.  
    37. for(int x = minX; x <= maxX; x++){
    38. for(int y = minY; y <= maxY; y++){
    39. for(int z = minZ; z <= maxZ; z++){
    40. Block block = world.getBlockAt(x, y, z);
    41. block.setType(Material.SNOW_BLOCK);
    42. }
    43. }
    44. }
    45. } else {
    46. Bukkit.getServer().broadcastMessage(prefix + ChatColor.AQUA + "Spleef failed to start due to lack of players!");
    47. spleef.clear();
    48. spleefjoin.clear();
    49. return;
    50. }
    51. for (String p : spleefjoin){
    52. if(Bukkit.getPlayerExact(p).isOnline()){
    53. Player pl = Bukkit.getPlayerExact(p);
    54. PlayerInventory pi = pl.getInventory();
    55. pl.teleport(loc3);
    56. pi.clear();
    57. pi.setArmorContents(null);
    58. usedkit.add(pl.getName());
    59. pi.addItem(new ItemStack (Material.DIAMOND_SPADE, 1));
    60. }
    61. }
    62. }
    63. }, 1200);
    64. } else {
    65. p.sendMessage(ChatColor.RED + "Spleef is already going on!");
    66. return true;
    67. }
    68. }
    69. }
    70. if (cmd.getName().equalsIgnoreCase("joinspleef")){
    71. if (!spleefjoin.contains(p.getName())){
    72. p.sendMessage(prefix + ChatColor.AQUA + "You have joined spleef");
    73. Bukkit.getServer().broadcastMessage(prefix + ChatColor.AQUA + p.getDisplayName() + " has joined spleef.");
    74. spleefjoin.add(p.getName());
    75. return true;
    76. } else {
    77. p.sendMessage(ChatColor.RED + "You have already joined spleef.");
    78. return true;
    79. }
    80. }
    81. return false;
    82. }

    Thanks!

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  2. Offline

    Rocoty

    Is this in your main class or a separate command executor? Make sure you are only ever making one instance of the command executor. If you are in doubt as to what I am saying, post the code of the entire class (and the entire main class) and I will explain what is going on.
     
  3. Here is my code in my main class to register the class.
    Code:java
    1. pm.registerEvents(new spleef(this), (this));
    2. getCommand("joinspleef").setExecutor(new spleef(this));
    3. getCommand("startspleef").setExecutor(new spleef(this));
    4.  
    5.  
     
  4. Offline

    Rocoty

    Don't call 'new spleef(this)' more than once. Doing so will result in multiple distinct instances and most certainly ensure that you will not be working with the same ArrayLists. Instantiate spleef once and store the value in a variable, which you can then pass as an argument to the setExecutor methods.
     
  5. Rocoty
    Sorry I am kind of new, could I get an example?
     
  6. Offline

    Rocoty

    Code:java
    1. Foo bar = new Foo();
    2. fuzz(bar);
    3. fizz(bar);
    4. buzz(bar);

    Generic example ^
     
Thread Status:
Not open for further replies.

Share This Page