Solved Begginer Questions about code-Hashmap + arena

Discussion in 'Plugin Development' started by ChroniclerBat, Feb 9, 2014.

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

    ChroniclerBat

    HI guys!
    obviously this is not done in any way, but this code does not run properly, and I would like to know why before i continue.
    when i send the commands, nothing happens.
    heres the code:
    Code:java
    1. package me.zaxdas247.ZombieSurvival;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Location;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.block.SignChangeEvent;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class ZombieSurvival extends JavaPlugin implements CommandExecutor, Listener{
    17. public int signCount = 0;
    18.  
    19. HashMap<String, Integer> playerCount = new HashMap<String, Integer>();
    20.  
    21. HashMap<String, Integer> Sign = new HashMap<String, Integer>();
    22.  
    23. HashMap<String, Location> spawnLocation = new HashMap<String, Location>();
    24. public void onDisable(){
    25. getLogger().info("Disabled!");
    26. }
    27.  
    28. public void onEnable(){
    29. getLogger().info("Enabled!");
    30. }
    31. // These To well logged in the Console Saying "Disabled!" and "Enabled!"
    32.  
    33. @EventHandler
    34. public boolean onCommand(Command cmd, CommandSender sender, String label,
    35. String[] args) {
    36. Player player = (Player) sender;
    37.  
    38. if (!(sender instanceof Player)) {
    39. sender.sendMessage(ChatColor.DARK_RED
    40. + "This Command Is Only Available To Players!");
    41.  
    42. } else {
    43.  
    44. if (cmd.getName().equalsIgnoreCase("zs")) {
    45. if (args[1].length() == 0) {
    46.  
    47. player.sendMessage(ChatColor.RED
    48. + "Error! Usage is: /zs <create;delete;lobby> "
    49. + ChatColor.GOLD + "ArenaName");
    50.  
    51. } else if (args[1].equalsIgnoreCase("create")) {
    52. if (args[2] == null) {
    53. player.sendMessage(ChatColor.RED
    54. + "Error! Please specify an arena name");
    55. } else {
    56.  
    57. spawnLocation.put(args[2], player.getLocation());
    58. player.sendMessage(ChatColor.RED
    59. + "You have set the spawn location for the arena "
    60. + ChatColor.GOLD + args[2] + ChatColor.RED
    61. + "!");
    62.  
    63. }
    64. } else if (args[1].equalsIgnoreCase("join")) {
    65. if (args[2] == null
    66. || spawnLocation.containsKey(args[2])) {
    67. player.sendMessage(ChatColor.RED
    68. + "Error! Please specify an arena");
    69. } else {
    70. player.teleport(spawnLocation.get(args[2]));
    71. }
    72.  
    73. }
    74. }
    75.  
    76. }
    77. return false;
    78. }
    79.  
    80.  
    81. public void onSignChange(SignChangeEvent sign) {
    82. Player player = sign.getPlayer();
    83.  
    84. if (player.isOp()) {
    85. signCount++;
    86.  
    87. if (sign.getLine(0).equalsIgnoreCase("[Marble]")) {
    88. sign.setLine(0, ChatColor.AQUA + "[MARBLE]");
    89. sign.setLine(1, ChatColor.DARK_GREEN + "ZombieS(" + signCount
    90. + ")");
    91. //if (playerCount.size() == 5)
    92. sign.setLine(2, ChatColor.AQUA + "sdassd");
    93.  
    94.  
    95. }
    96. }
    97. }
    98. }
    99.  


    and here's the plugin.yml, in case that helps:
    Code:
    name: zombi
    main: me.zaxdas247.ZombieSurvival.ZombieSurvival
    version: 1.0
    description: >
                Sals Fifth Plugin
    commands:
      zs:
          description: ZombieSurvival minigame command
    
    any help is greatly appreciate as i know, just as with anything, ill have tto learn this bukkit platfrom thing through mistakes.

    Thanks in advance,
    ChroniclerBat
     
  2. Offline

    HariHD

    ChroniclerBat You haven't registered the events in onEnable()

    Code:java
    1. Bukkit.getServer().getPluginManager().registerEvents(this, this);


    Also the command method doesn't need an @EventHandler you may want to use @Override

    Update: Also your SignChangeEvent needs a @EventHandler
     
  3. Offline

    Jugglernaught

    HariHD ChroniclerBat
    Most likely, it is in your onCommand event. I am not really sure about the cmd.getName().equals..... But I do know that if you do label.equalsIgnoreCase("..."); that works. Also, arguments start at 0 so you will need to change that. An example is, /zs create. The zs is the label and the create part is args[0]
     
  4. Offline

    Harmings

    ChroniclerBat
    You don't need to implement CommandExecutor in your main class but maybe it isn't working because of the @EventHandler you have above your onCommand method, that is only suppost to be above your events
     
  5. Offline

    ChroniclerBat

    Jugglernaught Harmings
    So my code is now this:
    Code:java
    1. package me.zaxdas247.ZombieSurvival;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Location;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.SignChangeEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15.  
    16. public class ZombieSurvival extends JavaPlugin implements Listener{
    17. public int signCount = 0;
    18.  
    19. HashMap<String, Integer> playerCount = new HashMap<String, Integer>();
    20.  
    21. HashMap<String, Integer> Sign = new HashMap<String, Integer>();
    22.  
    23. HashMap<String, Location> spawnLocation = new HashMap<String, Location>();
    24. public void onDisable(){
    25. getLogger().info("Disabled!");
    26. }
    27.  
    28. public void onEnable(){
    29. getLogger().info("Enabled!");
    30. }
    31.  
    32.  
    33.  
    34.  
    35. public boolean onCommand(Command cmd, CommandSender sender, String label,
    36. String[] args) {
    37. Player player = (Player) sender;
    38.  
    39. if (!(sender instanceof Player)) {
    40. sender.sendMessage(ChatColor.DARK_RED
    41. + "This Command Is Only Available To Players!");
    42.  
    43. } else {
    44.  
    45. if (label.equalsIgnoreCase("zs")) {
    46. if (args[0].length() == 0) {
    47.  
    48. sender.sendMessage(ChatColor.RED
    49. + "Error! Usage is: /zs <create;delete;lobby> "
    50. + ChatColor.GOLD + "ArenaName");
    51.  
    52. } else if (args[1].equalsIgnoreCase("create")) {
    53. if (args[1].length() == 0) {
    54. sender.sendMessage(ChatColor.RED
    55. + "Error! Please specify an arena name");
    56. } else {
    57.  
    58. spawnLocation.put(args[2], player.getLocation());
    59. sender.sendMessage(ChatColor.RED
    60. + "You have set the spawn location for the arena "
    61. + ChatColor.GOLD + args[2] + ChatColor.RED
    62. + "!");
    63.  
    64. }
    65. } else if (args[0].equalsIgnoreCase("join")) {
    66. if (args[1] == null
    67. || spawnLocation.containsKey(args[1])) {
    68. sender.sendMessage(ChatColor.RED
    69. + "Error! Please specify an arena");
    70. } else {
    71. player.teleport(spawnLocation.get(args[1]));
    72. }
    73.  
    74. }
    75. }
    76.  
    77. }
    78. return false;
    79. }
    80.  
    81. @EventHandler
    82. public void onSignChange(SignChangeEvent sign) {
    83. Player player = sign.getPlayer();
    84.  
    85. if (player.isOp()) {
    86. signCount++;
    87.  
    88. if (sign.getLine(0).equalsIgnoreCase("[Marble]")) {
    89. sign.setLine(0, ChatColor.AQUA + "[MARBLE]");
    90. sign.setLine(1, ChatColor.DARK_GREEN + "ZombieS(" + signCount
    91. + ")");
    92. //if (playerCount.size() == 5)
    93. sign.setLine(2, ChatColor.AQUA + "sdassd");
    94.  
    95.  
    96. }
    97. }
    98. }
    99. }
    100.  

    Edit:
    after registering events, Signs work perfectly. Please help!
    am i allowed to call in chasechocolate?
     
  6. Offline

    Jugglernaught

    ChroniclerBat
    How many, or what errors did you get? Are you getting any or is it looking fine?
     
  7. Offline

    xTigerRebornx

    ChroniclerBat Your logic for you if statement is messed up, since you are doing all the name checking in the else statement, it will only be run if they are running it from console, which I am guessing you are not (and you don't wan't to let it be sent from console based on your code)
    So, you just need to fix the logic of your if statement
     
  8. Offline

    ChroniclerBat

    Jugglernaught xTigerRebornx
    so my code is now this, but its still not working:
    Code:java
    1. package me.zaxdas247.ZombieSurvival;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.block.SignChangeEvent;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class ZombieSurvival extends JavaPlugin implements Listener {
    17. public int signCount = 0;
    18.  
    19. HashMap<String, Integer> playerCount = new HashMap<String, Integer>();
    20.  
    21. HashMap<String, Integer> Sign = new HashMap<String, Integer>();
    22.  
    23. HashMap<String, Location> spawnLocation = new HashMap<String, Location>();
    24.  
    25. public void onDisable() {
    26. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    27. getLogger().info("Disabled!");
    28. }
    29.  
    30. public void onEnable() {
    31. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    32. getLogger().info("Enabled!");
    33. }
    34.  
    35. public boolean onCommand(Command cmd, CommandSender sender, String label,
    36. String[] args) {
    37. Player player = (Player) sender;
    38.  
    39.  
    40. if (label.equalsIgnoreCase("zs")) {
    41. if (args[0].length() == 0) {
    42.  
    43. sender.sendMessage(ChatColor.RED
    44. + "Error! Usage is: /zs <create;delete;lobby> "
    45. + ChatColor.GOLD + "ArenaName");
    46.  
    47. } else if (args[1].equalsIgnoreCase("create")) {
    48. if (args[1].length() == 0) {
    49. sender.sendMessage(ChatColor.RED
    50. + "Error! Please specify an arena name");
    51. } else {
    52.  
    53. spawnLocation.put(args[2], player.getLocation());
    54. sender.sendMessage(ChatColor.RED
    55. + "You have set the spawn location for the arena "
    56. + ChatColor.GOLD + args[2] + ChatColor.RED
    57. + "!");
    58.  
    59. }
    60. } else if (args[0].equalsIgnoreCase("join")) {
    61. if (args[1] == null || spawnLocation.containsKey(args[1])) {
    62. sender.sendMessage(ChatColor.RED
    63. + "Error! Please specify an arena");
    64. } else {
    65. player.teleport(spawnLocation.get(args[1]));
    66. }
    67.  
    68. }
    69. }
    70. return false;
    71.  
    72. }
    73.  
    74.  
    75. @EventHandler
    76. public void onSignChange(SignChangeEvent sign) {
    77. Player player = sign.getPlayer();
    78.  
    79. if (player.isOp()) {
    80. signCount++;
    81.  
    82. if (sign.getLine(0).equalsIgnoreCase("[Marble]")) {
    83. sign.setLine(0, ChatColor.AQUA + "[MARBLE]");
    84. sign.setLine(1, ChatColor.DARK_GREEN + "ZombieS(" + signCount
    85. + ")");
    86. // if (playerCount.size() == 5)
    87. sign.setLine(2, ChatColor.AQUA + "sdassd");
    88.  
    89. }
    90. }
    91. }
    92. }
    93.  
    94.  

    and on reload i am getting the error:
    error while disabling zombi (is it up to date?)
     
  9. Offline

    Garris0n

    What is the rest of the error?
     
  10. Offline

    chasechocolate

    ChroniclerBat don't register your events on the onDisable() method.
     
  11. Offline

    ChroniclerBat

    chasechocolate
    thanks, that removed the error, but do you know why the commands aren't working?
     
  12. Offline

    chasechocolate

  13. Offline

    ChroniclerBat

    chasechocolate
    Im sorry, i haven't really worked with Java all that much, like 2-3 months, and i was wondering where that return statement would be?

    FIGURED IT OUT!
    Thanks for all your help guys!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page