TextCommands.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. IFormattableTextComponent text;
  18. if(message instanceof IFormattableTextComponent) {
  19. text = (IFormattableTextComponent) message;
  20. } else {
  21. text = new StringTextComponent(String.valueOf(message));
  22. }
  23. text.modifyStyle(style -> {
  24. return style.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, in[1].getString(sc)));
  25. });
  26. return text;
  27. });
  28. sm.registerFunction("text.hover", (sc, in) -> {
  29. Object message = in[0].get(sc);
  30. IFormattableTextComponent text;
  31. if(message instanceof IFormattableTextComponent) {
  32. text = (IFormattableTextComponent) message;
  33. } else {
  34. text = new StringTextComponent(String.valueOf(message));
  35. }
  36. text.modifyStyle(style -> {
  37. return style.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new StringTextComponent(in[1].getString(sc))));
  38. });
  39. return text;
  40. });
  41. sm.registerFunction("text.link", (sc, in) -> {
  42. StringTextComponent text = new StringTextComponent(in[0].getString(sc));
  43. text.modifyStyle(style -> {
  44. return style.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, in[1].getString(sc)));
  45. });
  46. return text;
  47. });
  48. sm.registerFunction("text.clipboard", (sc, in) -> {
  49. StringTextComponent text = new StringTextComponent(in[0].getString(sc));
  50. text.modifyStyle(style -> {
  51. return style.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, in[1].getString(sc)));
  52. });
  53. return text;
  54. });
  55. sm.registerFunction("text.copytext", (sc, in) -> {
  56. String s = in[1].getString(sc).replace(" ", "%20");
  57. StringTextComponent text = new StringTextComponent(in[0].getString(sc));
  58. text.modifyStyle(style -> {
  59. return style.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "http://minecraft.hammerle.me/showtext.php/?text=" + s));
  60. });
  61. return text;
  62. });
  63. sm.registerFunction("text.entity", (sc, in) -> {
  64. CompoundNBT tag = new CompoundNBT();
  65. ((Entity) in[0].get(sc)).writeWithoutTypeId(tag);
  66. return tag.toString();
  67. });
  68. }
  69. }