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 package com.meeku.tutorialPlugin;import org.bukkit.Bukkit;import org.bukkit.Location;import org.bukkit.World;import org.bukkit.command.Command;import org.bukkit.command.CommandSender;import org.bukkit.entity.Player;import org.bukkit.plugin.java.JavaPlugin;import org.fusesource.jansi.Ansi.Color; import io.netty.handler.codec.rtsp.RtspHeaders.Names;import net.milkbowl.vault.*;import java.util.ArrayList;import net.md_5.bungee.api.ChatColor; public class SpigotBlankPlugin extends JavaPlugin{ @Override public void onEnable() { getLogger().info("Plugin absolutely activated"); ArrayList<String> names = new ArrayList<String>(); } public void onDisable() { getLogger().info("Plugin absolutely deactivated"); } public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { Player player = (Player) sender; if (label.equalsIgnoreCase("battle")) { if (!(sender instanceof Player)) { sender.sendMessage(ChatColor.RED + "Only players can enter battles"); return true; } else if (args.length != 0) { sender.sendMessage(ChatColor.RED + "Incorrect Usage! /battle help for details!"); return true; } else if (args[0].equalsIgnoreCase("setspawn1")) { World w = Bukkit.getServer().getWorld(getConfig().getString("spawn.world")); double x = getConfig().getDouble("spawn.x"); double y = getConfig().getDouble("spawn.y"); double z = getConfig().getDouble("spawn.z"); } else if (args[0].equalsIgnoreCase("setspawn2")) { World w2 = Bukkit.getServer().getWorld(getConfig().getString("spawn.world2")); double x2 = getConfig().getDouble("spawn.x2"); double y2 = getConfig().getDouble("spawn.y2"); double z2 = getConfig().getDouble("spawn.z2"); hasSetSpawn1 = true; } else if (args[0].equalsIgnoreCase("join")) { if (names.size() == 0) { names.add(player); player.teleport(new Location(w, x, y, z); Bukkit.broadcastMessage(Color.GREEN + "" + player + Color.MAGENTA + " has joined the queue! Use /battle join to fight them. (1/2)"); } if (names.size() == 1) { names.add(player); player.teleport(new Location(w2, x2, y2, z2)); Bukkit.broadcastMessage(Color.GREEN + "" + names.get(0) + " is now fighting " + names.get(1)); names.remove(0); names.remove(1); } } else if (args[0].equalsIgnoreCase("help")) { sender.sendMessage(ChatColor.GREEN + "Use /battle enter to join the queue for a fight."); return true; } else { sender.sendMessage(ChatColor.RED + "For help, please use /battle help"); return true; } return true; } } } 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).
@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 ArrayList<String> myList = ... @Overridepublic void onEnable... @Overridepublic boolean onCommand...... { // Modify the ArrayList all you like here. myList.add/remove/whatever()}
@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).
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.