Solved Command Bug Help

Discussion in 'Plugin Development' started by tovd1234, Oct 25, 2014.

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

    tovd1234

    So I've been busy with this command. I've just testing and it almost worked. If you use /block it will convert 9 diamonds in 1 block. But the thing is, if I have for example 11 diamonds it will take them all away. And if I have 18, I only get one block. I have tried a couple of ways to fix this, but I couldn't figure it out.

    Code:java
    1. package nl.tovd1234.blocker;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Main extends JavaPlugin implements Listener {
    12.  
    13. public void onEnable() {
    14. getServer().getPluginManager().registerEvents(this, this);
    15. getLogger().info("Blocker Plugin Enabled! - By tovd1234");
    16. }
    17.  
    18. @Override
    19. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    20. Player player = (Player) sender;
    21. if (cmd.getName().equalsIgnoreCase("block")) {
    22. if (player.hasPermission("b.block"))
    23. if (player.getInventory().contains(Material.DIAMOND, 9)){
    24. player.getInventory().remove(Material.DIAMOND);
    25. player.getInventory().addItem(new ItemStack(Material.DIAMOND_BLOCK));
    26. player.sendMessage("§a§lYour items have been converted into blocks");}
    27.  
    28. else {
    29. player.sendMessage("§a§lYou do not have enough items.");
    30. }
    31. }
    32. return false;
    33. }
    34. }


    Thanks, tovd1234
     
  2. Offline

    mine-care

    soo, first of all:
    Player player = (Player) sender; <-- read my signature

    secondly:
    player.getInventory().remove(Material.DIAMOND); <-- this removes all diamonds from the inv instead you can:
    player.getInventory().removeItem(new ItemStack(Material.DIAMOND, 9));

    thirdly:
    it wont give 2 diamond blocks since you do: player.getInventory().addItem(new temStack(Material.DIAMOND_BLOCK)); <-- this adds 1 diamond block.

    to fix it all:
    you have to use a loop.
    Code:java
    1. while(player.getInventory().contains(Material.DIAMOND, 9)){
    2. //While players inventorry has 9 or more items, do the folowing:
    3. player.getInventory().remove(new ItemStack(Material.DIAMOND,9));
    4. //remove the 9 diamonds
    5. player.getInventory().addItem(new ItemStack(Material.DIAMOND_BLOCK));
    6. //add a diamond block
    7. }
    8. player.updateinventory(); //even tho its depricated, you still use it to update their inventorry after the while has finished.
    9.  


    sorry, the code is not written in a java editor so there might be errors.
    Hope that helps
     
  3. Offline

    tovd1234

    mine-care

    It works fine now, but player.updateInventory(); looks like this: player.updateInventory();

    I don't know what it means, but I think its an error.

    And how do I code it so if I have diamonds for multiple blocks, that it uses as many diamonds as possible?
     
  4. Offline

    mine-care

    Okay first of all the player.updateInventory(); means that the updateinventory(); method is depricated then the code offered above should already create as many diamond blocks as it can ith player's diamonds in inventorry.
    Does it or does it creater only 1 diamond block?
     
  5. Offline

    tovd1234

    mine-care
    If I execute the command with 64 diamonds in my inventory, it only makes 1 diamond block and removes 9 diamonds.

    I also tried making this possiblke with gold and iron, I used || for it. But it gave me an Error.

    Code:java
    1. package nl.tovd1234.blocker;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Main extends JavaPlugin implements Listener {
    12.  
    13. public void onEnable() {
    14. getServer().getPluginManager().registerEvents(this, this);
    15. getLogger().info("Blocker Plugin Enabled! - By tovd1234");
    16. }
    17.  
    18. @Override
    19. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    20. Player player = (Player) sender;
    21. if (cmd.getName().equalsIgnoreCase("block")) {
    22. if (player.hasPermission("b.block"))
    23. if (player.getInventory().contains(Material.DIAMOND, 9)){
    24. player.getInventory().removeItem(new ItemStack(Material.DIAMOND, 9));
    25. player.getInventory().addItem(new ItemStack(Material.DIAMOND_BLOCK));
    26. player.updateInventory();
    27. player.sendMessage("§a§lYour items have been converted into blocks");}
    28.  
    29. else {
    30. player.sendMessage("§a§lYou do not have enough items.");
    31. }
    32. }
    33. return false;
    34. }
    35. }
     
  6. Offline

    mine-care

    if(player.hasPermission("b.block")) <-- this needs { }
    also you are not using the code i posted above :) replace all from line 23 up to line 31 with that i posted above
     
  7. Offline

    tovd1234

    mine-care

    I this right?
    Code:java
    1. package nl.tovd1234.blocker;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Main extends JavaPlugin implements Listener {
    12.  
    13. public void onEnable() {
    14. getServer().getPluginManager().registerEvents(this, this);
    15. getLogger().info("Blocker Plugin Enabled! - By tovd1234");
    16. }
    17.  
    18. @Override
    19. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    20. Player player = (Player) sender;
    21. if (cmd.getName().equalsIgnoreCase("block")) {
    22. if (player.hasPermission("b.block")){
    23. while(player.getInventory().contains(Material.DIAMOND, 9)){
    24. //While players inventory has 9 or more items, do the folowing:
    25. player.getInventory().remove(new ItemStack(Material.DIAMOND,9));
    26. //remove the 9 diamonds
    27. player.getInventory().addItem(new ItemStack(Material.DIAMOND_BLOCK));}
    28. //add a diamond block
    29. }
    30. player.updateInventory(); //even tho its depricated, you still use it to update their inventory after the while has finished.
    31. player.sendMessage("§a§lYour items have been converted into blocks");}
    32.  
    33. else {
    34. player.sendMessage("§a§lYou do not have enough items.");
    35. }
    36. return false;
    37. }
    38. }

    I have a problem though. My server crashes when I use /block while I have more then 9 diamonds in my inventory, so it only works when I have 9 diamonds.
    Or do I have to remove

    Code:java
    1. else {
    2. player.sendMessage("§a§lYou do not have enough items.");


    }
     
  8. Offline

    mine-care

    umm the error shown in console?
     
  9. Offline

    tovd1234

    mine-care
    No, the server just freezes. If I try to use /stop in the console it doesn't do anything as well.
     
  10. Offline

    mine-care

    tovd1234 what are the specs of the server? Because that means that it eather has hard time on the while but when it's done it will be back, or that our while is infinite and it causes mayhem. Ok just thought of something, move the player.updateInventory() in the while(){


    }
     
  11. Offline

    tovd1234

    mine-care
    4485 MB Ram. Jre 8 64 Bit.

    Now I have this:
    Code:java
    1. package nl.tovd1234.blocker;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Main extends JavaPlugin implements Listener {
    12.  
    13. public void onEnable() {
    14. getServer().getPluginManager().registerEvents(this, this);
    15. getLogger().info("Blocker Plugin Enabled! - By tovd1234");
    16. }
    17. public void onDisable() {
    18. getServer().getPluginManager().registerEvents(this, this);
    19. getLogger().info("Blocker Plugin Disabled! - By tovd1234");
    20. }
    21.  
    22. @Override
    23. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    24. Player player = (Player) sender;
    25. if (cmd.getName().equalsIgnoreCase("block")) {
    26. if (player.hasPermission("b.block")){
    27. player.sendMessage("§a§lYour items have been converted into blocks");}
    28. while(player.getInventory().contains(Material.DIAMOND, 9)){
    29. //While players inventory has 9 or more items, do the following:
    30. player.getInventory().remove(new ItemStack(Material.DIAMOND,9));
    31. //remove the 9 diamonds
    32. player.getInventory().addItem(new ItemStack(Material.DIAMOND_BLOCK));}
    33. //add a diamond block
    34. player.updateInventory();
    35. }
    36.  
    37. else {
    38. player.sendMessage("§a§lYou do not have enough items.");
    39. }
    40. return false;
    41. }
    42. }


    I also think that it creates an infinitive loop is you execute the command, and thats probably why the server crashes. I don't know how, but I think I have to cancel the while at some time so it won't freeze.

    There is also another problem; I think the while doesn't check if the player has the diamonds. Because I can just use /block and get a diamond block without having any diamonds.
     
  12. Offline

    mine-care

    Well the wile is self canceling when the condition given to it is false so in this case the condition is if player has 9 or more diamonds, and since we remove them when it runs player should end up with no or less than 9 diamonds in his inventorry.

    Umm does it still do it after the player.updateinventorry?
     
  13. Offline

    tovd1234

    mine-care

    What does still do it?


    And I know why while didn't work. I forgot to add {} after it!

    But now I have another problem. I tried /block with a 64 diamonds in my hand, my whole inventory got filled with diamond blocks and the 64 diamonds were still in there. That also happened when I had 10 diamonds in my inventory. After that the server froze again.
     
  14. Offline

    mine-care

    I mean does the server still crash after we added the player.updateinventorry() in the while?
     
  15. Offline

    tovd1234

    mine-care

    Yes, its in there right now and it still crashes



    Code:java
    1. package nl.tovd1234.blocker;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Main extends JavaPlugin implements Listener {
    12.  
    13. public void onEnable() {
    14. getServer().getPluginManager().registerEvents(this, this);
    15. getLogger().info("Blocker Plugin Enabled! - By tovd1234");
    16. }
    17. public void onDisable() {
    18. getServer().getPluginManager().registerEvents(this, this);
    19. getLogger().info("Blocker Plugin Disabled! - By tovd1234");
    20. }
    21.  
    22. @Override
    23. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    24. Player player = (Player) sender;
    25. if (cmd.getName().equalsIgnoreCase("block")) {
    26. if (player.hasPermission("b.block")){
    27. while(player.getInventory().contains(Material.DIAMOND, 9)){
    28. //While players inventory has 9 or more items, do the following:
    29. player.getInventory().remove(new ItemStack(Material.DIAMOND,9));
    30. //remove the 9 diamonds
    31. player.getInventory().addItem(new ItemStack(Material.DIAMOND_BLOCK));
    32. //add a diamond block
    33. player.updateInventory();}
    34. }
    35. else {
    36. player.sendMessage("§a§lYou do not have enough items.");
    37. }
    38. }
    39. return false;
    40. }
    41. }
    42.  


    If I have more then 9 diamonds my inventory get filled with stacks of diamond blocks. Idk how...

    And the else does not work either. If I have less then 9 diamonds it won't say the message.
     
  16. Offline

    mine-care

    Ok, when I get on Mai pc I will check it too :3
     
  17. Offline

    tovd1234

  18. Offline

    fireblast709

    tovd1234 your infinite loop occurs because it never removes diamonds. remove(ItemStack) will look for the exact same stack, and I bet you don't have a slot with exactly 9 diamonds in your inventory. Use removeItem(ItemStack...items).
     
  19. Offline

    tovd1234


    Thnx!! But do you know why the else is not working? Sorry I'm kinda new to this ^^
     
  20. Offline

    tovd1234

Thread Status:
Not open for further replies.

Share This Page