[Shoebill] ColorCode supported Info Chat

    Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

    • [Shoebill] ColorCode supported Info Chat

      ColorCode supported Info Chat


      youtube.com/watch?v=65JCdFzNLAM


      Das ist ein Info Chat, der PlayerTextdraws erstellt und diese in einer einstellbaren Zeit erscheinen lässt (mit Animation) und diesen PlayerTextdraw eine einstellbare Zeit anzeigt.

      Java-Quellcode: ChatMessage.java

      1. /**
      2. *
      3. */
      4. package me.alf21.util.chat;
      5. import java.util.regex.Matcher;
      6. import java.util.regex.Pattern;
      7. import net.gtaun.shoebill.constant.TextDrawAlign;
      8. import net.gtaun.shoebill.constant.TextDrawFont;
      9. import net.gtaun.shoebill.data.Color;
      10. import net.gtaun.shoebill.object.Destroyable;
      11. import net.gtaun.shoebill.object.Player;
      12. import net.gtaun.shoebill.object.PlayerTextdraw;
      13. import net.gtaun.shoebill.object.Timer;
      14. /**
      15. * @author Alf21
      16. *
      17. */
      18. public class ChatMessage implements Destroyable{
      19. private PlayerTextdraw textdraw;
      20. private int slot;
      21. private Timer timer;
      22. private boolean sliding;
      23. private String text;
      24. private int count;
      25. private Player player;
      26. private Long time;
      27. private int currentEndIndex;
      28. public ChatMessage(Player player, String text, int slot) {
      29. this.player = player;
      30. this.text = removeNonColorCodes(text).trim();
      31. this.slot = slot;
      32. count = 1;
      33. createTextdraw(0,0);
      34. time = System.currentTimeMillis();
      35. }
      36. public void slideIn() {
      37. sliding = true;
      38. reset();
      39. final int length = getTextLength();
      40. final int interval = (int) Chat.slideTime/length;
      41. currentEndIndex = 0;
      42. timer = Timer.create(interval, length, (factualInterval) -> {
      43. currentEndIndex++;
      44. currentEndIndex = cutTextIn(currentEndIndex);
      45. textdraw.hide();
      46. textdraw.setText(text.substring(0, currentEndIndex));
      47. textdraw.show();
      48. if(count == length) {
      49. sliding = false;
      50. timer.stop();
      51. }
      52. count++;
      53. });
      54. timer.start();
      55. }
      56. public void slideOut() {
      57. sliding = true;
      58. reset();
      59. final int length = getTextLength();
      60. final int interval = (int) Chat.slideTime/length;
      61. currentEndIndex = text.length();
      62. timer = Timer.create(interval, length-1, (factualInterval) -> {
      63. currentEndIndex--;
      64. currentEndIndex = cutTextOut(currentEndIndex);
      65. textdraw.hide();
      66. textdraw.setText(text.substring(0, currentEndIndex));
      67. textdraw.show();
      68. if(count == length-1) {
      69. sliding = false;
      70. timer.stop();
      71. }
      72. count++;
      73. });
      74. timer.start();
      75. }
      76. public void moveUp() {
      77. reset();
      78. final int maxCount = 15;
      79. final int interval = (int) Chat.slideTime/maxCount;
      80. timer = Timer.create(interval, maxCount, (factualInterval) -> {
      81. textdraw.hide();
      82. textdraw.destroy();
      83. createTextdraw(0, (float) -count*(15f/(float) maxCount));
      84. textdraw.show();
      85. if(count == maxCount) {
      86. slot++;
      87. timer.stop();
      88. }
      89. count++;
      90. });
      91. timer.start();
      92. }
      93. public void hide() {
      94. textdraw.hide();
      95. }
      96. public void show() {
      97. textdraw.show();
      98. }
      99. private void reset() {
      100. if(timer != null)
      101. timer.destroy();
      102. count = 1;
      103. }
      104. private void createTextdraw(float x, float y) {
      105. textdraw = PlayerTextdraw.create(player, 630+x, 385-15*slot+y);
      106. textdraw.setText(text);
      107. textdraw.setAlignment(TextDrawAlign.RIGHT);
      108. textdraw.setBackgroundColor(Color.BLACK);
      109. textdraw.setFont(TextDrawFont.get(1));
      110. textdraw.setLetterSize(0.5f, 1);
      111. textdraw.setColor(Color.WHITE);
      112. textdraw.setOutlineSize(1);
      113. textdraw.setProportional(true);
      114. textdraw.setSelectable(false);
      115. }
      116. /**
      117. * @return the sliding
      118. */
      119. public boolean isSliding() {
      120. return sliding;
      121. }
      122. /**
      123. * @param sliding the sliding to set
      124. */
      125. public void setSliding(boolean sliding) {
      126. this.sliding = sliding;
      127. }
      128. /**
      129. * @return the time when textdraw was created
      130. */
      131. public Long getTime() {
      132. return time;
      133. }
      134. /**
      135. * @return the textdraw
      136. */
      137. public PlayerTextdraw getTextdraw() {
      138. return textdraw;
      139. }
      140. /**
      141. * @return the player
      142. */
      143. public Player getPlayer() {
      144. return player;
      145. }
      146. /* (non-Javadoc)
      147. * @see net.gtaun.shoebill.object.Destroyable#destroy()
      148. */
      149. @Override
      150. public void destroy() {
      151. if(timer != null) {
      152. if(timer.isRunning())
      153. timer.stop();
      154. timer.destroy();
      155. timer = null;
      156. }
      157. if(textdraw != null) {
      158. textdraw.hide();
      159. textdraw.destroy();
      160. textdraw = null;
      161. }
      162. }
      163. /* (non-Javadoc)
      164. * @see net.gtaun.shoebill.object.Destroyable#isDestroyed()
      165. */
      166. @Override
      167. public boolean isDestroyed() {
      168. if(timer != null || textdraw != null)
      169. return false;
      170. return true;
      171. }
      172. private String removeNonColorCodes(String text) {
      173. final String prefixOfColorCodes = "COLOR"; //The prefix of any color code like "COLOR_RED" <- HERE>> if String == "": "_RED"
      174. final String connectionOfColorCodes = "\u005F"; //The connection label or Unicode, here: "\u005F" == "_" <- HERE>> if String == "": "COLORRED"
      175. String replaceStr = prefixOfColorCodes + connectionOfColorCodes;
      176. text = text.replaceAll("\u007E" + "w" + "\u007E", replaceStr + "WHITE");
      177. text = text.replaceAll("\u007E" + "r" + "\u007E", replaceStr + "RED");
      178. text = text.replaceAll("\u007E" + "g" + "\u007E", replaceStr + "GREEN");
      179. text = text.replaceAll("\u007E" + "b" + "\u007E", replaceStr + "DARK" + connectionOfColorCodes + "BLUE");
      180. text = text.replaceAll("\u007E" + "y" + "\u007E", replaceStr + "YELLOW");
      181. text = text.replaceAll("\u007E" + "p" + "\u007E", replaceStr + "PURPLE");
      182. text = text.replaceAll("\u007E" + "l" + "\u007E", replaceStr + "BLACK");
      183. text = text.replaceAll("\u007E" + "h" + "\u007E", replaceStr + "LIGHTER");
      184. text = text.replaceAll("\u007E+([\\w[\u003C\u003E&&[\\S]]]?)\u007E", ""); //replace unknown colors and Commands
      185. text = text.replaceAll("\u007E", ""); //replace other "~"
      186. text = text.replaceAll(replaceStr + "WHITE", "\u007E" + "w" + "\u007E");
      187. text = text.replaceAll(replaceStr + "RED", "\u007E" + "r" + "\u007E");
      188. text = text.replaceAll(replaceStr + "GREEN", "\u007E" + "g" + "\u007E");
      189. text = text.replaceAll(replaceStr + "DARK" + connectionOfColorCodes + "BLUE", "\u007E" + "b" + "\u007E");
      190. text = text.replaceAll(replaceStr + "YELLOW", "\u007E" + "y" + "\u007E");
      191. text = text.replaceAll(replaceStr + "PURPLE", "\u007E" + "p" + "\u007E");
      192. text = text.replaceAll(replaceStr + "BLACK", "\u007E" + "l" + "\u007E");
      193. text = text.replaceAll(replaceStr + "LIGHTER", "\u007E" + "h" + "\u007E");
      194. return text;
      195. }
      196. private int cutTextIn(int endIndex) {
      197. int currentEndIndex = endIndex;
      198. String string = text.substring(0, currentEndIndex);
      199. while(string.trim().length() < string.length()){
      200. currentEndIndex++;
      201. string = text.substring(0, currentEndIndex);
      202. }
      203. //check whether string contains color codes
      204. Matcher matcher = Pattern.compile("\u007E+([\\w[\u003C\u003E&&[\\S]]]{1})\u007E").matcher(text); //~r~|
      205. if(matcher.find()) {
      206. //match ~
      207. int matcherState = 0;
      208. matcher = Pattern.compile("\u007E+([\\w[\u003C\u003E&&[\\S]]]{1})\u007E\\z").matcher(string); //~r~|...
      209. Matcher matcher2 = Pattern.compile("\u007E+([\\w[\u003C\u003E&&[\\S]]]{1})\u007E+([\\w[\u003C\u003E&&[\\S]]]{1})\u007E\\z").matcher(string); //~s~r~|...
      210. if(matcher.find() && !matcher2.find()) {
      211. matcher = Pattern.compile("\u007E+([\\w[\u003C\u003E&&[\\S]]]{1})\u007E\\z").matcher(string); //~r~|...
      212. matcherState = 3;
      213. }
      214. else {
      215. matcher = Pattern.compile("\u007E+([\\w[\u003C\u003E&&[\\S]]]{1})\\z").matcher(string); //~r|...
      216. if(matcher.find()) {
      217. matcher = Pattern.compile("\u007E+([\\w[\u003C\u003E&&[\\S]]]{1})\\z").matcher(string); //~r|...
      218. matcherState = 4;
      219. }
      220. else {
      221. matcher = Pattern.compile("\u007E\\z").matcher(string); //~|...
      222. matcherState = 5;
      223. }
      224. }
      225. while(matcher.find()) {
      226. //check whether color code is after this color code
      227. if(currentEndIndex+matcherState <= text.length()) {
      228. currentEndIndex += matcherState;
      229. string = text.substring(0, currentEndIndex);
      230. while(string.trim().length() < string.length()){
      231. currentEndIndex++;
      232. string = text.substring(0, currentEndIndex);
      233. }
      234. matcher = Pattern.compile("\u007E+([\\w[\u003C\u003E&&[\\S]]]{1})\u007E\\z").matcher(string); //~r~| <- update the matcher
      235. if(!matcher.find()) {
      236. currentEndIndex -= 2;
      237. string = text.substring(0, currentEndIndex);
      238. }
      239. else
      240. matcher = Pattern.compile("\u007E+([\\w[\u003C\u003E&&[\\S]]]{1})\u007E\\z").matcher(string); //~r~| <- update the matcher
      241. matcherState = 3;
      242. }
      243. }
      244. string = text.substring(0, currentEndIndex);
      245. while(string.trim().length() < string.length()){
      246. currentEndIndex++;
      247. string = text.substring(0, currentEndIndex);
      248. }
      249. matcher2 = Pattern.compile("\u007E\\z").matcher(string); //~|...
      250. if(matcher2.find()) {
      251. if(currentEndIndex+3 <= text.length())
      252. currentEndIndex += 3;
      253. }
      254. }
      255. return currentEndIndex;
      256. }
      257. private int cutTextOut(int endIndex) {
      258. int currentEndIndex = endIndex;
      259. String string = text.substring(0, currentEndIndex);
      260. while(string.trim().length() < string.length()){
      261. currentEndIndex--;
      262. string = text.substring(0, currentEndIndex);
      263. }
      264. //check whether string contains color codes
      265. Matcher matcher = Pattern.compile("\u007E+([\\w[\u003C\u003E&&[\\S]]]{1})\u007E").matcher(text); //~r~|
      266. if(matcher.find()) {
      267. //match ~
      268. int matcherState = 0;
      269. matcher = Pattern.compile("\u007E+([\\w[\u003C\u003E&&[\\S]]]{1})\u007E\\z").matcher(string); //~r~|...
      270. if(matcher.find()) {
      271. matcher = Pattern.compile("\u007E+([\\w[\u003C\u003E&&[\\S]]]{1})\u007E\\z").matcher(string); //~r~|...
      272. matcherState = 3;
      273. }
      274. else {
      275. matcher = Pattern.compile("\u007E+([\\w[\u003C\u003E&&[\\S]]]{1})\\z").matcher(string); //~r|...
      276. String string2 = text.substring(0, currentEndIndex+1);
      277. Matcher matcher2 = Pattern.compile("\u007E+([\\w[\u003C\u003E&&[\\S]]]{1})\u007E\\z").matcher(string2); //~r~|...
      278. if(matcher.find() && matcher2.find()) {
      279. matcher = Pattern.compile("\u007E+([\\w[\u003C\u003E&&[\\S]]]{1})\\z").matcher(string); //~r|...
      280. matcherState = 2;
      281. }
      282. else {
      283. matcher = Pattern.compile("\u007E\\z").matcher(string); //~|...
      284. matcherState = 1;
      285. }
      286. }
      287. while(matcher.find()) {
      288. //check whether color code is before this color code
      289. if(currentEndIndex-matcherState > 0) {
      290. currentEndIndex -= matcherState;
      291. string = text.substring(0, currentEndIndex);
      292. while(string.trim().length() < string.length()){
      293. currentEndIndex--;
      294. string = text.substring(0, currentEndIndex);
      295. }
      296. matcher = Pattern.compile("\u007E+([\\w[\u003C\u003E&&[\\S]]]{1})\u007E\\z").matcher(string); //~r~| <- update the matcher
      297. matcherState = 3;
      298. }
      299. }
      300. string = text.substring(0, currentEndIndex);
      301. while(string.trim().length() < string.length()){
      302. currentEndIndex--;
      303. string = text.substring(0, currentEndIndex);
      304. }
      305. }
      306. return currentEndIndex;
      307. }
      308. private int getTextLength() {
      309. int length = text.replace(" ", "").length();
      310. Matcher matcher = Pattern.compile("\u007E+([\\w[\u003C\u003E&&[\\S]]]{1})\u007E").matcher(text); //~r~
      311. while(matcher.find()) {
      312. length -= 3;
      313. }
      314. return length;
      315. }
      316. }
      Alles anzeigen


      Java-Quellcode: Chat.java

      1. /**
      2. *
      3. */
      4. package me.alf21.util.chat;
      5. import java.util.ArrayList;
      6. import java.util.HashMap;
      7. import java.util.Map;
      8. import net.gtaun.shoebill.object.Destroyable;
      9. import net.gtaun.shoebill.object.Player;
      10. import net.gtaun.shoebill.object.Timer;
      11. /**
      12. * @author Alf21
      13. */
      14. public class Chat implements Destroyable {
      15. private Timer timer;
      16. public static final int updateDelay = 50;
      17. public static final int displayTime = 5000;
      18. public static final int slideTime = 500;
      19. private ArrayList<ChatMessage> chatMessages;
      20. private ChatMessage currentChatMessage;
      21. private Map<Player, String> queue;
      22. public Chat() {
      23. reset();
      24. initializeTimer();
      25. }
      26. //TODO Chat right bottom Corner, with new Infos sliding down if new message received
      27. // 5 Slots, 6 available Textdraws
      28. public void sendMessage(Player player, String text) {
      29. if(chatMessages != null && chatMessages.size() > 5) {
      30. //slide getSlot > 5 out
      31. }
      32. queue.put(player, text);
      33. if(!timer.isRunning())
      34. timer.start();
      35. }
      36. private void reset() {
      37. if(chatMessages != null && !chatMessages.isEmpty()) {
      38. chatMessages.stream().forEach((chatMessage) -> {
      39. chatMessage.hide();
      40. chatMessage.destroy();
      41. });
      42. }
      43. chatMessages = new ArrayList<ChatMessage>();
      44. queue = new HashMap<Player, String>();
      45. currentChatMessage = null;
      46. }
      47. private void initializeTimer() {
      48. timer = Timer.create(updateDelay, (factualInterval) -> {
      49. Long systemTime = System.currentTimeMillis();
      50. ArrayList<ChatMessage> removeMessages = new ArrayList<ChatMessage>(); //TODO Solution with Iterator
      51. if(chatMessages != null && !chatMessages.isEmpty()) {
      52. chatMessages.stream().forEach((chatMessage) -> {
      53. if(systemTime-chatMessage.getTime() >= slideTime) {
      54. if(currentChatMessage != null) {
      55. currentChatMessage = null;
      56. }
      57. }
      58. if(systemTime-chatMessage.getTime() >= displayTime+2*slideTime) {
      59. removeMessages.add(chatMessage);
      60. }
      61. else if(systemTime-chatMessage.getTime() >= displayTime+slideTime) {
      62. if(!chatMessage.isSliding()) {
      63. chatMessage.slideOut();
      64. }
      65. }
      66. });
      67. removeMessages.stream().forEach((removeMessage) -> {
      68. removeChatMessage(removeMessage);
      69. });
      70. removeMessages.clear();
      71. }
      72. if(currentChatMessage == null && !queue.isEmpty()) {
      73. Map.Entry<Player,String> entry = queue.entrySet().iterator().next();
      74. Player player = entry.getKey();
      75. currentChatMessage = new ChatMessage(player, entry.getValue(), 0);
      76. queue.remove(player);
      77. chatMessages.add(currentChatMessage);
      78. }
      79. if(currentChatMessage != null) {
      80. if(chatMessages.size() > 1) {
      81. chatMessages.stream().forEach((chatMessage) -> {
      82. if(chatMessage != currentChatMessage)
      83. chatMessage.moveUp();
      84. });
      85. }
      86. if(!currentChatMessage.isSliding()) {
      87. currentChatMessage.slideIn();
      88. }
      89. }
      90. if(chatMessages != null && chatMessages.isEmpty()) {
      91. timer.stop();
      92. reset();
      93. }
      94. });
      95. }
      96. private void removeChatMessage(ChatMessage chatMessage) {
      97. if(chatMessages.contains(chatMessage)) {
      98. chatMessages.remove(chatMessage);
      99. }
      100. chatMessage.destroy();
      101. }
      102. /* (non-Javadoc)
      103. * @see net.gtaun.shoebill.object.Destroyable#destroy()
      104. */
      105. @Override
      106. public void destroy() {
      107. if(timer != null) {
      108. if(timer.isRunning())
      109. timer.stop();
      110. timer.destroy();
      111. timer = null;
      112. }
      113. if(currentChatMessage != null) {
      114. currentChatMessage.destroy();
      115. currentChatMessage = null;
      116. }
      117. if(chatMessages != null) {
      118. chatMessages.stream().forEach((chatMessage) -> chatMessage.destroy());
      119. chatMessages = null;
      120. }
      121. if(queue != null) {
      122. queue = null;
      123. }
      124. }
      125. /* (non-Javadoc)
      126. * @see net.gtaun.shoebill.object.Destroyable#isDestroyed()
      127. */
      128. @Override
      129. public boolean isDestroyed() {
      130. if(timer != null || currentChatMessage != null || chatMessages != null || queue != null)
      131. return false;
      132. return true;
      133. }
      134. //TODO: add background box and increase if new message and resize on message destroy
      135. }
      Alles anzeigen


      Nutzung:
      In einem Plugin bei onEnable() oder im Gamemode bei onInit() den Chat initialisieren, indem ihr die Referenz speichert (Chat chat = new Chat()) und bei onDisable() den Chat mit chat.destroy(); zerstören und dann z.B. unter Commands

      Java-Quellcode

      1. @Command
      2. @CommandHelp("/chatme")
      3. public boolean chatme(Player player, String msg) {
      4. EventSystem.getInstance().getChat().sendMessage(player, msg);
      5. return true;
      6. }

      Ich würde mich über ein Feedback oder Ideen freuen!

      Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von Alf21 ()