Getting values from the config for a new ItemStack ?!

Discussion in 'Plugin Development' started by mikekats, Aug 7, 2016.

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

    mikekats

    Okey so i am trying to get the values for the ItemStack from the config :
    Code:
    if(fTime > 0) {
                ItemStack Item1 = new ItemStack(Material.getMaterial(plugin.getConfig().getString("Material1")), 3 * fTime);
                p.getInventory().addItem(Item1);
                p.sendMessage("You got your items!");
            }
    But it doesnt work.... this works :
    Code:
    if(fTime > 0) {
                ItemStack Item1 = new ItemStack(Material.STONE, 3 * fTime);
                p.getInventory().addItem(Item1);
                p.sendMessage("You got your items!");
            }
    So the problem is that i cant get the values from the config...

    P.S. I tried getting ints and not strings, that didn't work either ..

    P.S.2: It has plugin. ... because this is in the listener class and i have an instance of the main so i can use getConfig and stuff..
     
  2. @mikekats Post your full server log and you will need to use toUpperCase for the material or use matchMaterial. Follow naming conventions
     
  3. Offline

    mikekats

    @bwfcwalshy

    I want to do it with IDs anyway :
    Code:
    if(fTime > 0) {
                ItemStack Item1 = new ItemStack(Material.getMaterial(plugin.getConfig().getInt("Item1id")), 3 * fTime);
                p.getInventory().addItem(Item1);
                p.sendMessage("You got your items!");
            }
    Error :
    Code:
    Could not pass event PlayerBedLeaveEvent to SleeperZ v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:509) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:494) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.EntityHuman.a(EntityHuman.java:1181) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.EntityPlayer.a(EntityPlayer.java:545) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:1129) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.PacketPlayInEntityAction.a(PacketPlayInEntityAction.java:34) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.PacketPlayInEntityAction.handle(PacketPlayInEntityAction.java:46) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
    Caused by: java.lang.NullPointerException
            at me.spooky.main.SleeperZListener.OnPlayerBedLeaveEvent(SleeperZListener.java:47) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_101]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:298) ~[spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            ... 15 more
    >
    Line 47 on Listener class :
    Code:
    ItemStack Item1 = new ItemStack(Material.getMaterial(plugin.getConfig().getInt("Item1id")), 3 * fTime);
    P.S. The "Item1id" is set to the config and its there
    config.yml :
    Code:
    Item1id: '1'
    No one knows how to ? :'(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 7, 2016
  4. @mikekats
    My guess is that you never define the "plugin" variable, but I'd have to see the code to confirm it.

    Also, because the number in your config is enclosed in apostrophes, YAML will perceive it as a String, not an int, and because of that, it will always give you 0 (because it returns null, but null as an int becomes 0).

    You should also avoid using the numerical ID's for items, it is deprecated and has been replaced by string names for materials.

    You should also, as @bwfcwalshy pointed out, follow naming conventions. This will make your code way more readable to both you, and others, such as us, reading your code.
     
  5. Offline

    mikekats

    @AlvinB

    plugin is there at the code .. i use an instance of my main class :
    Code:
    private SleeperZMain plugin;
    
        public SleeperZListener(SleeperZMain plugin) {
            this.plugin = plugin;
        }
    can somehow remove the apostrophes ?

    i have a command to set the values on my main class :
    Code:
    if(cmd.getName().equalsIgnoreCase("add")){
                            if(args.length == 0) {
                                p.sendMessage(ChatColor.DARK_GREEN + "Please specify the item's id and the amount!");
                                p.sendMessage(ChatColor.DARK_GREEN + "Usage /add (id) (amount)");
                            }
                            else if(args.length == 2) {
    
                                if(!(getConfig().contains("Item1id")&&getConfig().contains("Item1idValue"))) {
    
    
                                        getConfig().set("Item1id", args[0]);
                                        getConfig().set("Item1idValue", args[1]);
                                      
                                        p.sendMessage("Values have been set!");
    
                                        saveConfig();
                                    }
                             
    
                                else {
    
                                    getConfig().set("Item1id", args[0]);
                                    getConfig().set("Item1idValue", args[1]);
                                  
                                    p.sendMessage("Values have been set!");
                                  
                                    saveConfig();
                                  
                                }
                            }
                     }
                   
                     return true;
                }
              
            }
            return false;
        }
    @bwfcwalshy do you know what is going on ? :/
     
    Last edited: Aug 7, 2016
  6. @mikekats
    Can you show me where oyu use this "SleeperZListener" constructor?

    Also, yes you can remove the apostrophes, simply when saving it instead of saving the string, use Integer.parseInt(aString) to get it as an int.
     
  7. Offline

    mikekats

    Here is the whole listener class :
    EDIT:
    Deleted it cause there was no need for it to be here :p he was asking for my main class

    Also can i have some example code on how to remove the apostrophes ?
     
    Last edited: Aug 7, 2016
  8. @mikekats
    I meant, can you show me the class where you call this constructor (presumably your main class)?

    Alright so, right now you are just saving a normal String. But if you instead were to take this String, put it into Integer.parseInt() you would get an int. Take this int, and save it to the config.
     
  9. Offline

    mikekats

    @AlvinB
    Oh you mean this part of my main class :
    Code:
    public class SleeperZMain extends JavaPlugin {
    
        public void onEnable() {
       
            PluginManager manager = getServer().getPluginManager();
            manager.registerEvents(new SleeperZListener(null), this);
       
            ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
            String command = "gamerule doDaylightCycle false";
            String cmd = "time set 18000";
            Bukkit.dispatchCommand(console, command);
            Bukkit.dispatchCommand(console, cmd);
    
            saveConfig();
         
            }
    
    
        public void onDisable() {
       
            saveConfig();
       
            }
    Here is the command were I set the values :
    Code:
    if(cmd.getName().equalsIgnoreCase("add")){
                if(args.length == 0) {
                    p.sendMessage(ChatColor.DARK_GREEN + "Please specify the item's id and the amount!");
                    p.sendMessage(ChatColor.DARK_GREEN + "Usage /add (id) (amount)");
                }
                else if(args.length == 2) {
                
                     if(cmd.getName().equalsIgnoreCase("add")){
                            if(args.length == 0) {
                                p.sendMessage(ChatColor.DARK_GREEN + "Please specify the item's id and the amount!");
                                p.sendMessage(ChatColor.DARK_GREEN + "Usage /add (id) (amount)");
                            }
                            else if(args.length == 2) {
    
                                if(!(getConfig().contains("Item1id")&&getConfig().contains("Item1idValue"))) {
    
    
                                        getConfig().set("Item1id", args[0]);
                                        getConfig().set("Item1idValue", args[1]);
                                    
                                        p.sendMessage("Values have been set!");
    
                                        saveConfig();
                                    }
                           
    
                                else {
    
                                    getConfig().set("Item1id", args[0]);
                                    getConfig().set("Item1idValue", args[1]);
                                
                                    p.sendMessage("Values have been set!");
                                
                                    saveConfig();
                                }
                            }
    You mean like that ? @ArsenArsen Would this work? :
    Code:
    getConfig().set("Item1id", Integer.parseInt(args[0]));
     
    Last edited: Aug 7, 2016
  10. Offline

    ArsenArsen

    Attempts to get the Material with the given name.

    This is a normal lookup, names must be the precise name they are given in the enum.

    @AlvinB, that maybe too, but getMaterial matches precise enumerated constant name. What's better is to use matchMaterial, which allows mixed cases, but for IDs you need to loop through the materials and compare if the IDs are the same.
     
  11. @ArsenArsen
    The whole point is he is trying to do it with numerical ID's, and getMaterial(int) does precisely that.

    That's your problem. You put in null as the plugin! Just replace it with "this" and you should be all good.

    And yes, that last part with saving configs should work too.
     
  12. Offline

    ArsenArsen

    Well.. It doesn't the first three lines were a quote from the docs. What it does is take a valueOf and returns it, and if it catches something, return null.
     
  13. @ArsenArsen
    Well, there are two getMaterial() methods. One takes a String, one takes an int. The String one does what you described above, the int one gets the Material from the numerical ID.

    [​IMG]
     
  14. Offline

    ArsenArsen

    Oh.. Alright. Thanks for letting me know.
     
  15. Offline

    mikekats

    @ArsenArsen You know whats up ?
    now when i run this command :
    Code:
    else if (cmd.getName().equalsIgnoreCase("loc1")) {
            
                getConfig().set(".x", Integer.parseInt(".x", p.getLocation().getBlockX()));
                getConfig().set(".z", Integer.parseInt(".y", p.getLocation().getBlockY()));
                getConfig().set(".z", Integer.parseInt(".z", p.getLocation().getBlockZ()));
            
                saveConfig();
            
                p.sendMessage(ChatColor.DARK_RED + "Location 1 has been set!");
            
                return true;
            
            }
    i get this error :
    Code:
     null
    org.bukkit.command.CommandException: Unhandled exception executing command 'loc1' in plugin SleeperZ v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~[spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1043) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:880) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
    Caused by: java.lang.NumberFormatException: radix 59 greater than Character.MAX_RADIX
            at java.lang.Integer.parseInt(Unknown Source) ~[?:1.8.0_101]
            at me.spooky.main.SleeperZMain.onCommand(SleeperZMain.java:88) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            ... 13 more
     
    Last edited: Aug 7, 2016
  16. @mikekats .x is not a valid path, do something like location.x
     
  17. Offline

    mikekats

    @bwfcwalshy I did change it :
    Code:
    else if (cmd.getName().equalsIgnoreCase("loc1")) {
               
                getConfig().set("location.x", Integer.parseInt("location.x", p.getLocation().getBlockX()));
                getConfig().set("location.y", Integer.parseInt("location.y", p.getLocation().getBlockY()));
                getConfig().set("location.z", Integer.parseInt("location.z", p.getLocation().getBlockZ()));
               
                saveConfig();
               
                p.sendMessage(ChatColor.DARK_RED + "Location 1 has been set!");
               
                return true;
               
            }
    But it didnt work still same error:
    Code:
    null
    org.bukkit.command.CommandException: Unhandled exception executing command 'loc2' in plugin SleeperZ v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~[spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1043) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:880) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
    Caused by: java.lang.NumberFormatException: radix 57 greater than Character.MAX_RADIX
            at java.lang.Integer.parseInt(Unknown Source) ~[?:1.8.0_101]
            at me.spooky.main.SleeperZMain.onCommand(SleeperZMain.java:104) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            ... 13 more
     
  18. @mikekats
    You are trying to use "Integer.parseInt()" on "location.x" and the number as the second argument. And of course, it won't find a number in the string "location.x". All you need to do is put the p.getLocation().getBlockX() as the only argument to the parseInt() method.
     
  19. Offline

    mikekats


    If i have it like that :
    Code:
    getConfig().set("location.x", Integer.parseInt(p.getLocation().getBlockX()));
    parseInt is underlined .. it asks for a string in frond of p.getLocation().getBlockX() .. should i put null ?
     
  20. @mikekats Ypu are trying to parse an int to an int, just use the method
     
  21. Offline

    mikekats

    How? ... i am pretty sure i tried everything or i am being stupid ..

    EDIT :

    fixed by casting it into an int :
    Code:
    getConfig().set("location.x", (int) p.getLocation().getBlockX());
     
    Last edited: Aug 7, 2016
  22. @mikekats getBlockX is an int, no need to cast parse or anything, just have p.getLocation().getBlockX()
     
Thread Status:
Not open for further replies.

Share This Page