TextCommands.java 3.1 KB

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