NmsUtilities.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package me.km.nms;
  2. import java.util.List;
  3. import me.hammerle.code.Script;
  4. import me.hammerle.exceptions.IllegalStringException;
  5. import me.km.api.Location;
  6. import net.minecraft.block.Block;
  7. import net.minecraft.block.state.IBlockState;
  8. import net.minecraft.entity.Entity;
  9. import net.minecraft.entity.EntityLiving;
  10. import net.minecraft.entity.player.EntityPlayer;
  11. import net.minecraft.entity.player.EntityPlayerMP;
  12. import net.minecraft.init.Blocks;
  13. import net.minecraft.inventory.IInventory;
  14. import net.minecraft.nbt.JsonToNBT;
  15. import net.minecraft.nbt.NBTException;
  16. import net.minecraft.nbt.NBTTagCompound;
  17. import net.minecraft.network.play.server.SPacketChat;
  18. import net.minecraft.tileentity.TileEntity;
  19. import net.minecraft.util.math.BlockPos;
  20. import net.minecraft.util.math.Vec3d;
  21. import net.minecraft.util.text.ChatType;
  22. import net.minecraft.util.text.Style;
  23. import net.minecraft.util.text.TextComponentString;
  24. import net.minecraft.util.text.TextFormatting;
  25. import net.minecraft.util.text.event.ClickEvent;
  26. import net.minecraft.world.World;
  27. import net.minecraft.world.chunk.storage.AnvilChunkLoader;
  28. public class NmsUtilities
  29. {
  30. // -----------------------------------------------------------------------------------
  31. // Entity Tools
  32. // -----------------------------------------------------------------------------------
  33. public static List<Entity> getCollidingEntities(Entity e, double x, double y, double z)
  34. {
  35. return e.getEntityWorld().getEntitiesWithinAABBExcludingEntity(e, e.getCollisionBoundingBox().expand(x, y, z));
  36. }
  37. public static List<Entity> getCollidingEntities(Entity e)
  38. {
  39. return getCollidingEntities(e, 0, 0, 0);
  40. }
  41. public static void walkTo(EntityLiving liv, Vec3d v, double speed)
  42. {
  43. liv.getMoveHelper().setMoveTo(v.x, v.y, v.z, speed);
  44. }
  45. public static void canDestroyBlocks(EntityLiving liv)
  46. {
  47. liv.tasks.addTask(1, new PathfinderGoalDestroyBlock(liv));
  48. }
  49. public static String getNbtString(Entity ent)
  50. {
  51. NBTTagCompound tag = new NBTTagCompound();
  52. ent.writeToNBTAtomically(tag);
  53. return tag.toString();
  54. }
  55. public static Entity getEntityFromNbtString(Script sc, String s, Location l)
  56. {
  57. try
  58. {
  59. NBTTagCompound nbt = JsonToNBT.getTagFromJson(s);
  60. Vec3d v = l.getPos();
  61. Entity ent = AnvilChunkLoader.readWorldEntityPos(nbt, l.getWorld(), v.x, v.y, v.z, true);
  62. if(ent == null)
  63. {
  64. return null;
  65. }
  66. else
  67. {
  68. ent.setLocationAndAngles(v.x, v.y, v.z, ent.rotationYaw, ent.rotationPitch);
  69. if(ent instanceof EntityLiving)
  70. {
  71. ((EntityLiving) ent).onInitialSpawn(ent.getEntityWorld().getDifficultyForLocation(new BlockPos(ent)), null);
  72. }
  73. }
  74. return ent;
  75. }
  76. catch(NBTException ex)
  77. {
  78. throw new IllegalStringException(sc, s);
  79. }
  80. }
  81. // -------------------------------------------------------------------------
  82. // Block Tools
  83. // -------------------------------------------------------------------------
  84. @SuppressWarnings(value = {"unchecked", "deprecation"})
  85. public static void setBlockWithData(World w, BlockPos pos, int dv, Block block, String data)
  86. {
  87. NBTTagCompound nbttagcompound = new NBTTagCompound();
  88. boolean flag = false;
  89. if (data != null && block.hasTileEntity())
  90. {
  91. try
  92. {
  93. nbttagcompound = JsonToNBT.getTagFromJson(data);
  94. flag = true;
  95. }
  96. catch(NBTException ex)
  97. {
  98. throw new IllegalStringException(data);
  99. }
  100. }
  101. TileEntity tileentity = w.getTileEntity(pos);
  102. if (tileentity != null)
  103. {
  104. if (tileentity instanceof IInventory)
  105. {
  106. ((IInventory) tileentity).clear();
  107. }
  108. w.setBlockState(pos, Blocks.AIR.getDefaultState(), block == Blocks.AIR ? 2 : 4);
  109. }
  110. IBlockState iblockdata = block.getStateFromMeta(dv);
  111. if(w.setBlockState(pos, iblockdata, 2))
  112. {
  113. if (flag)
  114. {
  115. TileEntity tileentity1 = w.getTileEntity(pos);
  116. if (tileentity1 != null)
  117. {
  118. nbttagcompound.setInteger("x", pos.getX());
  119. nbttagcompound.setInteger("y", pos.getY());
  120. nbttagcompound.setInteger("z", pos.getZ());
  121. tileentity1.readFromNBT(nbttagcompound);
  122. }
  123. }
  124. w.notifyNeighborsRespectDebug(pos, iblockdata.getBlock(), false);
  125. }
  126. }
  127. // -------------------------------------------------------------------------
  128. // NBT Tools
  129. // -------------------------------------------------------------------------
  130. public static String highlightNbtSyntax(String text)
  131. {
  132. StringBuilder sb = new StringBuilder(text);
  133. int counter = 0;
  134. for(char c : text.toCharArray())
  135. {
  136. switch(c)
  137. {
  138. case '}':
  139. sb.replace(counter, counter + 1, "§c} §r");
  140. counter += 6;
  141. continue;
  142. case '{':
  143. sb.replace(counter, counter + 1, "§c{§r");
  144. counter += 5;
  145. continue;
  146. case ']':
  147. sb.replace(counter, counter + 1, "§c] §r");
  148. counter += 6;
  149. continue;
  150. case '[':
  151. sb.replace(counter, counter + 1, "§c[§r");
  152. counter += 5;
  153. continue;
  154. case ',':
  155. sb.replace(counter, counter + 1, "§6, §r");
  156. counter += 6;
  157. continue;
  158. case '"':
  159. sb.replace(counter, counter + 1, "§a\"");
  160. counter += 3;
  161. continue;
  162. case ':':
  163. sb.replace(counter, counter + 1, "§8:");
  164. counter += 3;
  165. continue;
  166. case '0':
  167. sb.replace(counter, counter + 1, "§50");
  168. counter += 3;
  169. continue;
  170. case '1':
  171. sb.replace(counter, counter + 1, "§51");
  172. counter += 3;
  173. continue;
  174. case '2':
  175. sb.replace(counter, counter + 1, "§52");
  176. counter += 3;
  177. continue;
  178. case '3':
  179. sb.replace(counter, counter + 1, "§53");
  180. counter += 3;
  181. continue;
  182. case '4':
  183. sb.replace(counter, counter + 1, "§54");
  184. counter += 3;
  185. continue;
  186. case '5':
  187. sb.replace(counter, counter + 1, "§55");
  188. counter += 3;
  189. continue;
  190. case '6':
  191. sb.replace(counter, counter + 1, "§56");
  192. counter += 3;
  193. continue;
  194. case '7':
  195. sb.replace(counter, counter + 1, "§57");
  196. counter += 3;
  197. continue;
  198. case '8':
  199. sb.replace(counter, counter + 1, "§58");
  200. counter += 3;
  201. continue;
  202. case '9':
  203. sb.replace(counter, counter + 1, "§59");
  204. counter += 3;
  205. continue;
  206. default:
  207. counter++;
  208. }
  209. }
  210. return sb.toString();
  211. }
  212. // -------------------------------------------------------------------------
  213. // Copy Tools
  214. // -------------------------------------------------------------------------
  215. public static void sendCopyableText(EntityPlayer p, String s)
  216. {
  217. s = s.replace('{', 'Ɛ');
  218. s = s.replace('}', 'Ƒ');
  219. s = s.replace('[', 'ƒ');
  220. s = s.replace(']', 'Ɠ');
  221. s = s.replace('(', 'ƕ');
  222. s = s.replace(')', 'Ɩ');
  223. s = s.replace(" ", "%20");
  224. s = s.replace('\'', 'Ɩ');
  225. s = s.replace('"', 'Ɩ');
  226. s = s.replace("\\", "");
  227. sendLink(p, "Hier drücken zum Kopieren.", "http://minecraft.hammerle.me/showtext.php/?text=" + s);
  228. }
  229. private static void sendLink(EntityPlayer p, String s, String link)
  230. {
  231. TextComponentString text = new TextComponentString(s);
  232. Style style = text.getStyle();
  233. style.setColor(TextFormatting.RED);
  234. style.setBold(true);
  235. style.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, link));
  236. p.sendMessage(text);
  237. }
  238. // -------------------------------------------------------------------------
  239. // Action-Bar
  240. // -------------------------------------------------------------------------
  241. public static void sendActionBar(EntityPlayerMP p, String message)
  242. {
  243. p.connection.sendPacket(new SPacketChat(new TextComponentString(message), ChatType.GAME_INFO));
  244. }
  245. }