Help with adding/replacing items

Discussion in 'Plugin Development' started by felreava, Jun 15, 2014.

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

    felreava

    The goal I'm currently trying to accomplish is to remove a sword, and an ItemStack of emeralds with specific lore/enchants from the player's inventory and then to add an enchanted/named/lore(d?) sword of the same type.

    The issue I'm having is weird however. It all works fine, the items are removed, and I have a new sword. But it's not... really a new sword. If you mouse over it shows the enchant, lore, and name. But if you update it by throwing it on the ground and picking it up for example, it's the old sword again. However, if you update it by moving it around in your inventory, then all is well!

    I'm really hoping someone could shed some insight on this weird problem I'm having.

    Here's the code for this portion of the plugin:
    DropListener Class
    Code:java
    1. @EventHandler
    2. public void onSignClick(PlayerInteractEvent event){
    3. if(event.getClickedBlock() == null) return;
    4. if(event.getAction() == Action.RIGHT_CLICK_BLOCK){
    5. if(event.getItem() == null) return;
    6. Block block = event.getClickedBlock();
    7. Material mat = block.getType();
    8. if((mat == Material.SIGN) || (mat == Material.SIGN_POST) || (mat == Material.WALL_SIGN)){
    9. Player player = event.getPlayer();
    10. Inventory inv = player.getInventory();
    11. ItemStack item = event.getItem();
    12. ItemMeta meta = item.getItemMeta();
    13. Material type = item.getType();
    14. Sign sign = (Sign) block.getState();
    15. String line1 = sign.getLine(0);
    16. String line3 = sign.getLine(2);
    17.  
    18. if((line1.equalsIgnoreCase("[Infuse]")) && (line3.equalsIgnoreCase("Sword/Axe/Bow"))){
    19. if((type == Material.IRON_SWORD) ||
    20. (type == Material.GOLD_SWORD) ||
    21. (type == Material.DIAMOND_SWORD)){
    22.  
    23. DropItems.swords(player, item);
    24. }
    25. }
    26. }
    27. }
    28. }


    DropItems Class
    Code:java
    1. public static void swords(Player player, ItemStack item){
    2. Material type = item.getType();
    3. ItemMeta meta = item.getItemMeta();
    4. Inventory inv = player.getInventory();
    5. int amount = DropItems.knight.getAmount();
    6. while(amount < 65){
    7. if(inv.contains(DropItems.knight)){
    8. if(meta.hasLore()) return;
    9. DropItems.knight.setAmount(1);
    10. inv.removeItem(DropItems.knight);
    11. inv.removeItem(item);
    12.  
    13. if(type == Material.IRON_SWORD){
    14. inv.setItem(inv.firstEmpty(), ironSword);
    15. player.updateInventory();
    16. }else if(type == Material.GOLD_SWORD){
    17. inv.setItem(inv.firstEmpty(), goldSword);
    18. player.updateInventory();
    19. }else if(type == Material.DIAMOND_SWORD){
    20. inv.setItem(inv.firstEmpty(), diamondSword);
    21. player.updateInventory();
    22. }
    23. break;
    24. }
    25. amount++;
    26. DropItems.knight.setAmount(amount);
    27. }
    28. DropItems.knight.setAmount(1);
    29. return;
    30. }
     
Thread Status:
Not open for further replies.

Share This Page