ScriptUtils.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package me.km.snuviscript;
  2. import me.km.exception.IllegalStringLocationException;
  3. import me.km.exception.IllegalItemStackStringException;
  4. import me.km.api.Location;
  5. import java.util.Arrays;
  6. import java.util.Collection;
  7. import java.util.stream.Collectors;
  8. import me.km.dimensions.ModDimensions;
  9. import me.km.utils.ItemStackUtils;
  10. import net.minecraft.block.Block;
  11. import net.minecraft.item.Item;
  12. import net.minecraft.item.ItemStack;
  13. import net.minecraft.util.math.BlockPos;
  14. import net.minecraft.util.math.Vec3d;
  15. public class ScriptUtils
  16. {
  17. public static Number getNumber(String s)
  18. {
  19. Double d = Double.parseDouble(s);
  20. if(d.intValue() == d)
  21. {
  22. return d.intValue();
  23. }
  24. else if(d.longValue() == d)
  25. {
  26. return d.longValue();
  27. }
  28. return d;
  29. }
  30. public static int getInt(Object o)
  31. {
  32. return ((Number) o).intValue();
  33. }
  34. public static byte getByte(Object o)
  35. {
  36. return ((Number) o).byteValue();
  37. }
  38. public static double getDouble(Object o)
  39. {
  40. return ((Number) o).doubleValue();
  41. }
  42. public static float getFloat(Object o)
  43. {
  44. return ((Number) o).floatValue();
  45. }
  46. public static String connect(Collection<Object> c, String s, int skip)
  47. {
  48. return c.stream().skip(skip).map(o -> o.toString()).collect(Collectors.joining(s));
  49. }
  50. public static String connect(Object[] c, String s, int skip)
  51. {
  52. return Arrays.stream(c, skip, c.length).map(o -> o.toString()).collect(Collectors.joining(s));
  53. }
  54. public static String connect(Collection<Object> c, int skip)
  55. {
  56. return c.stream().skip(skip).map(o -> String.valueOf(o)).collect(Collectors.joining());
  57. }
  58. public static String connect(Object[] c, int skip)
  59. {
  60. return Arrays.stream(c, skip, c.length).map(o -> String.valueOf(o)).collect(Collectors.joining());
  61. }
  62. /** Generiert einen String aus dem gegebenen Double, ein etwaiges <code>.0</code> wird entfernt.
  63. * @param d ein Double
  64. * @return der generierte String
  65. */
  66. public static String doubleToString(double d)
  67. {
  68. if((int) d == d)
  69. {
  70. return String.valueOf((int) d);
  71. }
  72. return String.valueOf(d);
  73. }
  74. /** Generiert eine Location aus dem gegebenen String.
  75. * @param location String-Location
  76. * @throws IllegalStringLocationException wenn der String nicht das Format "world-name:x:y:z" oder "..:yaw:pitch" besitzt.
  77. * @return die generierte Location
  78. */
  79. public static Location getLocation(String location) throws IllegalStringLocationException
  80. {
  81. String[] parts = location.split(":");
  82. try
  83. {
  84. if(parts.length > 4)
  85. {
  86. return new Location(ModDimensions.getWorldFromName(parts[0]), new Vec3d(Double.parseDouble(parts[1]),
  87. Double.parseDouble(parts[2]), Double.parseDouble(parts[3])),
  88. Float.parseFloat(parts[4]), Float.parseFloat(parts[5]));
  89. }
  90. return new Location(ModDimensions.getWorldFromName(parts[0]), new Vec3d(Double.parseDouble(parts[1]), Double.parseDouble(parts[2]), Double.parseDouble(parts[3])));
  91. }
  92. catch(Exception ex)
  93. {
  94. throw new IllegalStringLocationException(location);
  95. }
  96. }
  97. /** Rundet eine Location zu einer Block-Location
  98. * @param l Location
  99. * @return die generierte Location
  100. */
  101. public static Location roundLocation(Location l)
  102. {
  103. return new Location(l.getWorld(), new BlockPos(l.getPos()));
  104. }
  105. /** Generiert einen Location-String aus der gegebenen Location.
  106. * @param l eine Location
  107. * @return Location-String im Format "world-name:x:y:z"
  108. */
  109. public static String getLocationString(Location l)
  110. {
  111. Vec3d v = l.getPos();
  112. return ModDimensions.getWorldName(l.getWorld()) + ":" + v.xCoord + ":" + v.yCoord + ":" + v.zCoord + ":" + l.getYaw()+ ":" + l.getPitch();
  113. }
  114. /** Generiert einen Block-Location-String aus der gegebenen Location.
  115. * @param l eine Location
  116. * @return Location-String im Format "world-name:x:y:z"
  117. */
  118. public static String getBlockLocationString(Location l)
  119. {
  120. Vec3d v = l.getPos();
  121. return ModDimensions.getWorldName(l.getWorld()) + ":" + ((int) v.xCoord) + ":" + ((int) v.yCoord) + ":" + ((int) v.zCoord);
  122. }
  123. public static Item getItem(String stack) throws IllegalItemStackStringException
  124. {
  125. Item item = Item.getByNameOrId(stack);
  126. if(item == null)
  127. {
  128. Block b = Block.getBlockFromName(stack);
  129. if(b == null)
  130. {
  131. throw new IllegalItemStackStringException(stack);
  132. }
  133. return Item.getItemFromBlock(b);
  134. }
  135. return item;
  136. }
  137. /** Gibt einen ItemStack zurück
  138. * @param stack ein ItemStack-String im Format "Material", "Material:DV" oder "Material:DV:Amount"
  139. * @throws IllegalItemStackStringException wenn das Format nicht erfüllt ist
  140. * @return den ItemStack
  141. */
  142. private static ItemStack getSimpleItemStack(String stack) throws IllegalItemStackStringException
  143. {
  144. String[] parts = stack.split(":");
  145. if(!parts[0].startsWith("km:") && !parts[0].startsWith("minecraft:"))
  146. {
  147. parts[0] = "minecraft:" + parts[0];
  148. }
  149. parts[0] = parts[0].toLowerCase();
  150. int amount = 1;
  151. int data = 0;
  152. if(parts.length >= 2)
  153. {
  154. try
  155. {
  156. data = Integer.parseInt(parts[1]);
  157. if(data < 0)
  158. {
  159. throw new IllegalItemStackStringException(stack);
  160. }
  161. }
  162. catch(NumberFormatException ex)
  163. {
  164. throw new IllegalItemStackStringException(stack);
  165. }
  166. if(parts.length >= 3)
  167. {
  168. try
  169. {
  170. amount = Integer.parseInt(parts[2]);
  171. if(amount < 1)
  172. {
  173. throw new IllegalItemStackStringException(stack);
  174. }
  175. }
  176. catch(NumberFormatException ex)
  177. {
  178. throw new IllegalItemStackStringException(stack);
  179. }
  180. }
  181. }
  182. return new ItemStack(getItem(parts[0]), amount, data);
  183. }
  184. /** Gibt einen ItemStack zurück
  185. * @param args ein Array der mindestens einen gültigen ItemStack-String enthalten muss.
  186. * Weiters kann auch ein Name und eine Lore mitgegeben werden, andernfalls muss der
  187. * Array vorher an seiner Grenze sein oder null verwendet werden.
  188. * @param start die Stelle, an der der ItemStack beginnt
  189. * @throws IllegalItemStackStringException wenn das Format des ItemStack-Strings nicht erfüllt ist
  190. * @return den ItemStack
  191. */
  192. public static ItemStack getItemStack(Object[] args, int start) throws IllegalItemStackStringException
  193. {
  194. if(args[start].toString().startsWith("{"))
  195. {
  196. return ItemStackUtils.getStackFromNbtString(connect(args, " ", start).replace("'", "\""));
  197. }
  198. ItemStack stack = getSimpleItemStack(args[start].toString());
  199. if(args.length >= start + 2)
  200. {
  201. if(!(args[start + 1] == null))
  202. {
  203. stack.setStackDisplayName(args[start + 1].toString());
  204. }
  205. }
  206. if(args.length >= start + 3)
  207. {
  208. if(!(args[start + 2] == null))
  209. {
  210. for(int i = start + 2; i < args.length; i++)
  211. {
  212. ItemStackUtils.addLore(stack, args[i].toString());
  213. }
  214. }
  215. }
  216. return stack;
  217. }
  218. /** Gibt einen ItemStack-String zurück
  219. * @param stack ein ItemStack
  220. * @return den ItemStack-String im Format "Material:DV:Amount"
  221. */
  222. public static String getItemStackString(ItemStack stack)
  223. {
  224. return ItemStackUtils.getNbtString(stack);
  225. }
  226. /** Konvertiert einen String in die gewünschten Formate (true, false, null, Zahl, ...)
  227. * @param s ein String
  228. * @return ein konvertiertes Object
  229. */
  230. public static Object convertInput(String s)
  231. {
  232. if(s == null)
  233. {
  234. return null;
  235. }
  236. else if(s.startsWith("\"") && s.endsWith("\""))
  237. {
  238. if(s.length() <= 1)
  239. {
  240. return "\"";
  241. }
  242. return s.substring(1, s.length() - 1);
  243. }
  244. else if(s.startsWith("$"))
  245. {
  246. return new Variable(s);
  247. }
  248. else if(s.equals("true"))
  249. {
  250. return true;
  251. }
  252. else if(s.equals("false"))
  253. {
  254. return false;
  255. }
  256. else if(s.equals("null"))
  257. {
  258. return null;
  259. }
  260. try
  261. {
  262. return getNumber(s);
  263. }
  264. catch(NumberFormatException ex)
  265. {
  266. return s;
  267. }
  268. }
  269. }