MessageSender.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package me.km.api;
  2. import java.util.ArrayList;
  3. import me.km.KajetansMod;
  4. import net.minecraft.command.ICommandSender;
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.util.math.BlockPos;
  7. import net.minecraft.util.text.ITextComponent;
  8. import net.minecraft.util.text.TextComponentUtils;
  9. import net.minecraft.util.text.TextFormatting;
  10. import net.minecraft.util.text.TextComponentString;
  11. import net.minecraft.world.World;
  12. public class MessageSender
  13. {
  14. private final ArrayList<ITextComponent> prefixes;
  15. private final ArrayList<TextFormatting> colors;
  16. public MessageSender()
  17. {
  18. prefixes = new ArrayList<>();
  19. colors = new ArrayList<>();
  20. }
  21. public void registerPrefix(String name, TextFormatting color)
  22. {
  23. TextComponentString text = new TextComponentString(name);
  24. text.getStyle().setColor(color);
  25. prefixes.add(new TextComponentString("[").appendSibling(text).appendText("] "));
  26. colors.add(color);
  27. }
  28. public void send(ICommandSender cs, String msg, int id)
  29. {
  30. if(cs == null)
  31. {
  32. return;
  33. }
  34. cs.sendMessage(prefixes.get(id).createCopy().appendText(msg));
  35. }
  36. public void send(ICommandSender cs, String msg)
  37. {
  38. send(cs, msg, 0);
  39. }
  40. public void sendToPlayers(World w, BlockPos l, double radius, String msg)
  41. {
  42. ITextComponent s = prefixes.get(0).createCopy().appendText(msg);
  43. Utils.getNearbyEntities(w, l, radius, EntityPlayer.class).forEach(p -> ((EntityPlayer) p).sendMessage(s));
  44. }
  45. public void sendBroadcast(String msg)
  46. {
  47. KajetansMod.server.getPlayerList().sendMessage(prefixes.get(0).createCopy().appendText(msg));
  48. }
  49. public void sendListElement(ICommandSender cs, String msg, int id)
  50. {
  51. if(cs == null)
  52. {
  53. return;
  54. }
  55. TextComponentString text = new TextComponentString("");
  56. TextComponentString text2 = new TextComponentString(" - ");
  57. text2.getStyle().setColor(colors.get(id));
  58. text.appendSibling(text2);
  59. cs.sendMessage(text.appendText(msg));
  60. }
  61. public void sendListElement(ICommandSender cs, String msg)
  62. {
  63. sendListElement(cs, msg, 0);
  64. }
  65. public void sendHelpListElement(ICommandSender cs, String msg, String msg2, int id)
  66. {
  67. if(cs == null)
  68. {
  69. return;
  70. }
  71. TextComponentString text = new TextComponentString("");
  72. TextComponentString text2 = new TextComponentString(" - " + msg + " ");
  73. text2.getStyle().setColor(colors.get(id));
  74. text.appendSibling(text2);
  75. cs.sendMessage(text.appendText(msg2));
  76. }
  77. public void sendHelpListElement(ICommandSender cs, String msg, String msg2)
  78. {
  79. sendHelpListElement(cs, msg, msg2, 0);
  80. }
  81. public void sendWarning(ICommandSender cs, String msg, int id)
  82. {
  83. if(cs == null)
  84. {
  85. return;
  86. }
  87. TextComponentString text = new TextComponentString(msg);
  88. text.getStyle().setColor(TextFormatting.RED);
  89. cs.sendMessage(prefixes.get(id).createCopy().appendSibling(text));
  90. }
  91. public void sendWarning(ICommandSender cs, String msg)
  92. {
  93. if(cs == null)
  94. {
  95. return;
  96. }
  97. sendWarning(cs, msg, 0);
  98. }
  99. public void sendToConsole(String msg, int id)
  100. {
  101. send(KajetansMod.server, msg, id);
  102. }
  103. public void sendToConsole(String msg)
  104. {
  105. sendToConsole(msg, 0);
  106. }
  107. public void sendWarningToConsole(String msg, int id)
  108. {
  109. sendWarning(KajetansMod.server, msg, id);
  110. }
  111. public void sendWarningToConsole(String msg)
  112. {
  113. sendWarningToConsole(msg, 0);
  114. }
  115. }