Toggable Command not working

Discussion in 'Plugin Development' started by mydeblob, Aug 31, 2013.

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

    mydeblob

    I am trying to make a command like /afk where the first time you type it it goes on, and the second time you type it it goes off. I am just using an arraylist to do this, however it's not working. It is always going on. Any help would be greatly appreciated
    Edit: I am just using the /afk command as an example. In this the actual command is /duty
    Code:
    Code:java
    1. package mydeblob.prisontemplar.gaurd;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.HashSet;
    6. import java.util.Map;
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.GameMode;
    10. import org.bukkit.Material;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.configuration.file.FileConfiguration;
    14. import org.bukkit.enchantments.Enchantment;
    15. import org.bukkit.entity.Player;
    16. import org.bukkit.event.EventHandler;
    17. import org.bukkit.event.Listener;
    18. import org.bukkit.event.entity.PlayerDeathEvent;
    19. import org.bukkit.event.player.PlayerDropItemEvent;
    20. import org.bukkit.event.player.PlayerLoginEvent;
    21. import org.bukkit.event.player.PlayerRespawnEvent;
    22. import org.bukkit.inventory.ItemStack;
    23. import org.bukkit.inventory.PlayerInventory;
    24. import org.bukkit.plugin.java.JavaPlugin;
    25.  
    26. public class GaurdPlugin extends JavaPlugin implements Listener{
    27. HashSet<String> gaurd = new HashSet<String>();
    28. FileConfiguration config;
    29. private Map <Player, ItemStack[]> pArmor = new HashMap <Player, ItemStack[]>();
    30. private Map <Player, ItemStack[]> pItems = new HashMap <Player, ItemStack[]>();
    31. private Map <Player, ItemStack> pHand = new HashMap <Player, ItemStack>();
    32. public ArrayList<String> duty = new ArrayList<String>();
    33. public void onEnable(){
    34. getLogger().info("--------------------------------------------------------------------------------------");
    35. getLogger().info("The Gaurd Overseer Plugin has been enabled, made by mydeblob");
    36. getLogger().info("--------------------------------------------------------------------------------------");
    37. getServer().getPluginManager().registerEvents(this, this);
    38. getConfig().options().copyDefaults();
    39. saveConfig();
    40. }
    41. public void onDisable(){
    42. getLogger().info("--------------------------------------------------------------------------------------");
    43. getLogger().info("The Gaurd Plugin has been disabled, made by mydeblob, made for prison.templarcraft.org");
    44. getLogger().info("--------------------------------------------------------------------------------------");
    45. saveConfig();
    46. }
    47. @Override
    48. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    49. Player player = (Player) sender;
    50. PlayerInventory pi = player.getInventory();
    51. //itemstacks
    52. ItemStack chelm = new ItemStack(Material.CHAINMAIL_HELMET , 1);
    53. ItemStack cchest = new ItemStack(Material.CHAINMAIL_CHESTPLATE , 1);
    54. ItemStack cpants = new ItemStack(Material.CHAINMAIL_LEGGINGS , 1);
    55. ItemStack cboots = new ItemStack(Material.CHAINMAIL_BOOTS , 1);
    56. ItemStack stick = new ItemStack(Material.STICK , 1);
    57. ItemStack sword = new ItemStack(Material.DIAMOND_SWORD , 1);
    58. ItemStack enchantapple = new ItemStack(Material.GOLDEN_APPLE, 32, (short)1);
    59. ItemStack porkchop = new ItemStack(Material.GRILLED_PORK, 32);
    60. //enchantments
    61. sword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 8);
    62. sword.addUnsafeEnchantment(Enchantment.FIRE_ASPECT,4);
    63. chelm.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 20);
    64. cchest.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 20);
    65. cpants.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 20);
    66. cboots.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 20);
    67. chelm.addUnsafeEnchantment(Enchantment.DURABILITY, 50);
    68. cchest.addUnsafeEnchantment(Enchantment.DURABILITY, 50);
    69. cpants.addUnsafeEnchantment(Enchantment.DURABILITY, 50);
    70. cboots.addUnsafeEnchantment(Enchantment.DURABILITY, 50);
    71. stick.addUnsafeEnchantment(Enchantment.KNOCKBACK, 50);
    72. if(sender instanceof Player){
    73. if(player.hasPermission("gaurdoverseer.gaurd")){
    74. if(cmd.getName().equalsIgnoreCase("duty")){
    75. if(!duty.contains(player.getName())){
    76. player.setGameMode(GameMode.SURVIVAL);
    77. gaurd.add(sender.getName());
    78. pArmor.put(player, pi.getArmorContents());
    79. pItems.put(player, pi.getContents());
    80. pHand.put(player, pi.getItemInHand());
    81. pi.addItem(sword);
    82. pi.addItem(stick);
    83. pi.addItem(enchantapple);
    84. pi.addItem(porkchop);
    85. pi.setHelmet(chelm);
    86. pi.setChestplate(cchest);
    87. pi.setLeggings(cpants);
    88. pi.setBoots(cboots);
    89. Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GREEN + "TemplarPrison" + ChatColor.WHITE + "]" + ChatColor.RED + sender.getName() + " has gone on duty! To list all on duty gaurds do /dutylist!");
    90. sender.sendMessage(ChatColor.GOLD + "You have gone on duty!");
    91. }
    92. if(duty.contains(player.getName())){
    93. gaurd.remove(sender.getName());
    94. pi.clear();
    95. pi.setHelmet(new ItemStack(Material.AIR));
    96. pi.setLeggings(new ItemStack(Material.AIR));
    97. pi.setChestplate(new ItemStack(Material.AIR));
    98. pi.setBoots(new ItemStack(Material.AIR));
    99. Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GREEN + "TemplarPrison" + ChatColor.WHITE + "]" + ChatColor.RED + sender.getName() + " has gone off duty! To list all on duty gaurds do /dutylist!");
    100. sender.sendMessage(ChatColor.GOLD + "You are now off duty!");
    101. if(pArmor.containsKey(player)){
    102. pi.setArmorContents(pArmor.get(player));
    103. }
    104. if(pItems.containsKey(player)){
    105. pi.setContents(pItems.get(player));
    106. }
    107. if(pHand.containsKey(player)){
    108. pi.setItemInHand(pHand.get(player));
    109. }
    110. }
    111. return true;
    112. }
    113. if(player.hasPermission("gaurdoverseer.list")){
    114. if(cmd.getName().equalsIgnoreCase("dutylist")){
    115. sender.sendMessage(ChatColor.WHITE + "[" + ChatColor.GREEN + "TemplarPrison" + ChatColor.WHITE + "]" + "List of gaurds currently on duty: " + gaurd.toString()); //gaurd.toString lists all the players in the array
    116. return true;
    117. }
    118. }
    119. }
    120. }
    121. return false;
    122. }//End of the oncommand method
    123. @EventHandler
    124. public void onPlayerLogin(PlayerLoginEvent event){
    125. Player player = (Player) event.getPlayer();
    126. player.sendMessage( ChatColor.WHITE + "[" + ChatColor.GREEN + "TemplarPrison" + ChatColor.WHITE + "]" + ChatColor.BLUE + "Running GaurdOverseer, by mydeblob");
    127. }
    128.  
    129. @EventHandler
    130. public void onPlayerDeathEvent(PlayerDeathEvent event){
    131. Player player = (Player) event.getEntity();
    132. if(event.getEntity() instanceof Player){
    133. if(gaurd.contains(player.getName())){
    134. event.getDrops().clear();
    135.  
    136. }
    137. }
    138. }
    139. @EventHandler
    140. public void onPlayerDropItem(PlayerDropItemEvent event){
    141. Player player = (Player) event.getPlayer();
    142. if(gaurd.contains(player.getName())){
    143. event.getItemDrop(); event.setCancelled(true);
    144. }
    145. }
    146. @EventHandler
    147. public void onPlayerRespawn(PlayerRespawnEvent event){
    148. Player player = (Player) event.getPlayer();
    149. if(gaurd.contains(player.getName())){
    150. PlayerInventory pi = player.getInventory();
    151. //itemstacks
    152. ItemStack chelm = new ItemStack(Material.CHAINMAIL_HELMET , 1);
    153. ItemStack cchest = new ItemStack(Material.CHAINMAIL_CHESTPLATE , 1);
    154. ItemStack cpants = new ItemStack(Material.CHAINMAIL_LEGGINGS , 1);
    155. ItemStack cboots = new ItemStack(Material.CHAINMAIL_BOOTS , 1);
    156. ItemStack stick = new ItemStack(Material.STICK , 1);
    157. ItemStack sword = new ItemStack(Material.DIAMOND_SWORD , 1);
    158. ItemStack enchantapple = new ItemStack(Material.GOLDEN_APPLE, 32, (short)1);
    159. ItemStack porkchop = new ItemStack(Material.GRILLED_PORK, 32);
    160. //enchantments
    161. sword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 8);
    162. sword.addUnsafeEnchantment(Enchantment.FIRE_ASPECT,4);
    163. chelm.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 20);
    164. cchest.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 20);
    165. cpants.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 20);
    166. cboots.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 20);
    167. chelm.addUnsafeEnchantment(Enchantment.DURABILITY, 50);
    168. cchest.addUnsafeEnchantment(Enchantment.DURABILITY, 50);
    169. cpants.addUnsafeEnchantment(Enchantment.DURABILITY, 50);
    170. cboots.addUnsafeEnchantment(Enchantment.DURABILITY, 50);
    171. stick.addUnsafeEnchantment(Enchantment.KNOCKBACK, 50);
    172. pi.addItem(sword);
    173. pi.addItem(stick);
    174. pi.addItem(enchantapple);
    175. pi.addItem(porkchop);
    176. pi.setHelmet(chelm);
    177. pi.setChestplate(cchest);
    178. pi.setLeggings(cpants);
    179. pi.setBoots(cboots);
    180.  
    181. }
    182. }
    183. }
     
  2. Offline

    dakoslug

    Maybe my eyes are tired but I don't see you adding the player's name to the arraylist?
     
  3. Offline

    mydeblob

    dakoslug
    Line 77 and 93
    Code:java
    1. gaurd.add(sender.getName());

    Code:java
    1. gaurd.remove(sender.getName());
    2.  
     
  4. Offline

    dakoslug

    Your arraylist was named duty wasn't it?
     
  5. Offline

    mydeblob

    dakoslug
    Wow, Guess my eyes are the ones that are tired. That would be the problem. Thank you!
     
    dakoslug likes this.
Thread Status:
Not open for further replies.

Share This Page