Help me

Discussion in 'Plugin Development' started by Corndogoz, Jun 9, 2014.

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

    Corndogoz

    so my plugin for some reason always has the boolean start true, can someone please tell me why?
    Code:java
    1. /*
    2. * To change this license header, choose License Headers in Project Properties.
    3. * To change this template file, choose Tools | Templates
    4. * and open the template in the editor.
    5. */
    6. package wizardsway;
    7.  
    8. import java.util.ArrayList;
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.Location;
    12. import org.bukkit.Material;
    13. import org.bukkit.block.Sign;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.block.Action;
    18. import org.bukkit.event.player.PlayerInteractEntityEvent;
    19. import org.bukkit.event.player.PlayerInteractEvent;
    20. import org.bukkit.event.player.PlayerQuitEvent;
    21. import org.bukkit.inventory.ItemStack;
    22.  
    23. /**
    24. *
    25. * @author plankton
    26. */
    27. public class PlayerListener implements Listener {
    28.  
    29. ArrayList<Location> signs = new ArrayList<Location>();
    30.  
    31. private WizardsWay plugin;
    32.  
    33. public PlayerListener(WizardsWay plugin) {
    34. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    35. this.plugin = plugin;
    36. }
    37.  
    38. @EventHandler
    39. public void onPlayerUse(PlayerInteractEntityEvent event) {
    40. Player p = event.getPlayer();
    41. if (p.getItemInHand().getType() == Material.BLAZE_ROD && event.getRightClicked() instanceof Player) {
    42. Player strike = (Player) event.getRightClicked();
    43. if (!plugin.wizards.contains(strike.getName()) && plugin.players.contains(strike.getName())) {
    44. strike.getWorld().strikeLightning(strike.getLocation());
    45. strike.sendMessage(ChatColor.DARK_PURPLE + "[Ww]" + ChatColor.YELLOW + "You have been switched to the wizard side!");
    46. plugin.players.remove(strike.getName());
    47. plugin.turnwizard(strike);
    48. strike.getInventory().addItem(new ItemStack(Material.BLAZE_ROD));
    49. }
    50. }
    51. }
    52.  
    53.  
    54. @EventHandler
    55. public void onPlayerLeave(PlayerQuitEvent event) {
    56. if (plugin.wizards.contains(event.getPlayer().getName())) {
    57. plugin.wizards.remove(event.getPlayer().getName());
    58. } else if (plugin.players.contains(event.getPlayer().getName())) {
    59. plugin.players.remove(event.getPlayer().getName());
    60. }
    61. }
    62. }
    63.  
     
  2. Offline

    Necrodoom

    I don't see a boolean named start.
     
  3. I cannot either see any boolean variables there.

    Can you tell us which line or function is causing problems, what is it supposed to do and what is it doing now?

    The salvation is in the details.
     
  4. Offline

    Corndogoz

    Sorry, thats the player listener, here is where the problem is:
    Code:java
    1. /*
    2. * To change this license header, choose License Headers in Project Properties.
    3. * To change this template file, choose Tools | Templates
    4. * and open the template in the editor.
    5. */
    6. package wizardsway;
    7.  
    8. import java.util.ArrayList;
    9. import java.util.HashMap;
    10. import java.util.Random;
    11. import java.util.UUID;
    12. import org.bukkit.Bukkit;
    13. import org.bukkit.ChatColor;
    14. import org.bukkit.Color;
    15. import org.bukkit.Location;
    16. import org.bukkit.Material;
    17. import org.bukkit.command.Command;
    18. import org.bukkit.command.CommandSender;
    19. import org.bukkit.entity.Player;
    20. import org.bukkit.inventory.ItemStack;
    21. import org.bukkit.inventory.meta.LeatherArmorMeta;
    22. import org.bukkit.plugin.java.JavaPlugin;
    23.  
    24. /**
    25. *
    26. * @author plankton
    27. */
    28. public class WizardsWay extends JavaPlugin {
    29.  
    30. HashMap<String, Location> backlocation = new HashMap<String, Location>();
    31.  
    32. Random rand = new Random();
    33.  
    34. ArrayList<String> players = new ArrayList<String>();
    35. ArrayList<String> wizards = new ArrayList<String>();
    36.  
    37. boolean start = false;
    38.  
    39. public void onEnable() {
    40. new PlayerListener(this);
    41. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() { //for the "plugin" variable, use "this" if you're running this from the main class
    42. public void run() {
    43. if (start = true && players.size() == 1) {
    44. start = false;
    45. for (String name : players) {
    46. Bukkit.getServer().broadcastMessage(ChatColor.DARK_PURPLE + "[Ww]" + ChatColor.YELLOW + name + " has won wizardsway!");
    47. players.remove(name);
    48. }
    49.  
    50. for (String name : wizards) {
    51. Player p = (Player) Bukkit.getServer().matchPlayer(name);
    52. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    53. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    54. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    55. p.getInventory().setBoots(new ItemStack(Material.AIR));
    56. wizards.remove(p.getName());
    57. p.teleport(backlocation.get(p.getName()));
    58. }
    59. }
    60. }
    61. }, 0, 2); //The first number is the amount of ticks before the loop is started. And the second number is the number of ticks before it repeats. (there are usually ~20 ticks in a second)
    62.  
    63. }
    64.  
    65. public void onDisable() {
    66.  
    67. }
    68.  
    69. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    70. Player p = (Player) sender;
    71. if (cmd.getName().equalsIgnoreCase("ww")) {
    72. int length = args.length;
    73. if (length == 1) {
    74. if (args[0].equalsIgnoreCase("setspawn")) {
    75. getConfig().set("WizardsWay.Location", p.getLocation());
    76. saveConfig();
    77. Bukkit.broadcastMessage(ChatColor.DARK_PURPLE + "[Ww]" + ChatColor.YELLOW + "WizardsWay arena created, you may now use /ww join");
    78. }
    79. if (args[0].equalsIgnoreCase("join")) {
    80. if (start = false) {
    81. backlocation.put(p.getName(), p.getLocation());
    82. p.teleport((Location) getConfig().get("WizardsWay.Location"));
    83. int team = rand.nextInt(2);
    84. if (!players.contains(p.getName()) || !wizards.contains(p.getName())) {
    85. if (team == 0) {
    86. turnwizard(p);
    87. } else {
    88. players.add(p.getName());
    89. }
    90. } else {
    91. p.sendMessage(ChatColor.DARK_PURPLE + "[Ww]" + ChatColor.YELLOW + "You already joined the game");
    92. }
    93. } else {
    94. p.sendMessage(ChatColor.DARK_PURPLE + "[Ww]" + ChatColor.YELLOW + "Game already started");
    95. }
    96. }
    97. if (args[0].equalsIgnoreCase("start")) {
    98. if (wizards.size() > 0 && players.size() > 0) {
    99. for (String name : wizards) {
    100. Bukkit.getServer().getPlayer(name).getInventory().addItem(new ItemStack(Material.BLAZE_ROD));
    101. start = true;
    102. }
    103. } else {
    104. p.sendMessage(ChatColor.DARK_PURPLE + "[Ww]" + ChatColor.YELLOW + "Not enough players/wizards!");
    105. }
    106. }
    107. if (args[0].equalsIgnoreCase("leave")) {
    108. if (wizards.contains(p.getName()) || players.contains(p.getName())) {
    109. p.teleport(backlocation.get(p.getName()));
    110. if (wizards.contains(p.getName())) {
    111. wizards.remove(p.getName());
    112. } else if (players.contains(p.getName())) {
    113. players.remove(p.getName());
    114. }
    115. } else {
    116. p.sendMessage(ChatColor.DARK_PURPLE + "[Ww]" + ChatColor.YELLOW + "You are not in game!");
    117. }
    118. }
    119.  
    120. }
    121. }
    122. return false;
    123.  
    124. }
    125.  
    126. public void turnwizard(Player p) {
    127. ItemStack helm = new ItemStack(Material.LEATHER_HELMET);
    128. ItemStack chest = new ItemStack(Material.LEATHER_CHESTPLATE);
    129. ItemStack pants = new ItemStack(Material.LEATHER_LEGGINGS);
    130. ItemStack boots = new ItemStack(Material.LEATHER_BOOTS);
    131. LeatherArmorMeta hmeta = (LeatherArmorMeta) helm.getItemMeta();
    132. LeatherArmorMeta cmeta = (LeatherArmorMeta) chest.getItemMeta();
    133. LeatherArmorMeta pmeta = (LeatherArmorMeta) pants.getItemMeta();
    134. LeatherArmorMeta bmeta = (LeatherArmorMeta) boots.getItemMeta();
    135. hmeta.setColor(Color.YELLOW);
    136. cmeta.setColor(Color.PURPLE);
    137. pmeta.setColor(Color.YELLOW);
    138. bmeta.setColor(Color.PURPLE);
    139. helm.setItemMeta(hmeta);
    140. chest.setItemMeta(cmeta);
    141. pants.setItemMeta(pmeta);
    142. boots.setItemMeta(bmeta);
    143. p.getInventory().setHelmet(helm);
    144. p.getInventory().setChestplate(chest);
    145. p.getInventory().setLeggings(pants);
    146. p.getInventory().setBoots(boots);
    147. wizards.add(p.getName());
    148. }
    149. }
    150.  


    when i type /ww join, it always says the game started when it didnt

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

    JaguarJo

    Moved thread. This doesn't belong in WIP, it goes here.
     
  6. Offline

    theguynextdoor

    In pretty much all your if statements, you use

    if (start = false)
    or
    if (start = true)

    I believe you mean to use == for a comparison.
     
  7. Offline

    Corndogoz

    ok, i will try it and why would it make a difference?

    thank you, it works!

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

    theguynextdoor

    Glad it works. Do note this however.

    Instead of comparing booleans using ==, there are some quicker ways to compare the boolean.

    Instead of
    if (start == true) {
    you could just do
    if (start) {


    Instead of
    if (start == false) {
    you could just do
    if (!start) {

    Both ways are quicker and prevent you from missing then second = and changing it from a comparison to an assignment.
     
  9. Offline

    Corndogoz

    ok, thank you
     
Thread Status:
Not open for further replies.

Share This Page