NmsUtilities.java 9.4 KB

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