Printing multiple arguments

Status
This thread has been locked.

rchy

Premium
Feedback score
1
Posts
716
Reactions
258
Resources
0
So I'm making a report plugin but when they specify a reason I don't know how to allow all arguments past #2

code:

Code:
        if(label.equalsIgnoreCase("report")){
            if(args.length == 2){
                Player target = Bukkit.getPlayer(args[0]);
                if(target != null){
                p.sendMessage("§6MCKitPvP »§7 You have reported " + target.getName() + " for " + args[1] + "!" );
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

Charlie

Developer & Designer
Supreme
Feedback score
1
Posts
950
Reactions
635
Resources
0
I'll write something up for you.

Give me like 15 minutes.


You have to use a string builder and what it is doing is taking everything onwards from the second argument and using a string builder to append it together.

The string reason = str.toString(); is your reason :)

rchy
 
Last edited:

rchy

Premium
Feedback score
1
Posts
716
Reactions
258
Resources
0
StringBuilder builder = new StringBuilder();

for(int i = 1; i<args.length; i++) {
builder.append(args + " ");
}
String msg = builder.toString();

Takes 2min, not 15..
I have no idea how to implement this sorry ;-;
 

Charlie

Developer & Designer
Supreme
Feedback score
1
Posts
950
Reactions
635
Resources
0
StringBuilder builder = new StringBuilder();

for(int i = 1; i<args.length; i++) {
builder.append(args + " ");
}
String msg = builder.toString();

Takes 2min, not 15..
No need to hate, I was finishing off a clients plugin... therefore I had to put that first.
 

Charlie

Developer & Designer
Supreme
Feedback score
1
Posts
950
Reactions
635
Resources
0
Wasn't hate...? Don't take it personally.
Aight, maybe it wasn't hate but you can't presume it takes me 15 minutes to create a string builder, and iterate through the arguments, to then append the args together.
 

rchy

Premium
Feedback score
1
Posts
716
Reactions
258
Resources
0
No need to hate, I was finishing off a clients plugin... therefore I had to put that first.
Code:
package me.rchy.reports;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class Reports extends JavaPlugin {
   
    @SuppressWarnings({ "deprecation", "unused" })
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
       
        if(label.equalsIgnoreCase("report")) {
            if(!(sender instanceof Player)) {
                return false;
            }
           
            Player p = (Player) sender;
           
            if(args.length == 0 || args.length == 1) {
                p.sendMessage("§6MCKitPvP » §7Please specify a player & a reason.");
            }
           
            if(args.length == 2) {
                Player target = Bukkit.getPlayer(args[1]);
               
                if(target == null) return false;
               
                p.sendMessage("§6MCKitPvP » §7You have reported " + target.getName() + "!");
               
                StringBuilder str = new StringBuilder();
                for (int i = 1; i < args.length; i++) {
                        str.append(args[i] + " ");
                }
                String reason = str.toString();
               
                // Export the report to mysql/configuration file
               
                for(Player player : Bukkit.getOnlinePlayers()){
                    if(player.hasPermission("mckitpvp.staff")){
                        List<String> staff = new ArrayList<String>();
                        staff.add(p.getName());
                       
                        for(String name : staff){
                            player.sendMessage("§6MCKitPvP » §7  " + p.getName() + " has reported " + target.getName() + " for " + reason + "!");
                        }
                    }
                }
            }
        }
        return false;
    }
}

What's wrong with this?
 

pramsing

Premium
Feedback score
1
Posts
323
Reactions
173
Resources
10
Code:
package me.rchy.reports;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class Reports extends JavaPlugin {
  
    @SuppressWarnings({ "deprecation", "unused" })
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
      
        if(label.equalsIgnoreCase("report")) {
            if(!(sender instanceof Player)) {
                return false;
            }
          
            Player p = (Player) sender;
          
            if(args.length == 0 || args.length == 1) {
                p.sendMessage("§6MCKitPvP » §7Please specify a player & a reason.");
            }
          
            if(args.length == 2) {
                Player target = Bukkit.getPlayer(args[1]);
              
                if(target == null) return false;
              
                p.sendMessage("§6MCKitPvP » §7You have reported " + target.getName() + "!");
              
                StringBuilder str = new StringBuilder();
                for (int i = 1; i < args.length; i++) {
                        str.append(args[i] + " ");
                }
                String reason = str.toString();
              
                // Export the report to mysql/configuration file
              
                for(Player player : Bukkit.getOnlinePlayers()){
                    if(player.hasPermission("mckitpvp.staff")){
                        List<String> staff = new ArrayList<String>();
                        staff.add(p.getName());
                      
                        for(String name : staff){
                            player.sendMessage("§6MCKitPvP » §7  " + p.getName() + " has reported " + target.getName() + " for " + reason + "!");
                        }
                    }
                }
            }
        }
        return false;
    }
}

What's wrong with this?
The problem is that you're checking if there's 2 args after the command, so everything after that check will only run if there's 2 args after the command. If there's more or less than 2, it wont run.
Could use >= 2 instead of == 2, this would make the code run if there's 2 or more args after the command.
 

Charlie

Developer & Designer
Supreme
Feedback score
1
Posts
950
Reactions
635
Resources
0
The problem is that you're checking if there's 2 args after the command, so everything after that check will only run if there's 2 args after the command. If there's more or less than 2, it wont run.
Could use >= 2 instead of == 2, this would make the code run if there's 2 or more args after the command.
Great catch :)[DOUBLEPOST=1452538781,1452538681][/DOUBLEPOST]
I didn't presume anything, I simply said "It doesn't take 15 min", quite frankly I said it took 2min when indeed it didn't took less than a minute but I said 2 given most peoples experience on here is quite low.
The experience bit is true. But I had to do something before writing the snippet, thus increasing the time it takes me to even start the snippet.
 

rchy

Premium
Feedback score
1
Posts
716
Reactions
258
Resources
0
Six
Code:
package me.rchy.reports;


import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class Reports extends JavaPlugin {
    @SuppressWarnings({ "deprecation", "unused" })
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
     
        if(label.equalsIgnoreCase("report")) {
            if(!(sender instanceof Player)) {
                return false;
            }
         
            Player p = (Player) sender;
         
            if(args.length < 2) {
                p.sendMessage("§6MCKitPvP » §7Please specify a player & a reason.");
            }else if(args.length == 2) {
                Player target = Bukkit.getPlayer(args[1]);
             
                if(!(target.isOnline())) return false;
             
                p.sendMessage("§6MCKitPvP » §7You have reported " + target.getName() + "!");
             
                StringBuilder str = new StringBuilder();
                for (int i = 1; i < args.length; i++) {
                        str.append(args[i] + " ");
                }
                String reason = str.toString();
             
                // Export the report to mysql/configuration file
             
                for(Player player : Bukkit.getOnlinePlayers()){
                     Bukkit.broadcast("§6MCKitPvP » §7  " + p.getName() + " has reported " + target.getName() + " for " + reason + "!", "mckitpvp.staff");
                }
            }
        }
        return false;
    }
}
    }
Code:
org.bukkit.command.CommandException: Unhandled exception executing command 'report' in plugin MCKitPvPReport v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[minecraft_server.jar:git-Bukkit-2642f9b]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[minecraft_server.jar:git-Bukkit-2642f9b]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:619) ~[minecraft_server.jar:git-Bukkit-2642f9b]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1101) [minecraft_server.jar:git-Bukkit-2642f9b]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:961) [minecraft_server.jar:git-Bukkit-2642f9b]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) [minecraft_server.jar:git-Bukkit-2642f9b]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) [minecraft_server.jar:git-Bukkit-2642f9b]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [minecraft_server.jar:git-Bukkit-2642f9b]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_65]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_65]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [minecraft_server.jar:git-Bukkit-2642f9b]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:672) [minecraft_server.jar:git-Bukkit-2642f9b]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [minecraft_server.jar:git-Bukkit-2642f9b]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:628) [minecraft_server.jar:git-Bukkit-2642f9b]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:536) [minecraft_server.jar:git-Bukkit-2642f9b]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_65]
Caused by: java.lang.NullPointerException
        at me.rchy.reports.Reports.onCommand(Reports.java:29) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[minecraft_server.jar:git-Bukkit-2642f9b]
        ... 15 more
 

Charlie

Developer & Designer
Supreme
Feedback score
1
Posts
950
Reactions
635
Resources
0
Why are you looping through all online players & broadcasting while doing so...also what's line 29


EDIT: The looping and broadcasting is my fault remove the for loop around Bukkit.broadcast
Were you trying to loop through the online players and send them the message and then realised you could do the Bukkit#broadcast method and forgot to remove the for loop ?
 
Status
This thread has been locked.
Top