Solved Inventory.setItem . Need help!

Discussion in 'Spigot Plugin Development' started by Chronuak, Feb 19, 2020.

Thread Status:
Not open for further replies.
  1. Hello everyone,
    I want to create an inventory.
    If you click on one item you'll teleport to a place which is written in a special file.
    Moreover in the file is written what item it is and what the display name is.
    My problem:
    I want to set the item in the inv but I don't know how to do that.
    My whole code (Important in line 22):
    Code (Text):


    public class Teleporter implements Listener{
       
        public HashMap<String, Location> locationHashMap = new HashMap<>();
       
        @EventHandler
        public void onIneract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            try {
                if(e.getItem().getItemMeta().getDisplayName().equalsIgnoreCase("§6§1Navigator")) {
                    if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                        Inventory inv = Bukkit.createInventory(null, 36, "§6§1Navigator" );
                        List<String> list = getWarps();
                       
                        for(String s : list) {
                            try {
                                String name = s.split(":")[0];
                                Material material = Material.valueOf(s.split(":")[1].toUpperCase());
                                int slot = Integer.parseInt(s.split(":")[2]);
                                Location location = new Location(Bukkit.getWorld(s.split(":")[3]), Double.valueOf(s.split(":")[4]),
                                    Double.valueOf(s.split(":")[5]), Double.valueOf(s.split(":")[6]), Float.valueOf(s.split(":")[7]),
                                    Float.valueOf(s.split(":")[8]));
                                locationHashMap.put(name, location);
                                inv.setItem(slot, arg1);
                            }catch (Exception e2) {

                            }
                        }
                        e.getPlayer().openInventory(inv);
                    }
                }            
            }catch(NullPointerException e1) {  
           
            }
        }  
       
       
        public void onInventoryClick(InventoryClickEvent e) {
            if(e.getClickedInventory().getName().equalsIgnoreCase("§6§1Navigator")) {
                if(e.getCurrentItem() != null) {
                    if(e.getCurrentItem().getItemMeta().hasDisplayName()) {
                        String displayname = e.getCurrentItem().getItemMeta().getDisplayName();
                        if(locationHashMap.containsKey(displayname)) {
                            e.getWhoClicked().closeInventory();
                            e.getWhoClicked().teleport(locationHashMap.get(displayname));
                            ((Player) e.getWhoClicked()).playSound(e.getWhoClicked().getLocation(), Sound.ENDERMAN_TELEPORT, 2F, 1F);
                        }
                    }
                }
                e.setCancelled(true);
            }  
        }
           
               
        public List<String> getWarps() {
            try {
                return TeleporterSetFile.getcfg().getStringList("navigator");
            }catch (Exception e) {
               
            }
         return new ArrayList<>();
        }
       
    }      
       

     
    entory but I don't know how to do that.
    My whole code (Important in line 46):
     
  2. SteelPhoenix

    Moderator

    Why are you catching generic exceptions and then ignoring them instead of fixing the issues with your code?
     
    • Agree Agree x 3
    • Funny Funny x 1
  3. That's because I first want the item to work and then I can improve those little details
     
  4. I just wrote it on hastebin so I didn't tried it.
    But I improved your code so that you don't need the "try - catch".

    Try that:
    Code (Java):
    public class Teleporter implements Listener {
     
        public HashMap<String, Location> locationHashMap = new HashMap<>();
     
        @EventHandler
        public void onIneract(PlayerInteractEvent e) {
            Player player = e.getPlayer();
            ItemStack stack = player.getItemInHand();
            if (stack == null || !stack.hasItemMeta() || !stack.getItemMeta().hasDisplayName() || !stack.getItemMeta().hasLore()) {
                return;
            }
            if (stack.getItemMeta().getDisplayName().equalsIgnoreCase("§6§1Navigator")) {
                if (e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                    e.setCancelled(true);
                    Inventory inv = Bukkit.createInventory(null, 36, "§6§1Navigator" );
                    List<String> list = getWarps();
                    for (String s : list) {
                        String name = s.split(":")[0];
                        Material material = Material.valueOf(s.split(":")[1].toUpperCase());
                        int slot = Integer.parseInt(s.split(":")[2]);
                     
                        World world = Bukkit.getWorld(s.split(":")[3]);
                        double x = Double.parseDouble(s.split(":")[4]);
                        double y = Double.parseDouble(s.split(":")[5]);
                        double z = Double.parseDouble(s.split(":")[6]);
                        float yaw = Float.parseFloat(s.split(":")[7);
                        float pitch = Float.parseFloat(s.split(":")[8);

                        Location location = new Location(world, x, y, z, yaw, pitch);
                        locationHashMap.put(name, location);

                        ItemStack itemStack = new ItemStack(material);
                        inv.setItem(slot, itemStack);
                    }
                    player.openInventory(inv);
                }
            }
        }

        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (e.getClickedInventory() == null) {
                return;
            }
            if (e.getCurrentItem() == null || !e.getCurrentItem().hasItemMeta() || !e.getCurrentItem().getItemMeta().hasDisplayName() || !e.getCurrentItem().getItemMeta().hasLore()) {
                return;
            }
            if (e.getClickedInventory().getName().equalsIgnoreCase("§6§1Navigator")) {
                e.setCancelled(true);
                String displayname = e.getCurrentItem().getItemMeta().getDisplayName();
                if (locationHashMap.containsKey(displayname)) {
                    Player player = (Player) e.getWhoClicked();
                    player.closeInventory();
                    player.teleport(locationHashMap.get(displayname));
                    player.playSound(player.getLocation(), Sound.ENDERMAN_TELEPORT, 2F, 1F);
                }
            }
        }

        public List<String> getWarps() {
            try {
                return TeleporterSetFile.getcfg().getStringList("navigator");
            } catch (Exception e) {
           
            }
            return new ArrayList<>();
        }
    }
     
    • Like Like x 1
  5. SteelPhoenix

    Moderator

    Exceptions = it's not working; ignoring them won't make them go away. How do you expect the item to work if you can't get the foundation to function.
     
    • Agree Agree x 1
  6. Hi thanks for your reply. However I now can't open the inventory.
    This is the error message:
    Code (Text):
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:195) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:191) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:717) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_221]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_221]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_221]
    Caused by: java.lang.NumberFormatException: For input string: "world"
            at java.lang.NumberFormatException.forInputString(Unknown Source) ~[?:1.8.0_221]
            at java.lang.Integer.parseInt(Unknown Source) ~[?:1.8.0_221]
            at java.lang.Integer.parseInt(Unknown Source) ~[?:1.8.0_221]
            at de.chronuak.lobby.utilis.items.Teleporter.onIneract(Teleporter.java:36) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_221]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_221]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_221]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_221]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            ... 18 more
    In line 36 is written:
    Code (Text):
     int slot = Integer.parseInt(s.split(":")[2]);
     
  7. And this is written in the location.yml:
    Code (Text):
    navigator:
    - Test:BED:world:249.66902548141982:71.0:243.69465889422182:-290.384:-3.5141754
     
     
  8. SteelPhoenix

    Moderator

    "world" is not a number as it literally tells you "java.lang.NumberFormatException: For input string: "world"".
     
  9. so what should I do?
     
  10. Learn Java.
    I mean, the error and the person above me literally tell you what's wrong.
     
    • Agree Agree x 2
    • Like Like x 1
  11. try this
    Code (Java):
    public class Teleporter implements Listener {
        public HashMap<String, Location> locationHashMap = new HashMap<>();
        @EventHandler
        public void onIneract(PlayerInteractEvent e) {
            Player player = e.getPlayer();
            ItemStack stack = player.getItemInHand();
            if (stack == null || !stack.hasItemMeta() || !stack.getItemMeta().hasDisplayName() || !stack.getItemMeta().hasLore()) {
                return;
            }
            if (stack.getItemMeta().getDisplayName().equalsIgnoreCase("§6§1Navigator")) {
                if (e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                    e.setCancelled(true);
                    Inventory inv = Bukkit.createInventory(null, 36, "§6§1Navigator" );
                    List<String> list = getWarps();
                    for (String s : list) {
                        String name = s.split(":")[0];
                        Material material = Material.valueOf(s.split(":")[1].toUpperCase());
                   
                        World world = Bukkit.getWorld(s.split(":")[2]);
                        double x = Double.parseDouble(s.split(":")[3]);
                        double y = Double.parseDouble(s.split(":")[4]);
                        double z = Double.parseDouble(s.split(":")[5]);
                        float yaw = Float.parseFloat(s.split(":")[6);
                        float pitch = Float.parseFloat(s.split(":")[7);

                        Location location = new Location(world, x, y, z, yaw, pitch);
                        locationHashMap.put(name, location);

                        ItemStack itemStack = new ItemStack(material);
                        inv.addItem(itemStack);
                    }
                    player.openInventory(inv);
                }
            }
        }

        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (e.getClickedInventory() == null) {
                return;
            }
            if (e.getCurrentItem() == null || !e.getCurrentItem().hasItemMeta() || !e.getCurrentItem().getItemMeta().hasDisplayName() || !e.getCurrentItem().getItemMeta().hasLore()) {
                return;
            }
            if (e.getClickedInventory().getName().equalsIgnoreCase("§6§1Navigator")) {
                e.setCancelled(true);
                String displayname = e.getCurrentItem().getItemMeta().getDisplayName();
                if (locationHashMap.containsKey(displayname)) {
                    Player player = (Player) e.getWhoClicked();
                    player.closeInventory();
                    player.teleport(locationHashMap.get(displayname));
                    player.playSound(player.getLocation(), Sound.ENDERMAN_TELEPORT, 2F, 1F);
                }
            }
        }

        public List<String> getWarps() {
            try {
                return TeleporterSetFile.getcfg().getStringList("navigator");
            } catch (Exception e) {
         
            }
            return new ArrayList<>();
        }
    }
     
    • Agree Agree x 1
    • Funny Funny x 1
  12. The problem now is, that the items in the inventory are named like their material and the slot is not that what I specified before.
    And as a result I don't know if the teleportation works.
    It would be great if anyone knows what the error is.
     
  13. i know what the error is, its that you have no idea how java works, as youre struggling to use arrays, a fundamental level of java (one typically learns this around the time they learn what an if statement is).
    when you're splitting your string into an array, youre simply getting the wrong strings from the wrong indexes. figure out the right indexes and change the numbers. or change your string around.
     
    • Agree Agree x 1
  14. I really don't understand people like you. He asked for help because he doesn't know what to do.
    If he was good at programming, this thread wouldn't exist.
    Stop criticizing him and just help him.
    Don't take it badly but your comments are not helpful.
     
    • Optimistic Optimistic x 1
  15. I read that you are from Germany.
    I am German myself.
    If you like, I'll go through the code with you.
    Adde me if necessary gladly on Discord: Flubbii#9540
     
  16. SteelPhoenix

    Moderator

    Blunt though it may be, he is not wrong. OP clearly lacks basic Java knowledge, and this is the source of his problems - not the Spigot API. OP should watch/read/try some fundamental Java tutorials - which is what those comments pointed out - to just learn a bit; basic knowledge is expected here.

    Sources:
    https://www.spigotmc.org/threads/read-me-for-everyone-asking-questions-on-here.72946/
    https://www.spigotmc.org/threads/beginner-programming-mistakes-and-why-youre-making-them.278876/
     
    • Agree Agree x 1
  17. i am helping though. im telling him what his problem is and suggesting a solution. i then told him what hes doing wrong and how to fix it. but you call it not helping because i didnt spoonfeed him lol. spoonfeeding hurts people, it doesnt help. ("blah blah i got spoonfeeded and i know what im doing", sure but not everyone takes the effort to learn the code, they just copy and paste, and then they come back over and over again for the most basic issues that a 5 minute lesson over arrays could solve for the rest of time, instead of him spending hours waiting for someone to spoonfeed him the code).

    /e think u meant i wasnt nice. sure i wasnt but sometimes being an ass gets it through their head and helps them in the long run.
     
    • Agree Agree x 1
  18. You are also fundamentally right.
    I don't find the thread creator's approach wrong in any way.
    He has problems with his code.
    This is the category for "Spigot Plugin Development".
    So if he has questions I find it perfectly ok to ask them.
    But what I don't think is okay is to expose him.
    I think it's good to give him useful tips. But something like "Learn Java" is not target-oriented and therefore wastes memory resources.
     
  19. give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime

    ~ Anne Isabella Thackeray Ritchie's (1837–1919)
     
  20. i literally told him what he is doing wrong and how to fix it
     
    • Agree Agree x 1
Thread Status:
Not open for further replies.