config.yml item stack

Discussion in 'Plugin Development' started by IsHashing, Nov 9, 2020.

  1. Offline

    IsHashing

    Code:
    ItemStack itemMeta = new ItemStack(Material.GLASS);
                                 
                                itemMeta = ItemStack.deserialize(Home.getPlugin().Config.getString("aa."+key+".b"));
     
  2. Offline

    timtower Moderator Moderator

    @IsHashing You do realize that you can also do Config#getItemStack right?
     
  3. Offline

    IsHashing


    how can I do this
    Code:
    for(String key : keys){
                                ItemStack itemMeta = new ItemStack(getConfig().getItemStack(Home.getPlugin().Config.getConfig().getString("a."+key+".b")));
                           
                                }
     
    Last edited: Nov 9, 2020
  4. Offline

    Strahan

    How can you do what? That code would be reading a rather weird config, being that you are pulling the ItemStack from a dynamic path based on another config path.

    Also, do not hang methods off of other methods that may return null. If that weird dynamic pathing is actually what you want, you'd be better served to do:
    Code:
    for (String key : keys) {
      String itemPath = Home.getPlugin().Config.getConfig().getString("a."+key+".b");
      if (itemPath == null) continue;
    
      ItemStack item = getConfig().getItemStack(itemPath);
      if (item == null) continue;
    
      // do stuff
    }
    Why in your code, by the way, do you reference getConfig() by Home.getPlugin() then later directly? Is this code running in the class that extends JavaPlugin? If so, just reference getConfig() directly. If it isn't, then the direct getConfig() part would fail.
     
  5. Offline

    IsHashing

    Code:
    Inventory inv = Bukkit.createInventory(null, 3 * 5, ChatColor.translateAlternateColorCodes('&', "&bShop"));
                                p.openInventory(inv);
                                Set<String> keys = Home.getPlugin().Config.getConfig().getConfigurationSection("a").getKeys(false);
                                for(String key : keys){
                                          String itemPath = Home.getPlugin().Config.getConfig().getString("a."+key+".b");
                                          if (itemPath == null) continue;
    
                                          ItemStack item = getConfig().getItemStack(itemPath);
                                          if (item == null) continue;
                                       
                                          inv.addItem(item);
    error
     
    Last edited: Nov 9, 2020
  6. Offline

    Strahan

    You should read your errors when they happen, it would have told you exactly what is wrong. 15 is not a multiple of 9, you cannot create an inventory with a size of 15.

    Also is this code running in its own separate class from the main one or the one that extends JavaPlugin? Because when I wrote that example I just copied and pasted what you had and reorganized it. Now that I look at it, your method of accessing the configuration object is rather odd.

    PS, in the future, post the full stack trace when you have an error. Simply saying "error" doesn't tell us anything useful, so I'm assuming that the error would be from the first thing wrong in the code you posted but it could be something else in a part you didn't post for all I know.
     
  7. Offline

    IsHashing


    Code:
    public class Home extends JavaPlugin implements Listener{
        public C_Config Config = new C_Config(this, "config.yml");
        private static Home plugin;
        public static Home getInstance() {
            return plugin;
        }
        public static Home getPlugin() {
            return JavaPlugin.getPlugin(Home.class);
        }

    Code:
    if(args[0].equalsIgnoreCase("comprar") && args[1].equalsIgnoreCase(args[1])) {
                            if(a == null) {
                                Inventory inv = Bukkit.createInventory(null, 3 * 5, ChatColor.translateAlternateColorCodes('&', "&bLoja de a "));
                                p.sendMessage("Esta a não existe veja uma em nosso menu");
                                p.openInventory(inv);
                                
                                Set<String> keys = Home.getPlugin().Config.getConfig().getConfigurationSection("a").getKeys(false);
                                for(String key : keys){
                                          String itemPath = Home.getPlugin().Config.getConfig().getString("a."+key+".b");
                                          if (itemPath == null) continue;
    
                                          ItemStack item = getConfig().getItemStack(itemPath);
    
                                          if (item == null) continue;
    
                                          inv.addItem(item);
                          
                                }
                                 

    log dos erros
    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'maqu
    ina' in plugin a v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    gotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServe
    r.java:641) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:1162) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java
    :997) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :45) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :1) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_261]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_261]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigotmc-1
    .8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:7
    15) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    74) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    54) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :557) [spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_261]
    Caused by: java.lang.IllegalArgumentException: Chests must have a size that is a
     multiple of 9!
            at org.apache.commons.lang.Validate.isTrue(Validate.java:136) ~[spigotmc
    -1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.createInventory(CraftServe
    r.java:1524) ~[spigotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.Bukkit.createInventory(Bukkit.java:938) ~[spigotmc-1.8.8-R
    0.1.jar:git-Spigot-e4d4710-e1ebe52]
            at net.hash.Home.onCommand(Home.java:145) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    gotmc-1.8.8-R0.1.jar:git-Spigot-e4d4710-e1ebe52]
            ... 15 more
    >
    
    
    resolved

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 10, 2020
  8. Offline

    Strahan

    I'm assuming you re-read my earlier post and fixed the problem I pointed out as you have added "resolved", but just to reiterate, you need to read those stack traces. Look for the caused line, it tells you what's up:
    [​IMG]
     

Share This Page