Utils.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. package me.km.api;
  2. import com.mojang.authlib.GameProfile;
  3. import java.io.File;
  4. import me.km.utils.ItemStackBuilder;
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. import java.util.Random;
  9. import java.util.stream.Collectors;
  10. import me.km.KajetansMod;
  11. import me.km.dimensions.ModTeleporter;
  12. import me.km.entities.EntityItemProjectile;
  13. import me.km.exception.PlayerNotFoundException;
  14. import me.km.items.ItemDagger;
  15. import me.km.items.ItemHammer;
  16. import me.km.items.ItemStick;
  17. import me.km.items.ItemWand;
  18. import net.minecraft.entity.Entity;
  19. import net.minecraft.entity.player.EntityPlayer;
  20. import net.minecraft.block.Block;
  21. import net.minecraft.block.BlockButton;
  22. import net.minecraft.block.BlockContainer;
  23. import net.minecraft.block.BlockDoor;
  24. import net.minecraft.block.BlockPistonMoving;
  25. import net.minecraft.block.BlockTrapDoor;
  26. import net.minecraft.block.properties.IProperty;
  27. import net.minecraft.block.state.IBlockState;
  28. import net.minecraft.enchantment.EnchantmentHelper;
  29. import net.minecraft.entity.EntityLivingBase;
  30. import net.minecraft.entity.SharedMonsterAttributes;
  31. import net.minecraft.entity.player.EntityPlayerMP;
  32. import net.minecraft.entity.projectile.EntityArrow;
  33. import net.minecraft.entity.projectile.EntityFireball;
  34. import net.minecraft.entity.projectile.EntityThrowable;
  35. import net.minecraft.init.Blocks;
  36. import net.minecraft.init.Enchantments;
  37. import net.minecraft.init.Items;
  38. import net.minecraft.init.MobEffects;
  39. import net.minecraft.item.Item;
  40. import net.minecraft.item.ItemAxe;
  41. import net.minecraft.item.ItemHoe;
  42. import net.minecraft.item.ItemPickaxe;
  43. import net.minecraft.item.ItemSpade;
  44. import net.minecraft.util.math.AxisAlignedBB;
  45. import net.minecraft.util.math.BlockPos;
  46. import net.minecraft.util.math.Vec3d;
  47. import net.minecraft.world.World;
  48. import net.minecraft.item.ItemStack;
  49. import net.minecraft.item.ItemSword;
  50. import net.minecraft.nbt.NBTTagCompound;
  51. import net.minecraft.nbt.NBTUtil;
  52. import net.minecraft.network.play.server.SPacketEntityVelocity;
  53. import net.minecraft.tileentity.TileEntitySkull;
  54. import net.minecraft.util.CombatRules;
  55. import net.minecraft.util.DamageSource;
  56. import net.minecraft.util.math.RayTraceResult;
  57. import net.minecraft.world.WorldServer;
  58. public class Utils
  59. {
  60. public static void breakBlock(World w, BlockPos pos, EntityPlayer p)
  61. {
  62. IBlockState state = w.getBlockState(pos);
  63. state.getBlock().dropBlockAsItem(w, pos, state, EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, p.getHeldItemMainhand()));
  64. w.setBlockToAir(pos);
  65. }
  66. private static ItemStack getPlayerHead(GameProfile gameprofile)
  67. {
  68. ItemStack stack = new ItemStack(Items.SKULL, 1, 3);
  69. NBTTagCompound com = stack.getTagCompound();
  70. if(com == null)
  71. {
  72. return stack;
  73. }
  74. gameprofile = TileEntitySkull.updateGameprofile(gameprofile);
  75. com.setTag("SkullOwner", NBTUtil.writeGameProfile(new NBTTagCompound(), gameprofile));
  76. return stack;
  77. }
  78. public static ItemStack getPlayerHead(String name)
  79. {
  80. return getPlayerHead(KajetansMod.playerbank.getDataBank().getOfflinePlayer(name));
  81. }
  82. public static ItemStack getPlayerHead(EntityPlayer p)
  83. {
  84. return getPlayerHead(p.getGameProfile());
  85. }
  86. public static void setVelocity(Entity ent, double x, double y, double z)
  87. {
  88. ent.motionX = x;
  89. ent.motionY = y;
  90. ent.motionZ = z;
  91. updateVelocity(ent);
  92. }
  93. public static void addVelocity(Entity ent, double x, double y, double z)
  94. {
  95. ent.motionX += x;
  96. ent.motionY += y;
  97. ent.motionZ += z;
  98. updateVelocity(ent);
  99. }
  100. private static void updateVelocity(Entity ent)
  101. {
  102. if(ent instanceof EntityPlayerMP)
  103. {
  104. EntityPlayerMP p = (EntityPlayerMP) ent;
  105. SPacketEntityVelocity packet = new SPacketEntityVelocity(p);
  106. p.connection.sendPacket(packet);
  107. }
  108. }
  109. public static void scaleVelocity(Entity ent, double scale)
  110. {
  111. ent.motionX *= scale;
  112. ent.motionY *= scale;
  113. ent.motionZ *= scale;
  114. }
  115. public static boolean hasPlayedBefore(EntityPlayer p)
  116. {
  117. return new File("./world/playerdata/" + p.getUniqueID() + ".dat").exists();
  118. }
  119. public static <T extends Comparable<T>> T getStateValue(IBlockState state, IProperty<T> property)
  120. {
  121. try
  122. {
  123. return state.getValue(property);
  124. }
  125. catch(IllegalArgumentException ex)
  126. {
  127. return null;
  128. }
  129. }
  130. // -------------------------------------------------------------------------
  131. // Befehle für Werkzeuge
  132. // -------------------------------------------------------------------------
  133. public enum ToolTypes
  134. {
  135. PICKAXE, AXE, HOE, SHOVEL, SWORD, MUSKET, DAGGER, HAMMER, STICK, WAND, SPEAR
  136. // MUSKET, SPEAR
  137. }
  138. public static ToolTypes getToolType(ItemStack stack)
  139. {
  140. if(stack == null)
  141. {
  142. return null;
  143. }
  144. Item item = stack.getItem();
  145. if(item instanceof ItemHoe)
  146. {
  147. return ToolTypes.HOE;
  148. }
  149. else if(item instanceof ItemPickaxe)
  150. {
  151. return ToolTypes.PICKAXE;
  152. }
  153. else if(item instanceof ItemAxe)
  154. {
  155. return ToolTypes.AXE;
  156. }
  157. else if(item instanceof ItemSpade)
  158. {
  159. return ToolTypes.SHOVEL;
  160. }
  161. else if(item instanceof ItemSword)
  162. {
  163. return ToolTypes.SWORD;
  164. }
  165. else if(item instanceof ItemDagger)
  166. {
  167. return ToolTypes.DAGGER;
  168. }
  169. else if(item instanceof ItemHammer)
  170. {
  171. return ToolTypes.HAMMER;
  172. }
  173. else if(item instanceof ItemStick)
  174. {
  175. return ToolTypes.STICK;
  176. }
  177. else if(item instanceof ItemWand)
  178. {
  179. return ToolTypes.WAND;
  180. }
  181. return null;
  182. }
  183. // -------------------------------------------------------------------------
  184. // Zufallszahlen
  185. // -------------------------------------------------------------------------
  186. public static int randomInt(int min, int max)
  187. {
  188. return new Random().nextInt((max - min) + 1) + min;
  189. }
  190. public static boolean randomBoolean()
  191. {
  192. return new Random().nextBoolean();
  193. }
  194. // -------------------------------------------------------------------------
  195. // String-Tools
  196. // -------------------------------------------------------------------------
  197. public static String formatString(String original)
  198. {
  199. if(original.length() == 0)
  200. {
  201. return original;
  202. }
  203. else if(original.length() == 1)
  204. {
  205. return original.toUpperCase();
  206. }
  207. String[] parts = original.split("_");
  208. for(int i = 0; i < parts.length; i++)
  209. {
  210. parts[i] = parts[i].substring(0, 1).toUpperCase() + parts[i].substring(1).toLowerCase();
  211. }
  212. return connectSpaces(parts, 0);
  213. }
  214. public static String formatToEnumString(String original)
  215. {
  216. StringBuilder sb = new StringBuilder();
  217. for(char c : original.toCharArray())
  218. {
  219. if(c == ' ')
  220. {
  221. sb.append("_");
  222. }
  223. else if(Character.isUpperCase(c))
  224. {
  225. sb.append("_");
  226. sb.append(c);
  227. }
  228. else
  229. {
  230. sb.append(Character.toUpperCase(c));
  231. }
  232. }
  233. if(sb.charAt(0) == '_')
  234. {
  235. sb.deleteCharAt(0);
  236. }
  237. return sb.toString();
  238. }
  239. public static String connect(String[] array, int start, String c)
  240. {
  241. if(start >= array.length)
  242. {
  243. return "";
  244. }
  245. return Arrays.stream(array, start, array.length).collect(Collectors.joining(c));
  246. }
  247. public static String connectSpaces(String[] array, int start)
  248. {
  249. return connect(array, start, " ");
  250. }
  251. public static String connect(String[] array, int start)
  252. {
  253. if(start >= array.length)
  254. {
  255. return "";
  256. }
  257. return Arrays.stream(array, start, array.length).collect(Collectors.joining());
  258. }
  259. // -------------------------------------------------------------------------
  260. // ItemStack-Tools
  261. // -------------------------------------------------------------------------
  262. public static void dropRandomTreeItem(World w, BlockPos l, IBlockState b)
  263. {
  264. Block block = b.getBlock();
  265. int identifier = block.getMetaFromState(b) % 4;
  266. if(b == Blocks.LEAVES2)
  267. {
  268. identifier += 4;
  269. }
  270. ArrayList<Object> list = new ArrayList<>();
  271. list.add(Items.NAME_TAG);
  272. list.add(Items.LEAD);
  273. list.add(Blocks.WEB);
  274. if(identifier == 3)
  275. {
  276. list.add(Blocks.VINE);
  277. list.add(Items.DYE);
  278. list.add(Blocks.VINE);
  279. list.add(Items.DYE);
  280. list.add(Blocks.VINE);
  281. list.add(Items.DYE);
  282. }
  283. if(identifier == 0 || identifier == 5)
  284. {
  285. list.add(Items.APPLE);
  286. list.add(Items.APPLE);
  287. list.add(Items.APPLE);
  288. list.add(Items.APPLE);
  289. list.add(Items.GOLDEN_APPLE);
  290. }
  291. list.add(Items.STICK);
  292. list.add(Items.STICK);
  293. list.add(Items.STICK);
  294. list.add(Items.STICK);
  295. list.add(Items.STRING);
  296. list.add(Items.STRING);
  297. list.add(Items.FEATHER);
  298. list.add(Items.FEATHER);
  299. if(identifier == 4)
  300. {
  301. list.add(Items.BONE);
  302. list.add(Items.BONE);
  303. list.add(Items.BONE);
  304. }
  305. int rand = Utils.randomInt(0, 19);
  306. if(rand >= list.size())
  307. {
  308. return;
  309. }
  310. if(list.get(rand) == Items.DYE)
  311. {
  312. new ItemStackBuilder(Items.DYE, 1, (short) 3).drop(w, l);
  313. return;
  314. }
  315. if(list.get(rand) instanceof Item)
  316. {
  317. new ItemStackBuilder((Item) list.get(rand), 1).drop(w, l);
  318. return;
  319. }
  320. new ItemStackBuilder((Block) list.get(rand), 1).drop(w, l);
  321. }
  322. // -------------------------------------------------------------------------
  323. // Inventory-Tools
  324. // -------------------------------------------------------------------------
  325. public static EntityPlayer getPlayerFromProjectile(Entity ent)
  326. {
  327. if(ent instanceof EntityArrow)
  328. {
  329. EntityArrow arrow = (EntityArrow) ent;
  330. if(arrow.shootingEntity instanceof EntityPlayer)
  331. {
  332. return (EntityPlayer) arrow.shootingEntity;
  333. }
  334. return null;
  335. }
  336. else if(ent instanceof EntityThrowable)
  337. {
  338. EntityThrowable thrown = (EntityThrowable) ent;
  339. if(thrown.getThrower() instanceof EntityPlayer)
  340. {
  341. return (EntityPlayer) thrown.getThrower();
  342. }
  343. return null;
  344. }
  345. else if(ent instanceof EntityFireball)
  346. {
  347. EntityFireball fire = (EntityFireball) ent;
  348. if(fire.shootingEntity instanceof EntityPlayer)
  349. {
  350. return (EntityPlayer) fire.shootingEntity;
  351. }
  352. return null;
  353. }
  354. else if(ent instanceof EntityItemProjectile)
  355. {
  356. EntityItemProjectile item = (EntityItemProjectile) ent;
  357. if(item.getItemThrower() instanceof EntityPlayer)
  358. {
  359. return (EntityPlayer) item.getItemThrower();
  360. }
  361. return null;
  362. }
  363. return null;
  364. }
  365. public static EntityPlayer getDamager(DamageSource ds)
  366. {
  367. Entity ent = ds.getTrueSource();
  368. if(ent == null)
  369. {
  370. return null;
  371. }
  372. if(ent instanceof EntityPlayer)
  373. {
  374. return (EntityPlayer) ent;
  375. }
  376. return null;//getPlayerFromProjectile(ent);
  377. }
  378. // -------------------------------------------------------------------------
  379. // Entities
  380. // -------------------------------------------------------------------------
  381. public static Vec3d getEyeLocation(Entity ent)
  382. {
  383. return ent.getPositionVector().addVector(0, ent.getEyeHeight(), 0);
  384. }
  385. public static void teleportEntity(Entity ent, Location l)
  386. {
  387. int dim = l.getWorld().provider.getDimension();
  388. if(ent instanceof EntityPlayerMP)
  389. {
  390. EntityPlayerMP p = (EntityPlayerMP) ent;
  391. KajetansMod.playerbank.saveLocation(p);
  392. if(ent.dimension != dim)
  393. {
  394. KajetansMod.server.getPlayerList().transferPlayerToDimension(p, dim, new ModTeleporter(KajetansMod.server.getWorld(dim)));
  395. }
  396. if(l.getYaw() != 0 || l.getPitch() != 0)
  397. {
  398. p.connection.setPlayerLocation(l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch());
  399. }
  400. else
  401. {
  402. p.connection.setPlayerLocation(l.getX(), l.getY(), l.getZ(), ent.rotationYaw, ent.rotationPitch);
  403. }
  404. }
  405. else
  406. {
  407. if(ent.dimension != dim)
  408. {
  409. WorldServer n = KajetansMod.server.getWorld(dim);
  410. KajetansMod.server.getPlayerList().transferEntityToWorld(ent, ent.dimension, KajetansMod.server.getWorld(ent.dimension), n, new ModTeleporter(n));
  411. }
  412. if(l.getYaw() != 0 && l.getPitch() != 0)
  413. {
  414. ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch());
  415. }
  416. else
  417. {
  418. ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), ent.rotationYaw, ent.rotationPitch);
  419. }
  420. }
  421. }
  422. public static void teleportEntity(Entity ent, Entity ent2)
  423. {
  424. teleportEntity(ent, new Location(ent2));
  425. }
  426. // -------------------------------------------------------------------------
  427. // Entities aus Umgebung
  428. // -------------------------------------------------------------------------
  429. @SuppressWarnings("unchecked")
  430. public static List<Entity> getEntitiesExcluding(Entity ent, World w, BlockPos pos, BlockPos pos2)
  431. {
  432. return w.getEntitiesWithinAABBExcludingEntity(ent, new AxisAlignedBB(pos, pos2).grow(1));
  433. }
  434. @SuppressWarnings("unchecked")
  435. public static <T extends Entity> List<T> getEntities(World w, double x, double y, double z, double radius, Class<T> type)
  436. {
  437. double sqareRadius = radius * radius;
  438. return w.getEntitiesWithinAABB(type, new AxisAlignedBB(
  439. x - radius, y - radius, z - radius,
  440. x + radius, y + radius, z + radius),
  441. ent -> ent.getDistanceSq(x, y, z) <= sqareRadius);
  442. }
  443. public static List<EntityPlayer> getPlayers(World w, double x, double y, double z, double radius)
  444. {
  445. double sqareRadius = radius * radius;
  446. return w.playerEntities.stream().filter(p -> p.getDistanceSq(x, y, z) <= sqareRadius).collect(Collectors.toList());
  447. }
  448. public static List<EntityPlayer> getPlayers(Entity not, double radius)
  449. {
  450. double sqareRadius = radius * radius;
  451. double x = not.posX;
  452. double y = not.posY;
  453. double z = not.posZ;
  454. return not.world.playerEntities.stream().filter(p -> p != not && p.getDistanceSq(x, y, z) <= sqareRadius).collect(Collectors.toList());
  455. }
  456. public static <T extends Entity> T getEntity(World w, double x, double y, double z, double radius, Class<T> type)
  457. {
  458. return getEntities(w, x, y, z, radius, type).stream()
  459. .min((e1, e2) -> Double.compare(e1.getDistanceSq(x, y, z), e2.getDistanceSq(x, y, z)))
  460. .orElse(null);
  461. }
  462. private static boolean doesIntersect(AxisAlignedBB bound, Vec3d start, Vec3d unit)
  463. {
  464. double lowerX = Double.NEGATIVE_INFINITY;
  465. double upperX = Double.POSITIVE_INFINITY;
  466. if(unit.x != 0)
  467. {
  468. if(unit.x > 0)
  469. {
  470. lowerX = (bound.minX - start.x) / unit.x;
  471. upperX = (bound.maxX - start.x) / unit.x;
  472. }
  473. else
  474. {
  475. lowerX = (bound.maxX - start.x) / unit.x;
  476. upperX = (bound.minX - start.x) / unit.x;
  477. }
  478. }
  479. double lowerY = Double.NEGATIVE_INFINITY;
  480. double upperY = Double.POSITIVE_INFINITY;
  481. if(unit.y != 0)
  482. {
  483. if(unit.y > 0)
  484. {
  485. lowerY = (bound.minY - start.y) / unit.y;
  486. upperY = (bound.maxY - start.y) / unit.y;
  487. }
  488. else
  489. {
  490. lowerY = (bound.maxY - start.y) / unit.y;
  491. upperY = (bound.minY - start.y) / unit.y;
  492. }
  493. }
  494. double lowerZ = Double.NEGATIVE_INFINITY;
  495. double upperZ = Double.POSITIVE_INFINITY;
  496. if(unit.z != 0)
  497. {
  498. if(unit.z > 0)
  499. {
  500. lowerZ = (bound.minZ - start.z) / unit.z;
  501. upperZ = (bound.maxZ - start.z) / unit.z;
  502. }
  503. else
  504. {
  505. lowerZ = (bound.maxZ - start.z) / unit.z;
  506. upperZ = (bound.minZ - start.z) / unit.z;
  507. }
  508. }
  509. return Math.max(Math.max(lowerX, lowerY), lowerZ) < Math.min(Math.min(upperX, upperY), upperZ);
  510. }
  511. @SuppressWarnings("unchecked")
  512. public static <T extends Entity> T getTargetedEntity(EntityPlayer p,
  513. double radius, double mX, double mY, double mZ, Class<T> type)
  514. {
  515. World w = p.getEntityWorld();
  516. BlockPos l = getPlayerTarget(p, radius);
  517. Vec3d eye = getEyeLocation(p);
  518. if(mX != 0 || mY != 0 || mZ != 0)
  519. {
  520. Random r = p.getRNG();
  521. mX *= r.nextDouble();
  522. mY *= r.nextDouble();
  523. mZ *= r.nextDouble();
  524. }
  525. Vec3d unit = new Vec3d(l.getX() - eye.x + mX, l.getY() - eye.y + mY, l.getZ() - eye.z + mZ);
  526. List<Entity> col = getEntitiesExcluding(p, w, new BlockPos(eye), l);
  527. col.removeIf(ent -> !type.isAssignableFrom(ent.getClass()));
  528. // Entfernt Entities, die sich nicht mit dem Blick-Vektor schneiden
  529. col.removeIf(ent -> !doesIntersect(ent.getEntityBoundingBox().grow(0.1), eye, unit));
  530. return (T) col.stream().sorted((Entity e1, Entity e2) ->
  531. {
  532. return Double.compare(e1.getDistanceSq(eye.x, eye.y, eye.z), e2.getDistanceSq(eye.x, eye.y, eye.z));
  533. }).findFirst().orElse(null);
  534. }
  535. @SuppressWarnings("unchecked")
  536. public static <T extends Entity> T getTargetedEntity(EntityPlayer p, double radius, Class<T> type)
  537. {
  538. return getTargetedEntity(p, radius, 0, 0, 0, type);
  539. }
  540. // -------------------------------------------------------------------------
  541. // Player-Tools
  542. // -------------------------------------------------------------------------
  543. public static EntityPlayer getNearestPlayer(World w, Vec3d v)
  544. {
  545. return w.playerEntities.stream().min((p1, p2) -> Double.compare(p1.getDistanceSq(v.x, v.y, v.z), p2.getDistanceSq(v.x, v.y, v.z))).orElse(null);
  546. }
  547. public static BlockPos getPlayerTarget(EntityPlayer p, double range, boolean boundingBox)
  548. {
  549. if(range > 128)
  550. {
  551. range = 128;
  552. }
  553. World w = p.getEntityWorld();
  554. Vec3d start = getEyeLocation(p);
  555. Vec3d end = start.add(p.getLookVec().scale(range));
  556. RayTraceResult ray = w.rayTraceBlocks(start, end, true, !boundingBox, false);
  557. if(ray == null)
  558. {
  559. return new BlockPos(end);
  560. }
  561. return ray.getBlockPos();
  562. }
  563. public static BlockPos getPlayerTarget(EntityPlayer p, double range)
  564. {
  565. return getPlayerTarget(p, range, false);
  566. }
  567. public static BlockPos getPlayerTarget(EntityPlayer p)
  568. {
  569. return getPlayerTarget(p, 20);
  570. }
  571. public static EntityPlayerMP getPlayerByName(String name) throws PlayerNotFoundException
  572. {
  573. String nameLower = name.toLowerCase();
  574. return KajetansMod.server.getPlayerList().getPlayers().stream()
  575. .filter(pl -> pl.getName().toLowerCase().contains(nameLower))
  576. .findFirst().orElseThrow(() -> new PlayerNotFoundException(name));
  577. }
  578. // -------------------------------------------------------------------------
  579. // Spawn
  580. // -------------------------------------------------------------------------
  581. public static Location getSpawn()
  582. {
  583. // Player changeDimension, WorldServer, MinecraftServer
  584. return KajetansMod.conf.getLocation("spawn");
  585. }
  586. public static void setSpawn(World w, Vec3d v, float yaw, float pitch)
  587. {
  588. SimpleConfig conf = KajetansMod.conf;
  589. conf.setLocation("spawn", new Location(w, v, yaw, pitch));
  590. conf.save();
  591. w.setSpawnPoint(new BlockPos(v.x, v.y, v.z));
  592. }
  593. // -------------------------------------------------------------------------
  594. // Warps
  595. // -------------------------------------------------------------------------
  596. public static Location getWarp(Module m, String name)
  597. {
  598. SimpleConfig conf = new SimpleConfig(m, "warp", name, true);
  599. if(conf.exists())
  600. {
  601. return conf.getLocation("warp");
  602. }
  603. return null;
  604. }
  605. // -------------------------------------------------------------------------
  606. // Blocks
  607. // -------------------------------------------------------------------------
  608. public static BlockPos getSameNearbyBlock(World w, BlockPos pos, Block b)
  609. {
  610. Location l = new Location(w, pos);
  611. if(l.getRelativeBlockState(1, 0, 0).getBlock() == b)
  612. {
  613. return pos.add(1, 0, 0);
  614. }
  615. else if(l.getRelativeBlockState(-1, 0, 0).getBlock() == b)
  616. {
  617. return pos.add(-1, 0, 0);
  618. }
  619. else if(l.getRelativeBlockState(0, 0, 1).getBlock() == b)
  620. {
  621. return pos.add(0, 0, 1);
  622. }
  623. else if(l.getRelativeBlockState(0, 0, -1).getBlock() == b)
  624. {
  625. return pos.add(0, 0, -1);
  626. }
  627. return null;
  628. }
  629. public static boolean shouldBeProtected(Block b)
  630. {
  631. return (b instanceof BlockDoor ||
  632. b instanceof BlockContainer ||
  633. b == Blocks.LEVER ||
  634. b instanceof BlockButton ||
  635. b instanceof BlockTrapDoor) && !(b instanceof BlockPistonMoving);
  636. }
  637. // -------------------------------------------------------------------------
  638. // Roman Numbers
  639. // -------------------------------------------------------------------------
  640. public static String intToRoman(int i)
  641. {
  642. switch(i)
  643. {
  644. case 1: return "I";
  645. case 2: return "II";
  646. case 3: return "III";
  647. case 4: return "IV";
  648. case 5: return "V";
  649. case 6: return "VI";
  650. case 7: return "VII";
  651. case 8: return "VIII";
  652. case 9: return "IX";
  653. case 10: return "X";
  654. case 11: return "XI";
  655. case 12: return "XII";
  656. case 13: return "XIII";
  657. case 14: return "XIV";
  658. case 15: return "XV";
  659. case 16: return "XVI";
  660. case 17: return "XVII";
  661. case 18: return "XVIII";
  662. case 19: return "XIX";
  663. case 20: return "XX";
  664. }
  665. return "I";
  666. }
  667. public static int romanToInt(String s)
  668. {
  669. switch(s)
  670. {
  671. case "I": return 1;
  672. case "II": return 2;
  673. case "III": return 3;
  674. case "IV": return 4;
  675. case "V": return 5;
  676. case "VI": return 6;
  677. case "VII": return 7;
  678. case "VIII": return 8;
  679. case "IX": return 9;
  680. case "X": return 10;
  681. case "XI": return 11;
  682. case "XII": return 12;
  683. case "XIII": return 13;
  684. case "XIV": return 14;
  685. case "XV": return 15;
  686. case "XVI": return 16;
  687. case "XVII": return 17;
  688. case "XVIII": return 18;
  689. case "XIX": return 19;
  690. case "XX": return 20;
  691. }
  692. return 1;
  693. }
  694. }