Create a list of UUIDs, save it to a file, then load it again

Discussion in 'Plugin Development' started by marti.2001, Apr 3, 2013.

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

    marti.2001

    Here is my code:
    Code (open)
    Code:java
    1. package me.marti.2001.deathzombies;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import java.util.UUID;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Material;
    9. import org.bukkit.entity.Entity;
    10. import org.bukkit.entity.EntityType;
    11. import org.bukkit.entity.LivingEntity;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.entity.EntityDeathEvent;
    16. import org.bukkit.event.entity.PlayerDeathEvent;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.inventory.meta.SkullMeta;
    19. import org.bukkit.plugin.PluginManager;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21. import org.bukkit.potion.PotionEffect;
    22. import org.bukkit.potion.PotionEffectType;
    23.  
    24. public class Main extends JavaPlugin implements Listener {
    25.  
    26. public void onEnable(){
    27. PluginManager pm = getServer().getPluginManager();
    28. pm.registerEvents(this, this);
    29. getConfig().options().copyDefaults(true);
    30. saveConfig();
    31. for (String string: strs) {
    32. uuids.add(UUID.fromString(string));
    33. }
    34. }
    35.  
    36. List<String> strs = getConfig().getStringList("zombiesUUID");
    37. List<UUID> uuids= new ArrayList<UUID>();
    38.  
    39. public void onDisable(){
    40. strs.add(uuids.toString());
    41. getConfig().set("zombiesUUID", strs);
    42. }
    43. private ItemStack head(String name){
    44. ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1);
    45. skull.setDurability((short)3);
    46. SkullMeta meta = (SkullMeta)skull.getItemMeta();
    47. meta.setOwner(name);
    48. skull.setItemMeta(meta);
    49. return skull;
    50. }
    51.  
    52. @EventHandler
    53. public void onPlayerDeath(PlayerDeathEvent e){
    54. Entity zombie1 = e.getEntity().getPlayer().getWorld().spawnEntity(e.getEntity().getPlayer().getLocation(), EntityType.ZOMBIE);
    55. LivingEntity zombie2 = (LivingEntity) zombie1;
    56. uuids.add(zombie2.getUniqueId());
    57. zombie2.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, Integer.MAX_VALUE, 3));
    58. zombie2.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, Integer.MAX_VALUE, 1));
    59. zombie2.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, Integer.MAX_VALUE, 2));
    60. zombie2.setCustomName(ChatColor.RED + e.getEntity().getPlayer().getDisplayName());
    61. e.getEntity().getPlayer().sendMessage(ChatColor.DARK_GRAY+"[Zombie] " + ChatColor.GRAY + "A zombie has been spawned on X: " + zombie2.getLocation().getBlockX() + ", Y: " + zombie2.getLocation().getBlockY() + ", Z:" + zombie2.getLocation().getBlockZ() + ".");
    62. zombie2.getEquipment().setHelmet(head(e.getEntity().getPlayer().getName()));
    63. zombie2.getEquipment().setHelmetDropChance(0);
    64. if(e.getEntity().getPlayer().getInventory().getChestplate() != null){
    65. zombie2.getEquipment().setChestplate(e.getEntity().getPlayer().getEquipment().getChestplate());
    66. zombie2.getEquipment().setChestplateDropChance(0);
    67. }
    68. if(e.getEntity().getPlayer().getInventory().getLeggings() != null){
    69. zombie2.getEquipment().setLeggings(e.getEntity().getPlayer().getEquipment().getLeggings());
    70. zombie2.getEquipment().setLeggingsDropChance(0);
    71. }
    72. if(e.getEntity().getPlayer().getInventory().getBoots() != null){
    73. zombie2.getEquipment().setBoots(e.getEntity().getPlayer().getEquipment().getBoots());
    74. zombie2.getEquipment().setBootsDropChance(0);
    75. }
    76. if(e.getEntity().getPlayer().getInventory().getItemInHand() != null){
    77. zombie2.getEquipment().setItemInHand(e.getEntity().getPlayer().getEquipment().getItemInHand());
    78. zombie2.getEquipment().setItemInHandDropChance(0);
    79. }
    80.  
    81. }
    82. @EventHandler
    83. public void onEntityDeath(EntityDeathEvent e){
    84. if(e.getEntity().getType().equals(EntityType.ZOMBIE) && e.getEntity().getKiller() instanceof Player && uuids.contains(e.getEntity().getUniqueId())){
    85. uuids.remove(e.getEntity().getUniqueId());
    86. e.setDroppedExp(150);
    87. e.getEntity().getKiller().setHealth(1);
    88. e.getEntity().getKiller().addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 400, 2));
    89. e.getEntity().getKiller().addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 400, 2));
    90. e.getEntity().getKiller().addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 400, 3));
    91. }
    92. }
    93.  
    94. }
    95.  
     
  2. Offline

    NoLiver92

    marti.2001 what is line 29 doing? and secondly your declaring a variable outside of the on enable class (lines 36 & 37). put these in your on enable under the save config on line 30. you are trying to load something from the config before the files has been created.

    Do this and then see if there are any more errors
     
  3. Offline

    marti.2001

    NoLiver92 When I move lines 36 and 37 in onEnable() under saveConfig, Eclipse tells me there are errors. When I move them back and delete line 29, I get "File cannot be null" again:(
     
  4. Offline

    NoLiver92

    what are the errors? they should either be prefixed with public or private or moved inside a function. if you can post the error stack trace here it might help a bit more.
     
  5. Offline

    marti.2001

    NoLiver92 here's the error:
    Error (open)

    Code:
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalArgumentException: File cannot be null
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:182)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.v1_5_R2.CraftServer.loadPlugins(CraftServer.java:239)
        at org.bukkit.craftbukkit.v1_5_R2.CraftServer.reload(CraftServer.java:603)
        at org.bukkit.Bukkit.reload(Bukkit.java:184)
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:188)
        at org.bukkit.craftbukkit.v1_5_R2.CraftServer.dispatchCommand(CraftServer.java:523)
        at org.bukkit.craftbukkit.v1_5_R2.CraftServer.dispatchServerCommand(CraftServer.java:512)
        at net.minecraft.server.v1_5_R2.DedicatedServer.am(DedicatedServer.java:261)
        at net.minecraft.server.v1_5_R2.DedicatedServer.r(DedicatedServer.java:226)
        at net.minecraft.server.v1_5_R2.MinecraftServer.q(MinecraftServer.java:474)
        at net.minecraft.server.v1_5_R2.MinecraftServer.run(MinecraftServer.java:407)
        at net.minecraft.server.v1_5_R2.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.IllegalArgumentException: File cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:203)
        at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:170)
        at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:117)
        at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:111)
        at me.marti201.zombipriumirane.Main.<init>(Main.java:35)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:178)
        ... 14 more
    
     
  6. Offline

    NoLiver92

    this is the error:
    Code:
    List<String> strs = getConfig().getStringList("zombiesUUID");
    List<UUID> uuids= new ArrayList<UUID>();
    you must put this in the on enable or put public or private before it. the best bet as its calling the config is to put it in the main:

    Code:
    public void onEnable(){
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvents(this, this);
    saveDefaultConfig();
    List<String> strs = getConfig().getStringList("zombiesUUID");
    List<UUID> uuids= new ArrayList<UUID>();
    for (String string: strs) {
    uuids.add(UUID.fromString(string));
    }
    }
    
    Note also in the config.yml inside your developer (in eclipse its in the project folder navigaton bar) MUST HAVE
    Otherwise you wont get anything as it will come back empty. change your on enable to the one i posted and delete lines 36 & 37. once done if you get errors repost your code and any error stack trace you have
     
  7. Offline

    marti.2001

    NoLiver92
    If I use your onEnable(), eclipse tells me there are many errors.
    If I put public or private before line 36 & 37, I get the same error as the previous one.
     
  8. Offline

    NoLiver92

    ok well tell me what there errors are, i cant help unless you actually state the errors
     
  9. Offline

    marti.2001

    the errors are the same as these
     
  10. Offline

    NoLiver92

    these are the errors i am after, once you changed the code the error stack trace will be diffrent (it may not seem like it but the line numbers telling you where the errors are will be diffrent).

    In eclipse when you use my on enable and delete lines 36&37 do you get lots of red lines everywhere?
     
  11. Offline

    marti.2001

    Ok, the errors are:
    Show Spoiler

    Code:
    2013-04-05 11:32:33 [SEVERE] Could not load 'plugins\DeathZombies.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalArgumentException: File cannot be null
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:182)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.v1_5_R2.CraftServer.loadPlugins(CraftServer.java:239)
        at org.bukkit.craftbukkit.v1_5_R2.CraftServer.<init>(CraftServer.java:217)
        at net.minecraft.server.v1_5_R2.PlayerList.<init>(PlayerList.java:55)
        at net.minecraft.server.v1_5_R2.DedicatedPlayerList.<init>(SourceFile:11)
        at net.minecraft.server.v1_5_R2.DedicatedServer.init(DedicatedServer.java:105)
        at net.minecraft.server.v1_5_R2.MinecraftServer.run(MinecraftServer.java:379)
        at net.minecraft.server.v1_5_R2.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.IllegalArgumentException: File cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:203)
        at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:170)
        at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:117)
        at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:111)
        at me.marti.2001.deathzombies.Main.<init>(Main.java:35)   //Line 35 is  public List<String> strs = getConfig().getStringList("zombiesUUID");
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:178)
        ... 9 more

    I get red lines where I use "uuids".
     
  12. Offline

    NoLiver92

    hover over the red lines and see what they say (you might have to import some items etc) clear the red errors from eclipse. then you will hopefully get somewhere.

    also is the key and a default list in the config file when it loads. if the key and list are not in the default config when you run the plugin it could also cause this error
     
  13. Offline

    marti.2001

    NoLiver92 In onEnable it says 'Public is not permitted'. The other underlined things say 'uuids/strs can't be resolved'
     
  14. Offline

    NoLiver92

    hang on, post your on enable code here
     
  15. Offline

    marti.2001

    NoLiver92
    Code:
    public void onEnable(){ PluginManager pm = getServer().getPluginManager();
    pm.registerEvents(this, this);
    saveDefaultConfig();
    List<String> strs = getConfig().getStringList("zombiesUUID"); List<UUID> uuids= new ArrayList<UUID>();
    for (String string: strs) {
    uuids.add(UUID.fromString(string));
    }
    }
    
     
  16. Offline

    NoLiver92

    ok well im slightly stumped at this. Not entirely sure whats wrong
     
  17. Offline

    ClassyInvader69

    marti.2001 Try moving "List<String> strs = getConfig().getStringList("zombiesUUID"); List<UUID> uuids= new ArrayList<UUID>();" out of your onEnable()
     
  18. Offline

    fireblast709

  19. Offline

    ClassyInvader69

Thread Status:
Not open for further replies.

Share This Page