NBTUtils.java 9.8 KB

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