How to stop a command being ran twice in the same tick?

Discussion in 'Plugin Development' started by bloodless2010, Aug 25, 2013.

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

    bloodless2010

    Hey, I'm currently making a command that makes a icon-menu popup, but I found a dupe bug, if the icon menu opens twice in the same tick, you can dupe the items (it isn't client sided, it IS server sided)
    I heard on the IRC that you can solve this by doing a scheduler and making it only be able to run once in a tick,
    How can I do it?
    Here's my command:
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("commands")){
    2. String[] spawnlore = {ChatColor.AQUA+"Warp to the spawn", ChatColor.RED+"/spawn"};
    3. String[] homelore = {ChatColor.AQUA+"Home sweet home!", ChatColor.RED+"/home"};
    4. String[] wildlore = {ChatColor.AQUA+"Fight enemies!", ChatColor.RED+"/warp wild"};
    5. String[] enchantlore = {ChatColor.AQUA+"Enchant things!", ChatColor.RED+"/warp enchant"};
    6. String[] vaultlore = {ChatColor.AQUA+"Secure Vault!", ChatColor.RED+"/warp vault"};
    7. String[] donatelore = {ChatColor.AQUA+"Donate for awesome benifits!", ChatColor.RED+"/warp donate"};
    8. String[] marketlore = {ChatColor.AQUA+"Buy awesome stuff!", ChatColor.RED+"/warp market"};
    9. String[] pvparenalore = {ChatColor.AQUA+"Battle enemies!", ChatColor.RED+"/warp pvparena"};
    10. String[] drugstorelore = {ChatColor.AQUA+"Buy & Sell drugs!", ChatColor.RED+"/warp drugstore"};
    11.  
    12.  
    13. IconMenu commandselect = new IconMenu("Commands Menu", 9, new IconMenu.OptionClickEventHandler() {
    14. @Override
    15. public void onOptionClick(IconMenu.OptionClickEvent event) {
    16.  
    17. String command = null;
    18. command = event.getName();
    19. command = command.toLowerCase();
    20. command = ChatColor.stripColor(command);
    21. if (command.equals("wild") || command.equals("enchant") || command.equals("vault") || command.equals("donate") || command.equals("market") || command.equals("pvparena") || command.equals("drugstore"))
    22. {
    23. command = "warp " + command;
    24. }
    25. if (command.equals("home")) {
    26. command = "home home";
    27. }
    28. event.getPlayer().chat("/"+command);
    29. event.setWillClose(true);
    30. }
    31. }, this)
    32. .setOption(0, new ItemStack(Material.NETHER_STAR, 1), ChatColor.GREEN+"Spawn", spawnlore)
    33. .setOption(1, new ItemStack(Material.BED, 1), ChatColor.GREEN+"Home", homelore)
    34. .setOption(2, new ItemStack(Material.DIAMOND_SWORD), ChatColor.GREEN+"Wild", wildlore)
    35. .setOption(3, new ItemStack(Material.ENCHANTMENT_TABLE), ChatColor.GREEN+"Enchant", enchantlore)
    36. .setOption(4, new ItemStack(Material.IRON_DOOR), ChatColor.GREEN+"Vault", vaultlore)
    37. .setOption(5, new ItemStack(Material.DIAMOND), ChatColor.GREEN+"Donate", donatelore)
    38. .setOption(6, new ItemStack(Material.EMERALD), ChatColor.GREEN+"Market", marketlore)
    39. .setOption(7, new ItemStack(Material.DIAMOND_CHESTPLATE), ChatColor.GREEN+"PvPArena", pvparenalore)
    40. .setOption(8, new ItemStack(Material.SUGAR), ChatColor.GREEN+"DrugStore", drugstorelore);
    41. commandselect.open((Player) sender);
    42. }
    43. return false;
    44. }


    I am using this as my IconMenu: http://hastebin.com/naxobiquwu.avrasm (Just for reference)

    Any idea how I can make sure it's only ran once every tick? Please I really need this fixed.
     
  2. Offline

    SuperOmegaCow

    add them to an array when they type the command and remove then 1 tick later. Then at the start of the command check if they are in the array.

    bloodless2010 tell me if you want code examples if you dont get it.

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

    bloodless2010

    SuperOmegaCow I have no idea sorry, I've been tring to do a delay thing but it's failing, can you show me an example?
     
  4. Offline

    SuperOmegaCow

    bloodless2010 one sec playing halo

    public ArrayList<String> halo= new ArrayList<String>();
    if(!halo.contains(sender.getName()){
    halo.add(sender.getName());

    //YOUR CODE BLABLABLA

    plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){


    public void run() {

    halo.remove(sender.getName());


    }

    }, 1);
    }
    }

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

    bloodless2010

    SuperOmegaCow
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("commands") || cmd.getName().equalsIgnoreCase("commandmenu")){
    2. String[] spawnlore = {ChatColor.AQUA+"Warp to the spawn", ChatColor.RED+"/spawn"};
    3. String[] homelore = {ChatColor.AQUA+"Home sweet home!", ChatColor.RED+"/home"};
    4. String[] wildlore = {ChatColor.AQUA+"Fight enemies!", ChatColor.RED+"/warp wild"};
    5. String[] enchantlore = {ChatColor.AQUA+"Enchant things!", ChatColor.RED+"/warp enchant"};
    6. String[] vaultlore = {ChatColor.AQUA+"Secure Vault!", ChatColor.RED+"/warp vault"};
    7. String[] donatelore = {ChatColor.AQUA+"Donate for awesome benifits!", ChatColor.RED+"/warp donate"};
    8. String[] marketlore = {ChatColor.AQUA+"Buy awesome stuff!", ChatColor.RED+"/warp market"};
    9. String[] pvparenalore = {ChatColor.AQUA+"Battle enemies!", ChatColor.RED+"/warp pvparena"};
    10. String[] drugstorelore = {ChatColor.AQUA+"Buy & Sell drugs!", ChatColor.RED+"/warp drugstore"};
    11. final ArrayList<String> ppl= new ArrayList<String>();
    12.  
    13. IconMenu commandselect = new IconMenu("Commands Menu", 9, new IconMenu.OptionClickEventHandler() {
    14. @Override
    15. public void onOptionClick(IconMenu.OptionClickEvent event) {
    16. String command = null;
    17. command = event.getName();
    18. command = command.toLowerCase();
    19. command = ChatColor.stripColor(command);
    20. if (command.equals("wild") || command.equals("enchant") || command.equals("vault") || command.equals("donate") || command.equals("market") || command.equals("pvparena") || command.equals("drugstore"))
    21. {
    22. command = "warp " + command;
    23. }
    24. if (command.equals("home")) {
    25. command = "home home";
    26. }
    27. event.getPlayer().chat("/"+command);
    28. event.setWillClose(true);
    29. }
    30. }, this)
    31. .setOption(0, new ItemStack(Material.NETHER_STAR, 1), ChatColor.GREEN+"Spawn", spawnlore)
    32. .setOption(1, new ItemStack(Material.BED, 1), ChatColor.GREEN+"Home", homelore)
    33. .setOption(2, new ItemStack(Material.DIAMOND_SWORD), ChatColor.GREEN+"Wild", wildlore)
    34. .setOption(3, new ItemStack(Material.ENCHANTMENT_TABLE), ChatColor.GREEN+"Enchant", enchantlore)
    35. .setOption(4, new ItemStack(Material.IRON_DOOR), ChatColor.GREEN+"Vault", vaultlore)
    36. .setOption(5, new ItemStack(Material.DIAMOND), ChatColor.GREEN+"Donate", donatelore)
    37. .setOption(6, new ItemStack(Material.EMERALD), ChatColor.GREEN+"Market", marketlore)
    38. .setOption(7, new ItemStack(Material.DIAMOND_CHESTPLATE), ChatColor.GREEN+"PvPArena", pvparenalore)
    39. .setOption(8, new ItemStack(Material.SUGAR), ChatColor.GREEN+"DrugStore", drugstorelore);
    40. if(!ppl.contains(sender.getName())) {
    41. ppl.add(sender.getName());
    42. commandselect.open((Player) sender); //opens the menu thing
    43. getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    44. public void run() {
    45. ppl.remove(sender.getName());
    46. }
    47. }, 1);
    48. }
    49. }


    Is the code I am using, but still doesn't work, what am I doing wrong
     
  6. Offline

    SuperOmegaCow

    bloodless2010 here is the whole code
    Code:java
    1. public ArrayList<String> halo= new ArrayList<String>();
    2. if(cmd.getName().equalsIgnoreCase("commands") || cmd.getName().equalsIgnoreCase("commandmenu")){
    3. if(!halo.contains(sender.getName()){
    4. halo.add(sender.getName());
    5.  
    6. String[] spawnlore = {ChatColor.AQUA+"Warp to the spawn", ChatColor.RED+"/spawn"};
    7. String[] homelore = {ChatColor.AQUA+"Home sweet home!", ChatColor.RED+"/home"};
    8. String[] wildlore = {ChatColor.AQUA+"Fight enemies!", ChatColor.RED+"/warp wild"};
    9. String[] enchantlore = {ChatColor.AQUA+"Enchant things!", ChatColor.RED+"/warp enchant"};
    10. String[] vaultlore = {ChatColor.AQUA+"Secure Vault!", ChatColor.RED+"/warp vault"};
    11. String[] donatelore = {ChatColor.AQUA+"Donate for awesome benifits!", ChatColor.RED+"/warp donate"};
    12. String[] marketlore = {ChatColor.AQUA+"Buy awesome stuff!", ChatColor.RED+"/warp market"};
    13. String[] pvparenalore = {ChatColor.AQUA+"Battle enemies!", ChatColor.RED+"/warp pvparena"};
    14. String[] drugstorelore = {ChatColor.AQUA+"Buy & Sell drugs!", ChatColor.RED+"/warp drugstore"};
    15. final ArrayList<String> ppl= new ArrayList<String>();
    16.  
    17. IconMenu commandselect = new IconMenu("Commands Menu", 9, new IconMenu.OptionClickEventHandler() {
    18. @Override
    19. public void onOptionClick(IconMenu.OptionClickEvent event) {
    20. String command = null;
    21. command = event.getName();
    22. command = command.toLowerCase();
    23. command = ChatColor.stripColor(command);
    24. if (command.equals("wild") || command.equals("enchant") || command.equals("vault") || command.equals("donate") || command.equals("market") || command.equals("pvparena") || command.equals("drugstore"))
    25. {
    26. command = "warp " + command;
    27. }
    28. if (command.equals("home")) {
    29. command = "home home";
    30. }
    31. event.getPlayer().chat("/"+command);
    32. event.setWillClose(true);
    33. }
    34. }, this)
    35. .setOption(0, new ItemStack(Material.NETHER_STAR, 1), ChatColor.GREEN+"Spawn", spawnlore)
    36. .setOption(1, new ItemStack(Material.BED, 1), ChatColor.GREEN+"Home", homelore)
    37. .setOption(2, new ItemStack(Material.DIAMOND_SWORD), ChatColor.GREEN+"Wild", wildlore)
    38. .setOption(3, new ItemStack(Material.ENCHANTMENT_TABLE), ChatColor.GREEN+"Enchant", enchantlore)
    39. .setOption(4, new ItemStack(Material.IRON_DOOR), ChatColor.GREEN+"Vault", vaultlore)
    40. .setOption(5, new ItemStack(Material.DIAMOND), ChatColor.GREEN+"Donate", donatelore)
    41. .setOption(6, new ItemStack(Material.EMERALD), ChatColor.GREEN+"Market", marketlore)
    42. .setOption(7, new ItemStack(Material.DIAMOND_CHESTPLATE), ChatColor.GREEN+"PvPArena", pvparenalore)
    43. .setOption(8, new ItemStack(Material.SUGAR), ChatColor.GREEN+"DrugStore", drugstorelore);
    44.  
    45. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    46.  
    47.  
    48. public void run() {
    49.  
    50. halo.remove(sender.getName());
    51.  
    52.  
    53. }
    54.  
    55. }, 1);
    56. }
    57. }}
     
  7. Offline

    bloodless2010

    SuperOmegaCow Well no, you can only use 'final' not public, and that code doesn't work, I tried it myself.
     
  8. Offline

    SuperOmegaCow

    bloodless2010 I have done the exact same thing and it worked for me!
     
  9. Offline

    bloodless2010

    SuperOmegaCow Bullshit, you missed out
    commandselect.open((Player) sender);
    Which opens the menu, so it's IMPOSSIBLE for it to have worked for you, please learn Java before trying to help people, please!
     
  10. Offline

    SuperOmegaCow

    bloodless2010 well I don"t have eclipse to show errors atm so if you just think about the code I gave logically you would understand what you have to do.
     
  11. Offline

    bloodless2010

    SuperOmegaCow Yes, logically you would think it would, but it doesn't.
     
Thread Status:
Not open for further replies.

Share This Page