MessageSender.java 3.7 KB

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