PvP Plugin Help

Discussion in 'Plugin Development' started by ChromeHD__, Mar 1, 2014.

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

    ChromeHD__

    Code:java
    1. package com.chrome.Main;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.HashMap;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class Main extends JavaPlugin implements Listener {
    19. private HashMap<String, String> requests = new HashMap<String, String>();
    20. public File SecurityQuestions = new File(getDataFolder() + "");
    21. public File Config = new File(getDataFolder(), "Config.yml");
    22. public FileConfiguration SecurityQuestion;
    23.  
    24. public void onEnable()
    25. {
    26. //Listener pvpask = new this();
    27. Bukkit.getPluginManager().registerEvents(this, this);
    28. if(!SecurityQuestions.exists()){
    29. SecurityQuestions.mkdir();
    30. System.out.println("[SecurityQuestions] SecurityQuestions file created");
    31. }else{
    32. System.out.println("[SecurityQuestions] SecurityQuestion file detected");
    33. }
    34.  
    35. if(Config.exists()){
    36. System.out.println("[SecurityQuestions] Config.yml file detected");
    37. }else{
    38. try {
    39. Config.createNewFile();
    40. } catch (IOException e) {
    41. e.printStackTrace();
    42. }
    43. }
    44. getLogger().info("Enabled!");
    45.  
    46. }
    47.  
    48. public void onDisable(){
    49. getLogger().info("Disabled!");
    50. }
    51.  
    52. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    53. {
    54. StringBuilder sb = new StringBuilder();
    55. for (int i = 0; i < args.length; i++){
    56. sb.append(args[i]).append(" ");
    57. }
    58. String allArgs = sb.toString().trim();
    59.  
    60. Player p = (Player)sender;
    61. Player t = Bukkit.getPlayer(args[0]);
    62.  
    63. if(cmd.getLabel().equalsIgnoreCase("1v1")) {
    64. if(p.hasPermission("1v1.others")){
    65. if(args.length == 0){
    66. p.sendMessage(ChatColor.GREEN +"[1v1]: "+ ChatColor.AQUA +"/1v1 <player>");
    67. }else{
    68.  
    69. }
    70.  
    71. if (args.length == 1) {
    72. if (Bukkit.getOfflinePlayer(args[0]).isOnline()) {
    73. requests.put(sender.getName(), t.getName());
    74. p.sendMessage(ChatColor.GREEN +"[1v1]: "+ ChatColor.AQUA +"You sent a 1v1 Request to " + ChatColor.GOLD + t.getDisplayName());
    75. t.sendMessage(ChatColor.GREEN +"[1v1]: "+ ChatColor.AQUA +"You have just received a 1v1 request from " + ChatColor.GOLD + p.getDisplayName());
    76. t.sendMessage(ChatColor.GREEN +"[1v1]: "+ ChatColor.AQUA +"To Accept type /1v1accept ");
    77.  
    78. }else{
    79. p.sendMessage(ChatColor.GREEN +"[1v1]: "+ ChatColor.GOLD + allArgs + ChatColor.GREEN + " is offline!");
    80. }
    81.  
    82. }
    83. }
    84. }
    85.  
    86.  
    87. return false;
    88. }
    89. @EventHandler
    90. public void onPrepocess(PlayerCommandPreprocessEvent e) {
    91. final Player p = e.getPlayer();
    92. HashMap<String, String> requests = new HashMap<String, String>();
    93. if (e.getMessage().equalsIgnoreCase("/1v1accept") && requests.containsKey(p.getName())) {
    94. String reciever = requests.get(p.getName());
    95. p.sendMessage(ChatColor.GREEN +"[1v1]: " + ChatColor.GOLD + reciever + ChatColor.AQUA + " accepted your 1v1 Request");
    96. if (p.getServer().getPlayer(reciever) != null) {
    97. Player recieverp = p.getServer().getPlayer(reciever);
    98. recieverp.sendMessage(ChatColor.GREEN +"[1v1]: "+ ChatColor.AQUA +"You accepted " + ChatColor.GOLD + p.getName() + ChatColor.AQUA +"'s 1v1 request!");
    99. p.teleport(recieverp.getLocation());
    100. }
    101. }
    102. }
    103.  
    104. }
    105. [/i]



    Stacktrace:
    This just causes internal errors
     
  2. Offline

    Alshain01

    Line 61, you didn't check to see if there were arguments before trying to convert it to a player.
     
  3. Offline

    Gater12

    ChromeHD__
    The error states an ArrayIndexOutOfBoundsException on line 61. In 61 you declare a player variable from the first argument, WITHOUT checking first. This will throw an ArrayIndexOutOfBoundsException if obviously nothing is there (hence the null message).
     
  4. Offline

    ChromeHD__

    Seadragon91 im completely lost how would i fix this
     
  5. Offline

    Alshain01


    Adjust your check upward a bit. You did check it, just too late

    Code:java
    1.  
    2. if(args.length == 0) {
    3. p.sendMessage(ChatColor.GREEN +"[1v1]: "+ ChatColor.AQUA +"/1v1 <player>");
    4. return;
    5. }
    6. Player t = Bukkit.getPlayer(args[0]);


    Also, make sure you check that t != null and your not checking sender before casting it, sender doesn't have to be a player

    Code:java
    1.  
    2. if(!(sender instanceof Player)) {
    3. // Send an error telling them it can't be the console.
    4. return;
    5. }
    6. Player p = (Player)sender;
    7.  


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

    ChromeHD__

    Alshain01 Now it says nothing no stacktrace

    Does anyone know how I can fix this

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

    Alshain01

    Ummm, that's a good thing, isn't it?
     
  8. Offline

    ChromeHD__

  9. Offline

    Gater12

  10. Offline

    ChromeHD__

    Gater12 Gater12
    Code:java
    1. package com.chrome.PvPAsk;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.HashMap;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class Main extends JavaPlugin implements Listener {
    19. private HashMap<String, String> requests = new HashMap<String, String>();
    20. public File SecurityQuestions = new File(getDataFolder() + "");
    21. public File Config = new File(getDataFolder(), "Config.yml");
    22. public FileConfiguration SecurityQuestion;
    23.  
    24. public void onEnable()
    25. {
    26. //Listener pvpask = new this();
    27. Bukkit.getPluginManager().registerEvents(this, this);
    28. if(!SecurityQuestions.exists()){
    29. SecurityQuestions.mkdir();
    30. System.out.println("[SecurityQuestions] SecurityQuestions file created");
    31. }else{
    32. System.out.println("[SecurityQuestions] SecurityQuestion file detected");
    33. }
    34.  
    35. if(Config.exists()){
    36. System.out.println("[SecurityQuestions] Config.yml file detected");
    37. }else{
    38. try {
    39. Config.createNewFile();
    40. } catch (IOException e) {
    41. e.printStackTrace();
    42. }
    43. }
    44. getLogger().info("Enabled!");
    45.  
    46. }
    47.  
    48. public void onDisable(){
    49. getLogger().info("Disabled!");
    50. }
    51.  
    52. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    53. {
    54. StringBuilder sb = new StringBuilder();
    55. for (int i = 0; i < args.length; i++){
    56. sb.append(args[i]).append(" ");
    57. }
    58. String allArgs = sb.toString().trim();
    59.  
    60. Player p = (Player)sender;
    61. if(!(sender instanceof Player)) {
    62. System.out.println("This is a command for a player");
    63. return true;
    64. }
    65. if(cmd.getLabel().equalsIgnoreCase("1v1")) {
    66. if(p.hasPermission("1v1.others")){
    67. if(args.length == 0) {
    68. p.sendMessage(ChatColor.GREEN +"[1v1]: "+ ChatColor.AQUA +"/1v1 <player>");
    69. return true;
    70. }
    71. Player t = Bukkit.getPlayer(args[0]);
    72.  
    73. if (args.length == 1) {
    74. if (Bukkit.getOfflinePlayer(args[0]).isOnline()) {
    75. requests.put(sender.getName(), t.getName());
    76. p.sendMessage(ChatColor.GREEN +"[1v1]: "+ ChatColor.AQUA +"You sent a 1v1 Request to " + ChatColor.GOLD + t.getDisplayName());
    77. t.sendMessage(ChatColor.GREEN +"[1v1]: "+ ChatColor.AQUA +"You have just received a 1v1 request from " + ChatColor.GOLD + p.getDisplayName());
    78. t.sendMessage(ChatColor.GREEN +"[1v1]: "+ ChatColor.AQUA +"To Accept type /1v1accept ");
    79.  
    80. }else{
    81. p.sendMessage(ChatColor.GREEN +"[1v1]: "+ ChatColor.GOLD + allArgs + ChatColor.GREEN + " is offline!");
    82. }
    83.  
    84. }
    85. }
    86. }
    87.  
    88.  
    89. return false;
    90. }
    91. @EventHandler
    92. public void onPrepocess(PlayerCommandPreprocessEvent e) {
    93. final Player p = e.getPlayer();
    94. HashMap<String, String> requests = new HashMap<String, String>();
    95. if (e.getMessage().equalsIgnoreCase("/1v1accept") && requests.containsKey(p.getName())) {
    96. String reciever = requests.get(p.getName());
    97. p.sendMessage(ChatColor.GREEN +"[1v1]: " + ChatColor.GOLD + reciever + ChatColor.AQUA + " accepted your 1v1 Request");
    98. if (p.getServer().getPlayer(reciever) != null) {
    99. Player recieverp = p.getServer().getPlayer(reciever);
    100. recieverp.sendMessage(ChatColor.GREEN +"[1v1]: "+ ChatColor.AQUA +"You accepted " + ChatColor.GOLD + p.getName() + ChatColor.AQUA +"'s 1v1 request!");
    101. p.teleport(recieverp.getLocation());
    102. }
    103. }
    104. }
    105.  
    106. }
    107. [/i]
     
  11. Offline

    Alshain01

    Ah, well that wasn't the original question. To be honest, I couldn't even tell that's what it was supposed to do really.

    You shouldn't be using the PlayerCommandPreprocessEvent unless you know precisely how it works. In your case, your using it as a command handler which is wrong, all that code should be in the onCommand.

    This statement will always be false... or produce an error since your not checking that getOfflinePlayer is not null.
    Code:java
    1. Bukkit.getOfflinePlayer(args[0]).isOnline()


    This is not wrong, but inefficient. Always use an iterator when it's available. Avoid for loops.
    Code:java
    1. for (int i = 0; i < args.length; i++) {

    should be:

    Code:java
    1. for(String a : args) {
    2. sb.append(a).append(" ")
    3. }


    Clean up your code and repost it. I'm having trouble reading it.

    Your new code is checking the sender too late, you have to check before you cast.

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

    ChromeHD__

    Alshain01 can you point me to what line i need to fix

    im completely lost

    i need help someone help please

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  13. K basically this is your code:
    Code:java
    1. Player p = (Player)sender;
    2. if(!(sender instanceof Player)) {
    3. System.out.println("This is a command for a player");
    4. return true;
    5. }

    The problem is that your already saying that sender is a type of Player and then checking it after, simply put line 1 in the if statement (using else) to fix your problem.
     
  14. Offline

    ChromeHD__

Thread Status:
Not open for further replies.

Share This Page