Solved Not loading YML?

Discussion in 'Plugin Development' started by bowlerguy66, Sep 26, 2015.

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

    bowlerguy66

    So I am trying to have multiple YML files so I can organize data easier. I have done it with 5 files, but its not loading right for the file that is the same for the others.

    Loading the YMLs (open)


    The file that isn't loading is the gundata.yml

    Code:
            gunDataFile = new File(getDataFolder(), "gundata.yml");
            gdFile = YamlConfiguration.loadConfiguration(gunDataFile);
            Bukkit.broadcastMessage("LOADED GUNDATA");
           
            playerFile = new File(getDataFolder(), "players.yml");
            pFile = YamlConfiguration.loadConfiguration(playerFile);
            Bukkit.broadcastMessage("LOADED PLAYERS");
           
            chestFile = new File(getDataFolder(), "chests.yml");
            cFile = YamlConfiguration.loadConfiguration(chestFile);
            Bukkit.broadcastMessage("LOADED CHESTS");
    
            gravelFile = new File(getDataFolder(), "gravel.yml");
            gFile = YamlConfiguration.loadConfiguration(gravelFile);
            Bukkit.broadcastMessage("LOADED GRAVEL");
               
            serverFile = new File(getDataFolder(), "server.yml");
            sFile = YamlConfiguration.loadConfiguration(serverFile);
            Bukkit.broadcastMessage("LOADED SERVERFILE");
    

    Method to return the gundata.yml (open)

    Code:
        File gunDataFile;
        FileConfiguration gdFile;
       
        public FileConfiguration getGunData() {
            return gdFile;
        }
       
        public void saveGunData() {
            try {
                gdFile.save(gunDataFile);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    


    Error that is thrown when I try to use the getGunData() (open)

    Code:
    [12:59:21 ERROR]: Could not pass event PlayerInteractEvent to GunsCraft v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:227) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:194) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:190) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:717) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_45]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_45]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_45]
    Caused by: java.lang.NullPointerException
        at me.bowlerguy66.gunscraft.GunData.makeData(GunData.java:75) ~[?:?]
        at me.bowlerguy66.gunscraft.GunListener.onPlayerInteract(GunListener.java:36) ~[?:?]
        at sun.reflect.GeneratedMethodAccessor370.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_45]
        at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_45]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        ... 18 more
    


    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  2. Offline

    Gonmarte

    @bowlerguy66 You should add a loadFiles() method and in that method u need to check if the file existes and surround it with a try and load it and catch the exception, if the file doesnt existes you surruond it with a try and save it and catch the exception! and add it to the onEnable() method
     
  3. Offline

    bowlerguy66

    @Gonmarte I made the loadFiles() method but that didn't fix the problem. I think its because of the constructor between the classes.

    loadFiles() (open)

    Code:
        public void loadFiles() {
    
            gunDataFile = new File(getDataFolder(), "gundata.yml");
    
            if(gunDataFile.exists()) {
                try {
                    gdFile = YamlConfiguration.loadConfiguration(gunDataFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Gun data loaded properly");           
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Gun data could not be loaded!");           
                    e.printStackTrace();
                }
            } else {
                try {
                    gunDataFile.createNewFile();
                    gdFile = YamlConfiguration.loadConfiguration(gunDataFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Gun data created and loaded");           
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Gun data could not be created!");           
                    e.printStackTrace();               
                }
            }
           
            chestFile = new File(getDataFolder(), "chests.yml");
           
            if(chestFile.exists()) {
                try {
                    cFile = YamlConfiguration.loadConfiguration(chestFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Chest data loaded properly");
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Chest data could not be loaded!");
                    e.printStackTrace();
                }
            } else {
                try {
                    chestFile.createNewFile();
                    cFile = YamlConfiguration.loadConfiguration(chestFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Chest data created and loaded");           
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Chest data could not be created!");           
                    e.printStackTrace();               
                }
            }
           
            gravelFile = new File(getDataFolder(), "gravel.yml");
           
            if(gravelFile.exists()) {
                try {
                    gFile = YamlConfiguration.loadConfiguration(gravelFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Gravel data loaded properly");
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Gravel data could not be loaded!");
                    e.printStackTrace();
                }
            } else {
                try {
                    gravelFile.createNewFile();
                    gFile = YamlConfiguration.loadConfiguration(gravelFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Gravel data created and loaded");           
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Gravel data could not be created!");           
                    e.printStackTrace();               
                }
            }
           
            serverFile = new File(getDataFolder(), "server.yml");
           
            if(serverFile.exists()) {
                try {
                    sFile = YamlConfiguration.loadConfiguration(serverFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Server data loaded properly");
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Server data could not be loaded!");
                    e.printStackTrace();
                }
            } else {
                try {
                    serverFile.createNewFile();
                    sFile = YamlConfiguration.loadConfiguration(serverFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Server data created and loaded");           
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Server data could not be created!");           
                    e.printStackTrace();               
                }
            }       
    
            playerFile = new File(getDataFolder(), "players.yml");
           
            if(playerFile.exists()) {
                try {
                    pFile = YamlConfiguration.loadConfiguration(playerFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Player data loaded properly");
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Player data could not be loaded!");
                    e.printStackTrace();
                }
            } else {
                try {
                    playerFile.createNewFile();
                    pFile = YamlConfiguration.loadConfiguration(playerFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Player data created and loaded");           
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Player data could not be created!");           
                    e.printStackTrace();               
                }
            }       
           
        }
    


    This is the constructor

    Main class (open)

    Code:
    public GunData gd = new GunData(this);

    GunData (open)

    Code:
    private GunsCraft plugin;
       
    public GunData(GunsCraft instance) {
        this.plugin = instance;
    }


    And I am using the getGunData() in my main class to get access to the file.

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  4. Offline

    boysnnoco

    1. Code:
      File gunDataFile;
          FileConfiguration gdFile;
        
          public FileConfiguration getGunData() {
              return gdFile;
          }
        
          public void saveGunData() {
              try {
                  gdFile.save(gunDataFile);
              } catch (Exception e) {
                  System.out.println(e);
              }
          }
    Im assuming your getting the NPE here
     
  5. Offline

    bowlerguy66

    Yes, I have no idea why though
     
  6. Offline

    boysnnoco

    [Deleted]
     
  7. Offline

    bowlerguy66

    What?
     
  8. Offline

    boysnnoco

    Ignore what I said, it would be 4000000% easier if I could see the entire class GunData
     
  9. Offline

    bowlerguy66

    Code:
    package me.bowlerguy66.gunscraft;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.UUID;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class GunData {
    
        private GunsCraft plugin;
       
        public GunData(GunsCraft gunsCraft) {
            this.plugin = gunsCraft;
        }
       
        @SuppressWarnings("unused")
        private GunListener gl;
       
        public GunData(GunListener gunListener) {
            this.gl = gunListener;
        }
       
        public FileConfiguration getGunData() {
            return plugin.gdFile;
        }
       
        public boolean hasData(ItemStack item) {
           
            ItemMeta im = item.getItemMeta();
           
            if(im.hasLore()) {                       
               
                List<String> data = im.getLore();
               
                if(data.contains("Data:")) {
                    List<String> uuids = getGunData().getStringList("All");
                   
                    if(uuids.contains(data)) {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    
        public String getData(ItemStack item) {
           
            if(!hasData(item)) {
                return null;
            }
           
            for(String line : item.getItemMeta().getLore()) {
               
                if(line.contains("Data:")) {
                    return line;
                } else {
                    continue;
                }
            }
           
            return null;
        }
       
        public void makeData(ItemStack item, Player player) {
           
            String uuid = UUID.randomUUID().toString();
            String uuid2 = UUID.randomUUID().toString();
    
            plugin.testMethod();
           
            if(!getGunData().contains("All")) {
               
                List<String> filler = new ArrayList<String>();
               
                filler.add("TOP");
                getGunData().set("All", filler);
                plugin.saveGunData();
               
            }
           
            List<String> cuuids = getGunData().getStringList("All");
           
            if(cuuids.contains(uuid)) {
               
                item.getItemMeta().getLore().add(ChatColor.GRAY + "Data: " + uuid2);
                cuuids.add(uuid2);
               
               
                if(getGunType(item) != null) {
                    getGunData().set("Guns." + uuid2 + ".Type", getGunType(item));
                }
    
                if(getTier(item) >= 1 && getTier(item) <= 5) {
                    getGunData().set("Guns." + uuid2 + ".Tier", getTier(item));
                }   
               
                player.getInventory().getItemInHand().setType(Material.AIR);
                player.getInventory().addItem(item);
                return;
               
            }
           
            item.getItemMeta().getLore().add(ChatColor.GRAY + "Data: " + uuid);
            cuuids.add(uuid);
           
            if(getGunType(item) != null) {
                getGunData().set("Guns." + uuid + ".Type", getGunType(item));
            }
    
            if(getTier(item) >= 1 && getTier(item) <= 5) {
                getGunData().set("Guns." + uuid + ".Tier", getTier(item));
            }
                   
            player.getInventory().getItemInHand().setType(Material.AIR);
            player.getInventory().addItem(item);
            return;
           
        }
       
        public String getGunType(ItemStack item) {
           
            if(item.getType() == Material.WOOD_HOE || item.getType() == Material.STONE_HOE || item.getType() == Material.GOLD_HOE || item.getType() == Material.IRON_HOE || item.getType() == Material.DIAMOND_HOE) {
                return "Sniper";
            }
           
            if(item.getType() == Material.WOOD_SPADE || item.getType() == Material.STONE_SPADE || item.getType() == Material.GOLD_SPADE || item.getType() == Material.IRON_SPADE || item.getType() == Material.DIAMOND_SPADE) {
                return "Shotgun";
            }
    
            if(item.getType() == Material.WOOD_AXE || item.getType() == Material.STONE_AXE || item.getType() == Material.GOLD_AXE || item.getType() == Material.IRON_AXE || item.getType() == Material.DIAMOND_AXE) {
                return "Pistol";
            }
       
            if(item.getType() == Material.WOOD_PICKAXE || item.getType() == Material.STONE_PICKAXE || item.getType() == Material.GOLD_PICKAXE || item.getType() == Material.IRON_PICKAXE || item.getType() == Material.DIAMOND_PICKAXE) {
                return "Assault";
            }
    
            if(item.getType() == Material.BOW) {
                return "Crossbow";
            }
           
            return null;
           
        }
       
        public int getTier(ItemStack item) {
           
            if(item.getType() == Material.WOOD_HOE || item.getType() == Material.WOOD_SPADE || item.getType() == Material.WOOD_AXE || item.getType() == Material.WOOD_PICKAXE) {
                return 1;
            } else if(item.getType() == Material.STONE_HOE || item.getType() == Material.STONE_SPADE || item.getType() == Material.STONE_AXE || item.getType() == Material.STONE_PICKAXE) {
                return 2;
            } else if(item.getType() == Material.GOLD_HOE || item.getType() == Material.GOLD_SPADE || item.getType() == Material.GOLD_AXE || item.getType() == Material.GOLD_PICKAXE || item.getType() == Material.BOW) {
                return 3;
            } else if(item.getType() == Material.IRON_HOE || item.getType() == Material.IRON_SPADE || item.getType() == Material.IRON_AXE || item.getType() == Material.IRON_PICKAXE) {
                return 4;
            } else if(item.getType() == Material.DIAMOND_HOE || item.getType() == Material.DIAMOND_SPADE || item.getType() == Material.DIAMOND_AXE || item.getType() == Material.DIAMOND_PICKAXE) {
                return 5;
            }
    
            return 0;
           
        }
       
        public boolean isGun(ItemStack item) {
           
            if(item.getType() == Material.WOOD_HOE || item.getType() == Material.WOOD_SPADE || item.getType() == Material.WOOD_AXE || item.getType() == Material.WOOD_PICKAXE) {
                return true;
            } else if(item.getType() == Material.STONE_HOE || item.getType() == Material.STONE_SPADE || item.getType() == Material.STONE_AXE || item.getType() == Material.STONE_PICKAXE) {
                return true;
            } else if(item.getType() == Material.GOLD_HOE || item.getType() == Material.GOLD_SPADE || item.getType() == Material.GOLD_AXE || item.getType() == Material.GOLD_PICKAXE || item.getType() == Material.BOW) {
                return true;
            } else if(item.getType() == Material.IRON_HOE || item.getType() == Material.IRON_SPADE || item.getType() == Material.IRON_AXE || item.getType() == Material.IRON_PICKAXE) {
                return true;
            } else if(item.getType() == Material.DIAMOND_HOE || item.getType() == Material.DIAMOND_SPADE || item.getType() == Material.DIAMOND_AXE || item.getType() == Material.DIAMOND_PICKAXE) {
                return true;
            }
    
            return false;
           
        }
       
    }
     
  10. Offline

    boysnnoco

    Can I see your GunsCraft class?
     
  11. Offline

    bowlerguy66

    Code:
    package me.bowlerguy66.gunscraft;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class GunsCraft extends JavaPlugin {
               
        public Plugin plugin = this;
       
        public BukkitListener bl = new BukkitListener(this);               
        public Items items = new Items(this);
        public Recipes recipes = new Recipes(this);   
        public GunData gd = new GunData(this);
        public GunListener gl = new GunListener();
       
        int forcount = 0;
       
        List<String> gravel = new ArrayList<String>();
       
        File gravelFile;
        FileConfiguration gFile;
       
        public FileConfiguration getGravel() {   
            return gFile;
        }
       
        public void saveGravel() {
            try {
                gFile.save(gravelFile);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
       
        File chestFile;
        FileConfiguration cFile;
       
        public FileConfiguration getChest() {
            return cFile;
        }
       
        public void saveChest() {
            try {
                cFile.save(chestFile);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
       
        File playerFile;
        FileConfiguration pFile;
       
        public FileConfiguration getPlayer() {
            return pFile;
        }
       
        public void savePlayer() {
            try {
                pFile.save(playerFile);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
       
        File serverFile;
        FileConfiguration sFile;
       
        public FileConfiguration getServerConf() {
            return sFile;
        }
       
        public void saveServer() {
            try {
                sFile.save(serverFile);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
           
        File gunDataFile;
        FileConfiguration gdFile;
       
        public FileConfiguration getGunData() {
            return gdFile;
        }
       
        public void saveGunData() {
            try {
                gdFile.save(gunDataFile);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
       
        public void loadFiles() {
    
            gunDataFile = new File(getDataFolder(), "gundata.yml");
    
            if(gunDataFile.exists()) {
                try {
                    gdFile = YamlConfiguration.loadConfiguration(gunDataFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Gun data loaded properly");           
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Gun data could not be loaded!");           
                    e.printStackTrace();
                }
            } else {
                try {
                    gunDataFile.createNewFile();
                    gdFile = YamlConfiguration.loadConfiguration(gunDataFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Gun data created and loaded");           
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Gun data could not be created!");           
                    e.printStackTrace();               
                }
            }
           
            chestFile = new File(getDataFolder(), "chests.yml");
           
            if(chestFile.exists()) {
                try {
                    cFile = YamlConfiguration.loadConfiguration(chestFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Chest data loaded properly");
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Chest data could not be loaded!");
                    e.printStackTrace();
                }
            } else {
                try {
                    chestFile.createNewFile();
                    cFile = YamlConfiguration.loadConfiguration(chestFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Chest data created and loaded");           
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Chest data could not be created!");           
                    e.printStackTrace();               
                }
            }
           
            gravelFile = new File(getDataFolder(), "gravel.yml");
           
            if(gravelFile.exists()) {
                try {
                    gFile = YamlConfiguration.loadConfiguration(gravelFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Gravel data loaded properly");
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Gravel data could not be loaded!");
                    e.printStackTrace();
                }
            } else {
                try {
                    gravelFile.createNewFile();
                    gFile = YamlConfiguration.loadConfiguration(gravelFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Gravel data created and loaded");           
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Gravel data could not be created!");           
                    e.printStackTrace();               
                }
            }
           
            serverFile = new File(getDataFolder(), "server.yml");
           
            if(serverFile.exists()) {
                try {
                    sFile = YamlConfiguration.loadConfiguration(serverFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Server data loaded properly");
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Server data could not be loaded!");
                    e.printStackTrace();
                }
            } else {
                try {
                    serverFile.createNewFile();
                    sFile = YamlConfiguration.loadConfiguration(serverFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Server data created and loaded");           
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Server data could not be created!");           
                    e.printStackTrace();               
                }
            }       
    
            playerFile = new File(getDataFolder(), "players.yml");
           
            if(playerFile.exists()) {
                try {
                    pFile = YamlConfiguration.loadConfiguration(playerFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Player data loaded properly");
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Player data could not be loaded!");
                    e.printStackTrace();
                }
            } else {
                try {
                    playerFile.createNewFile();
                    pFile = YamlConfiguration.loadConfiguration(playerFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Player data created and loaded");           
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Player data could not be created!");           
                    e.printStackTrace();               
                }
            }       
           
        }
           
        @Override
        public void onEnable() {
           
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(bl, this);
            pm.registerEvents(gl, this);
    
            loadFiles();
           
            recipes.addRecipes();
    
            addGravel();
            regenerateGravel();
           
            lootTimer();
           
        }
       
        @Override
        public void onDisable() {
            fixAllGravel();
            saveChest();
            saveGravel();
            saveServer();
            saveGunData();
            recipes.removeRecipes();
        }   
       
        public void testMethod() {
            Bukkit.broadcastMessage("TEST WORKED");
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if(cmd.getName().equalsIgnoreCase("tois")) {
               
                if(!getGunData().contains("All")) {
                    List<String> filler = new ArrayList<String>();
                    filler.add("TOP");
                    getGunData().set("All", filler);
                    saveGunData();
                }
               
                for(String outcome : getGunData().getStringList("All")) {
                    Bukkit.broadcastMessage(outcome);
                }
               
            }
           
            if(label.equalsIgnoreCase("gc") || label.equalsIgnoreCase("guncraft")) {
               
                if(sender instanceof Player) {
                   
                } else {
                    sender.sendMessage(ChatColor.RED + "You must be a player!");
                    return true;
                }
               
                Player player = (Player) sender;
                           
                if(args[0].equalsIgnoreCase("creativemenu")) {
    
                    if(!player.hasPermission(new Permissions().creativeMenu)) {
                        player.sendMessage(new Msg().errNOPERM);
                        return true;
                    }
                   
                    openCreativeMenu(player);
                    return true;
    
                }
               
            }
           
            return false;
           
        }
    
        public String getUUID(Location loc) {
           
            if(loc.getBlock().getType() != Material.CHEST) {
                return null;
            }
           
            ConfigurationSection csec = getChest().getConfigurationSection("Chests");
           
            for(String uuid : csec.getKeys(false)) {
               
                String aworld = loc.getWorld().getName();
                int ax = loc.getBlockX();
                int ay = loc.getBlockY();
                int az = loc.getBlockZ();
               
                int bx = getChest().getInt("Chests." + uuid + ".X");
                int by = getChest().getInt("Chests." + uuid + ".Y");
                int bz = getChest().getInt("Chests." + uuid + ".Z");
           
                if(!aworld.equalsIgnoreCase("guncraft")) {
                    return null;
                }
           
                if((ax == bx) && (ay == by) && (az == bz)) {
                    return uuid;
                }
               
            }
    
            return null;
           
        }
       
        public void addGravel() {
            this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                   
                    ConfigurationSection csec = getGravel().getConfigurationSection("Gravel");
           
                    for(String block : csec.getKeys(false)) {
               
                        if(!gravel.contains(block)) {
                            gravel.add(block);
                        }
                       
                    }
                   
                }
            }, 0L, 20L);
        }
       
        public void regenerateGravel() {
            this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                   
                    if(gravel.size() == 0) {
                        return;
                    }
    
                    int a = gravel.size();
                    int r = new Random().nextInt(a) + 1;
                    forcount = 0;
                   
                    for(String uuid : gravel) {
                                           
                        forcount++;
                       
                        if(forcount == r) {
                           
                            forcount = 0;
                           
                            String world = getGravel().getString("Gravel." + uuid + ".World");
                            int x = getGravel().getInt("Gravel." + uuid + ".X");
                            int y = getGravel().getInt("Gravel." + uuid + ".Y");
                            int z = getGravel().getInt("Gravel." + uuid + ".Z");
                           
                            if(world == null) {
                                getGravel().set("Gravel." + uuid, null);
                                saveGravel();
                                return;
                            }
                           
                            Location loc = new Location(Bukkit.getWorld(world), x, y, z);
                            Block b = loc.getBlock();
                           
                            b.setType(Material.GRAVEL);
                           
                            getGravel().set("Gravel." + uuid, null);
                            saveGravel();
                           
                            gravel.remove(uuid);
                           
                        }                   
                    }
                   
                }           
            }, 0L, 20L);
        }
       
        public void fixAllGravel() {
           
            ConfigurationSection csec = getGravel().getConfigurationSection("Gravel");
           
            for(String uuid : csec.getKeys(false)) {
               
                String world = getGravel().getString("Gravel." + uuid + ".World");
                int x = getGravel().getInt("Gravel." + uuid + ".X");
                int y = getGravel().getInt("Gravel." + uuid + ".Y");
                int z = getGravel().getInt("Gravel." + uuid + ".Z");           
               
                Location loc = new Location(Bukkit.getWorld(world), x, y, z);
                Block b = loc.getBlock();
               
                b.setType(Material.GRAVEL);
               
                bl.gravelloc.remove(loc);
                getGravel().set("Gravel." + uuid, null);
                saveGravel();
           
            }
           
        }
       
        public boolean looted(Player player, Location loc) {
           
            String uuid = player.getUniqueId().toString();
           
            if(!getPlayer().contains("Players." + uuid + ".Looted")) {
               
                List<String> l = new ArrayList<String>();
               
                l.add("TOP");
               
                getPlayer().set("Players." + uuid + ".Looted", l);
                savePlayer();
                return false;
               
            }
           
            getPlayer().set("Players." + uuid + ".Name", player.getName().toString());
            savePlayer();
            List<String> lst = getPlayer().getStringList("Players." + uuid + ".Looted");
    
            if(lst.contains(getUUID(loc))) {
                return true;
            }
           
            return false;
           
        }
    
        @SuppressWarnings("deprecation")
        public void lootTimer() {
            this.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() {
                @Override
                public void run() {
                   
                    if(!getServerConf().contains("Server.LootTimer")) {
                        getServerConf().set("Server.LootTimer", 28800);
                        saveServer();
                        return;
                    }
                   
                    int time = getServerConf().getInt("Server.LootTimer");
                   
                    if(time <= 0) {
                        getServerConf().set("Server.LootTimer", 28800);
                        resetLoot();
                        saveServer();
                        return;                   
                    }
                                                   
                    getServerConf().set("Server.LootTimer", time - 1);
                    saveServer();
                    return;
                   
                }
            }, 0L, 20L);
        }
       
        public void resetLoot() {
       
            ConfigurationSection csec = getPlayer().getConfigurationSection("Players");
           
            for(String uuid : csec.getKeys(false)) {
               
                if(getPlayer().contains("Players." + uuid + ".Looted")) {
                   
                    getPlayer().set("Players." + uuid + ".Looted", null);
                    savePlayer();
                   
                }
               
            }
           
        }
       
        public ItemStack randomItem() {
           
            Random random = new Random();
            int gt = random.nextInt(100);
                           
            if(gt >= 0 && gt <= 5) {
               
                // GUN
                                       
                int tier = random.nextInt(1000002);
                                       
                if(tier == 0) {
                   
                    // TIER 5
                   
                    int type = random.nextInt(5);
                                   
                    if(type == 0) {
                        return items.t5aslt();
                    } else if(type == 1) {
                        return items.t5pst();
                    } else if(type == 2) {
                        return items.t5sht();
                    } else if(type == 3) {
                        return items.t5snp();
                    } else if(type == 4) {
                        return items.katana();
                    } else {
                        return new ItemStack(Material.OBSIDIAN);
                    }
                   
                } else if(tier >= 1 && tier <= 50000) {
                   
                    // TIER 4
                   
                    int type = random.nextInt(5);
                   
                    if(type == 0) {
                        return items.t4aslt();
                    } else if(type == 1) {
                        return items.t4pst();
                    } else if(type == 2) {
                        return items.t4sht();
                    } else if(type == 3) {
                        return items.t4snp();
                    } else if(type == 4) {
                        return items.machete();
                    } else {
                        return new ItemStack(Material.OBSIDIAN);
                    }
                   
                } else if(tier >= 50001 && tier <= 150000) {
                   
                    // TIER 3
                   
                    int type = random.nextInt(5);
                   
                    if(type == 0) {
                        return items.t3aslt();
                    } else if(type == 1) {
                        return items.t3pst();
                    } else if(type == 2) {
                        return items.t3sht();
                    } else if(type == 3) {
                        return items.t3snp();
                    } else if(type == 4) {
                        return items.crossbow();
                    } else {
                        return new ItemStack(Material.OBSIDIAN);
                    }
                   
                } else if(tier >= 150001 && tier <= 300000) {
                   
                    // TIER 2
               
                    int type = random.nextInt(5);
                   
                    if(type == 0) {
                        return items.t2aslt();
                    } else if(type == 1) {
                        return items.t2pst();
                    } else if(type == 2) {
                        return items.t2sht();
                    } else if(type == 3) {
                        return items.t2snp();
                    } else if(type == 4) {
                        return items.knife();
                    } else {
                        return new ItemStack(Material.OBSIDIAN);
                    }
                   
                } else if(tier >= 300001 && tier <= 1000000) {
                   
                    // TIER 1
                   
                    int type = random.nextInt(5);
                                   
                    if(type == 0) {
                        return items.t1aslt();
                    } else if(type == 1) {
                        return items.t1pst();
                    } else if(type == 2) {
                        return items.t1sht();
                    } else if(type == 3) {
                        return items.t1snp();
                    } else if(type == 4) {
                        return items.baseballBat();
                    } else {
                        return new ItemStack(Material.OBSIDIAN);
                    }
                   
                }
               
            } else if(gt >= 6 && gt <= 15) {
               
                // ITEM
    
                int item = random.nextInt(14);
               
                if(item == 0) {
                    return items.ironBar();
                } else if(item == 1) {
                    return items.handBook();
                } else if(item == 2) {
                    return items.stick();
                } else if(item == 3) {
                    return items.gunPowder();
                } else if(item == 4 || item == 5) {
                    return items.flamePowder();
                } else if(item == 6 || item == 7) {
                    return items.button();
                } else if(item == 8 || item == 9) {
                    return items.circuit();
                } else if(item == 10 || item == 11) {
                    return items.wax();
                } else if(item == 12 || item == 13) {
                    return items.light();
                }
               
            } else if(gt >= 16 && gt <= 25) {
               
                // AMMO
               
                int type = random.nextInt(5);
               
                if(type == 0) {
                    int i = random.nextInt(16) + 1;
                   
                    return items.setMeta(new ItemStack(Material.INK_SACK, i, (short) 1), ChatColor.GOLD + "Basic sniper ammo", Arrays.asList(ChatColor.GRAY + "Usage: Ammo for any sniper rifle.", ChatColor.GRAY + "Damage: Not specified, tell staff."));
                } else if(type == 1) {
                    int i = random.nextInt(16) + 1;
                   
                    return items.setMeta(new ItemStack(Material.INK_SACK, i, (short) 4), ChatColor.GOLD + "Basic shotgun ammo", Arrays.asList(ChatColor.GRAY + "Usage: Ammo for any crossbow.", ChatColor.GRAY + "Damage: Not specified, tell staff."));
                } else if(type == 2) {
                    int i = random.nextInt(32) + 1;
                   
                    return items.setMeta(new ItemStack(Material.INK_SACK, i, (short) 5), ChatColor.GOLD + "Basic pistol ammo", Arrays.asList(ChatColor.GRAY + "Usage: Ammo for any pistol.", ChatColor.GRAY + "Damage: Not specified, tell staff."));
                } else if(type == 3) {               
                    int i = random.nextInt(32) + 1;
                       
                    return new ItemStack(items.setMeta(new ItemStack(Material.INK_SACK, i, (short) 2), ChatColor.GOLD + "Basic assault ammo", Arrays.asList(ChatColor.GRAY + "Usage: Ammo for any assault rifle.", ChatColor.GRAY + "Damage: Not specified, tell staff.")));
                } else if(type == 4) {
                    int i = random.nextInt(8) + 1;
               
                    return new ItemStack(items.setMeta(new ItemStack(Material.ARROW, i), ChatColor.GOLD + "Crossbow bolt", Arrays.asList(ChatColor.GRAY + "Usage: Ammo for any crossbow.", ChatColor.GRAY + "Damage: Not specified, tell staff.")));
                }
               
            } else {
    
            }       
    
            return new ItemStack(Material.AIR);
           
        }
       
        public void openCreativeMenu(Player player) {
           
            Inventory crate = Bukkit.createInventory(null, 54, "Gun craft creative menu");
           
            crate.setItem(0, items.assltAmmo());
            crate.setItem(1, items.assltAmmoExplosive());
            crate.setItem(2, items.baseballBat());
            crate.setItem(3, items.button());
            crate.setItem(4, items.circuit());
            crate.setItem(5, items.crossbow());
            crate.setItem(6, items.crossbowBolt());
            crate.setItem(7, items.crossbowBoltFlaming());
            crate.setItem(8, items.flamePowder());
            crate.setItem(9, items.gunPowder());
            crate.setItem(10, items.hammer());
            crate.setItem(11, items.ironBar());
            crate.setItem(12, items.ironBarrel());
            crate.setItem(13, items.ironChunks());
            crate.setItem(14, items.ironPlate());
            crate.setItem(15, items.katana());
            crate.setItem(16, items.knife());
            crate.setItem(17, items.light());
            crate.setItem(18, items.lighter());
            crate.setItem(19, items.machete());
            crate.setItem(20, items.pstlAmmo());
            crate.setItem(21, items.pstlAmmoFlaming());
            crate.setItem(22, items.rock());
            crate.setItem(23, items.shtAmmo());
            crate.setItem(24, items.shtAmmoFlaming());
            crate.setItem(25, items.shtAmmoWax());
            crate.setItem(26, items.snipAmmo());
            crate.setItem(27, items.snipAmmoExplosive());
            crate.setItem(28, items.snipAmmoFlaming());
            crate.setItem(29, items.t1aslt());
            crate.setItem(30, items.t1pst());
            crate.setItem(31, items.t1sht());
            crate.setItem(32, items.t1snp());
            crate.setItem(33, items.t2aslt());
            crate.setItem(34, items.t2pst());
            crate.setItem(35, items.t2sht());
            crate.setItem(36, items.t2snp());
            crate.setItem(37, items.t3aslt());
            crate.setItem(38, items.t3pst());
            crate.setItem(39, items.t3sht());
            crate.setItem(40, items.t3snp());
            crate.setItem(41, items.t4aslt());
            crate.setItem(42, items.t4pst());
            crate.setItem(43, items.t4sht());
            crate.setItem(44, items.t4snp());
            crate.setItem(45, items.t5aslt());
            crate.setItem(46, items.t5pst());
            crate.setItem(47, items.t5sht());
            crate.setItem(48, items.t5snp());
            crate.setItem(49, items.wax());
            crate.setItem(50, items.handBook());
            crate.setItem(51, items.stick());
    
            player.openInventory(crate);
           
        }
       
        public void openCrateT1(Player player) {
           
            Inventory crate = Bukkit.createInventory(null, 9, "Tier 1 loot crate");
    
            for(int i = 0; i <= 8; i++) {
    
                crate.setItem(i, randomItem());
    
                player.openInventory(crate);
               
            }
           
        }
       
        public void openCrateT2(Player player) {
           
            Inventory crate = Bukkit.createInventory(null, 18, "Tier 2 loot crate");
    
            for(int i = 0; i <= 17; i++) {
               
                crate.setItem(i, randomItem());
               
                player.openInventory(crate);
               
            }
           
        }
       
        public void openCrateT3(Player player) {
           
            Inventory crate = Bukkit.createInventory(null, 27, "Tier 3 loot crate");
    
            for(int i = 0; i <= 26; i++) {
               
                crate.setItem(i, randomItem());
    
                player.openInventory(crate);
               
            }
           
        }
       
        public boolean hasInventory(Player player) {
            try {
                player.getInventory();
                return true;
            } catch(Exception e) {
                return false;
            }
        }
       
    }
     
  12. Offline

    boysnnoco

    Are you seeing anything getting loaded in the console like the Gun data created?
     
  13. Offline

    bowlerguy66

    Yes, gun data is created
     
  14. Offline

    boysnnoco

    check GunListener line 36
     
  15. Offline

    bowlerguy66

    I'm not at my computer at the moment, but I'm pretty sure that calls the makeData(Item i, Player player) method
     
  16. Offline

    NickenatorDev

    Code:
    public void createGunDataFile() {
            if (!this.gunDataFile.exists()) {
                try {
                    this.gunDataFile.createNewFile();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        }
    In your onEnable() method in your main class, white out "this.createGundataFile()" anywhere in the method. If you do not understand the code, the method is basically checking if the gun data configuration file exists. If not, it tries creating it in a try/catch statement.
     
  17. Offline

    bowlerguy66

    @NickenatorDev
    That's what the loadFiles() does

    bump

    Solved it! I just made the constructor static XD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  18. Offline

    Gonmarte

  19. Offline

    bowlerguy66

  20. Offline

    Gonmarte

    @bowlerguy66 I think your loadFiles() has some troubles :p
     
Thread Status:
Not open for further replies.

Share This Page