Crafting With A Command

Discussion in 'Plugin Development' started by P0PL0K, Dec 15, 2016.

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

    P0PL0K

    Is it possible that I can let players craft an item by using a plugin. An example would be letting them craft sugarcane into sugar just by typing a command. This would affect the entire inventory. Anyone know how?
     
  2. Offline

    Coopah

    @P0PL0K

    Easy enough
    - Make a command
    - Check if the player's inventory contains the item of the command
    - Remove the item (for instance if its sugarcane to sugar, remove the sugarcane)
    - Add the desired item (add the sugar)

    If you want to do this for the entire inventory, make sure you check the amount.
     
  3. @Coopah it's not that simple sadly. You would have to count the amount of sugar cane in their inventory, for which there is no implemented method.

    @P0PL0K there are two ways for you to do this. Coopah said, you could check if their whole inventory contains sugar cane, then you could count it (you will need to write your own method to do this), and then remove all sugar cane and add the necessary amount of sugar. The command would be something like /<command> <item>.

    Another way you can do it is when they send the command, have it like this: /<command> <item> <amount>, and then check if they have it in their hand instead of their inventory, which will make it easier to program if you are finding it hard. Then, just set the amount in their hand to the amount they have - the amount they need to craft the item(s) and add the sugar.

    EDIT: Apologies for saying there is no method to get the amount, I meant there is no method to remove an amount.

    Hope this helped.
     
    Last edited: Dec 16, 2016
  4. Offline

    HeartandSoul

    The way I would make something like this easy to manage is to create an inner class. In this case, I would make all of its methods static.

    Create a for loop, and check the item type for every index. When it finds a match, add the item to an ArrayList. When you run the command, you would create an enhanced for loop that removes all the objects in the list. Clear the list, then give the result.
     
  5. @Sulphate Or a simple for loop, check the item then just set the amount to getAmount - X or if there is less than X set it to null and continue the for loop
     
  6. Offline

    mythbusterma

    @Sulphate

    I'm not sure what you're talking about, there is in fact a method that does exactly that.

    From the JavaDoc page on Inventory:

    [​IMG]

    And I believe there is also a corresponding method for removing a certain amount of something.
     
    bwfcwalshy likes this.
  7. @mythbusterma My bad, I got myself confused. I meant there is no way to remove a specific amount from the inventory, for which @P0PL0K would have to write their own method. Sorry :p
     
  8. Offline

    Coopah

    @Sulphate
    My way would work fine haha, counting the amount of items in an inventory is simple..
     
  9. @Coopah you didn't specify a way, just basic guidelines.. I'm pretty sure they don't want all of the sugar cane removed at once. What if they only want to craft 5 sugar, say?

    @mythbusterma I'm almost certain there is no method for removing a certain amount unless I am greatly mistaken.
     
  10. Offline

    P0PL0K

    @Sulphate so to do this I'll need them to do /convert sugar 10 for example. How would I code it so then 10 cane is removed and replaced with sugar?
     
  11. Offline

    Coopah

    @Sulphate
    If you can get the total amount, obtaining a smaller proportion of that is really quite simple. I don't understand what is wrong with the idea I proposed lmao.

    @P0PL0K
    Just add another argument to the string which will be the amount. Check if the entered number is an integer and see if they contain that amount.
     
  12. @Coopah , look. I'm just trying to be helpful, what you said wasn't specific and you can't just 'remove' a certain amount. I was just letting him know that. I didn't say it was difficult, I didn't say it's not possible, I was just letting him know. I didn't say your idea is wrong, just incomplete.

    I've also kinda already suggested an amount argument.
     
  13. Offline

    Coopah

    @Sulphate
    Assuming he has a moderate understanding of Java/the Bukkit API, turning the simple summary I provided into a working function really isn't all that complicated and I don't believe it requires an informed step by step manual with a QnA attached as an appendix.
     
  14. Whatever, I'm not interested in arguing.
     
Thread Status:
Not open for further replies.

Share This Page