Invsee

Discussion in 'Spigot Plugin Development' started by Lolok, Apr 6, 2019.

  1. https://hastebin.com/igexanaler.cs

    I'm making a invsee plugin but I don't know how to edit the target inventory
    Code (Java):
    @EventHandler
        public void invClick(InventoryClickEvent e) {
            Inventory inv = e.getInventory();
            Player p = (Player) e.getWhoClicked();
            if (inv.getName().contains("Info: ")) {
                if (e.getCurrentItem() != null && e.getCurrentItem().getType() != null) {
                    inv.getContents();
                    String target = inv.getName().substring(inv.getName().lastIndexOf(": "));
                    Player t = Bukkit.getServer().getPlayer(target);
                    t.getInventory().setItem(e.getSlot(), e.getCurrentItem());
                }
            }
        }
    That's what I tried but doesn't work, some one can help me?
     
  2. Mehhhhh. Cache variables more to make this cleaner, like e.getCurrentItem(), and it's pointless to check if an item's type is null, that should never happen. inv.getContents() is doing nothing whatsoever on it's own, you're not storing the return value or anything. You should be using inventory holders to pass data along, not getting everything off the name of the inventory.
    You could also utilize if not returns to make the code less "wide". Add some debug messages inside each if statement to see if it's actually ever getting in there, it might not be executing that code in the if statement.
     
  3. Code (Java):
    Player targetPlayer = Bukkit.getServer().getPlayer(args[0]);
    Player player = (Player) sender;
    Inventory targetInv = targetPlayer.getInventory();
    player.openInventory(targetInv);
    Should work. (Use it in your command)
    args[0] = player name
     
  4. but I need a custom gui not the player inv
     
  5. Look at ChrisUMB’s reply, you need to put debug statements so you actually know what is going on or if your code is even running in the first place
     
  6. where i can show also the armor of the player
     
  7. What do you mean where? when you get the target(player object) you can access the armor contents from the inventory.
     

  8. I recive this error: https://hastebin.com/agixexuxim.cs


    Code (Java):
    @EventHandler
        public void invClick(InventoryClickEvent e) {
            Inventory inv = e.getInventory();
            Player p = (Player) e.getWhoClicked();
            if (inv.getName().contains("Info: ")) {
                String target = inv.getName().substring(inv.getName().lastIndexOf(": "));
                Player t = Bukkit.getServer().getPlayer(target);
                t.getInventory().setItem(e.getSlot(), e.getCurrentItem());
                System.out.println("Item " + e.getCurrentItem().toString());
                System.out.println("Slot " + e.getSlot());
            }
        }
     
  9. the current item is null. When they said you don’t need the null check it means you don’t need to null check the type, but you should keep the getCurrentItem null check
     
  10. someone can make for me the code, I have no idea about how to make this (it's for my first plugin)
     
    • Funny Funny x 5
  11. If you can’t help yourself why should we? You will just come back in ten minutes with the next error. Instead of begging someone to dig a hole for you, learn how to use a shovel. Programming is 90% logic and understanding, syntax is only a small part. Understand how java and the spigot api work then try again. Your basically trying to throw darts with your eyes closed. You will be lucky if you even hit the board, but getting anything better, forget about it. Learn java.
     
    • Agree Agree x 1

  12. I only asked for a few lines of code because I can't find a suitable event and I didn't find anything on google
    If you don't want to help me, a simple no is enough ;)
     
  13. I'm not quite sure whether you can clone inventories, but I'd the inventory and then open it and cancel all clicks in there,
     
  14. He’s got a point, if anyone gives you the code you will most likely just copy it, complain about any errors that are in the code because the user wrote it in pseudocode and then come back with another question. Use the Spigot docs to find any methods that you may need: https://hub.spigotmc.org/javadocs/spigot/overview-summary.html

    A few tips from my part though, these might be incorrect since I’m thinking of them without experimenting but here we go:
    When the player executes the command, add them to a HashMap: Map<UUID, Map<UUID, Inventory>>.
    Listen for inventory clicks, check that the player clicking is in the HashMap by doing a simple null check.
    Then do code for whatever else you need.
    Make sure to remove the player from the HashMap when they quit or are kicked to prevent memory leaks.
     
  15. You're right, I probably didn't express myself well, I was just asking for a pseudocode or the event to use, not the whole plugin
    Anyway thanks for the advice about the hashmap
     
  16. For me in the past, just letting the sender of a command open the inventory of a player worked, and it was auto updated when the sender did something, because its quite literally the other person's inventory.
     
  17. If you want someone to write your code, then this is the wrong section. There you go.
     
    • Funny Funny x 1
  18. Just saw the error: "Main.java:503". Dude, don't make everything inside ur main.
     
  19. Why? lol. You can make methods inside your main class, might not be best practice but its allowed.
     
  20. When you make everything inside your main, its just bad design. Yeah you can do it, but its just bad practise.