Solved Can u help me guys?

Discussion in 'Plugin Development' started by Gonmarte, Aug 28, 2015.

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

    Gonmarte

    Hi =)
    im getting some troubles with a plugin and i was asking for my self if u could help me.
    Im making a market plugin, so a player can use /market sell <price> to put in market is item(item in hand) and the other players can do /market to see all the items. Each player just can put 1 item in market.
    I created an Hashmap to store the item and the amount and a array list to check if the player has already sell an item( because each player just can sell 1 item).
    I have a few problems in this plugin.
    1 - The first problem is that i dont know how to get the itemstack in the market.
    What i mean is :
    I have a command /market remove and it should remove the item and the amout in the hashmap and return the item to the player but i cant return the item to the player.
    Here is the hashmap and the arraylist.
    Code:
    HashMap<ItemStack, Integer> blackmarket = new Hashmap<ItemStack, Integer>();
    
    ArrayList<String> playersInMarket = new ArrayList<String>();
    
    
    
    The code
    Code:
    else if(args[0].equalsIgnoreCase("remove")) {
                       
                        if(!playersInMarket.contains(player.getName())) {
                           
                            player.sendMessage(ChatColor.RED + "Tu ainda não colocaste nenhum item no BlackMarket!");
                           
                        }else {
                           
                            blackmarket.remove(player.getInventory().getItemInHand(), player.getInventory().getItemInHand().getAmount());
                           
                           //HERE IS THE ERROR
                            player.getInventory.addItem(blackMarket.get(player.getInventory.getIteminHand, player.getInvetory.getItemInHand.getAmount));
                           
                            player.sendMessage(ChatColor.DARK_RED + "Acabaste de remover um item do BlackMarket!");
                           
                            playersInMarket.remove(player.getName());
                           
                        }
                       
                    }
    
    Im getting other errors in the plugin, when i solve this noe ill share with u guys the next errors!
    Thank you =)
     
  2. Offline

    au2001

    @Gonmarte
    Code:
    blackmarket.remove(player.getInventory().getItemInHand(), player.getInventory().getItemInHand().getAmount());
    Only put the key to remove it from the hash map, not the value.

    Code:
    player.getInventory.addItem(blackMarket.get(player.getInventory.getIteminHand, player.getInvetory.getItemInHand.getAmount));
    You have to put "()" after each function:
    Code:
    player.getInventory().addItem(blackMarket.get(player.getInventory().getItemInHand(), player.getInvetory().getItemInHand().getAmount()));
     
  3. Offline

    Gonmarte

    @au2001
    i tried this and i got an error
    Code:
    player.getInventory().addItem(blackmarket.get(player.getInvetory.getItemInHand.getAmount));
    
    Then i tried this and it worked but it sad to add @supress warnings, should i add? It will work?
    Code:
    
    player.getInventory().addItem(new ItemStack(blackmarket.get(player.getInventory().getItemInHand().getAmount())));
    
    
    
     
  4. Offline

    au2001

    @Gonmarte No, that won't work, use:
    Code:
    ItemStack item = <get item here>;
    item.setAmount(<get amount here>);
    player.getInventory().addItem(item);
     
  5. Offline

    Gonmarte

    @au2001
    I got an error in ItemStack item = blackmarket.get(player.getInventory().getItemInHand()); it says to change ItemStack to Intenger.

    The rest is working.

    Code:
                    }else if(args[0].equalsIgnoreCase("remove")) {
                       
                        if(!playersInMarket.contains(player.getName())) {
                           
                            player.sendMessage(ChatColor.RED + "Tu ainda não colocaste nenhum item no BlackMarket!");
                           
                        }else {
                           
                            blackmarket.remove(player.getInventory().getItemInHand());
                           
                            ItemStack item = blackmarket.get(player.getInventory().getItemInHand());
                           
                            item.setAmount(blackmarket.get(player.getInventory().getItemInHand().getAmount()));
                           
                            player.getInventory().addItem(item);
                           
                            player.sendMessage(ChatColor.DARK_RED + "Acabaste de remover um item do BlackMarket!");
                           
                            playersInMarket.remove(player.getName());
    
    
     
  6. Offline

    au2001

    @Gonmarte Don't get the item's amount, just take the ItemInHand.

    EDIT: Also, shouldn't you check if the item is on the black market before giving him back?
     
  7. Offline

    Gonmarte

    @au2001 i did, but the error is not in setAmout() is in ItemStack item = <getItem>

    Code:
    }else if(args[0].equalsIgnoreCase("remove")) {
                       
                        if(!playersInMarket.contains(player.getName())) {
                           
                            player.sendMessage(ChatColor.RED + "Tu ainda não colocaste nenhum item no BlackMarket!");
                           
                        }else {
                           
                            blackmarket.remove(player.getInventory().getItemInHand());
                           
                             //THE ERROR
                            ItemStack item = blackmarket.get(player.getInventory().getItemInHand());
                           
                            item.setAmount(blackmarket.get(player.getInventory().getItemInHand()));
                           
                            player.getInventory().addItem(item);
                           
                            player.sendMessage(ChatColor.DARK_RED + "Acabaste de remover um item do BlackMarket!");
                           
                            playersInMarket.remove(player.getName());
    
    
     
  8. Offline

    au2001

    @Gonmarte What is the error? Is it a compiler error or a stack trace?

    EDIT: Just saw that, you're getting the item from the black market AFTER removing it...
     
  9. Offline

    Gonmarte

    When i do
    ItemStack item = blackmarket.get(player.getInventory().getItemInHand());
    it says to change the ItemStack to Integer.
    If i do
    ItemStack item = new ItemStack(blackmarket.get(player.getInventory().getItemInHand()))
    it says to add @Supress warning to item or to add @Supress Warning to command.

    @au2001

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  10. Offline

    au2001

    @Gonmarte Oh, yes because blackmarket#get returns an int.
    Just do:
    Code:
    ItemStack item = player.getInventory().getItemInHand();
     
  11. Offline

    Gonmarte

    @au2001 ur not understanding!
    Look to the code
    When i do /market sell im putting the item and the amout in the hashmap( The Integer is the amout and the itemStack is the Item that player is holding in hand) ----> HashMap<ItemStack, Integer> blackmarket = new HashMap<ItemStack, Integer>(); so now i need to remove. How i remove and return the item the the player putted in the hash map and the amount of the item?

    Code:
      
                    }else if(args[0].equalsIgnoreCase("sell")) {
                      
                        if(!playersInMarket.contains(player.getName())) {
                      
                        blackmarket.put(player.getInventory().getItemInHand(),player.getInventory().getItemInHand().getAmount());
                      
                        player.getInventory().remove(player.getItemInHand());
                      
                        player.sendMessage(ChatColor.DARK_RED + "Acabaste de colocar um item á venda no BlackMarket!");
                      
                        playersInMarket.add(player.getName());
                      
                    }else {
                      
                        player.sendMessage(ChatColor.RED + "Já colocaste um item no BlackMarket. Espera que alguém compre ou remove-o!");
                      
                    }
                  
                    }else if(args[0].equalsIgnoreCase("remove")) {
                      
                        if(!playersInMarket.contains(player.getName())) {
                          
                            player.sendMessage(ChatColor.RED + "Tu ainda não colocaste nenhum item no BlackMarket!");
                          
                        }else {
                          
                            blackmarket.remove(player.getInventory().getItemInHand());
                          
                            @SuppressWarnings("deprecation")
                            ItemStack item = new ItemStack(blackmarket.get(player.getInventory().getItemInHand()));
                          
                            item.setAmount(blackmarket.get(player.getInventory().getItemInHand()));
                          
                            player.getInventory().addItem(item);
                          
                            player.sendMessage(ChatColor.DARK_RED + "Acabaste de remover um item do BlackMarket!");
                          
                            playersInMarket.remove(player.getName());
                          
                        }
    
    
     
    Last edited: Aug 28, 2015
  12. Offline

    au2001

    @Gonmarte Use a HashMap<UUID, ItemStack>
    UUID is the UUID of the seller, and ItemStack contains getAmount which gives the amount of items sold.

    In your code, you can't get the player who sells the item so you have to change that.
     
  13. Offline

    Gonmarte

    @au2001 I LOVE U MEN <3
    It works :D Thank you so much!!
    Ill create another thread and share with u if i get another error in this plugin.
    SOLVED IT :D

    @au2001 just 1 thing. Instead of using UUID in the hasp can i use Player? because with UUID i need to use them method player.getUniqueId(); and i would like to use player.getName(); It would work?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  14. Offline

    au2001

    @Gonmarte A Player would cause memory leaks. His name would be fine, even if it COULD bug because of name changes...
     
Thread Status:
Not open for further replies.

Share This Page