SimpleConfig.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package me.km.api;
  2. import java.io.File;
  3. import me.hammerle.snuviscript.code.Script;
  4. import me.hammerle.snuviscript.config.SnuviConfig;
  5. import me.km.dimensions.ModDimensions;
  6. import me.km.exception.IllegalItemStackStringException;
  7. import me.km.utils.ItemStackUtils;
  8. import net.minecraft.item.ItemStack;
  9. public class SimpleConfig extends SnuviConfig
  10. {
  11. public SimpleConfig(Module m, String path, String name, boolean load)
  12. {
  13. super((String message, Exception ex, String function, String scriptname, Script sc, int line) ->
  14. {
  15. m.sendToConsole("error in config '" + name + "'");
  16. if(message != null)
  17. {
  18. m.sendListElementToConsole(message);
  19. }
  20. if(ex != null)
  21. {
  22. m.sendListElementToConsole(ex.getClass().getSimpleName());
  23. }
  24. if(function != null)
  25. {
  26. m.sendHelpListElementToConsole("function", function);
  27. }
  28. if(line != -1)
  29. {
  30. m.sendHelpListElementToConsole("line", function);
  31. }
  32. }, path, name);
  33. if(load && exists())
  34. {
  35. load();
  36. }
  37. }
  38. public SimpleConfig(Module m, Script sc, String path, String name, boolean load)
  39. {
  40. super(sc, path, name);
  41. if(load && exists())
  42. {
  43. load();
  44. }
  45. }
  46. public static File[] getFiles(String path)
  47. {
  48. return new File("./" + path).listFiles();
  49. }
  50. // -----------------------------------------------------------------------------------
  51. // get
  52. // -----------------------------------------------------------------------------------
  53. public Location getLocation(String key)
  54. {
  55. return new Location(ModDimensions.getWorldFromName(getString(key + ".world")),
  56. getDouble(key + ".x", 0), getDouble(key + ".y", 0), getDouble(key + ".z", 0),
  57. getFloat(key + ".yaw", 0), getFloat(key + ".pitch", 0));
  58. }
  59. public ItemStack getItemStack(String key)
  60. {
  61. Object s = conf.get(key);
  62. if(s == null)
  63. {
  64. return ItemStack.EMPTY;
  65. }
  66. try
  67. {
  68. return ItemStackUtils.getStackFromNbtString(s.toString());
  69. }
  70. catch(IllegalItemStackStringException ex)
  71. {
  72. return ItemStack.EMPTY;
  73. }
  74. }
  75. // -----------------------------------------------------------------------------------
  76. // Add Data
  77. // -----------------------------------------------------------------------------------
  78. public void setLocation(String key, Location l)
  79. {
  80. set(key + ".world", l.getWorld().getWorldInfo().getWorldName());
  81. set(key + ".x", l.getX());
  82. set(key + ".y", l.getY());
  83. set(key + ".z", l.getZ());
  84. set(key + ".yaw", l.getYaw());
  85. set(key + ".pitch", l.getPitch());
  86. }
  87. public void setItemStack(String key, ItemStack stack)
  88. {
  89. set(key, ItemStackUtils.getNbtString(stack));
  90. }
  91. }