if statement is not working 100% correct.

Discussion in 'Plugin Development' started by bennie3211, Nov 4, 2014.

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

    bennie3211

    Hi Bukkit,
    I've found a glitch in my code, but I can't find out whats making it glitching :l This is what I've:

    Code:java
    1. if (kitManager.getPlayerKit(pl) != null) {
    2. if (!canChange.contains(name)) {
    3. pl.sendMessage(prefix + "§cYou already choose a kit! Click again to change.");
    4. canChange.add(name);
    5.  
    6. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin,
    7. new Runnable() {
    8.  
    9. @Override
    10. public void run() {
    11. if (canChange.contains(name)) {
    12. canChange.remove(name);
    13. }
    14. }
    15. }, 80L);
    16. return;
    17. }
    18.  
    19. canChange.remove(name);
    20. kitManager.getPlayerKit().remove(name);
    21. pl.sendMessage(prefix + "§aYou can select a new kit again!");
    22. return;
    23. }
    24.  
    25. pl.sendMessage(prefix + "§aYou have selected §2" + line[1] + "§a.");


    And this is the output if you click like a retard on the kit sign:
    https://www.dropbox.com/s/uv88fhrpzlj2ncl/Schermafdruk 2014-11-04 17.04.42.png?dl=0

    And before people are telling me that I've waited 4 seconds before I clicked again, No I didn't! So does anyone know how to solve this?

    Thnx!
     
  2. Offline

    Unica

    bennie3211
    What is the problem?
    Like, what was supposed to happen?

    Secondly, post full code.
     
  3. Offline

    Funergy

    bennie3211 I saw you are dutch :p. (me too). I saw some spelling faults but okay.
    and now ON Topic.
    please send us the code of the Bukkit Runnable of the Delay thingy
     
    bennie3211 likes this.
  4. Offline

    bennie3211

    Funergy haha yup :p
    On topic:

    This is the class that contains the interactevent:
    Code:java
    1. package sign;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import kit.Kit;
    6. import kit.KitManager;
    7. import kitpvp.KitPvP;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.Effect;
    12. import org.bukkit.Location;
    13. import org.bukkit.Material;
    14. import org.bukkit.block.Sign;
    15. import org.bukkit.entity.Player;
    16. import org.bukkit.event.EventHandler;
    17. import org.bukkit.event.Listener;
    18. import org.bukkit.event.block.Action;
    19. import org.bukkit.event.player.PlayerInteractEvent;
    20.  
    21.  
    22. public class SignManager implements Listener {
    23.  
    24. public KitPvP plugin;
    25. public KitManager kitManager = new KitManager(plugin);
    26. private ArrayList<String> canChange = new ArrayList<String>();
    27.  
    28. private String prefix = "§a[§1KitPvP+§a]§f ";
    29.  
    30. public SignManager(KitPvP plugin) {
    31. this.plugin = plugin;
    32. }
    33.  
    34. @EventHandler
    35. public void clickSign(PlayerInteractEvent event) {
    36.  
    37. Player pl = event.getPlayer();
    38. final String name = pl.getName();
    39.  
    40. if ((!event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) || (!event.getAction().equals(Action.LEFT_CLICK_BLOCK))) {
    41.  
    42. if (event.getClickedBlock() != null) {
    43.  
    44. if (event.getClickedBlock().getState() instanceof Sign) {
    45.  
    46. Location loc = event.getClickedBlock().getLocation();
    47. Sign sign = (Sign) event.getClickedBlock().getState();
    48. String[] line = sign.getLines();
    49.  
    50. if (!ChatColor.stripColor(line[0]).equalsIgnoreCase("[KitPvP]"))
    51. return;
    52.  
    53. if (kitManager.getKitByName(ChatColor.stripColor(line[1])) == null) {
    54. pl.playEffect(loc, Effect.STEP_SOUND, Material.REDSTONE_BLOCK);
    55. pl.sendMessage(prefix + "§cKit §4" + line[1] + "§c doesn't exists!");
    56. return;
    57. }
    58.  
    59. Kit k = kitManager.getKitByName(ChatColor.stripColor(line[1]));
    60.  
    61. if (!pl.isOp() || !pl.hasPermission(k.getPermission()) || !pl.hasPermission("kitpvp.kit.*")) {
    62. pl.sendMessage(prefix + "§cYou don't have the permission for this kit!");
    63. return;
    64. }
    65.  
    66.  
    67. if (kitManager.getPlayerKit(pl) != null) {
    68. if (!canChange.contains(name)) {
    69. pl.sendMessage(prefix + "§cYou already choose a kit! Click again to change.");
    70. canChange.add(name);
    71.  
    72. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin,
    73. new Runnable() {
    74.  
    75. @Override
    76. public void run() {
    77. if (canChange.contains(name)) {
    78. canChange.remove(name);
    79. }
    80. }
    81. }, 80L);
    82. return;
    83. }
    84.  
    85. canChange.remove(name);
    86. kitManager.getPlayerKit().remove(name);
    87. pl.sendMessage(prefix + "§aYou can select a new kit again!");
    88. return;
    89. }
    90.  
    91. pl.sendMessage(prefix + "§aYou have selected §2" + line[1] + "§a.");
    92. loc.getWorld().playEffect(loc, Effect.STEP_SOUND, Material.EMERALD_BLOCK);
    93.  
    94. kitManager.giveKit(pl, k);
    95. kitManager.getPlayerKit().put(name, k);
    96. }
    97. }
    98. }
    99. }
    100. }
    101.  


    And here is the KitManager class:

    Code:java
    1. package kit;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Color;
    8. import org.bukkit.Material;
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.enchantments.Enchantment;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.inventory.meta.ItemMeta;
    14. import org.bukkit.inventory.meta.LeatherArmorMeta;
    15. import org.bukkit.plugin.Plugin;
    16. import org.bukkit.potion.PotionEffect;
    17. import org.bukkit.potion.PotionEffectType;
    18.  
    19. public class KitManager {
    20.  
    21. public KitManager(KitPvP plugin) {
    22. this.plugin = plugin;
    23. }
    24.  
    25. public KitPvP plugin;
    26.  
    27. private static ArrayList<Kit> kits = new ArrayList<Kit>();
    28. public HashMap<String, Kit> playerKit = new HashMap<String, Kit>();
    29.  
    30. public ArrayList<Kit> getKit() {
    31. return kits;
    32. }
    33.  
    34. public HashMap<String, Kit> getPlayerKit() {
    35. return this.playerKit;
    36. }
    37.  
    38. public void addKit(Kit k) {
    39. kits.add(k);
    40. }
    41.  
    42. public Kit getKitByName(String name) {
    43. for (Kit k : kits) {
    44. if (k.getName().equalsIgnoreCase(name)) {
    45. return k;
    46. }
    47. }
    48. return null;
    49. }
    50.  
    51. public Kit getPlayerKit(Player pl) {
    52. if (playerKit.containsKey(pl.getName())) {
    53. return playerKit.get(pl.getName());
    54. }
    55. return null;
    56. }
    57.  
    58. @SuppressWarnings("deprecation")
    59. public void giveKit(Player pl, Kit kit) {
    60. if (kit == null)
    61. return;
    62.  
    63. for (ItemStack i : kit.getItems()) {
    64. pl.getInventory().addItem(i);
    65. }
    66.  
    67. for (String potion : kit.getPotions()) {
    68.  
    69. if (!isNumber(potion.split("\\.")[1]))
    70. continue;
    71.  
    72. PotionEffectType type = getPotion(potion);
    73.  
    74. pl.addPotionEffect(new PotionEffect(type, Integer.MAX_VALUE, Integer.parseInt(potion.split("\\.")[1])));
    75. }
    76.  
    77. for (ItemStack i : kit.getArmor()) {
    78.  
    79. if (isHelmet(i.getType()))
    80. pl.getInventory().setHelmet(i);
    81.  
    82. if (isChestplate(i.getType()))
    83. pl.getInventory().setChestplate(i);
    84.  
    85. if (isLegging(i.getType()))
    86. pl.getInventory().setLeggings(i);
    87.  
    88. if (isBoots(i.getType()))
    89. pl.getInventory().setBoots(i);
    90. }
    91.  
    92. pl.updateInventory();
    93. }
    94. }


    Everything works correctly on the part inside the PlayerInteractEvent isn't working correctly :l

    EDIT:

    If I remove the BukkitRunnable from the PlayerInteractEvent, the glitch is gone! Can someone explain me why?
     
  5. Offline

    ColonelHedgehog

    Here:
    1. Code:Java
      1. if ((!event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) || (!event.getAction().equals(Action.LEFT_CLICK_BLOCK))) {

      Are you sure about this part?
     
  6. Offline

    bennie3211

    ColonelHedgehog What do you mean? It works correctly for me, no errors show up and it executes when it has to.
     
  7. Offline

    ColonelHedgehog

    Of course no error is going to show up, but why don't you just check if it actually is a right-click/left-click block? Then you wouldn't have to check if it's null.
     
    bennie3211 likes this.
  8. Offline

    bennie3211

    ColonelHedgehog I will change it. But do you know why the if statement doesn't work correctly?
     
  9. Offline

    ColonelHedgehog

    It's difficult to tell, could you elaborate on where the code seems to derail?
     
  10. Offline

    bennie3211

    ColonelHedgehog I think it goes wrong after checking the permission, this part:
    Code:java
    1.  
    2. if (kitManager.getPlayerKit(pl) != null) {
    3. if (!canChange.contains(name)) {
    4. pl.sendMessage(prefix + "§cYou already choose a kit! Click again to change.");
    5. canChange.add(name);
    6.  
    7. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin,
    8. new Runnable() {
    9.  
    10. @Override
    11. public void run() {
    12. if (canChange.contains(name)) {
    13. canChange.remove(name);
    14. }
    15. }
    16. }, 80L);
    17. return;
    18. }
    19.  
    20. canChange.remove(name);
    21. kitManager.getPlayerKit().remove(name);
    22. pl.sendMessage(prefix + "§aYou can select a new kit again!");
    23. return;
    24. }


    How it should work: if a player selects a kit for the first time and clicks again on the sign, it will ask the player if he wants to change his kit. If so, the player should click again on the sign and it will remove the player from a hashmap. After this the player can select a new kit.
    But I don't want that the name stays in the arraylist so I've added a runnable that removes the name from the arraylist after 4 seconds.

    Now the problem is that when a player spams his mouse button, it will send the question 2 times to the player, while it shouldn't.

    When I remove the runnable, the problem is gone but I still want to remove the name after 4 seconds from the arraylist. So any idea how to do this?
     
  11. Offline

    _Filip

    Create debug messages
     
  12. Offline

    Barinade

    Use a map for delays instead

    Also, look at line 19 of your original snippet.
     
  13. Offline

    _Filip

  14. Offline

    Barinade

    It's tidier and I would imagine less load
     
Thread Status:
Not open for further replies.

Share This Page