dispatchCommand() Not working

Discussion in 'Plugin Development' started by CraftCreeper6, Sep 8, 2014.

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

    CraftCreeper6

    Hi! I am creating a shop plugin. To reduce the hassle of linking all of the different Economy plugins I decided to use Bukkit.dispatchCommand();
    It was working perfectly fine until all of a sudden; it stopped working. Don't know why. Just did.
    Here is my method:
    Code:java
    1. public void giveMoney(Player p, int amount) {
    2. Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
    3. "eco give " + p.getName() + " " + amount);
    4. }
     
  2. Offline

    _feedDz

    its not in your method unless its something new in 1.8 (never experimented with 1.8 yet) can you post your other code? like were you call upon this?
     
  3. Offline

    CraftCreeper6

    _feedDz
    Here is my full code:)
    Code:java
    1. package me.CraftCreeper6.main;
    2.  
    3. import me.CraftCreeper6.items.Items;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.inventory.InventoryClickEvent;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.plugin.PluginManager;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class Main extends JavaPlugin implements Listener {
    19.  
    20. PluginManager pm = Bukkit.getServer().getPluginManager();
    21.  
    22. public ItemStack ITEM = null;
    23. public ItemStack ITEM1 = null;
    24. public ItemStack ITEM2 = null;
    25. public ItemStack ITEM3 = null;
    26. public ItemStack ITEM4 = null;
    27. public int amount = 0;
    28.  
    29. public void giveMoney(Player p, int amount) {
    30. Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
    31. "eco give " + p.getName() + " " + amount);
    32. }
    33.  
    34. public void onEnable() {
    35. getConfig().options().copyDefaults(true);
    36. saveConfig();
    37. pm.registerEvents(this, this);
    38. }
    39.  
    40. public void onDisable() {
    41. }
    42.  
    43. public boolean onCommand(CommandSender sender, Command cmd,
    44. String commandLabel, String[] args) {
    45. Player p = (Player) sender;
    46. if (commandLabel.equalsIgnoreCase("ksell")) {
    47. if (args.length > 1) {
    48. p.sendMessage("Error: Incorrect Command Syntax. Usage: /ksell [ITEM]");
    49. } else {
    50. if (!p.getInventory().contains(Material.matchMaterial(args[0]))) {
    51. p.sendMessage(ChatColor.RED + "Error: You do not have any "
    52. + args[0]);
    53. } else {
    54. ITEM = new ItemStack(Material.getMaterial(args[0]), 64);
    55. ITEM1 = new ItemStack(Material.getMaterial(args[0]), 32);
    56. ITEM2 = new ItemStack(Material.getMaterial(args[0]), 16);
    57. ITEM3 = new ItemStack(Material.getMaterial(args[0]), 8);
    58. ITEM4 = new ItemStack(Material.getMaterial(args[0]), 1);
    59. Items.openGUI(p, Material.matchMaterial(args[0]));
    60. }
    61. }
    62. }
    63. return true;
    64. }
    65.  
    66. @EventHandler
    67. public void onInvClick(InventoryClickEvent e) {
    68. if (!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase(
    69. "Items")) {
    70. return;
    71. }
    72. Player p = (Player) e.getWhoClicked();
    73.  
    74. e.setCancelled(true);
    75.  
    76. p.closeInventory();
    77. if ((e.getCurrentItem() == null)
    78. || (e.getCurrentItem().getType() == Material.AIR)) {
    79. p.closeInventory();
    80. return;
    81. }
    82. int a = getConfig().getInt(ITEM + "");
    83. int a2 = getConfig().getInt(ITEM + "") / 2;
    84. int a3 = getConfig().getInt(ITEM + "") / 3;
    85. int a4 = getConfig().getInt(ITEM + "") / 4;
    86. int a5 = getConfig().getInt(ITEM + "") / 5;
    87. if (e.getCurrentItem().getAmount() == 64) {
    88. if (!p.getInventory().contains(ITEM)) {
    89. return;
    90. } else
    91. p.getInventory().removeItem(ITEM);
    92. giveMoney(p, a);
    93. } else if (e.getCurrentItem().getAmount() == 32) {
    94. if (!p.getInventory().contains(ITEM1))
    95. return;
    96. else
    97. giveMoney(p, a2);
    98. p.getInventory().removeItem(ITEM1);
    99. } else if (e.getCurrentItem().getAmount() == 16) {
    100. if (!p.getInventory().contains(ITEM2, 16))
    101. return;
    102. else
    103. giveMoney(p, a3);
    104. p.getInventory().removeItem(ITEM2);
    105. } else if (e.getCurrentItem().getAmount() == 8) {
    106. if (!p.getInventory().contains(ITEM3, 8))
    107. return;
    108. else
    109. giveMoney(p, a4);
    110. p.getInventory().removeItem(ITEM3);
    111. } else if (e.getCurrentItem().getAmount() == 1) {
    112. if (!p.getInventory().contains(ITEM4, 1))
    113. return;
    114. else
    115. giveMoney(p, a5);
    116. p.getInventory().removeItem(ITEM4);
    117. }
    118. }
    119. }
     
  4. Offline

    DevRosemberg

    CraftCreeper6 Probably you didnt include your commands in the plugin.yml
     
  5. Offline

    _feedDz

    you for got the @Override above your command i know that and your @EventHandler on on disable your should saveConfig();
     
  6. Offline

    CraftCreeper6

    _feedDz
    This didn't work.

    Bump, need this bad :/

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

    _feedDz

    maybe if you do this.giveMoney(p, a) instead of giveMoney(p,a)
     
  8. Offline

    CraftCreeper6

    _feedDz
    I found out why. But now it's just giving me 0 instead of the amount I specify :/
     
  9. _feedDz @Override tags never fix things, and using the 'this' context for method calling doesn't change anthing.
    CraftCreeper6 Hook into Vault, it's pretty easy, and you'll support more plugins. :)
     
  10. Offline

    CraftCreeper6

    AdamQpzm
    How would I do this? :)
    By the way, I found the issue. For some reason. No matter what I do. The amount of money I give always returns 0 :/
     
  11. Offline

    CraftCreeper6

    AdamQpzm
    Unfortunately, I just remembered that no matter what I do, it always returns 0. Therefore hooking Vault in would do nothing :/
     
  12. CraftCreeper6 Please debug the code to see which part messes that up.
     
  13. Offline

    CraftCreeper6

    AdamQpzm
    It's the getting config part, everything else works fine.
     
  14. CraftCreeper6 Oh I see what you're doing now. 1) Somewhere after ITEM is assigned a value, print out the value of it. 2) Does your config contain that path?
     
  15. Offline

    CraftCreeper6

    AdamQpzm My config contains the path, yes. I will print the value now.

    AdamQpzm
    Ahh, I see. Because I made it an ItemStack it is printing like this:
    ItemStack{STONE x 64}
    How would I make it just STONE?
    Yes, definitely the issue. I know receive the money now :) But how do I change this? :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  16. CraftCreeper6 getType() returns a much friendlier output for your purpose ;)
     
  17. Offline

    CraftCreeper6

    AdamQpzm
    ItemStack doesn't allow getType(); :/
    Error:
    Type mismatch: cannot convert from Material to ItemStack
    On:
    ITEM = e.getCurrentItem().getType();
     
  18. CraftCreeper6 It actually does have getType() :) The error you're being given indicates that the method you're using returns a Material yet you've stated it's meant to be an ItemStack.
     
  19. Offline

    CraftCreeper6

    AdamQpzm
    I changed it to ItemStack so I could add amounts like x64, x32 etc... :)
     
  20. Offline

    AronTheGamer

    First learn to Java before telling shit ;)
     
  21. Offline

    _feedDz

    AronTheGamer i know java and you should do this.giveMoney because the give money method it in the same class so yes you should use this.givemoney
     
Thread Status:
Not open for further replies.

Share This Page