Event Issue Caused By A Boolean

Discussion in 'Plugin Development' started by callum2904, Dec 22, 2013.

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

    callum2904

    Hi, I have this code to register all the blocks place/broke when a player has create a new yml...
    I am not getting any errors but when i added some debug messages in it calls the event but doesn't call the part after the if(building == true){} or the first time i tried i used if(building){}


    Code:java
    1. package me.callum2904.SimCity.RegisterBuilding;
    2.  
    3. import java.util.Arrays;
    4. import java.util.List;
    5.  
    6. import me.callum2904.SimCity.FileManager;
    7. import me.callum2904.SimCity.Main;
    8.  
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Location;
    11. import org.bukkit.block.Block;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandExecutor;
    14. import org.bukkit.command.CommandSender;
    15. import org.bukkit.entity.Player;
    16. import org.bukkit.event.EventHandler;
    17. import org.bukkit.event.Listener;
    18. import org.bukkit.event.block.BlockBreakEvent;
    19. import org.bukkit.event.block.BlockPlaceEvent;
    20.  
    21. public class CreateBuilding implements Listener, CommandExecutor{
    22.  
    23. public Main plugin;
    24. public FileManager fm;
    25.  
    26. public CreateBuilding(Main p, FileManager fm){
    27. this.plugin = p;
    28. this.fm = fm;
    29. }
    30.  
    31. private String buildname;
    32. private boolean building;
    33.  
    34. @Override
    35. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    36.  
    37. if(commandLabel.equalsIgnoreCase("Building")){
    38. if(sender instanceof Player == false){
    39.  
    40. }else{
    41.  
    42. Player player = (Player) sender;
    43.  
    44. if(args.length == 0){
    45.  
    46. }else if(args.length == 1){
    47.  
    48. if(args[0].equalsIgnoreCase("Save")){
    49.  
    50. if(building){
    51. buildname = null;
    52. building = false;
    53. }else{
    54. player.sendMessage(ChatColor.BLUE + "You have not selected/created a building.");
    55. }
    56.  
    57. }
    58.  
    59. }else if(args.length == 2){
    60.  
    61. if(args[0].equalsIgnoreCase("Create")){
    62.  
    63. fm.reloadConfig(args[1]);
    64. fm.saveConfig(args[1]);
    65.  
    66. buildname = args[1];
    67. building = true;
    68.  
    69. player.sendMessage(ChatColor.BLUE + "File " + ChatColor.GOLD + buildname + ChatColor.BLUE + " Created! You can start to build!");
    70.  
    71. }
    72.  
    73. }
    74.  
    75. }
    76. }
    77.  
    78. return false;
    79. }
    80.  
    81.  
    82.  
    83. @EventHandler
    84. public void onblockplace(BlockPlaceEvent event){
    85. Player player = event.getPlayer();
    86. player.sendMessage("DEBUG: WORKING BLOCK PLACE");
    87.  
    88. if(building == true){
    89.  
    90. player.sendMessage("DEBUG: WORKING Block Place - Boolean");
    91. Block block = event.getBlock();
    92. Location loc = block.getLocation();
    93.  
    94. String configloc = block + "," + loc.getBlockX() + "," + loc.getBlockY() + "," + loc.getBlockZ();
    95.  
    96. List<String> locations = fm.getConfig(buildname).getStringList(buildname + ".BlockList");
    97. int i = locations.size();
    98.  
    99. fm.getConfig(buildname).set(buildname + ".BlockList." + i, configloc);
    100. fm.saveConfig(buildname);
    101.  
    102. player.sendMessage(ChatColor.BLUE + "Block " + ChatColor.GOLD + block + " placed at location " + ChatColor.GOLD + loc.getBlockX() + " " + loc.getBlockY() + " " + loc.getBlockZ() + ChatColor.BLUE + "in file " + ChatColor.GOLD + buildname);
    103.  
    104. }
    105.  
    106. }
    107.  
    108. @EventHandler
    109. public void onBlockBreak(BlockBreakEvent event){
    110. Player player = event.getPlayer();
    111. player.sendMessage("DEBUG: WORKING BLOCK BREAK");
    112.  
    113. if(building == true){
    114.  
    115. player.sendMessage("DEBUG: WORKING BLOCK BREAK - Boolean");
    116. Block block = event.getBlock();
    117. Location loc = block.getLocation();
    118.  
    119. String configloc = block + "," + loc.getBlockX() + "," + loc.getBlockY() + "," + loc.getBlockZ();
    120.  
    121. for(int i = fm.getConfig(buildname).getStringList(buildname + ".BlockList").size(); i >= 0; i ++){
    122. List<String> locations = fm.getConfig(buildname).getStringList(buildname + ".BlockList");
    123. locations.remove(configloc);
    124. fm.getConfig(buildname).set(buildname + ".BlockList", Arrays.asList(locations));
    125. fm.saveConfig(buildname);
    126.  
    127. player.sendMessage(ChatColor.BLUE + "Block " + ChatColor.GOLD + block + " removed at location " + ChatColor.GOLD + loc.getBlockX() + " " + loc.getBlockY() + " " + loc.getBlockZ() + ChatColor.BLUE + "in file " + ChatColor.GOLD + buildname);
    128. }
    129. }
    130. }
    131.  
    132.  
    133.  
    134. }
    135.  
     
  2. Offline

    GusGold

    Building must be false then...

    Also, FYI:
    Rather than using
    Code:java
    1. if(something == false){
    you can just use
    Code:text
    1. if(!something){
    The '!' is a NOT operator, and basically it takes the opposite of the evaluation (or the not of it). So !true is false and !false is true. You can use this on line 38 like so:
    Code:if(!sender instanceof Player){[/syntax
    1.  
    2.  
    3. FYI #2:
    4. The code you are using will only work for 1 person online. You have 1 buildname variable and 1 building variable. Have a think about what might happen if you use the [I]/building create xxx[/I] command, and then I place/break a block? Building will be true, and I will be 'building' to your setting. Try using some hashmaps :)
     
Thread Status:
Not open for further replies.

Share This Page