Code dosen't work

Discussion in 'Plugin Development' started by Niknea, Jan 12, 2014.

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

    Niknea

    Hey guys, so I am trying to my a plugin like mcpvp monk class, however it dosen't work, the goal is when you right click the blaze rod, the person you right clicks item in hand goes into there backpack and gets replaced by another item, however it dosen't work, any ideas? Here is my code
    Code:java
    1. package me.niknea.Kits;
    2.  
    3. import java.util.HashMap;
    4. import java.util.Random;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerInteractEntityEvent;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.inventory.PlayerInventory;
    14.  
    15. public class RodListener implements Listener
    16. {
    17. Kits plugin;
    18.  
    19. public RodListener(Kits plugin)
    20. {
    21. this.plugin = plugin;
    22. }
    23.  
    24.  
    25. public int cooldown = 15;
    26. public String monkCooldownMessage = ChatColor.BLUE + "You may monk them again in %s seconds!";
    27. public String monkedMessage = ChatColor.BLUE + "Monked!";
    28. public transient HashMap<ItemStack, Long> monkStaff = new HashMap<ItemStack, Long>();
    29. public boolean sendThroughInventory = true;
    30.  
    31. @SuppressWarnings("deprecation")
    32. @EventHandler
    33. public void onRightClick(PlayerInteractEntityEvent event) {
    34. ItemStack item = event.getPlayer().getItemInHand();
    35. Player player = event.getPlayer();
    36. if (event.getRightClicked() instanceof Player && item.getType() == Material.BLAZE_ROD) {
    37. long lastUsed = 0;
    38. if (monkStaff.containsKey(item))
    39. lastUsed = monkStaff.get(item);
    40. if (lastUsed + (1000 * cooldown) > System.currentTimeMillis()) {
    41. event.getPlayer().sendMessage(
    42. String.format(monkCooldownMessage,
    43. (-((System.currentTimeMillis() - (lastUsed + (1000 * cooldown))) / 1000))));
    44. } else {
    45. PlayerInventory inv = ((Player) event.getRightClicked()).getInventory();
    46. int slot = new Random().nextInt(sendThroughInventory ? 36 : 9);
    47. ItemStack replaced = inv.getItemInHand();
    48. if (replaced == null)
    49. replaced = new ItemStack(0);
    50. ItemStack replacer = inv.getItem(slot);
    51. if (replacer == null)
    52. replacer = new ItemStack(0);
    53. inv.setItemInHand(replacer);
    54. inv.setItem(slot, replaced);
    55. monkStaff.put(item, System.currentTimeMillis());
    56. event.getPlayer().sendMessage(monkedMessage);
    57. }
    58. }
    59. }
    60. }
    61.  
    62.  
    63.  
     
    metalhedd likes this.
  2. Offline

    remremrem

    what doesn't work and what does?
     
  3. Offline

    Niknea

  4. Offline

    Gopaintman

    Need to be MUCH mroe specific than that to expect ANY help.
    Are there any internal errors?
    Did you check to make sure you have cleared all syntax events?
    Have you registered events in the onEnable method?
    Have you made sure that there is an @EventHandler where appropriate?
     
  5. Offline

    xXSniperzzXx_SD

    Did you even register the listener? Niknea
     
  6. Offline

    Niknea

    Gopaintman I have tried all that.

    xXSniperzzXx_SD Yes here is my main class
    Code:java
    1. package me.niknea.Kits;
    2.  
    3.  
    4. import java.util.ArrayList;
    5. import java.util.HashMap;
    6. import java.util.List;
    7.  
    8. import org.bukkit.Material;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.Plugin;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Kits
    14. extends JavaPlugin
    15. {
    16. public static Kits plugin;
    17. public ArrayList<String> kit = new ArrayList<String>();
    18. public ArrayList<String> fisherman = new ArrayList<String>();
    19. public ArrayList<String> woodaxethor = new ArrayList<String>();
    20. public ArrayList<String> snake = new ArrayList<String>();
    21. public ArrayList<String> slug = new ArrayList<String>();
    22. public ArrayList<String> mutant = new ArrayList<String>();
    23. public ArrayList<String> superman = new ArrayList<String>();
    24. public ArrayList<String> kangaroo = new ArrayList<String>();
    25. public ArrayList<String> monk = new ArrayList<String>();
    26. public ArrayList<String> frosty = new ArrayList<String>();
    27. public ArrayList<String> prick = new ArrayList<String>();
    28.  
    29. @SuppressWarnings({ "unchecked", "rawtypes" })
    30. static HashMap<String, Material[]> matData = new HashMap();
    31. @SuppressWarnings({ "unchecked", "rawtypes" })
    32. static List<Player> Frostynomsg = new ArrayList();
    33.  
    34. public void onEnable()
    35. {
    36. plugin = this;
    37. getServer().getPluginManager().registerEvents(new DiscListener(this), this);
    38. getServer().getPluginManager().registerEvents(new onDeath (this), this);
    39. getServer().getPluginManager().registerEvents(new onJoin (this), this);
    40. getServer().getPluginManager().registerEvents(new Quit (this), this);
    41. getServer().getPluginManager().registerEvents(new FishermanListener (this), this);
    42. getServer().getPluginManager().registerEvents(new WAxeListener (this), this);
    43. getServer().getPluginManager().registerEvents(new SnakeListener (this), this);
    44. getServer().getPluginManager().registerEvents(new SlugListener (this), this);
    45. getServer().getPluginManager().registerEvents(new KangarooListener (this), this);
    46. getServer().getPluginManager().registerEvents(new FrostyListener (this), this);
    47. getServer().getPluginManager().registerEvents(new MutListener (this), this);
    48. getServer().getPluginManager().registerEvents(new PrickListener (this), this);
    49. getServer().getPluginManager().registerEvents(new RodListener (this), this);
    50. getCommand("shadow").setExecutor(new kitShadow(this));
    51. getCommand("fisherman").setExecutor(new Fisherman(this));
    52. getCommand("monk").setExecutor(new kitMonk(this));
    53. getCommand("pvp").setExecutor(new PvP(this));
    54. getCommand("archer").setExecutor(new Archer(this));
    55. getCommand("king").setExecutor(new King(this));
    56. getCommand("knight").setExecutor(new kitKnight(this));
    57. getCommand("grandpa").setExecutor(new Grandpa(this));
    58. getCommand("scout").setExecutor(new kitScout(this));
    59. getCommand("bowser").setExecutor(new Bowser(this));
    60. getCommand("boo").setExecutor(new kitBoo(this));
    61. getCommand("jesus").setExecutor(new kitJesus(this));
    62. getCommand("tank").setExecutor(new Tank(this));
    63. getCommand("chemist").setExecutor(new kitChemist(this));
    64. getCommand("hammerbro").setExecutor(new kitHammerbro(this));
    65. getCommand("sonic").setExecutor(new kitSonic(this));
    66. getCommand("urgal").setExecutor(new kitUrgal(this));
    67. getCommand("rabbit").setExecutor(new kitRabbit(this));
    68. getCommand("thor").setExecutor(new Thor(this));
    69. getCommand("snake").setExecutor(new Snake(this));
    70. getCommand("slug").setExecutor(new Slug(this));
    71. getCommand("mutant").setExecutor(new kitMutant(this));
    72. getCommand("superman").setExecutor(new kitSuperman(this));
    73. getCommand("kangaroo").setExecutor(new kitKangaroo(this));
    74. getCommand("frosty").setExecutor(new Frosty(this));
    75. getCommand("prick").setExecutor(new kitPrick(this));
    76. }
    77.  
    78. public void onDisable() {}
    79.  
    80. public static Plugin getPlugin()
    81. {
    82. return plugin;
    83. }
    84.  
    85. }


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

    xXSniperzzXx_SD

    Niknea Can you paste the full error?
     
  8. Offline

    Niknea

    xXSniperzzXx_SD I don't quite get an error, the code just doesn't work.
     
  9. Offline

    Gopaintman

    Put in broadcasts at every step of the way and see where it stops. Basically just put in debug messages.
     
  10. Offline

    xXSniperzzXx_SD

    Niknea Have you tried adding debugs?(Every 3rd line of code(Give or take a line) add a System.out.println(<debugNumber>))
     
  11. Offline

    Niknea

    xXSniperzzXx_SD The first one I did nothing happend... Here is my code
    Code:java
    1. package me.niknea.Kits;
    2.  
    3. import java.util.HashMap;
    4. import java.util.Random;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerInteractEntityEvent;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.inventory.PlayerInventory;
    14.  
    15. public class RodListener implements Listener
    16. {
    17. Kits plugin;
    18.  
    19. public RodListener(Kits plugin)
    20. {
    21. this.plugin = plugin;
    22. }
    23.  
    24.  
    25. public int cooldown = 15;
    26. public String monkCooldownMessage = ChatColor.BLUE + "You may monk them again in %s seconds!";
    27. public String monkedMessage = ChatColor.BLUE + "Monked!";
    28. public transient HashMap<ItemStack, Long> monkStaff = new HashMap<ItemStack, Long>();
    29. public boolean sendThroughInventory = true;
    30.  
    31. @SuppressWarnings("deprecation")
    32. @EventHandler
    33. public void onRightClick(PlayerInteractEntityEvent event) {
    34. ItemStack item = event.getPlayer().getItemInHand();
    35. Player player = event.getPlayer();
    36. if (event.getRightClicked() instanceof Player && item.getType() == Material.BLAZE_ROD) {
    37. System.out.println("1");
    38. long lastUsed = 0;
    39. if (monkStaff.containsKey(item))
    40. lastUsed = monkStaff.get(item);
    41. if (lastUsed + (1000 * cooldown) > System.currentTimeMillis()) {
    42. event.getPlayer().sendMessage(
    43. String.format(monkCooldownMessage,
    44. (-((System.currentTimeMillis() - (lastUsed + (1000 * cooldown))) / 1000))));
    45. } else {
    46. PlayerInventory inv = ((Player) event.getRightClicked()).getInventory();
    47. int slot = new Random().nextInt(sendThroughInventory ? 36 : 9);
    48. ItemStack replaced = inv.getItemInHand();
    49. if (replaced == null)
    50. replaced = new ItemStack(0);
    51. ItemStack replacer = inv.getItem(slot);
    52. if (replacer == null)
    53. replacer = new ItemStack(0);
    54. inv.setItemInHand(replacer);
    55. inv.setItem(slot, replaced);
    56. monkStaff.put(item, System.currentTimeMillis());
    57. event.getPlayer().sendMessage(monkedMessage);
    58. }
    59. }
    60. }
    61. }
    62.  
    63.  
    64.  

    The "1" didin't appear, any ideas?
     
  12. Offline

    Blingdaddy1

    No errors in console?
     
  13. Offline

    Niknea

  14. Offline

    Blingdaddy1

    Very odd.
     
  15. Offline

    Niknea

  16. Offline

    Gopaintman

    In the off chance, did you make sure you're compiling your plugin to the right folder? Also that you're reloading your server?
     
  17. Offline

    Niknea

  18. Offline

    GaaTavares

    Wow, does libraryaddict knows you are using his monk? Hahah
     
  19. Offline

    Niknea

  20. Offline

    GaaTavares

    clmcd42 likes this.
  21. Offline

    Niknea

    GaaTavares ...... Last time I'm going to ask my friend to send me his *custom* code.
     
  22. Offline

    GaaTavares

    So you friend did(copied) this code?
     
  23. Offline

    Niknea

    GaaTavares Probably, I'm just going to make my own now that I know it's copied
     
  24. Offline

    xXSniperzzXx_SD

    GaaTavares

    Before the if(event.getRightClick() ...
    put a System.out.println(event.getRightClicked().toString())

    Just see what it prints
     
  25. Offline

    GaaTavares

    Do it, glad I could alert you.

    I'm not who is having issues >.>

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

    xXSniperzzXx_SD

    GaaTavares
    Opps wrong Tag :eek:

    Niknea
    Before the if(event.getRightClick() ...
    put a System.out.println(event.getRightClicked().toString())

    Just see what it prints
     
  27. Offline

    Niknea

    xXSniperzzXx_SD Just to test if its with the code or on my part, I did it, however nothing happend, here is the code
    Code:java
    1. package me.niknea.Kits;
    2.  
    3. import java.util.HashMap;
    4. import java.util.Random;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerInteractEntityEvent;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.inventory.PlayerInventory;
    14.  
    15. public class RodListener implements Listener
    16. {
    17. Kits plugin;
    18.  
    19. public RodListener(Kits plugin)
    20. {
    21. this.plugin = plugin;
    22. }
    23.  
    24.  
    25. public int cooldown = 15;
    26. public String monkCooldownMessage = ChatColor.BLUE + "You may monk them again in %s seconds!";
    27. public String monkedMessage = ChatColor.BLUE + "Monked!";
    28. public transient HashMap<ItemStack, Long> monkStaff = new HashMap<ItemStack, Long>();
    29. public boolean sendThroughInventory = true;
    30.  
    31. @SuppressWarnings("deprecation")
    32. @EventHandler
    33. public void onRightClick(PlayerInteractEntityEvent event) {
    34. ItemStack item = event.getPlayer().getItemInHand();
    35. Player player = event.getPlayer();
    36. System.out.println(event.getRightClicked().toString());
    37. System.out.println("2");
    38. if (event.getRightClicked() instanceof Player && item.getType() == Material.BLAZE_ROD) {
    39. System.out.println("1");
    40. long lastUsed = 0;
    41. if (monkStaff.containsKey(item))
    42. lastUsed = monkStaff.get(item);
    43. if (lastUsed + (1000 * cooldown) > System.currentTimeMillis()) {
    44. event.getPlayer().sendMessage(
    45. String.format(monkCooldownMessage,
    46. (-((System.currentTimeMillis() - (lastUsed + (1000 * cooldown))) / 1000))));
    47. } else {
    48. PlayerInventory inv = ((Player) event.getRightClicked()).getInventory();
    49. int slot = new Random().nextInt(sendThroughInventory ? 36 : 9);
    50. ItemStack replaced = inv.getItemInHand();
    51. if (replaced == null)
    52. replaced = new ItemStack(0);
    53. ItemStack replacer = inv.getItem(slot);
    54. if (replacer == null)
    55. replacer = new ItemStack(0);
    56. inv.setItemInHand(replacer);
    57. inv.setItem(slot, replaced);
    58. monkStaff.put(item, System.currentTimeMillis());
    59. event.getPlayer().sendMessage(monkedMessage);
    60. }
    61. }
    62. }
    63. }
    64.  
    65.  
    66.  
     
  28. Offline

    GaaTavares

    Then you are not registering your events on this class
     
Thread Status:
Not open for further replies.

Share This Page