Utils.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. package me.km.api;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Collection;
  5. import java.util.List;
  6. import java.util.Random;
  7. import java.util.stream.Collectors;
  8. import me.km.KajetansMod;
  9. import me.km.exception.PlayerNotFoundException;
  10. import net.minecraft.entity.Entity;
  11. import net.minecraft.entity.player.EntityPlayer;
  12. import net.minecraft.block.Block;
  13. import net.minecraft.block.BlockButton;
  14. import net.minecraft.block.BlockContainer;
  15. import net.minecraft.block.BlockDoor;
  16. import net.minecraft.block.BlockPistonMoving;
  17. import net.minecraft.block.BlockTrapDoor;
  18. import net.minecraft.block.state.IBlockState;
  19. import net.minecraft.entity.player.EntityPlayerMP;
  20. import net.minecraft.init.Blocks;
  21. import net.minecraft.init.Items;
  22. import net.minecraft.item.Item;
  23. import net.minecraft.item.ItemAxe;
  24. import net.minecraft.item.ItemHoe;
  25. import net.minecraft.item.ItemPickaxe;
  26. import net.minecraft.item.ItemSpade;
  27. import net.minecraft.util.math.AxisAlignedBB;
  28. import net.minecraft.util.math.BlockPos;
  29. import net.minecraft.util.math.Vec3d;
  30. import net.minecraft.util.math.Vec3i;
  31. import net.minecraft.world.World;
  32. import net.minecraft.item.ItemStack;
  33. import net.minecraft.item.ItemSword;
  34. public class Utils
  35. {
  36. // -------------------------------------------------------------------------
  37. // Befehle für Werkzeuge
  38. // -------------------------------------------------------------------------
  39. public enum ToolTypes
  40. {
  41. PICKAXE, AXE, HOE, SHOVEL, SWORD, MUSKET, DAGGER, HAMMER, STICK, WAND, SPEAR
  42. }
  43. public static ToolTypes getToolType(ItemStack stack)
  44. {
  45. if(stack == null)
  46. {
  47. return null;
  48. }
  49. Item item = stack.getItem();
  50. if(item instanceof ItemHoe)
  51. {
  52. return ToolTypes.HOE;
  53. }
  54. else if(item instanceof ItemPickaxe)
  55. {
  56. return ToolTypes.PICKAXE;
  57. }
  58. else if(item instanceof ItemAxe)
  59. {
  60. return ToolTypes.AXE;
  61. }
  62. else if(item instanceof ItemSpade)
  63. {
  64. return ToolTypes.SHOVEL;
  65. }
  66. else if(item instanceof ItemSword)
  67. {
  68. return ToolTypes.SWORD;
  69. }
  70. return null;
  71. }
  72. // -------------------------------------------------------------------------
  73. // Zufallszahlen
  74. // -------------------------------------------------------------------------
  75. public static int randomInt(int min, int max)
  76. {
  77. return new Random().nextInt((max - min) + 1) + min;
  78. }
  79. public static boolean randomBoolean()
  80. {
  81. return new Random().nextBoolean();
  82. }
  83. // -------------------------------------------------------------------------
  84. // String-Tools
  85. // -------------------------------------------------------------------------
  86. public static String formatString(String original)
  87. {
  88. if(original.length() == 0)
  89. {
  90. return original;
  91. }
  92. else if(original.length() == 1)
  93. {
  94. return original.toUpperCase();
  95. }
  96. String[] parts = original.split("_");
  97. for(int i = 0; i < parts.length; i++)
  98. {
  99. parts[i] = parts[i].substring(0, 1).toUpperCase() + parts[i].substring(1).toLowerCase();
  100. }
  101. return connectSpaces(parts, 0);
  102. }
  103. public static String connect(String[] array, int start, String c)
  104. {
  105. if(start >= array.length)
  106. {
  107. return "";
  108. }
  109. return Arrays.stream(array, start, array.length).collect(Collectors.joining(c));
  110. }
  111. public static String connectSpaces(String[] array, int start)
  112. {
  113. return connect(array, start, " ");
  114. }
  115. public static String connect(String[] array, int start)
  116. {
  117. if(start >= array.length)
  118. {
  119. return "";
  120. }
  121. return Arrays.stream(array, start, array.length).collect(Collectors.joining());
  122. }
  123. // -------------------------------------------------------------------------
  124. // ItemStack-Tools
  125. // -------------------------------------------------------------------------
  126. public static List<String> getLore(ItemStack stack)
  127. {
  128. //TODO
  129. /*if(!stack.hasItemMeta() || !stack.getItemMeta().hasLore())
  130. {*/
  131. return new ArrayList<>();
  132. /*}
  133. return new ArrayList<>(stack.getItemMeta().getLore());*/
  134. }
  135. public static void setLore(ItemStack stack, String s, int i)
  136. {
  137. //TODO
  138. /*ItemMeta meta = stack.getItemMeta();
  139. List<String> list;
  140. if(meta.hasLore())
  141. {
  142. list = meta.getLore();
  143. }
  144. else
  145. {
  146. list = new ArrayList<>();
  147. }
  148. if(i >= list.size())
  149. {
  150. list.add(s);
  151. }
  152. else
  153. {
  154. list.set(i, s);
  155. }
  156. meta.setLore(list);
  157. stack.setItemMeta(meta);*/
  158. }
  159. public static ArrayList<String> buildLimitedLore(String whole, int limit, String addition)
  160. {
  161. ArrayList<String> list = new ArrayList<>();
  162. int pos = 0;
  163. int space;
  164. while(pos + limit < whole.length())
  165. {
  166. space = whole.lastIndexOf(" ", pos + limit);
  167. if(space == -1 || pos > space)
  168. {
  169. space = whole.indexOf(" ", pos + limit);
  170. }
  171. list.add(addition + whole.substring(pos, space));
  172. pos = space + 1;
  173. }
  174. list.add(addition + whole.substring(pos));
  175. return list;
  176. }
  177. public static ArrayList<String> buildLimitedLore(String whole, String addition)
  178. {
  179. return buildLimitedLore(whole, 25, addition);
  180. }
  181. public static void dropRandomTreeItem(World w, BlockPos l, IBlockState b)
  182. {
  183. Block block = b.getBlock();
  184. int identifier = block.getMetaFromState(b) % 4;
  185. if(b == Blocks.LEAVES2)
  186. {
  187. identifier += 4;
  188. }
  189. ArrayList<Object> list = new ArrayList<>();
  190. list.add(Items.NAME_TAG);
  191. list.add(Items.LEAD);
  192. list.add(Blocks.WEB);
  193. if(identifier == 3)
  194. {
  195. list.add(Blocks.VINE);
  196. list.add(Items.DYE);
  197. list.add(Blocks.VINE);
  198. list.add(Items.DYE);
  199. list.add(Blocks.VINE);
  200. list.add(Items.DYE);
  201. }
  202. if(identifier == 0 || identifier == 5)
  203. {
  204. list.add(Items.APPLE);
  205. list.add(Items.APPLE);
  206. list.add(Items.APPLE);
  207. list.add(Items.APPLE);
  208. list.add(Items.GOLDEN_APPLE);
  209. }
  210. list.add(Items.STICK);
  211. list.add(Items.STICK);
  212. list.add(Items.STICK);
  213. list.add(Items.STICK);
  214. list.add(Items.STRING);
  215. list.add(Items.STRING);
  216. list.add(Items.FEATHER);
  217. list.add(Items.FEATHER);
  218. if(identifier == 4)
  219. {
  220. list.add(Items.BONE);
  221. list.add(Items.BONE);
  222. list.add(Items.BONE);
  223. }
  224. int rand = Utils.randomInt(0, 19);
  225. if(rand >= list.size())
  226. {
  227. return;
  228. }
  229. if(list.get(rand) == Items.DYE)
  230. {
  231. new ItemStackBuilder(Items.DYE, 1, (short) 3).drop(w, l);
  232. return;
  233. }
  234. if(list.get(rand) instanceof Item)
  235. {
  236. new ItemStackBuilder((Item) list.get(rand), 1).drop(w, l);
  237. return;
  238. }
  239. new ItemStackBuilder((Block) list.get(rand), 1).drop(w, l);
  240. }
  241. // -------------------------------------------------------------------------
  242. // Inventory-Tools
  243. // -------------------------------------------------------------------------
  244. /*public static int getLowestStackAmount(CraftingInventory inv)
  245. {
  246. return Arrays.stream(inv.getMatrix()).filter(s -> s != null && s.getType() != Material.AIR).mapToInt(s -> s.getAmount()).min().orElse(0);
  247. }
  248. public static Integer searchInventoryFor(Inventory inv, ItemStack searchfor, boolean ignoreDv)
  249. {
  250. if(!ignoreDv)
  251. {
  252. return Arrays.stream(inv.getContents()).filter(s -> s != null && s.isSimilar(searchfor)).mapToInt(s -> s.getAmount()).sum();
  253. }
  254. Material m = searchfor.getType();
  255. return Arrays.stream(inv.getContents()).filter(s -> s != null && s.getType() == m).mapToInt(s -> s.getAmount()).sum();
  256. }*/
  257. // -------------------------------------------------------------------------
  258. // Entities
  259. // -------------------------------------------------------------------------
  260. public static Location getEntityLocation(Entity ent)
  261. {
  262. return new Location(ent.getEntityWorld(), ent.getPositionVector(), ent.rotationYaw, ent.rotationPitch);
  263. }
  264. public static BlockPos getEyeLocation(Entity ent)
  265. {
  266. return ent.getPosition().add(0, ent.getEyeHeight(), 0);
  267. }
  268. public static void teleportEntity(Entity ent, Location l)
  269. {
  270. Vec3d pos = l.getPos();
  271. if(ent instanceof EntityPlayerMP)
  272. {
  273. if(l.getYaw() != 0 && l.getPitch() != 0)
  274. {
  275. ((EntityPlayerMP) ent).connection.setPlayerLocation(pos.xCoord, pos.yCoord, pos.zCoord, l.getYaw(), l.getPitch());
  276. }
  277. else
  278. {
  279. ((EntityPlayerMP) ent).connection.setPlayerLocation(pos.xCoord, pos.yCoord, pos.zCoord, ent.rotationYaw, ent.rotationPitch);
  280. }
  281. }
  282. else
  283. {
  284. if(l.getYaw() != 0 && l.getPitch() != 0)
  285. {
  286. ent.setLocationAndAngles(pos.xCoord, pos.yCoord, pos.zCoord, l.getYaw(), l.getPitch());
  287. }
  288. else
  289. {
  290. ent.setLocationAndAngles(pos.xCoord, pos.yCoord, pos.zCoord, ent.rotationYaw, ent.rotationPitch);
  291. }
  292. }
  293. }
  294. public static void teleportEntity(Entity ent, Entity ent2)
  295. {
  296. teleportEntity(ent, new Location(ent2.world, ent.getPositionVector(), ent2.rotationYaw, ent2.rotationPitch));
  297. }
  298. // -------------------------------------------------------------------------
  299. // Entities aus Umgebung
  300. // -------------------------------------------------------------------------
  301. @SuppressWarnings("unchecked")
  302. public static List<Entity> getEntitiesExcluding(Entity ent, World w, BlockPos pos, BlockPos pos2)
  303. {
  304. return w.getEntitiesWithinAABBExcludingEntity(ent, new AxisAlignedBB(pos, pos2));
  305. }
  306. @SuppressWarnings("unchecked")
  307. public static <T extends Entity> Collection<T> getNearbyEntities(World w, BlockPos l, double radius, Class<T> type)
  308. {
  309. double sqareRadius = radius * radius;
  310. return w.getEntitiesWithinAABB(type, new AxisAlignedBB(
  311. l.getX() - radius, l.getY() - radius, l.getZ() - radius,
  312. l.getX() + radius, l.getY() + radius, l.getZ() + radius), ent -> ent.getDistanceSq(l) <= sqareRadius);
  313. }
  314. public static <T extends Entity> T getNearestEntity(World w, BlockPos l, double radius, Class<T> type)
  315. {
  316. return getNearbyEntities(w, l, radius, type).stream().min((e1, e2) -> Double.compare(
  317. e1.getDistanceSq(l),
  318. e2.getDistanceSq(l)))
  319. .orElse(null);
  320. }
  321. @SuppressWarnings("unchecked")
  322. public static <T extends Entity> T getTargetedEntity(EntityPlayer p, int radius, Class<T> type)
  323. {
  324. World w = p.getEntityWorld();
  325. BlockPos l = getPlayerTarget(p, radius);
  326. BlockPos eye = getEyeLocation(p);
  327. List<Entity> col = getEntitiesExcluding(p, w, eye, l);
  328. col.removeIf(ent -> !type.isAssignableFrom(ent.getClass()));
  329. // Entfernt Entities, die sich nicht mit dem Blick-Vektor schneiden
  330. Vec3d first = new Vec3d(eye.getX(), eye.getY(), eye.getZ());
  331. Vec3d second = new Vec3d(l.getX(), l.getY(), l.getZ());
  332. col.removeIf(ent ->
  333. {
  334. AxisAlignedBB bound = ent.getCollisionBoundingBox();
  335. if(bound == null)
  336. {
  337. return true;
  338. }
  339. return bound.calculateIntercept(first, second) == null;
  340. });
  341. return (T) col.stream().sorted((Entity e1, Entity e2) ->
  342. {
  343. return Double.compare(e1.getDistanceSq(eye), e2.getDistanceSq(eye));
  344. }).findFirst().orElse(null);
  345. }
  346. // -------------------------------------------------------------------------
  347. // Player-Tools
  348. // -------------------------------------------------------------------------
  349. public static EntityPlayer getNearestPlayer(World w, BlockPos l)
  350. {
  351. return w.getPlayers(EntityPlayer.class, p -> true).stream().min((p1, p2) -> Double.compare(p1.getDistanceSq(l), p2.getDistanceSq(l))).orElse(null);
  352. }
  353. public static BlockPos getPlayerTarget(EntityPlayer p, int range)
  354. {
  355. if(range > 64)
  356. {
  357. range = 64;
  358. }
  359. World w = p.getEntityWorld();
  360. BlockPos l = getEyeLocation(p);
  361. Vec3d unit2 = p.getLookVec();
  362. Vec3i unit = new Vec3i(unit2.xCoord, unit2.yCoord, unit2.zCoord);
  363. for(int i = 0; i < range; i++)
  364. {
  365. l = l.add(unit);
  366. if(w.isBlockFullCube(l))
  367. {
  368. break;
  369. }
  370. }
  371. return l;
  372. }
  373. public static BlockPos getPlayerTarget(EntityPlayer p)
  374. {
  375. return getPlayerTarget(p, 20);
  376. }
  377. public static EntityPlayerMP getPlayerByName(String name) throws PlayerNotFoundException
  378. {
  379. return KajetansMod.server.getPlayerList().getPlayers().stream()
  380. .filter(pl -> pl.getName().contains(name))
  381. .findFirst().orElseThrow(() -> new PlayerNotFoundException(name));
  382. }
  383. // -------------------------------------------------------------------------
  384. // Spawn
  385. // -------------------------------------------------------------------------
  386. public static Location getSpawn()
  387. {
  388. // Player changeDimension, WorldServer, MinecraftServer
  389. return KajetansMod.conf.getLocation("spawn");
  390. }
  391. public static void setSpawn(World w, Vec3d v, float yaw, float pitch)
  392. {
  393. SimpleConfig conf = KajetansMod.conf;
  394. conf.setLocation("spawn", new Location(w, v, yaw, pitch));
  395. conf.save();
  396. w.setSpawnPoint(new BlockPos(v.xCoord, v.yCoord, v.zCoord));
  397. }
  398. // -------------------------------------------------------------------------
  399. // Warps
  400. // -------------------------------------------------------------------------
  401. public static Location getWarp(Module m, String name)
  402. {
  403. SimpleConfig conf = new SimpleConfig(m, "warp/" + name, true);
  404. if(conf.exists())
  405. {
  406. return conf.getLocation("loc");
  407. }
  408. return null;
  409. }
  410. // -------------------------------------------------------------------------
  411. // Blocks
  412. // -------------------------------------------------------------------------
  413. public static Block getSameNearbyBlock(World w, BlockPos pos, Block b)
  414. {
  415. Block bcheck = Location.getRelativeBlock(w, pos, 1, 0, 0).getBlock();
  416. if(bcheck == b)
  417. {
  418. return bcheck;
  419. }
  420. bcheck = Location.getRelativeBlock(w, pos, -1, 0, 0).getBlock();
  421. if(bcheck == b)
  422. {
  423. return bcheck;
  424. }
  425. bcheck = Location.getRelativeBlock(w, pos, 0, 0, 1).getBlock();;
  426. if(bcheck == b)
  427. {
  428. return bcheck;
  429. }
  430. bcheck = Location.getRelativeBlock(w, pos, 0, 0, -1).getBlock();
  431. if(bcheck == b)
  432. {
  433. return bcheck;
  434. }
  435. return null;
  436. }
  437. public static boolean shouldBeProtected(Block b)
  438. {
  439. return b instanceof BlockDoor ||
  440. !(b instanceof BlockPistonMoving) ||
  441. b instanceof BlockContainer ||
  442. b == Blocks.LEVER ||
  443. b instanceof BlockButton ||
  444. b instanceof BlockTrapDoor;
  445. }
  446. // -------------------------------------------------------------------------
  447. // Roman Numbers
  448. // -------------------------------------------------------------------------
  449. public static String intToRoman(int i)
  450. {
  451. switch(i)
  452. {
  453. case 1: return "I";
  454. case 2: return "II";
  455. case 3: return "III";
  456. case 4: return "IV";
  457. case 5: return "V";
  458. case 6: return "VI";
  459. case 7: return "VII";
  460. case 8: return "VIII";
  461. case 9: return "IX";
  462. case 10: return "X";
  463. case 11: return "XI";
  464. case 12: return "XII";
  465. case 13: return "XIII";
  466. case 14: return "XIV";
  467. case 15: return "XV";
  468. case 16: return "XVI";
  469. case 17: return "XVII";
  470. case 18: return "XVIII";
  471. case 19: return "XIX";
  472. case 20: return "XX";
  473. }
  474. return "I";
  475. }
  476. public static int romanToInt(String s)
  477. {
  478. switch(s)
  479. {
  480. case "I": return 1;
  481. case "II": return 2;
  482. case "III": return 3;
  483. case "IV": return 4;
  484. case "V": return 5;
  485. case "VI": return 6;
  486. case "VII": return 7;
  487. case "VIII": return 8;
  488. case "IX": return 9;
  489. case "X": return 10;
  490. case "XI": return 11;
  491. case "XII": return 12;
  492. case "XIII": return 13;
  493. case "XIV": return 14;
  494. case "XV": return 15;
  495. case "XVI": return 16;
  496. case "XVII": return 17;
  497. case "XVIII": return 18;
  498. case "XIX": return 19;
  499. case "XX": return 20;
  500. }
  501. return 1;
  502. }
  503. }