Battle/Arena Plugin

Discussion in 'Plugin Development' started by PQE, Jan 29, 2017.

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

    PQE

    Hey, I just started learning Bukkit today. I've got some experience with Java so I'm alright on that side of it - just learning API. I had the idea to make a plugin for 1v1 battles. Well, needless to say, I ran into some issues. Here's what I got so far:

    Code:java
    1.  
    2. package com.meeku.tutorialPlugin;
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Location;
    5. import org.bukkit.World;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10. import org.fusesource.jansi.Ansi.Color;
    11.  
    12. import io.netty.handler.codec.rtsp.RtspHeaders.Names;
    13. import net.milkbowl.vault.*;
    14. import java.util.ArrayList;
    15. import net.md_5.bungee.api.ChatColor;
    16.  
    17. public class SpigotBlankPlugin extends JavaPlugin{
    18.  
    19. @Override
    20. public void onEnable() {
    21. getLogger().info("Plugin absolutely activated");
    22. ArrayList<String> names = new ArrayList<String>();
    23. }
    24.  
    25. public void onDisable() {
    26. getLogger().info("Plugin absolutely deactivated");
    27. }
    28.  
    29. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    30.  
    31. Player player = (Player) sender;
    32.  
    33. if (label.equalsIgnoreCase("battle")) {
    34. if (!(sender instanceof Player)) {
    35. sender.sendMessage(ChatColor.RED + "Only players can enter battles");
    36. return true;
    37. }
    38.  
    39. else if (args.length != 0) {
    40. sender.sendMessage(ChatColor.RED + "Incorrect Usage! /battle help for details!");
    41. return true;
    42. }
    43.  
    44. else if (args[0].equalsIgnoreCase("setspawn1")) {
    45. World w = Bukkit.getServer().getWorld(getConfig().getString("spawn.world"));
    46. double x = getConfig().getDouble("spawn.x");
    47. double y = getConfig().getDouble("spawn.y");
    48. double z = getConfig().getDouble("spawn.z");
    49. }
    50.  
    51. else if (args[0].equalsIgnoreCase("setspawn2")) {
    52. World w2 = Bukkit.getServer().getWorld(getConfig().getString("spawn.world2"));
    53. double x2 = getConfig().getDouble("spawn.x2");
    54. double y2 = getConfig().getDouble("spawn.y2");
    55. double z2 = getConfig().getDouble("spawn.z2");
    56. hasSetSpawn1 = true;
    57. }
    58.  
    59. else if (args[0].equalsIgnoreCase("join")) {
    60.  
    61. if (names.size() == 0) {
    62. names.add(player);
    63. player.teleport(new Location(w, x, y, z);
    64. Bukkit.broadcastMessage(Color.GREEN + "" + player + Color.MAGENTA + " has joined the queue! Use /battle join to fight them. (1/2)");
    65. }
    66.  
    67. if (names.size() == 1) {
    68. names.add(player);
    69. player.teleport(new Location(w2, x2, y2, z2));
    70. Bukkit.broadcastMessage(Color.GREEN + "" + names.get(0) + " is now fighting " + names.get(1));
    71. names.remove(0);
    72. names.remove(1);
    73. }
    74. }
    75.  
    76. else if (args[0].equalsIgnoreCase("help")) {
    77. sender.sendMessage(ChatColor.GREEN + "Use /battle enter to join the queue for a fight.");
    78. return true;
    79. }
    80.  
    81. else {
    82. sender.sendMessage(ChatColor.RED + "For help, please use /battle help");
    83. return true;
    84. }
    85.  
    86. return true;
    87. }
    88. }
    89.  
    90. }
    91.  


    I don't know how to refer back to the ArrayList I built in onEnable method, in my main command method. Any ideas how I could construct this (so far I'm only trying to teleport the players to the correct destination, and begin the fight).
     
  2. @PQE
    Declare the ArrayList as a field of the class instead of in the onEnable. That way you'll be able to access it in all methods you like.
    Code:java
    1. ArrayList<String> myList = ...
    2.  
    3. @Override
    4. public void onEnable...
    5.  
    6. @Override
    7. public boolean onCommand...... {
    8. // Modify the ArrayList all you like here.
    9. myList.add/remove/whatever()
    10. }
     
  3. Offline

    Drkmaster83

    @PQE, yes, what @AlvinB said. The reason you aren't able to access it is because of a concept called scope (here's a resource). Basically, if a variable isn't equally or less indented than what you're trying to use it in, you aren't able to use it (the program can't see it).
     
  4. Offline

    Zombie_Striker

    Don't use this import. If you are not running a bungee server (which is not supported here BTW) this will break your plugin. Use org.bukkit.ChatColor.

    Bukkit logs your plugins for you. You can remove this line, since what this will do is create two messages saying the plugin has been enabled.

    You have to do the instanceof check before you create this instance.

    Use cmd.getName() instead. label is only there for backwards compatibility.

    These variables do nothing. Did you mean to add more to this section?

    You only need to return true if you do not want the JVM to read past that point. A lot of these returns are not needed.
     
    PQE likes this.
Thread Status:
Not open for further replies.

Share This Page