TextCommands.java 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package me.km.snuviscript.commands;
  2. import me.hammerle.snuviscript.code.ScriptManager;
  3. import me.km.utils.Location;
  4. import net.minecraft.entity.Entity;
  5. import net.minecraft.item.ItemStack;
  6. import net.minecraft.nbt.CompoundNBT;
  7. import net.minecraft.util.text.*;
  8. import net.minecraft.util.text.event.ClickEvent;
  9. import net.minecraft.util.text.event.HoverEvent;
  10. public class TextCommands {
  11. public static void registerFunctions(ScriptManager sm) {
  12. sm.registerFunction("text.location", (sc, in) -> ((Location) in[0].get(sc)).toString());
  13. sm.registerFunction("text.locationblock", (sc, in) -> ((Location) in[0].get(sc)).toBlockString());
  14. sm.registerFunction("text.item", (sc, in) -> ((ItemStack) in[0].get(sc)).write(new CompoundNBT()).toString());
  15. sm.registerFunction("text.click", (sc, in) -> {
  16. Object message = in[0].get(sc);
  17. ITextComponent text;
  18. if(message instanceof ITextComponent) {
  19. text = (ITextComponent) message;
  20. } else {
  21. text = new StringTextComponent(String.valueOf(message));
  22. }
  23. Style style = text.getStyle();
  24. style.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, in[1].getString(sc)));
  25. return text;
  26. });
  27. sm.registerFunction("text.hover", (sc, in) -> {
  28. Object message = in[0].get(sc);
  29. ITextComponent text;
  30. if(message instanceof ITextComponent) {
  31. text = (ITextComponent) message;
  32. } else {
  33. text = new StringTextComponent(String.valueOf(message));
  34. }
  35. Style style = text.getStyle();
  36. style.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new StringTextComponent(in[1].getString(sc))));
  37. return text;
  38. });
  39. sm.registerFunction("text.link", (sc, in) -> {
  40. StringTextComponent text = new StringTextComponent(in[0].getString(sc));
  41. Style style = text.getStyle();
  42. style.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, in[1].getString(sc)));
  43. return text;
  44. });
  45. sm.registerFunction("text.clipboard", (sc, in) -> {
  46. StringTextComponent text = new StringTextComponent(in[0].getString(sc));
  47. Style style = text.getStyle();
  48. style.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, in[1].getString(sc)));
  49. return text;
  50. });
  51. sm.registerFunction("text.copytext", (sc, in) -> {
  52. String s = in[1].getString(sc);
  53. s = s.replace(" ", "%20");
  54. StringTextComponent text = new StringTextComponent(in[0].getString(sc));
  55. Style style = text.getStyle();
  56. style.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "http://minecraft.hammerle.me/showtext.php/?text=" + s));
  57. return text;
  58. });
  59. sm.registerFunction("text.entity", (sc, in) -> {
  60. CompoundNBT tag = new CompoundNBT();
  61. ((Entity) in[0].get(sc)).writeWithoutTypeId(tag);
  62. return tag.toString();
  63. });
  64. }
  65. }