Utils.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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.getTrueSource();
  354. if(ent == null)
  355. {
  356. return null;
  357. }
  358. if(ent instanceof EntityPlayer)
  359. {
  360. return (EntityPlayer) ent;
  361. }
  362. return null;//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. int dim = l.getWorld().provider.getDimension();
  378. if(ent instanceof EntityPlayerMP)
  379. {
  380. EntityPlayerMP p = (EntityPlayerMP) ent;
  381. KajetansMod.playerbank.saveLocation(p);
  382. if(ent.dimension != dim)
  383. {
  384. KajetansMod.server.getPlayerList().transferPlayerToDimension(p, dim, new ModTeleporter(KajetansMod.server.getWorld(dim)));
  385. }
  386. if(l.getYaw() != 0 || l.getPitch() != 0)
  387. {
  388. p.connection.setPlayerLocation(l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch());
  389. }
  390. else
  391. {
  392. p.connection.setPlayerLocation(l.getX(), l.getY(), l.getZ(), ent.rotationYaw, ent.rotationPitch);
  393. }
  394. }
  395. else
  396. {
  397. if(ent.dimension != dim)
  398. {
  399. WorldServer n = KajetansMod.server.getWorld(dim);
  400. KajetansMod.server.getPlayerList().transferEntityToWorld(ent, ent.dimension, KajetansMod.server.getWorld(ent.dimension), n, new ModTeleporter(n));
  401. }
  402. if(l.getYaw() != 0 && l.getPitch() != 0)
  403. {
  404. ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch());
  405. }
  406. else
  407. {
  408. ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), ent.rotationYaw, ent.rotationPitch);
  409. }
  410. }
  411. }
  412. public static void teleportEntity(Entity ent, Entity ent2)
  413. {
  414. teleportEntity(ent, new Location(ent2.world, ent2.getPositionVector(), ent2.rotationYaw, ent2.rotationPitch));
  415. }
  416. // -------------------------------------------------------------------------
  417. // Entities aus Umgebung
  418. // -------------------------------------------------------------------------
  419. @SuppressWarnings("unchecked")
  420. public static List<Entity> getEntitiesExcluding(Entity ent, World w, BlockPos pos, BlockPos pos2)
  421. {
  422. return w.getEntitiesWithinAABBExcludingEntity(ent, new AxisAlignedBB(pos, pos2).grow(1));
  423. }
  424. @SuppressWarnings("unchecked")
  425. public static <T extends Entity> Collection<T> getNearbyEntities(World w, Vec3d v, double radius, Class<T> type)
  426. {
  427. double sqareRadius = radius * radius;
  428. return w.getEntitiesWithinAABB(type, new AxisAlignedBB(
  429. v.x - radius, v.y - radius, v.z - radius,
  430. v.x + radius, v.y + radius, v.z + radius), ent -> ent.getDistanceSq(v.x, v.y, v.z) <= sqareRadius);
  431. }
  432. public static List<EntityPlayer> getNearbyPlayers(World w, double x, double y, double z, double radius)
  433. {
  434. double sqareRadius = radius * radius;
  435. return w.playerEntities.stream().filter(p -> p.getDistanceSq(x, y, z) <= sqareRadius).collect(Collectors.toList());
  436. }
  437. public static List<EntityPlayer> getNearbyPlayers(World w, Vec3d v, double radius)
  438. {
  439. return getNearbyPlayers(w, v.x, v.y, v.z, radius);
  440. }
  441. public static <T extends Entity> T getNearestEntity(World w, Vec3d v, double radius, Class<T> type)
  442. {
  443. return getNearbyEntities(w, v, radius, type).stream().min((e1, e2) -> Double.compare(
  444. e1.getDistanceSq(v.x, v.y, v.z),
  445. e2.getDistanceSq(v.x, v.y, v.z)))
  446. .orElse(null);
  447. }
  448. private static boolean doesIntersect(AxisAlignedBB bound, Vec3d start, Vec3d unit)
  449. {
  450. double lowerX = Double.NEGATIVE_INFINITY;
  451. double upperX = Double.POSITIVE_INFINITY;
  452. if(unit.x != 0)
  453. {
  454. if(unit.x > 0)
  455. {
  456. lowerX = (bound.minX - start.x) / unit.x;
  457. upperX = (bound.maxX - start.x) / unit.x;
  458. }
  459. else
  460. {
  461. lowerX = (bound.maxX - start.x) / unit.x;
  462. upperX = (bound.minX - start.x) / unit.x;
  463. }
  464. }
  465. double lowerY = Double.NEGATIVE_INFINITY;
  466. double upperY = Double.POSITIVE_INFINITY;
  467. if(unit.y != 0)
  468. {
  469. if(unit.y > 0)
  470. {
  471. lowerY = (bound.minY - start.y) / unit.y;
  472. upperY = (bound.maxY - start.y) / unit.y;
  473. }
  474. else
  475. {
  476. lowerY = (bound.maxY - start.y) / unit.y;
  477. upperY = (bound.minY - start.y) / unit.y;
  478. }
  479. }
  480. double lowerZ = Double.NEGATIVE_INFINITY;
  481. double upperZ = Double.POSITIVE_INFINITY;
  482. if(unit.z != 0)
  483. {
  484. if(unit.z > 0)
  485. {
  486. lowerZ = (bound.minZ - start.z) / unit.z;
  487. upperZ = (bound.maxZ - start.z) / unit.z;
  488. }
  489. else
  490. {
  491. lowerZ = (bound.maxZ - start.z) / unit.z;
  492. upperZ = (bound.minZ - start.z) / unit.z;
  493. }
  494. }
  495. return Math.max(Math.max(lowerX, lowerY), lowerZ) < Math.min(Math.min(upperX, upperY), upperZ);
  496. }
  497. @SuppressWarnings("unchecked")
  498. public static <T extends Entity> T getTargetedEntity(EntityPlayer p, double radius, Class<T> type)
  499. {
  500. World w = p.getEntityWorld();
  501. BlockPos l = getPlayerTarget(p, radius);
  502. Vec3d eye = getEyeLocation(p);
  503. Vec3d unit = new Vec3d(l.getX() - eye.x, l.getY() - eye.y, l.getZ() - eye.z);
  504. List<Entity> col = getEntitiesExcluding(p, w, new BlockPos(eye), l);
  505. col.removeIf(ent -> !type.isAssignableFrom(ent.getClass()));
  506. // Entfernt Entities, die sich nicht mit dem Blick-Vektor schneiden
  507. col.removeIf(ent -> !doesIntersect(ent.getEntityBoundingBox().grow(0.1), eye, unit));
  508. return (T) col.stream().sorted((Entity e1, Entity e2) ->
  509. {
  510. return Double.compare(e1.getDistanceSq(eye.x, eye.y, eye.z), e2.getDistanceSq(eye.x, eye.y, eye.z));
  511. }).findFirst().orElse(null);
  512. }
  513. // -------------------------------------------------------------------------
  514. // Player-Tools
  515. // -------------------------------------------------------------------------
  516. public static EntityPlayer getNearestPlayer(World w, Vec3d v)
  517. {
  518. 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);
  519. }
  520. public static BlockPos getPlayerTarget(EntityPlayer p, double range, boolean boundingBox)
  521. {
  522. if(range > 64)
  523. {
  524. range = 64;
  525. }
  526. World w = p.getEntityWorld();
  527. Vec3d start = getEyeLocation(p);
  528. Vec3d end = start.add(p.getLookVec().scale(range));
  529. RayTraceResult ray = w.rayTraceBlocks(start, end, true, !boundingBox, false);
  530. if(ray == null)
  531. {
  532. return new BlockPos(end);
  533. }
  534. return ray.getBlockPos();
  535. }
  536. public static BlockPos getPlayerTarget(EntityPlayer p, double range)
  537. {
  538. return getPlayerTarget(p, range, false);
  539. }
  540. public static BlockPos getPlayerTarget(EntityPlayer p)
  541. {
  542. return getPlayerTarget(p, 20);
  543. }
  544. public static EntityPlayerMP getPlayerByName(String name) throws PlayerNotFoundException
  545. {
  546. return KajetansMod.server.getPlayerList().getPlayers().stream()
  547. .filter(pl -> pl.getName().contains(name))
  548. .findFirst().orElseThrow(() -> new PlayerNotFoundException(name));
  549. }
  550. public static EntityPlayerMP getPlayerByDisplayName(ITextComponent name) throws PlayerNotFoundException
  551. {
  552. String s = name.getUnformattedText();
  553. return KajetansMod.server.getPlayerList().getPlayers().stream()
  554. .filter(pl -> pl.getDisplayName().getUnformattedText().equals(s))
  555. .findFirst().orElseThrow(() -> new PlayerNotFoundException(s));
  556. }
  557. // -------------------------------------------------------------------------
  558. // Spawn
  559. // -------------------------------------------------------------------------
  560. public static Location getSpawn()
  561. {
  562. // Player changeDimension, WorldServer, MinecraftServer
  563. return KajetansMod.conf.getLocation("spawn");
  564. }
  565. public static void setSpawn(World w, Vec3d v, float yaw, float pitch)
  566. {
  567. SimpleConfig conf = KajetansMod.conf;
  568. conf.setLocation("spawn", new Location(w, v, yaw, pitch));
  569. conf.save();
  570. w.setSpawnPoint(new BlockPos(v.x, v.y, v.z));
  571. }
  572. // -------------------------------------------------------------------------
  573. // Warps
  574. // -------------------------------------------------------------------------
  575. public static Location getWarp(Module m, String name)
  576. {
  577. SimpleConfig conf = new SimpleConfig(m, "warp/" + name, true);
  578. if(conf.exists())
  579. {
  580. return conf.getLocation("warp");
  581. }
  582. return null;
  583. }
  584. // -------------------------------------------------------------------------
  585. // Blocks
  586. // -------------------------------------------------------------------------
  587. public static BlockPos getSameNearbyBlock(World w, BlockPos pos, Block b)
  588. {
  589. Location l = new Location(w, pos);
  590. if(l.getRelativeBlockState(1, 0, 0).getBlock() == b)
  591. {
  592. return pos.add(1, 0, 0);
  593. }
  594. else if(l.getRelativeBlockState(-1, 0, 0).getBlock() == b)
  595. {
  596. return pos.add(-1, 0, 0);
  597. }
  598. else if(l.getRelativeBlockState(0, 0, 1).getBlock() == b)
  599. {
  600. return pos.add(0, 0, 1);
  601. }
  602. else if(l.getRelativeBlockState(0, 0, -1).getBlock() == b)
  603. {
  604. return pos.add(0, 0, -1);
  605. }
  606. return null;
  607. }
  608. public static boolean shouldBeProtected(Block b)
  609. {
  610. return (b instanceof BlockDoor ||
  611. b instanceof BlockContainer ||
  612. b == Blocks.LEVER ||
  613. b instanceof BlockButton ||
  614. b instanceof BlockTrapDoor) && !(b instanceof BlockPistonMoving);
  615. }
  616. // -------------------------------------------------------------------------
  617. // Roman Numbers
  618. // -------------------------------------------------------------------------
  619. public static String intToRoman(int i)
  620. {
  621. switch(i)
  622. {
  623. case 1: return "I";
  624. case 2: return "II";
  625. case 3: return "III";
  626. case 4: return "IV";
  627. case 5: return "V";
  628. case 6: return "VI";
  629. case 7: return "VII";
  630. case 8: return "VIII";
  631. case 9: return "IX";
  632. case 10: return "X";
  633. case 11: return "XI";
  634. case 12: return "XII";
  635. case 13: return "XIII";
  636. case 14: return "XIV";
  637. case 15: return "XV";
  638. case 16: return "XVI";
  639. case 17: return "XVII";
  640. case 18: return "XVIII";
  641. case 19: return "XIX";
  642. case 20: return "XX";
  643. }
  644. return "I";
  645. }
  646. public static int romanToInt(String s)
  647. {
  648. switch(s)
  649. {
  650. case "I": return 1;
  651. case "II": return 2;
  652. case "III": return 3;
  653. case "IV": return 4;
  654. case "V": return 5;
  655. case "VI": return 6;
  656. case "VII": return 7;
  657. case "VIII": return 8;
  658. case "IX": return 9;
  659. case "X": return 10;
  660. case "XI": return 11;
  661. case "XII": return 12;
  662. case "XIII": return 13;
  663. case "XIV": return 14;
  664. case "XV": return 15;
  665. case "XVI": return 16;
  666. case "XVII": return 17;
  667. case "XVIII": return 18;
  668. case "XIX": return 19;
  669. case "XX": return 20;
  670. }
  671. return 1;
  672. }
  673. }