Boolean not at the main class.

Discussion in 'Plugin Development' started by wydgabriel, Sep 16, 2014.

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

    wydgabriel

    Hello guys.
    I've transfered my gladiator stuff to another class and everything is perfect.
    But when i transfer the boolean, this is not being changed..
    So, I start with:

    boolean podetp = false;

    Well, the boolean start as false. And with some command, I put:

    podetp = true;

    And after a schedule it turns false again:

    podetp = false;

    It was working at the main class, but in another class, this boolean doesnt change to true, it's always false.

    It has not the ''=='' symbol, so, what am I doing wrong?
     
  2. Offline

    indyetoile

  3. Offline

    wydgabriel

    Code:java
    1. public class Comandos implements CommandExecutor {
    2.  
    3.  
    4.  
    5.  
    6. WithYourDestiny plugin;
    7.  
    8.  
    9.  
    10.  
    11. public boolean podetp = false;
    12. public Comandos(WithYourDestiny instance) {
    13. plugin = instance;
    14. }
    15.  
    16.  
    17.  
    18.  
    19.  
    20.  
    21. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    22.  
    23. final Player player = (Player)sender;
    24.  
    25. final Player p = (Player)sender;
    26. final Location loc = p.getLocation();
    27.  
    28. if (commandLabel.equalsIgnoreCase("gladiadoriniciar") && (sender.isOp())) {
    29.  
    30.  
    31. Bukkit.getServer().broadcastMessage(" ");
    32. Bukkit.getServer().broadcastMessage(" ");
    33. Bukkit.getServer().broadcastMessage(" ");
    34.  
    35. Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[EVENTO GLADIADOR INICIANDO EM 5 MINUTOS]");
    36. Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[DIGITE /gladiador PARA PARTICIPAR]");
    37. Bukkit.getServer().broadcastMessage(" ");
    38. Bukkit.getServer().broadcastMessage(" ");
    39. Bukkit.getServer().broadcastMessage(" ");
    40. this.podetp = true;
    41. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
    42.  
    43. {
    44. public void run() {
    45.  
    46. {

    ETC..

    Main class = WithYourDestiny
     
  4. Offline

    indyetoile

    wydgabriel
    Why don't you just use
    Code:text
    1. podetp = true;
    instead of putting this infront of it?

    Edit: And where do you define the countdown time?
     
  5. Offline

    wydgabriel

    The rest of the code is not important .
    Well, I tried dont put the ''this'' infront, but I doesnt work.
     
  6. Offline

    indyetoile

    wydgabriel
    It is, because somewhere you're setting it false again, after a scheduler which hasn't been set-up in your code.
     
  7. Offline

    McMhz

    wydgabriel
    Pretty sure it creates a new instance of the CommandExecutor every time you execute the command, Wich would reset it to false every time you execute the command, Plus that would just not work since it'd just return a "blank" object when you tried to do WhatEver.myboolean

    EDIT:
    A solution would be to store a static HashMap<UUID, Boolean> in the main class.
    If you do this make sure to either check if the HashMap contains the uuid before trying to fetch the boolean, OR do MyMainClass.myHashMap.put(playerUUID, false); in onJoin and onEnable.
     
  8. Offline

    wydgabriel

    Well , in this case, the boolean is not particular for each player, so, I would change every online player UUID ..
    I tried to put the boolean at another class that's not the command executor, and this is always true , even if I put it false..
     
  9. Offline

    McMhz

    Show me code?
     
  10. Offline

    wydgabriel

    Code:java
    1. public class Configuraciones {
    2.  
    3. private Configuraciones() { }
    4.  
    5. static Configuraciones instance = new Configuraciones();
    6. public boolean podetp = false;
    7. public static Configuraciones getInstance() {
    8. return instance;
    9. }
    10.  
    11. // THE REST //

    This is the another class that I put the boolean..

    Code:java
    1. public class Comandos implements CommandExecutor {
    2.  
    3.  
    4.  
    5.  
    6. WithYourDestiny plugin;
    7.  
    8.  
    9.  
    10.  
    11. public boolean podetp = false;
    12. public Comandos(WithYourDestiny instance) {
    13. plugin = instance;
    14. }
    15.  
    16.  
    17.  
    18.  
    19.  
    20.  
    21. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    22.  
    23. final Player player = (Player)sender;
    24.  
    25. final Player p = (Player)sender;
    26. final Location loc = p.getLocation();
    27.  
    28. if (commandLabel.equalsIgnoreCase("gladiadoriniciar") && (sender.isOp())) {
    29.  
    30.  
    31. Bukkit.getServer().broadcastMessage(" ");
    32. Bukkit.getServer().broadcastMessage(" ");
    33. Bukkit.getServer().broadcastMessage(" ");
    34.  
    35. Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[EVENTO GLADIADOR INICIANDO EM 5 MINUTOS]");
    36. Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[DIGITE /gladiador PARA PARTICIPAR]");
    37. Bukkit.getServer().broadcastMessage(" ");
    38. Bukkit.getServer().broadcastMessage(" ");
    39. Bukkit.getServer().broadcastMessage(" ");
    40. Configuraciones.getInstance().podetp = true;
    41. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
    42.  
    43. {
    44. public void run() {
    45.  
    46. {
    47.  
    48. Bukkit.getServer().broadcastMessage(" ");
    49. Bukkit.getServer().broadcastMessage(" ");
    50. Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[EVENTO GLADIADOR INICIADO!!]");
    51. Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[TEMPO RESTANTE: 10 MINUTOS]");
    52. Bukkit.getServer().broadcastMessage(" ");
    53. Bukkit.getServer().broadcastMessage(" ");
    54. Configuraciones.getInstance().podetp = false;
    55. }
    56.  
    57. }
    58. } , 200L);

    THE REST...

    Someone?

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

    FabeGabeMC

    wydgabriel
    Puedes hacer el boolean estático para que pueda ser accesado por otras clases.
    O algo así:
    Code:java
    1. private static boolean podetp = false;
    2.  
    3. public static boolean isPodetp() {
    4. return podetp;
    5. }
    6.  
    7. public static void setPodetp(boolean podetp) {
    8. tuClase.podetp = podetp;
    9. }
     
  12. It doesn't.

    wydgabriel Judging from your code, you haven't learned enough Java yet. Please learn the basics of Java before trying to make plugins, otherwise you run into issues.
     
  13. Offline

    wydgabriel

    I've never used booleans in this way.
    To set it to true and false in the middle of the code, what should I do?
    Thanks

    I think everybody is here because hasnt learned enough Java, otherwise nobody would have errors/issues.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  14. wydgabriel Not necessarily, there are plenty of people who have issues with the bukkit API - this is not a place made for basic Java issues, but sometimes you get help with them. You need to learn Java first, otherwise it will not work out.
     
  15. Offline

    wydgabriel

    Well, i'm here to learn, and we learn practicing and having issues, to in the future, we dont make the same mistakes. If you wont help me, no problem.
     
  16. wydgabriel Yes, you can and should learn from mistakes. But in order to learn from the mistakes in the first place, you need to have a base knowledge of the subject at hand. You are currently trying to learn too much at once, and it doesn't work out. Let's imagine that you've never written any English in your life. You have absolutely no idea how to. To learn, you don't jump straight in at the deep-end and try to write an epic novel, learning from your mistakes. Trying to learn basic Java from making plugins is this situation. It doesn't work, so do everybody - but mostly yourself - a favour and learn Java. I promise it isn't painful.
     
  17. Offline

    wydgabriel

    Some tip to learn java ?
     
  18. wydgabriel I'd recommend either the Oracle tutorials or buy a Java book.
     
  19. Offline

    fireblast709

    Two things:
    • Don't advice people to use static for the wrong reasons. In general you don't need any static at all.
    • Use a Set instead of a Map with the value being Boolean
     
  20. Offline

    wydgabriel

    And I still don't know why my boolean is not being changed to true...
     
  21. Offline

    fireblast709

    wydgabriel o I am sure it is changed, though I bet you create a new instance if the class to check it, in which case the boolean isn't shared (and thus false). Post the code where you try to get the value of the boolean
     
  22. Offline

    wydgabriel

    Code:java
    1. WithYourDestiny plugin;
    2.  
    3.  
    4.  
    5.  
    6. private boolean podetp = false;
    7. public Comandos(WithYourDestiny instance) {
    8. plugin = instance;
    9. }
    10.  
    11.  
    12.  
    13.  
    14.  
    15. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    16.  
    17. final Player player = (Player)sender;
    18.  
    19. final Player p = (Player)sender;
    20. final Location loc = p.getLocation();
    21.  
    22. if (commandLabel.equalsIgnoreCase("gladiadoriniciar") && (sender.isOp())) {
    23.  
    24.  
    25. Bukkit.getServer().broadcastMessage(" ");
    26. Bukkit.getServer().broadcastMessage(" ");
    27. Bukkit.getServer().broadcastMessage(" ");
    28.  
    29. Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[EVENTO GLADIADOR INICIANDO EM 5 MINUTOS]");
    30. Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[DIGITE /gladiador PARA PARTICIPAR]");
    31. Bukkit.getServer().broadcastMessage(" ");
    32. Bukkit.getServer().broadcastMessage(" ");
    33. Bukkit.getServer().broadcastMessage(" ");
    34. podetp = true;
    35. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
    36.  
    37. {
    38. public void run() {
    39.  
    40. {
    41.  
    42. Bukkit.getServer().broadcastMessage(" ");
    43. Bukkit.getServer().broadcastMessage(" ");
    44. Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[EVENTO GLADIADOR INICIADO!!]");
    45. Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[TEMPO RESTANTE: 10 MINUTOS]");
    46. Bukkit.getServer().broadcastMessage(" ");
    47. Bukkit.getServer().broadcastMessage(" ");
    48. podetp = false;
    49. }
    50.  
    51. }
    52. } , 200L);
    53.  
    54. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
    55. {
    56. public void run() {
    57.  
    58. {
    59.  
    60. Bukkit.getServer().broadcastMessage(" ");
    61. Bukkit.getServer().broadcastMessage(" ");
    62. Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[EVENTO GLADIADOR]");
    63. Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[TEMPO RESTANTE: 5 MINUTOS]");
    64. Bukkit.getServer().broadcastMessage(" ");
    65. Bukkit.getServer().broadcastMessage(" ");
    66. }
    67. }
    68. } , 400L);
    69.  
    70. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
    71. {
    72. public void run() {
    73.  
    74.  
    75.  
    76. Bukkit.getServer().broadcastMessage(" ");
    77. Bukkit.getServer().broadcastMessage(" ");
    78. Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[EVENTO GLADIADOR FINALIZADO!!]");
    79. Bukkit.getServer().broadcastMessage(" ");
    80. Bukkit.getServer().broadcastMessage(" ");
    81. for(Player p : Bukkit.getServer().getOnlinePlayers()) {
    82. int pontosatuais = Configuraciones.getInstance().getData().getInt(p.getName().toString());
    83. p.sendMessage(ChatColor.DARK_AQUA + "" + ChatColor.BOLD +"[Pontos da Arena Gladiador: "+pontosatuais+"]");
    84. if (p.getLocation().getWorld().getName().equalsIgnoreCase("glad") && (!p.hasPotionEffect(PotionEffectType.INVISIBILITY))){
    85.  
    86. p.getInventory().addItem(new ItemStack(Material.DIAMOND,1));
    87.  
    88. }
    89. if (p.hasPotionEffect(PotionEffectType.INVISIBILITY)) {
    90. p.removePotionEffect(PotionEffectType.INVISIBILITY);
    91.  
    92. Location spawn = Bukkit.getWorld("world").getSpawnLocation();
    93. p.teleport(spawn);
    94.  
    95. }
    96. Location spawn = Bukkit.getWorld("spawn").getSpawnLocation();
    97. p.teleport(spawn);
    98. }
    99. }
    100. } , 600L);
    101.  
    102.  
    103.  
    104. }
    105.  
    106. if (commandLabel.equalsIgnoreCase("gladpoints")) {
    107. int pontosatuais = Configuraciones.getInstance().getData().getInt(p.getName().toString());
    108. p.sendMessage(ChatColor.DARK_AQUA + "" + ChatColor.BOLD +"[Pontos da Arena Gladiador: "+pontosatuais+"]");
    109.  
    110.  
    111. }
    112.  
    113.  
    114.  
    115.  
    116. if (commandLabel.equalsIgnoreCase("gladiador")) {
    117. if (podetp = true) {
    118.  
    119. Location eua = Bukkit.getWorld("glad").getSpawnLocation();
    120. p.teleport(eua);
    121. } else {
    122.  
    123. p.sendMessage(ChatColor.DARK_AQUA + "" + ChatColor.BOLD +"[Gladiador em andamento/fechado, teleporte cancelado]");
    124.  
    125. }
    126.  
    127. }


    There it is, at the final of the code, commandlabel ''gladiador'' , check podetp boolean.

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

    fireblast709

    wydgabriel you used =, which is assignment. == checks whether they are equal (in fact, for booleans you don't even have to use == true)
     
  24. Offline

    wydgabriel


    Code:java
    1. if (commandLabel.equalsIgnoreCase("gladiador")) {
    2. if (podetp == true) {
    3.  


    Even with ''=='', it won't work.
     
  25. wydgabriel Debug the code by printing out the variables, and various execution paths to see which parts are executed.
     
  26. Offline

    fireblast709

    wydgabriel where do you set your command executors (post that code)
     
  27. Offline

    wydgabriel


    Code:java
    1. public void onEnable()
    2. {
    3. getServer().getPluginManager().registerEvents(this, this);
    4. new Comandos(this);
    5. new Comandosswarm(this);
    6.  
    7. getServer().getPluginManager().registerEvents(new NPCsQuests(), this);
    8. getServer().getPluginManager().registerEvents(new Listeners(this), this);
    9. PluginManager pm = getServer().getPluginManager();
    10. pm.registerEvents(new ReinoStuff(this), this);
    11. pm.registerEvents(new ItensConsumiveis(this), this);
    12. pm.registerEvents(new Listenersswarm(this), this);
    13. Configuraciones.getInstance().setup(this);
    14. getCommand("gladiador").setExecutor(new Comandos(this));
    15. getCommand("gladiadoriniciar").setExecutor(new Comandos(this));
    16. getCommand("especsair").setExecutor(new Comandos(this));
    17. getCommand("gladpoints").setExecutor(new Comandos(this));
    18. getCommand("swarminiciar").setExecutor(new Comandosswarm(this));
    19.  
    20.  
    21. }
     
  28. wydgabriel
     
  29. Offline

    wydgabriel

    Create a new instance IF THE class?

    Tried to put this.podetp == true , wont work too.
    It would be lot easier if I had english classes :(
    Sorry about bad english guys, I cant understand everything that you say.
     
  30. wydgabriel Without you knowing more Java, I don't know how to explain it to you anyway.
     
Thread Status:
Not open for further replies.

Share This Page