Syntax error: insert to complete classbody and mainclass [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Getting this syntax error when coding, I can't seem to find the source of the problem if any one knows place comment as quickly as possible thanks.
package me.carwyn123;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
org.bukkit.plugin.java.JavaPlugin;
public class Staffviewer extends JavaPlugin {
#Override
public void onEnable() {
getLogger().info("Staffviewer plugin now enabled / plugin made by carwyn123");
getConfig().options().copyDefaults(true);
saveConfig();
}
#Override
public void onDisable() {
getLogger().info("Staffviewer as been disabled / plugin made by carwyn123");
saveConfig();
}
public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args) {
// the staff list code
if (cmd.getName().equalsIgnoreCase("staff") && sender instanceof Player) {
if ( !sender.hasPermission("staffviewer.staff")) {
sender.sendMessage(ChatColor.GOLD + "[StaffViewer]" + ChatColor.AQUA + "You don't have permission");
return true;
}
Player player = (Player) sender;
player.sendMessage(ChatColor.AQUA + "================================");
player.sendMessage(ChatColor.GOLD + " Staff members");
player.sendMessage(ChatColor.AQUA + "================================");
player.sendMessage(ChatColor.GREEN + getConfig().getString("staffmembers"));
player.sendMessage(ChatColor.AQUA + "================================");
player.sendMessage(ChatColor.AQUA + getConfig().getString("staffmembers"));
return true;
}
// staff help code
if (cmd.getName().equalsIgnoreCase("staffhelp") && sender instanceof Player) {
if ( !sender.hasPermission("staffviewer.help")) {
sender.sendMessage(ChatColor.GOLD + "[StaffViewer]" + ChatColor.AQUA + "You don't have permission");
return true;
}
Player player = (Player) sender;
player.sendMessage(ChatColor.GOLD + "[StaffViewer]" + ChatColor.AQUA + "Here is the list of commands" );
player.sendMessage(ChatColor.GOLD + "---------------------------------");
player.sendMessage(ChatColor.GOLD + "[StaffViewer]" + ChatColor.AQUA + "/staff : gives the list of staff");
player.sendMessage(ChatColor.GOLD + "[StaffViewer]" + ChatColor.AQUA + "/staffadd: adds staff to the list, using config allows for more design");
player.sendMessage(ChatColor.GOLD + "[StaffViewer]" + ChatColor.AQUA + "/staffhelp: gets you to this page");
player.sendMessage(ChatColor.GOLD + "[StaffViewer]" + ChatColor.AQUA + "/staffinfo: gives you information about the plugin");
player.sendMessage(ChatColor.GOLD + "---------------------------------");
return true;
}
// staff information
if (cmd.getName().equalsIgnoreCase("staffinfo") && sender instanceof Player) {
if( !sender.hasPermission("staffviewer.info")) {
sender.sendMessage(ChatColor.GOLD + "[StaffViewer]" + ChatColor.AQUA + "You don't have permission");
return true ;
}
Player player = (Player) sender;
player.sendMessage(ChatColor.GOLD + "[StaffViewer]" + ChatColor.AQUA + "Here you will find information about the plugin");
player.sendMessage(ChatColor.AQUA + "--------------------------------------------------------------------");
player.sendMessage(ChatColor.GOLD + " This plugin was made by carwyn123");
player.sendMessage(ChatColor.GOLD + " If you find any bugs, please report as a ticked or commet on bukkit.dev");
player.sendMessage(ChatColor.GOLD + " Thanks for downloading this plugin, from the team");
player.sendMessage(ChatColor.AQUA + "--------------------------------------------------------------------");
return true;
}
// staff add code
if (cmd.getName().equalsIgnoreCase("staffadd")); {
if ( !sender.hasPermission("staffviewer.add")) {
sender.sendMessage(ChatColor.GOLD + "[StaffViewer]" + ChatColor.AQUA + "You don't have permission");
return true ;
}
if (args.length == 0) {
sender.sendMessage(ChatColor.GOLD + "[StaffViewer]" + ChatColor.AQUA + "You can't add know one");
return true;
}
StringBuilder str = new StringBuilder();
for (int i = 0; i < args.length; i++) {
str.append(args[i] + " ");
}
String staffmembers = str.toString();
getConfig().set("staffmembers", staffmembers);
saveConfig();
sender.sendMessage(ChatColor.GOLD + "[StaffViewer]" + ChatColor.AQUA + "You have added a staff member");
return true;
// staff line editor
}
if (cmd.getName().equalsIgnoreCase("staffedit")); {
if (!sender.hasPermission("staffviewer.edit")) {
sender.sendMessage(ChatColor.GOLD + "[StaffViewer]" + ChatColor.AQUA + "You don't have permission");
return true;
}
if (args.length == 0) {
sender.sendMessage(ChatColor.GOLD + "[StaffViewer]" + ChatColor.AQUA + "You need to add a message");
return true;
}
StringBuilder str1 = new StringBuilder();
for (int i = 0; i < args.length; i++) {
str1.append(args[i] + " ");
}
String staffedit = str1.toString();
getConfig().set("endline", staffedit);
saveConfig();
sender.sendMessage(ChatColor.GOLD + "[StaffViewer]" + ChatColor.AQUA + "You have added a message at the end");
return true;
}
}
The problem has been happening for about 2 hours now. I have searched a lot of sources on the internet and this seems to be a well known problem with people that are new to coding.

Add import declaration for org.bukkit.plugin.java.JavaPlugin;, change it to
import org.bukkit.plugin.java.JavaPlugin;
And add a } at the end to complete the class.

You had several errors in your code and you wrote ChatColor.GOLD + "[StaffViewer]" + ChatColor.AQUA several times, so I made a prefix variable for it.
Your final code is this:
package me.carwyn123;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class Staffviewer extends JavaPlugin {
String prefix=ChatColor.GOLD + "[StaffViewer]" + ChatColor.AQUA;
#Override
public void onEnable() {
getLogger().info("Staffviewer plugin now enabled / plugin made by carwyn123");
getConfig().options().copyDefaults(true);
saveConfig();
}
#Override
public void onDisable() {
getLogger().info("Staffviewer as been disabled / plugin made by carwyn123");
saveConfig();
}
public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args) {
// the staff list code
if (cmd.getName().equalsIgnoreCase("staff") && sender instanceof Player) {
if ( !sender.hasPermission("staffviewer.staff")) {
sender.sendMessage(prefix + "You don't have permission");
return true;
}
Player player = (Player) sender;
player.sendMessage(ChatColor.AQUA + "================================");
player.sendMessage(ChatColor.GOLD + " Staff members");
player.sendMessage(ChatColor.AQUA + "================================");
player.sendMessage(ChatColor.GREEN + getConfig().getString("staffmembers"));
player.sendMessage(ChatColor.AQUA + "================================");
player.sendMessage(ChatColor.AQUA + getConfig().getString("staffmembers"));
return true;
}
// staff help code
if (cmd.getName().equalsIgnoreCase("staffhelp") && sender instanceof Player) {
if ( !sender.hasPermission("staffviewer.help")) {
sender.sendMessage(prefix + "You don't have permission");
return true;
}
Player player = (Player) sender;
player.sendMessage(prefix + "Here is the list of commands" );
player.sendMessage(ChatColor.GOLD + "---------------------------------");
player.sendMessage(prefix + "/staff : gives the list of staff");
player.sendMessage(prefix + "/staffadd: adds staff to the list, using config allows for more design");
player.sendMessage(prefix + "/staffhelp: gets you to this page");
player.sendMessage(prefix + "/staffinfo: gives you information about the plugin");
player.sendMessage(ChatColor.GOLD + "---------------------------------");
return true;
}
// staff information
if (cmd.getName().equalsIgnoreCase("staffinfo") && sender instanceof Player) {
if( !sender.hasPermission("staffviewer.info")) {
sender.sendMessage(prefix + "You don't have permission");
return true ;
}
Player player = (Player) sender;
player.sendMessage(prefix + "Here you will find information about the plugin");
player.sendMessage(ChatColor.AQUA + "--------------------------------------------------------------------");
player.sendMessage(ChatColor.GOLD + " This plugin was made by carwyn123");
player.sendMessage(ChatColor.GOLD + " If you find any bugs, please report as a ticked or commet on bukkit.dev");
player.sendMessage(ChatColor.GOLD + " Thanks for downloading this plugin, from the team");
player.sendMessage(ChatColor.AQUA + "--------------------------------------------------------------------");
return true;
}
// staff add code
if (cmd.getName().equalsIgnoreCase("staffadd")){
if ( !sender.hasPermission("staffviewer.add")) {
sender.sendMessage(prefix + "You don't have permission");
return true ;
}
if (args.length == 0) {
sender.sendMessage(prefix + "You can't add know one");
return true;
}
StringBuilder str = new StringBuilder();
for (int i = 0; i < args.length; i++) {
str.append(args[i] + " ");
}
String staffmembers = str.toString();
getConfig().set("staffmembers", staffmembers);
saveConfig();
sender.sendMessage(prefix + "You have added a staff member");
return true;
}
// staff line editor
if (cmd.getName().equalsIgnoreCase("staffedit")){
if (!sender.hasPermission("staffviewer.edit")) {
sender.sendMessage(prefix + "You don't have permission");
return true;
}
if (args.length == 0) {
sender.sendMessage(prefix + "You need to add a message");
return true;
}
StringBuilder str1 = new StringBuilder();
for (int i = 0; i < args.length; i++) {
str1.append(args[i] + " ");
}
String staffedit = str1.toString();
getConfig().set("endline", staffedit);
saveConfig();
sender.sendMessage(prefix + "You have added a message at the end");
return true;
}
return false;
}
}

Related

How to make a loop not ask for input after the last run?

I am doing a project that requires me to run a loop that requires user input after every run and stops after a certain number of times. I have it working but have a weird quirk that I cannot figure out... How to make it stop asking for input after the last run. Any help would be appreciated. Thanks!
public class TestCard {
/**
* #param args
*/
public static void main(String[] args) {
int i = 0,
player = 0,
dealer = 0,
tie = 0;
Scanner input = new Scanner(System.in);
String answer = "";
do {
Card card1 = new Card();
Card card2 = new Card();
if (card1.getValue() > card2.getValue()) {
System.out.println("Your card: " + card1);
System.out.println("Dealer card: " + card2);
i++;
player++; // adds to player win total.
System.out.println("You win!");
System.out.println("Total score - Player: " + player + ", Dealer: " + dealer + ", Ties: " + tie);
} else if (card1.getValue() < card2.getValue()) {
System.out.println("Your card: " + card1);
System.out.println("Dealer card: " + card2);
i++;
dealer++;
System.out.println("Dealer wins!");
System.out.println("Total score - Player: " + player + ", Dealer: " + dealer + ", Ties: " + tie);
} else {
System.out.println("Your card: " + card1);
System.out.println("Dealer card: " + card2);
i++;
tie++;
System.out.println("Tie!");
System.out.println("Total score - Player: " + player + ", Dealer: " + dealer + ", Ties: " + tie);
}
System.out.println("Play again? Y/N (Draws left: " + (26 - i) + ")");
answer = input.next();
answer = answer.toLowerCase();
if (answer.equals("y")) {
card1.getValue();
card2.getValue();
} else if (answer.equals("n")) {
System.out.println("Thanks for playing!");
} else {
while (!answer.equals("y") && !answer.equals("n")) {
System.out.println("Please input Y or N");
answer = input.next();
answer = answer.toLowerCase();
}
}
}
while (answer.equals("y") && (i < 26));
System.out.println("Game over! Totals - Player: " + player + ", Dealer: " + dealer + ", Ties: " + tie + ", Total draws: " + i);
input.close();
}
}

Problem with vanish | When a player quit and rejoin, he can see me (while i'm vanished) | Minecraft Plugin Java

I'm having issues with my command /vanish: while i'm vanished, and a player join the game after my vanishing, he can see me. Here the code of the command and of the PlayerJoinEvent + updateVanish():
if(commandLabel.equalsIgnoreCase("vanish")) {
if (player.hasPermission("vanish.permission")) {
if(!vanish.contains(target)) {
vanish.add(target);
if (player == target) {
player.sendMessage(ChatColor.BLUE + "Ora sei in " + ChatColor.LIGHT_PURPLE + "Vanish" + ChatColor.BLUE+ ". Nessuno ti potrĂ  vedere");
} else {
player.sendMessage(ChatColor.BLUE + "Hai Impostato " + ChatColor.DARK_BLUE + target.getName() + ChatColor.YELLOW + " in " + ChatColor.LIGHT_PURPLE + "Vanish");
target.sendMessage(ChatColor.BLUE + "Ora sei in Vanish. Nessuno ti potrĂ  vedere");
}
for (Player persone : Bukkit.getOnlinePlayers()) {
if (!persone.hasPermission("visible.vanish")) {
persone.hidePlayer(this, target);
}
}
} else {
vanish.remove(target);
for (Player persone : Bukkit.getOnlinePlayers()) {
persone.showPlayer(this, target);
}
if (player == target) {
player.sendMessage(ChatColor.RED + "Ora non sei in " + ChatColor.LIGHT_PURPLE + "Vanish" + ChatColor.RED+ ". Tutti ti potranno vedere");
} else {
player.sendMessage(ChatColor.RED + "Hai disattivato " + ChatColor.DARK_BLUE + target.getName() + ChatColor.YELLOW + " dal " + ChatColor.LIGHT_PURPLE + "Vanish");
target.sendMessage(ChatColor.RED + "Ora non sei in Vanish. Tutti ti potranno vedere");
}
}
} else {
player.sendMessage(ChatColor.RED + "Non hai il permesso di usare questo comando");
}
return true;
}
Here the PlayerJoinEvent + updateVanish():
#EventHandler
public void OnPlayerJoin(PlayerJoinEvent e) {
updateVanish();
}
public void updateVanish() {
for (Player persone : Bukkit.getOnlinePlayers()) {
if (vanish.contains(persone)) {
for (Player persone2 : Bukkit.getOnlinePlayers() ) {
persone2.hidePlayer(this, persone);
}
}
}
}
Thanks <3!
This can be solved by creating an List containing all currently vanished players and then once a new player joins, use the PlayerJoinEvent to iterate trough all currently vanished players and vanish them for the newbie using:
Player.hidePlayer(t)
I currently do not own the tools to build proper code but if you are still indoubted after reading the above, I can detail better.

My Scoreboard is not stoppping when i do the command

Hello to whoever is reading this,
i am working on a plugin that amount other things lets you add a scoreboard with your deaths, but for the life of me i cannot figure out how to remove a scoreboard from a player, i am trying player.setScoreboard(null); but that does not seem to work, is there anyway somebody could tell me how to remove a scoreboard this is what i have
List<String> pop = new ArrayList<String>();
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (label.equalsIgnoreCase("scorecount")) {
if (!(sender instanceof Player)) {
sender.sendMessage("You cannot do that console!");
return true;
}
if (sender.hasPermission("scorecount.use")) {
if (args.length == 3) {
this.sendInvalid(sender);
Player pvp = Bukkit.getPlayer((String)args[1]);
if (pvp == null) {
sender.sendMessage((Object)ChatColor.RED + "Player " + args[1] + " is not online.");
return true;
}
if (args[0].equalsIgnoreCase("add")) {
if (pop.contains(pvp.getName())) {
sender.sendMessage((Object)ChatColor.RED + pvp.getName() + " Already has a ScoreList!");
return true;
}
pop.add(pvp.getName());
sender.sendMessage((Object)ChatColor.GREEN + pvp.getName() + " Now has the ScoreList!");
ScoreboardManager manager = Bukkit.getScoreboardManager();
Scoreboard board = (Scoreboard) manager.getNewScoreboard();
Objective obj = board.registerNewObjective("pvpScoreboard", "dummy", ChatColor.translateAlternateColorCodes('&', "&a&l<< &2&lPvP &a&l>>"));
obj.setDisplaySlot(DisplaySlot.SIDEBAR);
Score score = obj.getScore("____________");
score.setScore(3);
Score score2 = obj.getScore(ChatColor.AQUA + "Online Players: " + ChatColor.DARK_AQUA + Bukkit.getOnlinePlayers().size());
score2.setScore(2);
Score score3 = obj.getScore(ChatColor.AQUA + "Total Kills(mobs): " + ChatColor.DARK_AQUA + pvp.getStatistic(Statistic.MOB_KILLS));
score3.setScore(3);
pvp.setScoreboard(board);
pop.add(pvp.getName());
return true;
}
} else if (args.length == 2) {
Player player = Bukkit.getPlayer((String)args[1]);
if (player == null) {
sender.sendMessage((Object)ChatColor.RED + "Player " + args[1] + " is not online.");
return true;
}
if (args[0].equalsIgnoreCase("remove")) {
sender.sendMessage("Help");
Player pvp = Bukkit.getPlayer((String)args[1]);
pvp.setScoreboard(null);
pop.remove(pvp.getName());
sender.sendMessage((Object)ChatColor.GREEN + pvp.getName() + " no Longer has a Socrecount!");
pvp.setScoreboard(null);
pop.remove(pvp.getName());
return true;
}
}
}
}
return false;
}
private void sendInvalid(CommandSender sender) {
sender.sendMessage((Object)ChatColor.RED + "Invalid usage. Please use:");
sender.sendMessage((Object)ChatColor.RED + "/scorecount add <player>");
sender.sendMessage((Object)ChatColor.RED + "/scorecount remove <player>");
}
}
}```
I've not worked with Bukkit before, and I can't find the API documentation, but this tutorial suggests you need to do the following to "remove" a player's scoreboard:
pvp.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard());

Duel Plugin, sending requests and opening inventories with HashMaps

I asked a similar question to this today, but I need help with further development.
The HashMaps:
Map<UUID, UUID> duels = new HashMap<UUID, UUID>();
Map<UUID, UUID> selecting = new HashMap<UUID, UUID>();
The command:
if (cmd.getName().equalsIgnoreCase("duel")) {
if (!(args.length == 1)) {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " Usage: /duel <Player>");
return true;
} else if (args.length == 1) {
Player p = Bukkit.getServer().getPlayer(args[0]);
if (p != null) {
if (p.equals(sender)) {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " You cannot duel yourself!");
return true;
} else {
if (duels.containsKey(p) || duels.containsKey(sender)) {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "] " + ChatColor.RED + "Either you or " + ChatColor.BLUE + p.getName() + ChatColor.RED + " are already in a duel!");
return true;
} else
openKitSelector((Player) sender);
selecting.put(p.getUniqueId(), ((Player) sender).getUniqueId());
The KitSelector inventory:
public void openKitSelector(Player p) {
Inventory selector = Bukkit.createInventory(p, 9, ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "KitSelector" + ChatColor.DARK_RED + "]");
ItemStack diamond = new ItemStack(Material.DIAMOND_SWORD);
ItemMeta diamondMeta = diamond.getItemMeta();
diamondMeta.setDisplayName(ChatColor.DARK_AQUA + "Diamond Kit");
diamond.setItemMeta(diamondMeta);
ItemStack iron = new ItemStack(Material.IRON_SWORD);
ItemMeta ironMeta = iron.getItemMeta();
ironMeta.setDisplayName(ChatColor.DARK_GREEN + "Iron Kit");
iron.setItemMeta(ironMeta);
selector.setItem(0, diamond);
selector.setItem(1, iron);
p.openInventory(selector);
}
The Accept/Deny inventory:
private void openGUI(Player player) {
Inventory inv = Bukkit.createInventory(null, 9, ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "DuelRequest" + ChatColor.DARK_RED + "]");
ItemStack accept = new ItemStack(Material.EMERALD_BLOCK);
ItemMeta acceptMeta = accept.getItemMeta();
ItemStack decline = new ItemStack(Material.REDSTONE_BLOCK);
ItemMeta declineMeta = decline.getItemMeta();
acceptMeta.setDisplayName(ChatColor.GREEN + "Accept!");
accept.setItemMeta(acceptMeta);
declineMeta.setDisplayName(ChatColor.RED + "Decline!");
decline.setItemMeta(declineMeta);
inv.setItem(3, accept);
inv.setItem(5, decline);
player.openInventory(inv);
}
So what I need to happen is, when someone types /duel <player> , they will get an inventory up with a selection of items that will give them kits, which I have done correctly and tested. Then when they select a kit, the target of the /duel command will get an inventory with an accept item and decline item.
I got everything working up to the part were the target gets the inventory for accept or deny as I am not really experienced with HashMaps, and I am a little confused.
If you wanted to get the target of a duel request from the selecting HashMap, you could use:
UUID id = selecting.get(player.getUniqueId());
Player target = Bukkit.getPlayer(id);
So, if you wanted to open the kit selector for the target, you could use:
//get the UUID of the Player that is being targeted by the Player player
UUID id = selecting.get(player.getUniqueId());
//get the target Player from the UUID above
Player target = Bukkit.getPlayer(id);
//open the kit selector for the target Player above
openKitSelector(target);
To open the target's inventory after the duel requester has selected their kit, you would have to listen for InventoryCloseEvent:
#EventHandler
public void inventoryClose(InventoryCloseEvent e){
//called when a player closes their inventory
}
So, your code could look like this:
#EventHandler
public void inventoryClose(InventoryCloseEvent e){
//check if the inventory is the duel inventory
if(e.getInventory().getName().equals(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "DuelRequest" + ChatColor.DARK_RED + "]"){
//get the player who opened the inventory
Player p = (Player) e.getWhoClicked();
//now, check if the selecting HashMap contains the Player above
if(selecting.containsKey(p.getUniqueId())){
//get the target of the duel
Player target = Bukkit.getPlayer(selecting.get(p.getUniqueId()));
//open the kit selector for the target Player above
openKitSelector(target);
//now, remove the target's, and the sender's UUID from the selecting
//HashMap, to make sure that we don't accidentally open the kit
//selector for a player who is currently in a duel.
selecting.remove(p.getUniqueId());
//make sure the selecting HashMap contains the target's UUID before
//attempting to remove it
if(selecting.containsKey(target.getUniqueId())){
selecting.remove(target.getUniqueId());
}
}
}
}
If you wanted to make sure that the player clicks something in the inventory, you could use InventoryClickEvent:
#EventHandler
public void inventoryClick(InventoryClickEvent e){
//called when a player clicks something in a inventory
}
Also, you will have to switch around the order in which items are in the HashMap. Instead of using the target as the key, use the sender as the key:
selecting.put(((Player) sender).getUniqueId(), p.getUniqueId());
So, your onCommand could look like this:
if(cmd.getName().equalsIgnoreCase("duel")){
if(!(args.length == 1)){
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " Usage: /duel <Player>");
return true;
}
else if (args.length == 1){
Player p = Bukkit.getServer().getPlayer(args[0]);
if(p != null){
if(p.equals(sender)){
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " You cannot duel yourself!");
return true;
}
else{
if(duels.containsKey(p) || duels.containsKey(sender)){
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "] " + ChatColor.RED + "Either you or " + ChatColor.BLUE + p.getName() + ChatColor.RED + " are already in a duel!");
return true;
}
else{
openKitSelector((Player) sender);
//this line was changed from
//selecting.put(p.getUniqueId(), ((Player) sender).getUniqueId())
//to the new value, with the sender as the key,
//and the target as the value.
selecting.put(((Player) sender).getUniqueId(), p.getUniqueId());
}
}
}
}
}

If statement always returns false

I have a series of if statements that all seem to work except for when I reach two arguments after a command (Minecraft Bukkit server/API). With two arguments it returns false no matter what. I am using the command /jukebox play 13 to test it.
Current code:
#SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
Player p = (Player) sender;
if (cmd.getName().equalsIgnoreCase("jukebox")) {
if (args.length == 0) {
sender.sendMessage(ChatColor.GOLD + "** " + ChatColor.AQUA + "Jukebox version " + pdFile.getVersion() + ChatColor.GOLD + " **\n" + ChatColor.RED + "Usage: " + ChatColor.LIGHT_PURPLE + "/jukebox play (track)");
return true;
}
if (args.length == 1) {
if (args[0].equalsIgnoreCase("play")) {
String recordNames = "Stop, 13, Cat, Blocks, Chirp, Far, Mall, Mellohi, Stal, Strad, Ward, 11, Wait";
String regex = "\\[|\\]";
recordNames = recordNames.replaceAll(regex, "");
sender.sendMessage(ChatColor.AQUA + "Track selection: " + ChatColor.GREEN + recordNames + "\n" + ChatColor.AQUA + "Type " + ChatColor.LIGHT_PURPLE + "/jukebox play (track)" + ChatColor.AQUA + " to play a track.");
return true;
}
if (args.length == 2 && (args[0].equalsIgnoreCase("play"))) {
if (args[1].equalsIgnoreCase("13")) {
p.playEffect(p.getLocation(), Effect.RECORD_PLAY, 2256);
sender.sendMessage(ChatColor.AQUA + "Now playing " + ChatColor.GREEN + "13" + ChatColor.AQUA + ".");
return true;
}
else {
sender.sendMessage(ChatColor.AQUA + "Please enter a valid track name.");
}
}
}
}
return false;
}
}
Does anyone see why it is returning false? Just as a side note, if you see anything in here that could be coded more efficiently, feel free to suggest that too.
Your indention is throwing you off. Your closing braces are in the wrong place. Your args.length == 2 check is nested within the args.length ==1 check. At the minimum you will have to :
1. add a closing brace before the if statement for args.length == 2 check
2. delete a closing brace before the return false statement.
It looks like a bracket is in the wrong place:
if (args.length == 1) {
if (args[0].equalsIgnoreCase("play")) {
String recordNames = "Stop, 13, Cat, Blocks, Chirp, Far, Mall, Mellohi, Stal, Strad, Ward, 11, Wait";
String regex = "\\[|\\]";
recordNames = recordNames.replaceAll(regex, "");
sender.sendMessage(ChatColor.AQUA + "Track selection: " + ChatColor.GREEN + recordNames + "\n" + ChatColor.AQUA + "Type " + ChatColor.LIGHT_PURPLE + "/jukebox play (track)" + ChatColor.AQUA + " to play a track.");
return true;
}
should be:
if (args.length == 1) {
if (args[0].equalsIgnoreCase("play")) {
String recordNames = "Stop, 13, Cat, Blocks, Chirp, Far, Mall, Mellohi, Stal, Strad, Ward, 11, Wait";
String regex = "\\[|\\]";
recordNames = recordNames.replaceAll(regex, "");
sender.sendMessage(ChatColor.AQUA + "Track selection: " + ChatColor.GREEN + recordNames + "\n" + ChatColor.AQUA + "Type " + ChatColor.LIGHT_PURPLE + "/jukebox play (track)" + ChatColor.AQUA + " to play a track.");
return true;
}
}
notice the } at the end. Right now, you're code is doing this:
if(args.length == 1){
if(args.length > 1 && args[0].equalsIgnoreCase("play")){
}
}
which will always return false, as args cannot have a length of one, and also have a length greater than one
As other's have said, you've gotten the bracket nesting wrong, and your incorrect code indentation is misleading you.
Advice ... if you want to avoid this kind of problem in the future:
Pay more attention to your code style in general, and particularly indentation. It makes your code easier for >>you<< to read.
Use an IDE ... or a smart editor that is capable of correctly indenting Java. And make sure that you make use of its auto-indenting functionality.
If possible, configure your IDE / editor to use space characters not TAB characters for code indentation. If your code has TAB characters in it, then it will look different (i.e. incorrectly indented) on different systems.
As shoover pointed out the } for the if (args.length == 1) statement was in the wrong place.
The new code is:
#SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
Player p = (Player) sender;
if (cmd.getName().equalsIgnoreCase("jukebox")) {
if (args.length == 0) {
sender.sendMessage(ChatColor.GOLD + "** " + ChatColor.AQUA + "Jukebox version " + pdFile.getVersion() + ChatColor.GOLD + " **\n" + ChatColor.RED + "Usage: " + ChatColor.LIGHT_PURPLE + "/jukebox play (track)");
return true;
}
if (args.length == 1) {
if (args[0].equalsIgnoreCase("play")) {
String recordNames = "Stop, 13, Cat, Blocks, Chirp, Far, Mall, Mellohi, Stal, Strad, Ward, 11, Wait";
String regex = "\\[|\\]";
recordNames = recordNames.replaceAll(regex, "");
sender.sendMessage(ChatColor.AQUA + "Track selection: " + ChatColor.GREEN + recordNames + "\n" + ChatColor.AQUA + "Type " + ChatColor.LIGHT_PURPLE + "/jukebox play (track)" + ChatColor.AQUA + " to play a track.");
return true;
}
}
if (args.length > 1 && (args[0].equalsIgnoreCase("play"))) {
if (args[1].equalsIgnoreCase("13")) {
p.playEffect(p.getLocation(), Effect.RECORD_PLAY, 2256);
sender.sendMessage(ChatColor.AQUA + "Now playing " + ChatColor.GREEN + "13" + ChatColor.AQUA + ".");
return true;
}
else {
sender.sendMessage(ChatColor.AQUA + "Please enter a valid track name.");
return true;
}
}
}
return false;

Categories

Resources