Don't know how to go further.

Discussion in 'Plugin Development' started by rubenio1, Aug 25, 2018.

  1. Offline

    rubenio1

    Dear,
    This is my code at the moment this piece off code will check when the player run the command logtowood1 of they have a log in theire inventory. I they have it will run a specifiq spiece of code. (now it will send the message found) I want it to remove 1 logg from theire inventory and replace it to wood planks. How can I do this?
    Code:java
    1.  
    2. package me.rubenio1.test;
    3. import org.bukkit.plugin.java.JavaPlugin;
    4. import org.bukkit.Material;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.HumanEntity;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.inventory.Inventory;
    12. import org.bukkit.inventory.ItemStack;
    13. public class Test extends JavaPlugin {
    14. public void onEnable() {
    15. getCommand("logtowood1").setExecutor(new EventsClass());
    16. }
    17. public void onDisable() {
    18. }
    19. public class EventsClass implements Listener, CommandExecutor {
    20. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    21. if(sender instanceof Player) {
    22. if (cmd.getName().equalsIgnoreCase("logtowood1")) {
    23. Inventory i = ((HumanEntity) sender).getInventory();
    24. for(ItemStack inven : i.getContents()){
    25. if (inven == null) {
    26. continue;
    27. }
    28. if(inven.getType().equals(Material.LOG)){
    29. sender.sendMessage("found");
    30. }
    31. }
    32. }
    33. }
    34. return true;
    35. }
    36. }
    37. }
    38. [Syntax][/Syntax]
     
  2. Offline

    KarimAKL

    I think something like this should work:
    Code:Java
    1. sender.getInventory().removeItem(inven);
     
  3. Offline

    The_Spaceman

    inside the if(has log...) {}

    Code:java
    1. sender.getInventory().remove(inven.getType()); //dont know how many logs there will be removed
    2. for (ItemStack is : sender.getInventory().addItem(new ItemStack(Material.OAK_PLANKS)).values()) {
    3. sender.getWorld().dropItem(sender.getLocation(), is);//this is for when the player doesn't have space left, so it will be droped
    4. }

    The addItem will return a HashMap of the items that could not be added
     
  4. Offline

    rubenio1

    Thnx, Does anyone know how to specify an amount on the code of @The_Spaceman (for the amount of wood that will be removed)
     
    Last edited: Aug 26, 2018
  5. Offline

    The_Spaceman

    The itemstack you've checked is an itemstack with the material log. You can remove 1 of the amout value, and when that is 1 remove the itemstack. Or you can check how many will re removed by just testing of how many there will be removed…
     
  6. Offline

    rubenio1

    But is it also possible to give them 4 planks back and another question, can you specify the type of logg. Because now you only have logg and logg_2 with auto correction on eclipse.
     
    Last edited: Aug 26, 2018
  7. Offline

    The_Spaceman

    add more items:
    Code:
    new ItemStack(Material.YOUR_MATERIAL, 4)
    the 4 is the amount of items

    if you program with Bukkit 1.13:
    they replaced it all. so now you have OAK_PLANKS and ACACIA_LOG, etc...
    in lower than 1.13:
    Code:
    new ItemStack(Material.LOG, 1 /*amount of items*/, 2 /*damage value*/)
    I say you have to update your server to 1.13 because everything else is going to be outdated soon...

    I hope this will help you.
     
  8. Offline

    rubenio1

    Then I can do that but the pl is needed for 1.12.2 is that possible?
    (the error when doing the command in 1.12.2: https://pastebin.com/QRdVuM7N)

    Found it, thnx

    One last question: Is it possible to specify a type of wood in the detection: if(inven.getType().equals(Material.LOG)){
    @The_Spaceman

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 27, 2018
  9. Offline

    The_Spaceman

    If you want to check what type of log it is, you need to check the damagevalues...
    '.getDurability()'
     
  10. Offline

    rubenio1

    Where do I need to place it? (tried many places in the invent.getType)
     
  11. Offline

    KarimAKL

    Code:Java
    1. if (/*your itemstack*/.getDurability() == /*number here*/) {
     
  12. Offline

    rubenio1

  13. Online

    timtower Ninja on the waves Moderator

    @rubenio1 You need to check the item. inven.getDurability
     
  14. Offline

    rubenio1

    Like this?
    if(inven.getType().equals(Material.LOG)){
    if(inven.getDurability() == (2)){
     
  15. Online

    timtower Ninja on the waves Moderator

  16. Offline

    rubenio1

    Code:Java
    1.  
    2. if(inven.getType().equals(Material.LOG)){
    3. if(inven.getDurability() == (2)){
    4. ((HumanEntity) sender).getInventory().remove(inven.getType()); //don't know how many logs there will be removed
    5. for (ItemStack is : ((HumanEntity) sender).getInventory().addItem(new ItemStack(Material.LOG, 1 /*amount of items*/, (short) 2 /*damage value*/)).values()) {
    6. ((Entity) sender).getWorld().dropItem(((Entity) sender).getLocation(), is);//this is for when the player doesn't have space left, so it will be droped
    7. }
    8. }
    9. if(inven.getDurability() == (1)){
    10. ((HumanEntity) sender).getInventory().remove(inven.getType()); //don't know how many logs there will be removed
    11. for (ItemStack is : ((HumanEntity) sender).getInventory().addItem(new ItemStack(Material.LOG, 1 /*amount of items*/, (short) 1 /*damage value*/)).values()) {
    12. ((Entity) sender).getWorld().dropItem(((Entity) sender).getLocation(), is);//this is for when the player doesn't have space left, so it will be droped
    13. }
    14. }
    15. }
    16.  

    If I do this is just doesn't response @timtower
     
  17. Online

    timtower Ninja on the waves Moderator

  18. Offline

    rubenio1

  19. Online

    timtower Ninja on the waves Moderator

    @rubenio1 Print the durability to the console using the logger, send it to the players using sendMessage
     
  20. Offline

    rubenio1

    if(inven.getDurability() == (1)){
    Bukkit.getLogger().info("1");
    I have done this but nothing is comming thru in the console
     
  21. Online

    timtower Ninja on the waves Moderator

    @rubenio1 Print it before it gets checked.
     
  22. Offline

    rubenio1

    Found it, normal wood 1 dur. 0.
    But how can you specify how much wood theire will be removed.
    ((HumanEntity) sender).getInventory().remove(inven.getType());
    or better how can you let It go if you have 2 oak loggs and 3 pruce loggs (example <-) that you also get 2 oak planks and 3 spruce loggs back?
     
  23. Online

    timtower Ninja on the waves Moderator

    @rubenio1 You can make an ItemStack with type, amount and durability.
     
  24. Offline

    rubenio1

    ((HumanEntity) sender).getInventory().remove.inv.ItemStack(Material.WOOD, 1 /*amount of items*/, (short) 1 /*damage value*/);
    somethinh like this doesn't work
     
  25. Online

    timtower Ninja on the waves Moderator

    @rubenio1 1. Make a Player variable, don't cast every line.
    2. You can also change the amount of the old ItemStack.
     
  26. Offline

    rubenio1

    And how can you make it remove 1 wood? I can't find it
     
  27. Online

    timtower Ninja on the waves Moderator

  28. Offline

    rubenio1

    But how do you use it. This doesn't work: ((HumanEntity) sender).getInventory().remove(inven.getType(), 1);
     
  29. Online

    timtower Ninja on the waves Moderator

  30. Offline

    rubenio1

    I tried a lot off things but I just can't find it / don't get wat you mean. (with the itemstack)
     

Share This Page