Solved Get an item from an inventory and rename it

Discussion in 'Plugin Development' started by FilBuild, May 6, 2020.

Thread Status:
Not open for further replies.
  1. EDIT: I fixed the problem, kinda. The code works now, I still get error messages. I will mark this as fixed. I Updated the code


    Hello Guys!
    I'm new to Plugin Development and also my first language isn't English, but I hope you can still help me!

    So I'm trying to make a plugin which can give you a compass that points to your Spawnpoint. I tried to make it smart, so it's checks your inventory first, to make sure you don't already have a compass. When the compass get edited/added to the inventory it should have the coordinates of the spawnpoint, to which it's pointing, as a name (and of course it should point to the spawnpoint).

    I tried to code it, but it isn't working as i wanted it to:

    It's react to the command "/spi" and when you have no compass in your inventory its gives you a new one, which points to the right point. BUT it doesn't have the spawnpoint as a name, thats the first problem.

    It also reacts, if you have already a compass in your inventory, but nothing more, it doesn't "repoints" the compass.

    I looked into my code and added a few checks, so you can see how "far" the plugin works as intended.

    (I'm using 1.8.8 spigot as an external archive, my Main.class and plugin.yml are working as intended, they are not included here)

    EDIT: Thanks for fixing problem 1, maybe you can help me with problem 2 too. I edited the code and output.

    Code:
    package me.filbuild.setspawnpoint.commands;
    
    import java.text.DecimalFormat;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import me.filbuild.setspawnpoint.Main;
    import me.filbuild.setspawnpoint.utils.Utils;
    
    @SuppressWarnings("unused")
    public class SpInfo implements CommandExecutor{
        private Main plugin;
        DecimalFormat df = new DecimalFormat("#");
        public SpInfo(Main plugin) {
            this.plugin = plugin;
           
            plugin.getCommand("spinfo").setExecutor(this);
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(Utils.chat("&4 Not a Player!"));
            }else {
            Player p = (Player) sender;
            Location loc = p.getBedSpawnLocation();
            ItemStack comp = new ItemStack(Material.COMPASS);
            p.sendMessage(Utils.chat("&2Your SpawnPoint is located at &4" + df.format(loc.getX())+ " " + df.format(loc.getY())+ " " + df.format(loc.getZ()) + "&2!"));
            if (p.getInventory().contains(comp.getType())) {
                for(int slot = 0; slot < 36; slot = slot + 1){
                    ItemStack item = p.getInventory().getItem(slot);
                        if (item.getType() == comp.getType()){
                                ItemStack compinv = p.getInventory().getItem(slot);
                                p.getInventory().removeItem(compinv);
                                ItemMeta compinvmeta = compinv.getItemMeta();
                                compinvmeta.setDisplayName(Utils.chat("&4" + df.format(loc.getX())+ " " + df.format(loc.getY())+ " " + df.format(loc.getZ())));
                                compinv.setItemMeta(compinvmeta);
                                p.getInventory().addItem(compinv);
                                p.setCompassTarget(loc);
                        }
                    }
            }else{
                ItemMeta compmeta = comp.getItemMeta();
                compmeta.setDisplayName(Utils.chat("&4" + df.format(loc.getX())+ " " + df.format(loc.getY())+ " " + df.format(loc.getZ())));
                comp.setItemMeta(compmeta);
                p.getInventory().addItem(comp);
                p.setCompassTarget(loc);
            }
            }
            return false;
        }
    }
    
    
    And this is the Output:
    Output (open)

    [23:08:14 INFO]: FilBuild: Reload complete.
    [23:08:17 INFO]: FilBuild issued server command: /spi
    [23:08:17 INFO]: ItemStack{COMPASS x 1, UNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=-81 97 232}}
    [23:08:17 INFO]: test
    [23:08:17 INFO]: testItemStack{COMPASS x 1, UNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=-81 97 232}}
    [23:08:17 INFO]: testUNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=-81 97 232}
    [23:08:17 INFO]: testUNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=-81 97 232}
    [23:08:17 INFO]: null
    [23:08:17 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'spi' in plugin SetSpawnPoint v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.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_241]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_241]
    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_241]
    Caused by: java.lang.NullPointerException
    at me.filbuild.setspawnpoint.commands.SpInfo.onCommand(SpInfo.java:41) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    ... 15 more



    Thanks for reading!
     
    Last edited: May 7, 2020
  2. Offline

    johnny boy

    For the renaming the item issue you need to set the new itemMeta on the item by using Item#setItemMeta(meta);
     
  3. Thanks! That fixed problem 1. And it gives me more specific errormessages for problem 2:
    Here is my new code:
    Code:
    package me.filbuild.setspawnpoint.commands;
    
    import java.text.DecimalFormat;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import me.filbuild.setspawnpoint.Main;
    import me.filbuild.setspawnpoint.utils.Utils;
    
    @SuppressWarnings("unused")
    public class SpInfo implements CommandExecutor{
        private Main plugin;
        DecimalFormat df = new DecimalFormat("#");
        public SpInfo(Main plugin) {
            this.plugin = plugin;
          
            plugin.getCommand("spinfo").setExecutor(this);
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(Utils.chat("&4 Not a Player!"));
            }
            Player p = (Player) sender;
            Location loc = p.getBedSpawnLocation();
            ItemStack comp = new ItemStack(Material.COMPASS);
            p.sendMessage(Utils.chat("&2Your SpawnPoint is located at &4" + df.format(loc.getX())+ " " + df.format(loc.getY())+ " " + df.format(loc.getZ()) + "&2!"));
            if (p.getInventory().contains(comp.getType())) {
                for(int slot = 0; slot < 36; slot = slot + 1){
                    ItemStack item = p.getInventory().getItem(slot);
                    Bukkit.broadcastMessage("" + item);
                        if (item.getType() == comp.getType()){
                                Bukkit.broadcastMessage("test");
                                ItemStack compinv = p.getInventory().getItem(slot);
                                Bukkit.broadcastMessage("test" + compinv);
                                ItemMeta compinvmeta = compinv.getItemMeta();
                                Bukkit.broadcastMessage("test" + compinvmeta);
                                compinvmeta.setDisplayName(Utils.chat("&4" + df.format(loc.getX())+ " " + df.format(loc.getY())+ " " + df.format(loc.getZ())));
                                Bukkit.broadcastMessage("test" + compinvmeta);
                                compinv.setItemMeta(compinvmeta);
                                p.setCompassTarget(loc);
                        }
                    }
            }else{
                ItemMeta compmeta = comp.getItemMeta();
                compmeta.setDisplayName(Utils.chat("&4" + df.format(loc.getX())+ " " + df.format(loc.getY())+ " " + df.format(loc.getZ())));
                comp.setItemMeta(compmeta);
                p.getInventory().addItem(comp);
                p.setCompassTarget(loc);
            }
            return false;
        }
    }
    
    I also added a few more "bukkit broadcastmessages" with the variables. So its more obvious where the error is!

    Hope that helps

    Output (open)

    [23:08:14 INFO]: FilBuild: Reload complete.
    [23:08:17 INFO]: FilBuild issued server command: /spi
    [23:08:17 INFO]: ItemStack{COMPASS x 1, UNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=-81 97 232}}
    [23:08:17 INFO]: test
    [23:08:17 INFO]: testItemStack{COMPASS x 1, UNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=-81 97 232}}
    [23:08:17 INFO]: testUNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=-81 97 232}
    [23:08:17 INFO]: testUNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=-81 97 232}
    [23:08:17 INFO]: null
    [23:08:17 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'spi' in plugin SetSpawnPoint v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.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_241]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_241]
    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_241]
    Caused by: java.lang.NullPointerException
    at me.filbuild.setspawnpoint.commands.SpInfo.onCommand(SpInfo.java:41) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    ... 15 more
     
Thread Status:
Not open for further replies.

Share This Page