Utils.java 24 KB

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