ScriptUtils.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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].equals("km") || parts[0].equals("minecraft"))
  146. {
  147. if(parts.length <= 1)
  148. {
  149. throw new IllegalItemStackStringException(stack);
  150. }
  151. String[] newParts = new String[parts.length - 1];
  152. newParts[0] = parts[0].toLowerCase() + ":" + parts[1].toLowerCase();
  153. for(int i = 1; i < newParts.length; i++)
  154. {
  155. newParts[i] = parts[i + 1].toLowerCase();
  156. }
  157. parts = newParts;
  158. }
  159. else
  160. {
  161. parts[0] = "minecraft:" + parts[0].toLowerCase();
  162. }
  163. int amount = 1;
  164. int data = 0;
  165. if(parts.length >= 2)
  166. {
  167. try
  168. {
  169. data = Integer.parseInt(parts[1]);
  170. if(data < 0)
  171. {
  172. throw new IllegalItemStackStringException(stack);
  173. }
  174. }
  175. catch(NumberFormatException ex)
  176. {
  177. throw new IllegalItemStackStringException(stack);
  178. }
  179. if(parts.length >= 3)
  180. {
  181. try
  182. {
  183. amount = Integer.parseInt(parts[2]);
  184. if(amount < 1)
  185. {
  186. throw new IllegalItemStackStringException(stack);
  187. }
  188. }
  189. catch(NumberFormatException ex)
  190. {
  191. throw new IllegalItemStackStringException(stack);
  192. }
  193. }
  194. }
  195. return new ItemStack(getItem(parts[0]), amount, data);
  196. }
  197. /** Gibt einen ItemStack zurück
  198. * @param args ein Array der mindestens einen gültigen ItemStack-String enthalten muss.
  199. * Weiters kann auch ein Name und eine Lore mitgegeben werden, andernfalls muss der
  200. * Array vorher an seiner Grenze sein oder null verwendet werden.
  201. * @param start die Stelle, an der der ItemStack beginnt
  202. * @throws IllegalItemStackStringException wenn das Format des ItemStack-Strings nicht erfüllt ist
  203. * @return den ItemStack
  204. */
  205. public static ItemStack getItemStack(Object[] args, int start) throws IllegalItemStackStringException
  206. {
  207. if(args[start].toString().startsWith("{"))
  208. {
  209. return ItemStackUtils.getStackFromNbtString(connect(args, " ", start).replace("'", "\""));
  210. }
  211. ItemStack stack = getSimpleItemStack(args[start].toString());
  212. if(args.length >= start + 2)
  213. {
  214. if(!(args[start + 1] == null))
  215. {
  216. stack.setStackDisplayName(args[start + 1].toString());
  217. }
  218. }
  219. if(args.length >= start + 3)
  220. {
  221. if(!(args[start + 2] == null))
  222. {
  223. for(int i = start + 2; i < args.length; i++)
  224. {
  225. ItemStackUtils.addLore(stack, args[i].toString());
  226. }
  227. }
  228. }
  229. return stack;
  230. }
  231. /** Gibt einen ItemStack-String zurück
  232. * @param stack ein ItemStack
  233. * @return den ItemStack-String im Format "Material:DV:Amount"
  234. */
  235. public static String getItemStackString(ItemStack stack)
  236. {
  237. return ItemStackUtils.getNbtString(stack);
  238. }
  239. /** Konvertiert einen String in die gewünschten Formate (true, false, null, Zahl, ...)
  240. * @param s ein String
  241. * @return ein konvertiertes Object
  242. */
  243. public static Object convertInput(String s)
  244. {
  245. if(s == null)
  246. {
  247. return null;
  248. }
  249. else if(s.startsWith("\"") && s.endsWith("\""))
  250. {
  251. if(s.length() <= 1)
  252. {
  253. return "\"";
  254. }
  255. return s.substring(1, s.length() - 1);
  256. }
  257. else if(s.startsWith("$"))
  258. {
  259. return new Variable(s);
  260. }
  261. else if(s.equals("true"))
  262. {
  263. return true;
  264. }
  265. else if(s.equals("false"))
  266. {
  267. return false;
  268. }
  269. else if(s.equals("null"))
  270. {
  271. return null;
  272. }
  273. try
  274. {
  275. return getNumber(s);
  276. }
  277. catch(NumberFormatException ex)
  278. {
  279. return s;
  280. }
  281. }
  282. }