Solved What is the best way to save an indefinite number of books?

Discussion in 'Plugin Development' started by Kicksy, Jun 23, 2018.

Thread Status:
Not open for further replies.
  1. I want to save an indefinite number of books and then retrieve all of them via a command. The books should then also be able to easily delete from the document and retrieve. Saving everything in the config file is not a good idea. But I don´t know how to create a YML detei that is right for my needs. Can somebody help me with it?
     
    Last edited: Jun 23, 2018
  2. Offline

    Zombie_Striker

  3. Offline

    user_91277742

    @Kicksy Where do you have that error? , when you try to load or save the custom config?

    I will show , what i do to create and save a customconfig.
    First i create the custom yml like this
    Code:
        public File customFile = new File(this.getDataFolder() + "/example.yml");
        public FileConfiguration customConfig= YamlConfiguration.loadConfiguration(customFile);
    
    Next, i put in onEnable, this code to create by default the custom yml
    Code:
            if(!(getDataFolder().exists())){
                getDataFolder().mkdir();
            }
            try {
                customConfig.save(customFile);
                customConfig.load(customFile);
            } catch (IOException | InvalidConfigurationException e) {
                e.printStackTrace();
            }
    
    And next, i create this method, to do just one line to save/load the custom config
    Code:
        public void saveConfiguration() {
            try {
                customConfig.save(customFile);
                customConfig.load(customFile);
            } catch (IOException | InvalidConfigurationException e1) {
                e1.printStackTrace();
            }
        }
    
    So If you want to save and load the config, just use "saveConfiguration();" or "main.saveConfiguration(); if you are not in the main class.

    I hope i've helped you C:
     
    Last edited by a moderator: Jun 23, 2018
  4. I have the error in this line:
    Code:
    public FileConfiguration customConfig= YamlConfiguration.loadConfiguration(customFile);
     
  5. @Kicksy
    Whats the error say? and can you post your class
    EDIT: I think you imported the wrong YamlConfiguration
    this is the correct one
    import org.bukkit.configuration.file.YamlConfiguration;
     
    Last edited: Jun 23, 2018
    Kicksy likes this.
  6. Offline

    timtower Administrator Administrator Moderator

     
  7. Thanks @Blackwing_Forged the other import worked.

    Now I have created the file. How can I check how many books have been saved so that they will not be overwritten?
     
  8. Offline

    CommonSenze

    @Kicksy
    You would want to get the Configuration Section of where the books are saved and get the size of how many sub sections there are in that section with this method:
    Code:java
    1. config.getConfigurationSection("Section Name").getKeys(false).size()
     
  9. The file is not created.

    In the main class:
    Code:
        @Override
        public void onEnable() {
            Bukkit.broadcastMessage(prefix  + "Das Plugin wurde geladen.");
             Bukkit.getPluginManager().registerEvents(new Auswahl(), this);    
             registerCommands();
             buch.saveCustomYml(buch.customConfig, buch.customFile);
            
             if(!buch.customFile.exists()) {
                 try {
                    buch.customFile.createNewFile();
                } catch (Exception e) {
                    System.out.println("errie");
                }
             }
        }
    The class with the File:

    Code:
    package me.Kicksy.Auwahl;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    
    public class Buch {
    
        public static main plugin;
    
        public File customFile = new File(plugin.getDataFolder() + "/book.yml");
        public FileConfiguration customConfig= YamlConfiguration.loadConfiguration(customFile);
        
        
        public void saveCustomYml(FileConfiguration ymlConfig, File ymlFile) {
             try {
             ymlConfig.save(ymlFile);
             } catch (IOException e) {
             e.printStackTrace();
             }
            
         }
    
    }
    
     
  10. Offline

    timtower Administrator Administrator Moderator

    @Kicksy Stay away from statics please, you are currently having the problem that your plugin instance in Buch is null, so customFile has the wrong value, so customConfig has the wrong value.
     
  11. If I check on the beginning if the file exists in the onEnable method nothing works underneath. Why?
     
  12. Offline

    timtower Administrator Administrator Moderator

    @Kicksy Using the code above?
    Statics that are not initialized.
    That mixed with normal instances ( which is a bad thing )
     
  13. I have already removed the static.
     
  14. Offline

    timtower Administrator Administrator Moderator

    @Kicksy Then please post your new code.
     
  15. Code:
    package me.Kicksy.Auwahl;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    
    public class Buch {
    
        public main plugin;
    
        public File customFile = new File(plugin.getDataFolder() + "/book.yml");
        public FileConfiguration customConfig= YamlConfiguration.loadConfiguration(customFile);
       
       
        public void saveCustomYml(FileConfiguration ymlConfig, File ymlFile) {
             try {
             ymlConfig.save(ymlFile);
             } catch (IOException e) {
             e.printStackTrace();
             }
           
         }
    
    }
    
    Code:
    package me.Kicksy.Auwahl;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    
    import org.apache.logging.log4j.core.config.plugins.Plugin;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
    import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack;
    import org.bukkit.entity.Player;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import io.netty.buffer.ByteBuf;
    import io.netty.buffer.Unpooled;
    import net.minecraft.server.v1_12_R1.NBTTagCompound;
    import net.minecraft.server.v1_12_R1.NBTTagList;
    import net.minecraft.server.v1_12_R1.NBTTagString;
    import net.minecraft.server.v1_12_R1.PacketDataSerializer;
    import net.minecraft.server.v1_12_R1.PacketPlayOutCustomPayload;
    import ru.tehkode.permissions.bukkit.PermissionsEx;
    
    public class main extends JavaPlugin{
      
        public static Inventory inv;
        private Buch buch;
    
      
        ArrayList<String> msg = new ArrayList<>();
        PermissionsEx pex = (PermissionsEx)Bukkit.getPluginManager().getPlugin("PermissionsEx");
        public static String prefix = "§2[§4Aufgaben§2] §f";
    
      
        @Override
        public void onEnable() {
      
            if(!buch.customFile.exists()) {
                try {  
                    buch.customFile.createNewFile();
                } catch (Exception e) {
                    System.out.println("error");
                }
            }
            
            Bukkit.broadcastMessage(prefix  + "Das Plugin wurde geladen.");
            Bukkit.getPluginManager().registerEvents(new Auswahl(), this);    
            registerCommands();
            
            buch.saveCustomYml(buch.customConfig, buch.customFile);
            
              
        }
      
        @Override
        public void onDisable() {
            saveConfig();
            //buch.saveCustomYml(buch.customConfig, buch.customFile);
        }
      
        public void registerCommands() {
            Job cJob = new Job(this);
            getCommand("job").setExecutor(cJob);
          
            Angebot cAngebot = new Angebot(this);
            getCommand("angebot").setExecutor(cAngebot);
          
            Inv cInv = new Inv(this);
            getCommand("inv").setExecutor(cInv);
        }
      
        public void loadConfig()
        {
            getConfig().options().copyDefaults(true);
            saveConfig();
            reloadConfig();
        }
      
      
        public static void openBook(ItemStack book, Player p) {
            int slot = p.getInventory().getHeldItemSlot();
            ItemStack old = p.getInventory().getItem(slot);
            p.getInventory().setItem(slot, book);
    
          
           ByteBuf buf = Unpooled.buffer(256);
           buf.setByte(0, (byte)0);
           buf.writerIndex(1);
    
            PacketPlayOutCustomPayload packet = new PacketPlayOutCustomPayload("MC|BOpen", new PacketDataSerializer(buf));
            ((CraftPlayer)p).getHandle().playerConnection.sendPacket(packet);
            p.getInventory().setItem(slot, old);
        }
      
        public static ItemStack book(String title, String author, String... pages) {
                ItemStack is = new ItemStack(Material.WRITTEN_BOOK, 1);
                net.minecraft.server.v1_12_R1.ItemStack nmsis = CraftItemStack.asNMSCopy(is);
                NBTTagCompound bd = new NBTTagCompound();
                bd.setString("title", title);
                bd.setString("author", author);
                NBTTagList bp = new NBTTagList();
                for(String text : pages) {
                    bp.add(new NBTTagString(text));
                }
                bd.set("pages", bp);
                nmsis.setTag(bd);
                is = CraftItemStack.asBukkitCopy(nmsis);
                return is;
            }
    }
    
     
  16. Offline

    timtower Administrator Administrator Moderator

    @Kicksy You never set any of the variables in much, you never even make an instance of it.
     
  17. And how I do that?
     
  18. @Kicksy
    In the beginning of onEnable put
    buch = new Buch();
     
  19. I have this Error:
    Error (open)

    Code:
    [20:13:32 ERROR]: Error occurred while enabling Aufgaben v1.0.0 (Is it up to date?)
    java.lang.NullPointerException: null
            at me.Kicksy.Auwahl.Buch.<init>(Buch.java:13) ~[?:?]
            at me.Kicksy.Auwahl.main.onEnable(main.java:48) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:403) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:382) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:331) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.reload(CraftServer.java:753) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.Bukkit.reload(Bukkit.java:525) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:649) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1397) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1232) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_25]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_25]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_25]


    Buch Class (open)

    Code:
    package me.Kicksy.Auwahl;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    
    public class Buch {
    
        public main plugin;
    
        public File customFile = new File(plugin.getDataFolder() + "/book.yml");
        public FileConfiguration customConfig= YamlConfiguration.loadConfiguration(customFile);
    
    
        public void saveCustomYml(FileConfiguration ymlConfig, File ymlFile) {
             try {
             ymlConfig.save(ymlFile);
             } catch (IOException e) {
             e.printStackTrace();
             }
           
         }
    
    }
    


    Main class (open)

    Code:
    package me.Kicksy.Auwahl;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    
    import org.apache.logging.log4j.core.config.plugins.Plugin;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
    import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack;
    import org.bukkit.entity.Player;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import io.netty.buffer.ByteBuf;
    import io.netty.buffer.Unpooled;
    import net.minecraft.server.v1_12_R1.NBTTagCompound;
    import net.minecraft.server.v1_12_R1.NBTTagList;
    import net.minecraft.server.v1_12_R1.NBTTagString;
    import net.minecraft.server.v1_12_R1.PacketDataSerializer;
    import net.minecraft.server.v1_12_R1.PacketPlayOutCustomPayload;
    import ru.tehkode.permissions.bukkit.PermissionsEx;
    
    public class main extends JavaPlugin{
      
        public static Inventory inv;
        public Buch buch;
      
        ArrayList<String> msg = new ArrayList<>();
        PermissionsEx pex = (PermissionsEx)Bukkit.getPluginManager().getPlugin("PermissionsEx");
        public static String prefix = "§2[§4Aufgaben§2] §f";
    
      
    
              
        @Override
        public void onEnable() {
      
          
            buch = new Buch();
            if(!buch.customFile.exists()) {
                System.out.println("not");
                 try {  
                    buch.customFile.createNewFile();
                } catch (Exception e) {
                    System.out.println("error");
                }
             }
           
            Bukkit.broadcastMessage(prefix  + "Das Plugin wurde geladen.");
             Bukkit.getPluginManager().registerEvents(new Auswahl(), this);   
             registerCommands();
           
             buch.saveCustomYml(buch.customConfig, buch.customFile);
           
              
        }
      
        @Override
        public void onDisable() {
            saveConfig();
            //buch.saveCustomYml(buch.customConfig, buch.customFile);
        }
      
        public void registerCommands() {
            Job cJob = new Job(this);
            getCommand("job").setExecutor(cJob);
          
            Angebot cAngebot = new Angebot(this);
            getCommand("angebot").setExecutor(cAngebot);
          
            Inv cInv = new Inv(this);
            getCommand("inv").setExecutor(cInv);
        }
      
        public void loadConfig()
        {
            getConfig().options().copyDefaults(true);
            saveConfig();
            reloadConfig();
        }
      
      
        public static void openBook(ItemStack book, Player p) {
            int slot = p.getInventory().getHeldItemSlot();
            ItemStack old = p.getInventory().getItem(slot);
            p.getInventory().setItem(slot, book);
    
         
           ByteBuf buf = Unpooled.buffer(256);
           buf.setByte(0, (byte)0);
           buf.writerIndex(1);
    
            PacketPlayOutCustomPayload packet = new PacketPlayOutCustomPayload("MC|BOpen", new PacketDataSerializer(buf));
            ((CraftPlayer)p).getHandle().playerConnection.sendPacket(packet);
            p.getInventory().setItem(slot, old);
        }
      
         public static ItemStack book(String title, String author, String... pages) {
                ItemStack is = new ItemStack(Material.WRITTEN_BOOK, 1);
                net.minecraft.server.v1_12_R1.ItemStack nmsis = CraftItemStack.asNMSCopy(is);
                NBTTagCompound bd = new NBTTagCompound();
                bd.setString("title", title);
                bd.setString("author", author);
                NBTTagList bp = new NBTTagList();
                for(String text : pages) {
                    bp.add(new NBTTagString(text));
                }
                bd.set("pages", bp);
                nmsis.setTag(bd);
                is = CraftItemStack.asBukkitCopy(nmsis);
                return is;
            }
    }
    
     
    Last edited: Jun 24, 2018
  20. @Kicksy
    In your Buch class use a constructor to get the main class like this
    under the public main plugin;

    public Buch(main mainClass){
    plugin = mainClass;
    }

    and in your onEnable when you do
    buch = new Buch();
    you do
    buch = new Buch(this);
     
  21. Offline

    Zombie_Striker

    @Kicksy
    You're trying to get the data folder before you set it. Here is what you need to do.
    1. Create a constructor for Buch. Have the constructor require a 'main' instance.
    2. In the constructor, set the "plugin" field to be equal to the 'main' instance.
    3. Then, only after setting the plugin, set the customfile and load the yml.
     
  22. What am I doing wrong? Can someone please write the code and where I musst past it?

    Buch (open)

    Code:
    package me.Kicksy.Auwahl;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    
    public class Buch {
    
        public main plugin;
    
        public File customFile = new File(plugin.getDataFolder() + "/book.yml");
        public FileConfiguration customConfig= YamlConfiguration.loadConfiguration(customFile);
    
    
        public Buch(main main) {
            this.plugin = main;
        }
    
    
    
        public void saveCustomYml(FileConfiguration ymlConfig, File ymlFile) {
             try {
             ymlConfig.save(ymlFile);
             } catch (IOException e) {
             e.printStackTrace();
             }
            
         }
    
    }
    


    Main class (open)

    Code:
    package me.Kicksy.Auwahl;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    
    import org.apache.logging.log4j.core.config.plugins.Plugin;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
    import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack;
    import org.bukkit.entity.Player;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import io.netty.buffer.ByteBuf;
    import io.netty.buffer.Unpooled;
    import net.minecraft.server.v1_12_R1.NBTTagCompound;
    import net.minecraft.server.v1_12_R1.NBTTagList;
    import net.minecraft.server.v1_12_R1.NBTTagString;
    import net.minecraft.server.v1_12_R1.PacketDataSerializer;
    import net.minecraft.server.v1_12_R1.PacketPlayOutCustomPayload;
    import ru.tehkode.permissions.bukkit.PermissionsEx;
    
    public class main extends JavaPlugin{
       
        public static Inventory inv;
        public Buch buch;
       
        ArrayList<String> msg = new ArrayList<>();
        PermissionsEx pex = (PermissionsEx)Bukkit.getPluginManager().getPlugin("PermissionsEx");
        public static String prefix = "§2[§4Aufgaben§2] §f";
    
               
        @Override
        public void onEnable() {
       
            buch = new Buch(this);
            if(!buch.customFile.exists()) {
                System.out.println("not");
                 try {   
                    buch.customFile.createNewFile();
                } catch (Exception e) {
                    System.out.println("error");
                }
             }
            
            Bukkit.broadcastMessage(prefix  + "Das Plugin wurde geladen.");
             Bukkit.getPluginManager().registerEvents(new Auswahl(), this);    
             registerCommands();
            
             buch.saveCustomYml(buch.customConfig, buch.customFile);
            
               
        }
       
        @Override
        public void onDisable() {
            saveConfig();
            //buch.saveCustomYml(buch.customConfig, buch.customFile);
        }
       
        public void registerCommands() {
            Job cJob = new Job(this);
            getCommand("job").setExecutor(cJob);
           
            Angebot cAngebot = new Angebot(this);
            getCommand("angebot").setExecutor(cAngebot);
           
            Inv cInv = new Inv(this);
            getCommand("inv").setExecutor(cInv);
        }
       
        public void loadConfig()
        {
            getConfig().options().copyDefaults(true);
            saveConfig();
            reloadConfig();
        }
       
       
        public static void openBook(ItemStack book, Player p) {
            int slot = p.getInventory().getHeldItemSlot();
            ItemStack old = p.getInventory().getItem(slot);
            p.getInventory().setItem(slot, book);
    
          
           ByteBuf buf = Unpooled.buffer(256);
           buf.setByte(0, (byte)0);
           buf.writerIndex(1);
    
            PacketPlayOutCustomPayload packet = new PacketPlayOutCustomPayload("MC|BOpen", new PacketDataSerializer(buf));
            ((CraftPlayer)p).getHandle().playerConnection.sendPacket(packet);
            p.getInventory().setItem(slot, old);
        }
       
         public static ItemStack book(String title, String author, String... pages) {
                ItemStack is = new ItemStack(Material.WRITTEN_BOOK, 1);
                net.minecraft.server.v1_12_R1.ItemStack nmsis = CraftItemStack.asNMSCopy(is);
                NBTTagCompound bd = new NBTTagCompound();
                bd.setString("title", title);
                bd.setString("author", author);
                NBTTagList bp = new NBTTagList();
                for(String text : pages) {
                    bp.add(new NBTTagString(text));
                }
                bd.set("pages", bp);
                nmsis.setTag(bd);
                is = CraftItemStack.asBukkitCopy(nmsis);
                return is;
            }
    }
    
     
  23. @Kicksy

    public File customFile = new File(plugin.getDataFolder() + "/book.yml");
    public FileConfiguration customConfig= YamlConfiguration.loadConfiguration(customFile);

    needs to be under the constructor
     
  24. I still have the same error.
     
  25. Offline

    timtower Administrator Administrator Moderator

  26. @Kicksy
    Make 2 variables outside the constructor
    public File customFile;
    public FileConfiguration customConfig;
    and inside the constructor initialize them
     
    Zombie_Striker and Kicksy like this.
  27. Where is here the problem?

    Class:

    Code:
    package me.Kicksy.Auwahl;
    
    import javax.activation.MailcapCommandMap;
    
    import org.bukkit.Bukkit;
    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;
    
    public class Angebot implements CommandExecutor{
     
        private Buch buch;
        private main plugin;
        String prefix = main.prefix;
     
        public Angebot(main main) {
            this.plugin = main;
        }
     
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
          {
            Player p = (Player)sender;
         
            if (command.getName().equalsIgnoreCase("angebot")){
                if(args[0].equals("Miner")) {
                 
                    int slot = p.getInventory().getHeldItemSlot();
                    try {
                     
                 
                    if(p.getInventory().getItem(slot).getType().equals(Material.WRITTEN_BOOK)) {
                        ItemStack book = p.getInventory().getItem(slot);
                     
                        buch.customConfig.set("Angebot.Miner." + buch.customConfig.getConfigurationSection("Angebot.Miner.").getKeys(false).size(), book);
                        buch.saveCustomYml(buch.customConfig, buch.customFile);
                     
                     
                        //plugin.getConfig().set("Config.Angebot.Miner", book);
                        //plugin.saveConfig();
                     
                        p.getInventory().setItem(slot, new ItemStack(Material.AIR));
                     
                        p.sendMessage(prefix + "§6Das Angebot wurde an jeden in der Gruppe §7" + args[0] + " §6geschickt");
                        for(Player p1 :Bukkit.getOnlinePlayers()) {
                         
                            if(p1.hasPermission("Angebot.Miner")) {
                                p1.sendMessage(prefix + "§6Es ist ein neues Angebot verfügbar!");
                            }
                        }
                    }else {
                        p.sendMessage(prefix + "§cDas Item ist kein Beschreibendes Buch!");
                    }
                    } catch (Exception e) {
                            e.printStackTrace();
                        p.sendMessage(prefix + "§cError");
                    }
                }
            }
            return true; 
                 
                }
    
    }
    

    Error:
    Code:
    [23:02:14 WARN]: java.lang.NullPointerException
    [23:02:14 WARN]:        at me.Kicksy.Auwahl.Angebot.onCommand(Angebot.java:37)
    [23:02:14 WARN]:        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    [23:02:14 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
    [23:02:14 WARN]:        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:649)
    [23:02:14 WARN]:        at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1397)
    [23:02:14 WARN]:        at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1232)
    [23:02:14 WARN]:        at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45)
    [23:02:14 WARN]:        at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1)
    [23:02:14 WARN]:        at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13)
    [23:02:14 WARN]:        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    [23:02:14 WARN]:        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    [23:02:14 WARN]:        at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46)
    [23:02:14 WARN]:        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748)
    [23:02:14 WARN]:        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406)
    [23:02:14 WARN]:        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679)
    [23:02:14 WARN]:        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577)
    [23:02:14 WARN]:        at java.lang.Thread.run(Thread.java:745)

    Line 37 is:
    Code:
    buch.customConfig.set("Angebot.Miner." + buch.customConfig.getConfigurationSection("Angebot.Miner.").getKeys(false).size(), book);
     
  28. @Kicksy

    private Buch buch;

    you're never initializing this,
    get the variable from main

    in your constructor under
    this.plugin = main;
    put
    buch = this.plugin.buch;
     
    Kicksy likes this.
Thread Status:
Not open for further replies.

Share This Page