Solved [Possibly noobish] Command won't run?

Discussion in 'Plugin Development' started by PeregrineX, Aug 22, 2014.

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

    PeregrineX

    Sorry I'm new and have a lot of stuff wrong :/

    Anyways, after I fixed my last problem, I noticed that now my command won't even run. No stack trace, no output, nothing. I'm pretty sure it has something to do with my plugin.yml, but I don't see anything wrong with it.

    Here is my code:

    Code:java
    1. package me.PeregrineX.EquipEnhancement;
    2.  
    3. import java.util.Arrays;
    4. import java.util.List;
    5. import java.util.Map;
    6. import java.util.Set;
    7. import java.util.logging.Logger;
    8.  
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Material;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.enchantments.Enchantment;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class EquipEnhancement extends JavaPlugin
    20. {
    21. public static Logger log = Logger.getLogger("Minecraft");
    22.  
    23. int timesEnhanced = 0;
    24.  
    25. public void onLoad()
    26. {
    27. log.info("[EquipEnhancement] Loaded.");
    28. }
    29.  
    30. public void onEnable()
    31. {
    32. log.info("[EquipEnhancement] Starting up.");
    33. }
    34.  
    35. public void onDisable()
    36. {
    37. log.info("[EquipEnhancement] Stopping.");
    38. }
    39.  
    40. @SuppressWarnings("deprecation")
    41. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args)
    42. {
    43. if(commandLabel.equalsIgnoreCase("enhanceversion"))
    44. {
    45. String msg = "[EquipEnhancement]" + ChatColor.ITALIC + " Version 0.1";
    46. sender.sendMessage(msg);
    47. return true;
    48. }
    49. else if(commandLabel.equalsIgnoreCase("enhance") && args.length == 1)
    50. {
    51. if(Integer.parseInt(args[0]) < 10 && Integer.parseInt(args[0]) > 0)
    52. {
    53. int minusOne = Integer.parseInt(args[0]) - 1;
    54. Player player = (Player) sender;
    55. if(isItemInInv(player))
    56. {
    57. ItemStack slot = player.getInventory().getItem(minusOne);
    58. Map<Enchantment, Integer> enchMap = slot.getEnchantments();
    59. Set<Enchantment> enchSet = enchMap.keySet();
    60. Enchantment ench = enchSet.iterator().next();
    61. for(int badValue = -1; enchMap.containsValue(badValue); badValue = -1)
    62. {
    63. enchMap.remove(ench);
    64. }
    65. if(!enchSet.isEmpty())
    66. {
    67. if(timesEnhanced < 10)
    68. {
    69. timesEnhanced++;
    70. if(timesEnhanced != 9)
    71. {
    72. if(generateChance(10 * timesEnhanced))
    73. {
    74. enhanceItem(slot, player);
    75. }
    76. else
    77. {
    78. destroyItem(slot, player);
    79. }
    80. }
    81. else
    82. {
    83. if(Math.random() * 100 == 1)
    84. {
    85. //double enchantments
    86. }
    87. else
    88. {
    89. destroyItem(slot, player);
    90. }
    91. }
    92. }
    93. else
    94. {
    95. sender.sendMessage(ChatColor.RED + "You cannot enhance an item more than 10 times!");
    96. }
    97. }
    98. else
    99. {
    100. sender.sendMessage(ChatColor.RED + "The selected item slot must contain an enchanted item!");
    101. }
    102. }
    103. }
    104. else
    105. {
    106. sender.sendMessage(ChatColor.RED + "Usage: /enhance [Item Slot]");
    107. }
    108. return true;
    109. }
    110. else if(commandLabel.equalsIgnoreCase("giveopscrolls"))
    111. {
    112. if(sender.isOp())
    113. {
    114. ItemStack enhanceScroll = Items.EnhanceScroll.getItemStack();
    115. enhanceScroll.setAmount(64);
    116. getServer().getPlayer(sender.getName()).getInventory().addItem(enhanceScroll);
    117. }
    118. else
    119. {
    120. sender.sendMessage("You must be OP to use this!");
    121. }
    122. return true;
    123. }
    124. return true;
    125. }
    126.  
    127. private boolean generateChance(double percent)
    128. {
    129. return Math.random() * 100 <= percent;
    130. }
    131.  
    132. private void destroyItem(ItemStack slot, Player player)
    133. {
    134. player.getInventory().removeItem(slot);
    135. player.sendMessage(ChatColor.RED + "Your " + slot.toString().toLowerCase() + " has been destoryed!");
    136. }
    137.  
    138. private void enhanceItem(ItemStack slot, CommandSender sender)
    139. {
    140. renameItem(slot);
    141. sender.sendMessage(ChatColor.RED + "Your " + slot.toString().toLowerCase() + " has been successfully enhanced!");
    142. }
    143.  
    144. private void renameItem(ItemStack slot)
    145. {
    146. ItemMeta im = slot.getItemMeta();
    147. final StringBuilder result = new StringBuilder(im.getDisplayName().length());
    148. String[] words = im.getDisplayName().split("\\s");
    149. for(int i = 0, l = words.length; i < l; i++)
    150. {
    151. if(i>0)
    152. {
    153. result.append(" ");
    154. }
    155. result.append(Character.toUpperCase(words.charAt(0))).append(words.substring(1));
    156. }
    157. String finalName = result.toString().trim();
    158. im.setDisplayName(ChatColor.RED + finalName + "(+" + timesEnhanced + ")");
    159. slot.setItemMeta(im);
    160. }
    161.  
    162. public static boolean isInteger(String s) {
    163. try {
    164. Integer.parseInt(s);
    165. return false;
    166. }
    167. return true;
    168. }
    169.  
    170. private boolean isItemInInv(Player player)
    171. {
    172. for (ItemStack item : player.getInventory().getContents())
    173. {
    174. if(item.getType().equals(Material.PAPER) && item.hasItemMeta())
    175. {
    176. return true;
    177. }
    178. else
    179. {
    180. return false;
    181. }
    182. }
    183. return false;
    184. }
    185.  
    186. public enum Items
    187. {
    188. EnhanceScroll(Material.PAPER, "Equip Enhancement Scroll", Arrays.asList("Use this to upgrade your item enchantments!", "Type '/enhance [item slot]' to use!"));
    189.  
    190. private Material material;
    191. private String displayName;
    192. private List<String> lore;
    193.  
    194. private Items(Material material, String displayName, List<String> lore)
    195. {
    196. this.material = material;
    197. this.displayName = displayName;
    198. this.lore = lore;
    199. }
    200.  
    201. public ItemStack getItemStack()
    202. {
    203. ItemStack itemstack = new ItemStack(material, 1);
    204. ItemMeta itemMeta = itemstack.getItemMeta();
    205. itemMeta.setDisplayName(displayName);
    206. itemMeta.setLore(lore);
    207. itemstack.setItemMeta(itemMeta);
    208. return itemstack;
    209. }
    210. }
    211. }


    Code:
    name: EquipEnhancement
     
    author: PeregrineX
    main: me.PeregrineX.EquipEnhancement.EquipEnhancement
     
    commands:
        enhanceversion:
            description: States the version of the plugin.
            usage: /enhanceversion
     
        enhance:
            description: Enhances a selected item's enchantments.
            usage: /enhance [Item Slot]
     
        giveopscrolls:
            description: Gives an OP enhancement scrolls.
            usage: /giveopscrolls
     
    version: 0.1
    If there are other errors in here, please feel free to tell me :)

    EDIT: Dangit, Bukkit keeps messing up my formatting. I'll fix it -sigh-

    EDIT2: WTF IS THIS. THE FORMATTING KEEPS MESSING UP. I CAN'T EVEN--

    EDIT3: Almost fixed it. Close enough
     
  2. Offline

    Totom3

    PeregrineX Uh yeah the formatting makes it hard to read....

    Anyways did you try using comand.getName() instead of the label ? Someone else had a similar issue.

    Also, you'll get a nice exception if someone types /enhance wuirhih3ojfe2f.

    Also #2, your plugin will only let your entire playerbase to enhance anything up to 10 times. After that, no one can enhance anything.

    Also #3, when your plugin is enabled, the counter resets, allowing one to enhance his item again 10 times (and prevent everyone else of doing so by the way).

    Also #4, who taught you to use Logger.getLogger("Minecraft"); ?! Each plugin has a special & reserved logger. To get it : this.getLogger();

    Also #5, logging that your plugin has been enabled/disabled is useless. CraftBukkit does that each time automatically.
     
  3. Offline

    macboinc

    +
    Another reason, may not be valid, but, the version section is at the bottom of the plugin.yml;
     
  4. Offline

    Onlineids

    macboinc That won't effect anything.
     
  5. Offline

    PeregrineX

    1: sender.getName() has worked before and is working for other commands.
    2: I know, I'm working on that.
    3: Ah, I was worried about if it would do that.
    4: Now that I look, I do see how that can happen.
    5: This book I got for $20. It's really new, but it must be poorly written. I almost never use it, though, which I realize now might be a good thing.
    And 6: I've seen that, I just haven't registered in my head to change it.

    Thanks for helping me! Sorry I'm such a noob like everyone else :(
     
  6. Offline

    Necrodoom

    PeregrineX 1: reread the sentence. Not sender, command.
     
  7. Offline

    PeregrineX

    Oh, sorry, I was a little confused. I will try that.

    EDIT: I am also trying to rid those other things Totom3 mentioned.
     
  8. Offline

    FabeGabeMC

    PeregrineX
    Also use the logger that comes with the api. not "Minecraft".
    I assume you watch TheBCBroz*?
    Just use getLogger();
    ~Gabe
    *I hate saying it. (No offense).
     
  9. Offline

    PeregrineX


    I don't even know who that is...? Like I said, I got it from a book...

    Read before you assume please :)
     
  10. Offline

    PandazNWafflez

    PeregrineX for each command in your plugin.yml, add getCommand(commandName).setExecutor(this); to onEnable()
     
  11. Offline

    PeregrineX

    Then how come this command worked entirely before without that?
     
  12. Offline

    PandazNWafflez

    PeregrineX Did you do what I said and it worked? Afaik you have to do what I said but I might be wrong.
     
  13. Offline

    PeregrineX

    I was just asking. I will try that now.
    EDIT: PandazNWafflez Nope, didn't work.
     
  14. Offline

    Tecno_Wizard

    PeregrineX, to clarify, theBcBroz was a youtuber who make bukkit videos. The guy had no clue what the heck he was doing. I personally used apppljuze originally, and I only ever saw one mistake in his method. Kept using the coomand label instead of cmd.getName();
     
  15. Offline

    PeregrineX

    Ah, that's why he sounded so annoyed. Thanks for clarifying.

    Sorry for double post, but I figured this would be easier.

    Okay, now NONE of my commands will even work... I have no idea what is going on.

    Here is my code now, hopefully someone will notice something:

    Code:java
    1. package me.PeregrineX.EquipEnhancement;
    2.  
    3. import java.util.Arrays;
    4. import java.util.List;
    5. import java.util.Map;
    6. import java.util.Set;
    7. import java.util.logging.Logger;
    8.  
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Material;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.enchantments.Enchantment;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class EquipEnhancement extends JavaPlugin
    19. {
    20. public Logger log = this.getLogger();
    21.  
    22. public String enhance = "enhance";
    23. public String enhanceVersion = "enhanceversion";
    24. public String giveOpScrolls = "giveopscrolls";
    25.  
    26. public void onLoad()
    27. {
    28. log.info("[EquipEnhancement] Loaded.");
    29. }
    30.  
    31. @SuppressWarnings("deprecation")
    32. public boolean onCommand(Command sender, Command command, String commandLabel, String[] args)
    33. {
    34. Player player = (Player) command;
    35. if(commandLabel.equalsIgnoreCase("enhanceversion"))
    36. {
    37. String msg = "[EquipEnhancement]" + ChatColor.ITALIC + " Version 0.1";
    38. player.sendMessage(msg);
    39. return true;
    40. }
    41. else if(commandLabel.equalsIgnoreCase("enhance") && args.length == 1)
    42. {
    43. if(Integer.parseInt(args[0]) < 10 && Integer.parseInt(args[0]) > 0)
    44. {
    45. int minusOne = Integer.parseInt(args[0]) - 1;
    46. if(isItemInInv(player))
    47. {
    48. ItemStack slot = player.getInventory().getItem(minusOne);
    49. int timesEnhanced = 0;
    50. Map<Enchantment, Integer> enchMap = slot.getEnchantments();
    51. Set<Enchantment> enchSet = enchMap.keySet();
    52. Enchantment ench = enchSet.iterator().next();
    53. for(int badValue = -1; enchMap.containsValue(badValue); badValue = -1)
    54. {
    55. enchMap.remove(ench);
    56. }
    57. if(!enchSet.isEmpty())
    58. {
    59. if(timesEnhanced < 10)
    60. {
    61. if(timesEnhanced != 9)
    62. {
    63. if(generateChance(10 * timesEnhanced))
    64. {
    65. enhanceItem(slot, player);
    66. }
    67. else
    68. {
    69. destroyItem(slot, player);
    70. }
    71. }
    72. else
    73. {
    74. if(Math.random() * 100 == 1)
    75. {
    76. //double enchantments
    77. }
    78. else
    79. {
    80. destroyItem(slot, player);
    81. }
    82. }
    83. }
    84. else
    85. {
    86. player.sendMessage(ChatColor.RED + "You cannot enhance an item more than 10 times!");
    87. }
    88. }
    89. else
    90. {
    91. player.sendMessage(ChatColor.RED + "The selected item slot must contain an enchanted item!");
    92. }
    93. }
    94. }
    95. else
    96. {
    97. player.sendMessage(ChatColor.RED + "Usage: /enhance [Item Slot]");
    98. }
    99. return true;
    100. }
    101. else if(commandLabel.equalsIgnoreCase("giveopscrolls"))
    102. {
    103. if(player.isOp())
    104. {
    105. ItemStack enhanceScroll = Items.EnhanceScroll.getItemStack();
    106. enhanceScroll.setAmount(64);
    107. getServer().getPlayer(command.getName()).getInventory().addItem(enhanceScroll);
    108. }
    109. else
    110. {
    111. player.sendMessage("You must be OP to use this!");
    112. }
    113. return true;
    114. }
    115. return true;
    116. }
    117.  
    118. private boolean generateChance(double percent)
    119. {
    120. return Math.random() * 100 <= percent;
    121. }
    122.  
    123. private void destroyItem(ItemStack slot, Player player)
    124. {
    125. player.getInventory().removeItem(slot);
    126. player.sendMessage(ChatColor.RED + "Your " + slot.toString().toLowerCase() + " has been destoryed!");
    127. }
    128.  
    129. private void enhanceItem(ItemStack slot, Player player)
    130. {
    131. renameItem(player, slot);
    132. player.sendMessage(ChatColor.RED + "Your " + slot.toString().toLowerCase() + " has been successfully enhanced!");
    133. }
    134.  
    135. private void renameItem(Player player, ItemStack slot)
    136. {
    137.  
    138. int timesEnhanced = 0;
    139. ItemMeta im = slot.getItemMeta();
    140. final StringBuilder result = new StringBuilder(im.getDisplayName().length());
    141. String[] words = im.getDisplayName().split("\\s");
    142. for(int in = 0, l = words.length; in < l; in++)
    143. {
    144. if(in>0)
    145. {
    146. result.append(" ");
    147. }
    148. result.append(Character.toUpperCase(words[in].charAt(0))).append(words[in].substring(1));
    149. }
    150. String finalName = result.toString().trim();
    151. im.setDisplayName(ChatColor.RED + finalName + "(+" + timesEnhanced + ")");
    152. slot.setItemMeta(im);
    153. }
    154.  
    155. public static boolean isInteger(String s) {
    156. try
    157. {
    158. Integer.parseInt(s);
    159. }
    160. {
    161.  
    162. return false;
    163. }
    164. return true;
    165. }
    166.  
    167. private boolean isItemInInv(Player player)
    168. {
    169. for (ItemStack item : player.getInventory().getContents())
    170. {
    171. if(item.getType().equals(Material.PAPER) && item.hasItemMeta())
    172. {
    173. return true;
    174. }
    175. else
    176. {
    177. return false;
    178. }
    179. }
    180. return false;
    181. }
    182.  
    183. public enum Items
    184. {
    185. EnhanceScroll(Material.PAPER, "Equip Enhancement Scroll", Arrays.asList("Use this to upgrade your item enchantments!", "Type '/enhance [item slot]' to use!"));
    186.  
    187. private Material material;
    188. private String displayName;
    189. private List<String> lore;
    190.  
    191. private Items(Material material, String displayName, List<String> lore)
    192. {
    193. this.material = material;
    194. this.displayName = displayName;
    195. this.lore = lore;
    196. }
    197.  
    198. private ItemStack getItemStack()
    199. {
    200. ItemStack itemstack = new ItemStack(material, 1);
    201. ItemMeta itemMeta = itemstack.getItemMeta();
    202. itemMeta.setDisplayName(displayName);
    203. itemMeta.setLore(lore);
    204. itemstack.setItemMeta(itemMeta);
    205. return itemstack;
    206. }
    207. }
    208. }


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

    Totom3

    PeregrineX Yeah, I was sure you got your reference from some worthless "tutorial" youtube video. Respect for using a book instead [diamond][diamond][diamond]

    I believe the error is at line 34. You are trying to cast the command to a player, but that's not possible. It will throw a ClassCastException and will end the method right there, preventing the rest of your code from executing.
     
  17. Offline

    PeregrineX


    Ah, that worked! Thank you!

    I'll also try to fix those other errors that you mentioned. Again, thanks for your help! :) I will mark this as solved.
     
Thread Status:
Not open for further replies.

Share This Page