NBTUtils.java 9.8 KB

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