1.12.2 Update Inventory don't work

Discussion in 'Spigot Plugin Development' started by OrgeAlexj06, Jul 2, 2021.

  1. Hello, I am trying to create an adminShop but I have a problem. In this adminShop, when you want to buy charcoal, you can increase the number of items by pressing an item thanks to this procedure which adds 32 more items:

    @EventHandler
    public void onShopAddThirtyTwoBuyingCoalMenuInventoryClick(InventoryClickEvent event) {
    Inventory adminShopOre = event.getInventory();
    Player player = (Player)event.getWhoClicked();
    ItemStack current = event.getCurrentItem();

    if(current == null) return;

    if(adminShopOre.getName().equalsIgnoreCase("Adminshop - Coal")) {
    event.setCancelled(true);
    if(current.getType() == Material.COAL && current.getItemMeta().getDisplayName().equalsIgnoreCase("Add 32 Coals to the balance") {
    buyingCoal = buyingCoal + 32;
    buyingBalance = buyingBalance + 160;
    player.updateInventory();
    }
    }
    }

    The problem is that with the variable, I want to make it change the name of an item in the inventory like a kind of Price and Item number indicator. For the name of the "seeing" item, I did this:

    /** PURCHASE (BUYING) ALERT **/

    ItemStack buyingAlertCoal = new ItemStack(Material.getMaterial("FALLENGLORYMODS_BUNDLE_OF_CASH"));
    ItemMeta buyingAlertCoalM = buyingAlertCoal.getItemMeta();
    buyingAlertCoalM.setDisplayName("Items : " + buyingCoal + " Buying price : " + buyingBalance);
    buyingAlertCoal.setItemMeta(buyingAlertCoalM);
    adminShopCoal.setItem(9, buyingAlertCoal);
    Hope you got it all. thank you for helping me
    :)
     
  2. And the item didn't change? Or some other error/problem?
    Also, sometimes just updating the item in the inventory isn't enough - you need to update the view inventory or put the item directly to the view.
    You do this by replacing this line:
    Code (Java):
    Inventory adminShopOre = event.getInventory();
    With this line:
    Code (Java):
    InventoryView adminShopOre = event.getView();
     
  3. Hello !
    It still does not work. To help you further with my problem, when I put Sysout before and after I modify the buyingBalance and buyingPrice variables. The problem is always the fact that the inventory does not update. I change the item title for that, doesn't it have another way?
    Thank you ;)
     
  4. If you change it to InventoryView and it still didn't update then you have some other problem - does it even get to that line? (Are the compares passed?)
     
  5. Maybe it's a problem that comes directly from my class (something I did wrong). Here it is: https://pastebin.com/cNWhpc3S
     
  6. The class isn't a listener... no wonder nothing's happening - it's never called...
    Make the class implement Listener then register it.
     
  7. A really stupid mistake, though, I made it in the other classes but, it happens. However, that does not solve the problem, the name of the item is always the same ...
     
  8. Make sure you make the class implement Listener, the n register it as a Listener, then add log messages after everything in your function to see at which point it fails - then let us know and we'll help if still doesn't work.
     
  9. Here is where I put the Sysout:

    @EventHandler
    public void onShopAddOneBuyingCoalMenuInventoryClick(InventoryClickEvent event) {
    InventoryView adminShopOre = event.getView();
    Player player = (Player)event.getWhoClicked();
    ItemStack current = event.getCurrentItem();
    System.out.println("Test1");

    if(current == null) return;
    System.out.println("Test2");

    if(adminShopOre.getTitle().equalsIgnoreCase("AdminShop - Coal")) {
    event.setCancelled(true);
    System.out.println("Test3");
    if(current.getType() == Material.COAL && current.getItemMeta().getDisplayName().equalsIgnoreCase("Add 1 Coal for buy it")) {
    System.out.println("Test4");
    buyingCoal = buyingCoal + 1;
    buyingBalance = buyingBalance + 5;
    System.out.println("Test5");
    player.updateInventory();
    System.out.println("Test6");
    }
    }
    }

    And from 1 to 6, all are displayed in the server logs
     
  10. But... you didn't put the item in the InventoryView... There's no "adminShopOre.setItem(...);" ...
    You're expecting the code to guess it's supposed to do that?