2 commands COPY / PASTE

Discussion in 'Plugin Development' started by galaipa, Apr 14, 2014.

Thread Status:
Not open for further replies.
  1. Hi,
    This is what I want to do:
    - /sr copy (It copys the Meta of that item)
    - /sr paste (It pastes the Meta on a other item)

    How can I do this?

    Thx
     
  2. Offline

    amhokies

    It seems pretty straight forward. When the player does the command '/sr copy' save the ItemMeta as a value in a Map with the Player's name as a key. Then, when they do /sr paste, access the Map with the player's name and put the ItemMeta onto the item they are holding.
     
    galaipa likes this.
  3. Thank you!
    I need some help with the paste command. I have this
    Code:
    public HashMap<String, ItemStack> copy = new HashMap<String, ItemStack>();
    Copy:
    Code:java
    1. //COPY
    2. else if (cmd.getName().equalsIgnoreCase("sr")&& args[0].equalsIgnoreCase("copy") && player.hasPermission("sr.copy")) {
    3. ItemStack item = player.getItemInHand();
    4. copy.put(player.getName(), item);
    5. return true;
    6. }

    Paste: I dont know how to get the Meta from the HashMap
    Code:java
    1. //PASTE
    2. else if (cmd.getName().equalsIgnoreCase("sr")&& args[0].equalsIgnoreCase("paste") && player.hasPermission("sr.copy")) {
    3. ItemStack item1 = player.getItemInHand();
    4.  
    5. ItemMeta MetaData1 = item.getItemMeta();
    6.  
    7. ItemStack give = item1.setData(MetaData1);
    8. player.getInventory().addItem(give);
    9.  
    10. return true;
    11. }
     
  4. Offline

    amhokies

    galaipa
    Instead of using ItemStack in your Map, use ItemMeta instead.

    When you're pasting the ItemMeta, you can retrieve it by using the player's name. copy.get(player.getName()) should do it. You might want to check to make sure the Map contains the player's name first, though.
     
  5. amhokies
    I have created a code but something is wrong
    Code:java
    1. //PASTE
    2. else if (cmd.getName().equalsIgnoreCase("sr")&& args[0].equalsIgnoreCase("paste") && player.hasPermission("sr.copy")) {
    3. ItemStack item1 = player.getItemInHand();
    4. ItemStack item = copy.get(player.getName());
    5. ItemMeta MetaData = item.getItemMeta();
    6. item1.setData(MetaData);
    7. player.getInventory().addItem(item1);
    8.  
    9. sender.sendMessage(ChatColor.GREEN+(getTranslation("12")));
    10. return true;
    11. }
     
  6. Offline

    amhokies

    "Something is wrong" doesn't really tell me much.
     
  7. Offline

    Skyost

    Pretty simple : You learn how to use the Bukkit API.
     
  8. @amhokies
    Now it works but it has some bugs:
    1.It duplicates the paste item
    2.The copy command dosn't work if the item has the original name

    Thx
    Code:java
    1. //COPY
    2. else if (cmd.getName().equalsIgnoreCase("sr")&& args[0].equalsIgnoreCase("copy") && player.hasPermission("sr.copy")) {
    3. ItemStack item = player.getItemInHand();
    4. copy.put(player.getName(), item);
    5. sender.sendMessage(ChatColor.GREEN+(getTranslation("11")));
    6. return true;
    7. }
    8. //PASTE
    9. else if (cmd.getName().equalsIgnoreCase("sr")&& args[0].equalsIgnoreCase("paste") && player.hasPermission("sr.copy")) {
    10. ItemStack item1 = player.getItemInHand();
    11. ItemStack item = copy.get(player.getName());
    12. ItemMeta MetaData = item.getItemMeta();
    13. item1.setItemMeta(MetaData);
    14. player.getInventory().addItem(item1);
    15.  
    16. sender.sendMessage(ChatColor.GREEN+(getTranslation("12")));
    17. return true;
    18. }
     
  9. Offline

    FlareLine

    galaipa , amhokies has given you a perfectly good answer. Copy the ItemMeta instead of the ItemStack.
     
  10. FlareLine I fixed the problem with the duplication. I think that it dosn't matter if I use ItemMeta ore ItemStack to solve the 2 problem. However I will try to do that
     
Thread Status:
Not open for further replies.

Share This Page