ChatChannel.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package me.km.chatchannel;
  2. import java.util.function.BiConsumer;
  3. import java.util.function.Consumer;
  4. import java.util.function.Predicate;
  5. import me.km.KajetansMod;
  6. import net.minecraft.entity.player.ServerPlayerEntity;
  7. import net.minecraft.util.text.StringTextComponent;
  8. public class ChatChannel
  9. {
  10. // -------------------------------------------------------------------------
  11. // static stuff
  12. // -------------------------------------------------------------------------
  13. public static ChatChannel getDevChannel()
  14. {
  15. return DEV_CHANNEL;
  16. };
  17. private final static ChatChannel DEV_CHANNEL = new ChatChannel("Dev", TextColor.LIGHT_PURPLE,
  18. (cc, s) ->
  19. {
  20. StringTextComponent text = new StringTextComponent(s);
  21. cc.forEach(p -> p.sendMessage(text));
  22. }, p -> KajetansMod.perms.hasPermission(p, "script.error"));
  23. // -------------------------------------------------------------------------
  24. // chat channels
  25. // -------------------------------------------------------------------------
  26. private final Predicate<ServerPlayerEntity> filter;
  27. private final String prefix;
  28. private final String list;
  29. private final BiConsumer<ChatChannel, String> onSend;
  30. private ChatChannel(String name, TextColor tc,
  31. BiConsumer<ChatChannel, String> onSend, Predicate<ServerPlayerEntity> filter)
  32. {
  33. this.filter = filter;
  34. this.prefix = "[" + tc + name + TextColor.RESET + "] ";
  35. this.list = tc + " - ";
  36. this.onSend = onSend;
  37. }
  38. public void send(String msg)
  39. {
  40. onSend.accept(this, prefix + " " + msg);
  41. }
  42. public void sendWarning(String msg)
  43. {
  44. onSend.accept(this, prefix + " " + TextColor.RED + msg);
  45. }
  46. public void sendList(String msg)
  47. {
  48. onSend.accept(this, list + TextColor.RESET + msg);
  49. }
  50. public void sendList(String msg, String msg2)
  51. {
  52. onSend.accept(this, list + msg + TextColor.RESET + " " + msg2);
  53. }
  54. public void forEach(Consumer<ServerPlayerEntity> c)
  55. {
  56. KajetansMod.server.getPlayerList().getPlayers().stream().filter(filter).forEach(c);
  57. }
  58. }