отказ от старого ChatStyle
This commit is contained in:
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-30
|
||||
*/
|
||||
package mc.core.chat;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public enum ChatStyle {
|
||||
BLACK ('0'),
|
||||
DARK_BLUE ('1'),
|
||||
DARK_GREEN('2'),
|
||||
DARK_CYAN ('3'),
|
||||
DARK_RED ('4'),
|
||||
PURPLE ('5'),
|
||||
GOLD ('6'),
|
||||
GRAY ('7'),
|
||||
DARK_GRAY ('8'),
|
||||
BLUE ('9'),
|
||||
GREEN ('a'),
|
||||
CYAN ('b'),
|
||||
RED ('c'),
|
||||
PINK ('d'),
|
||||
YELLOW('e'),
|
||||
WHITE ('f');
|
||||
|
||||
public static final char SPECIAL_CHAR = '\u00a7'; // §
|
||||
private static final String codes = "0123456789aAbBcCdDeEfF";
|
||||
private static final Pattern EXCAPE_PATTERN = Pattern.compile(SPECIAL_CHAR + "[0-9a-f]", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
public static String format(char colorChar, String message) {
|
||||
char[] chars = message.toCharArray();
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
if (chars[i] == colorChar && codes.indexOf(chars[i+1]) > -1) {
|
||||
chars[i] = SPECIAL_CHAR;
|
||||
chars[i+1] = Character.toLowerCase(chars[i+1]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return String.valueOf(chars);
|
||||
}
|
||||
|
||||
public static String escapeStyle(String message) {
|
||||
return EXCAPE_PATTERN.matcher(message).replaceAll("");
|
||||
}
|
||||
|
||||
private char[] toString;
|
||||
|
||||
ChatStyle(char ch) {
|
||||
toString = new char[]{SPECIAL_CHAR, ch };
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.valueOf(toString);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ package mc.core.chat;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.player.Player;
|
||||
import mc.core.text.Text;
|
||||
import mc.core.text.TextColor;
|
||||
import org.slf4j.Marker;
|
||||
import org.slf4j.helpers.BasicMarkerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -20,7 +22,6 @@ import java.util.Map;
|
||||
@Slf4j
|
||||
public class CommanderChatProcessor extends SimpleChatProcessor {
|
||||
private static final Marker COMMAND_MARKER = new BasicMarkerFactory().getMarker("Command");
|
||||
private static final String UNKNOW_COMMAND_MSG = ChatStyle.RED + "Unknown command \"" + ChatStyle.WHITE + "%s" + ChatStyle.RED + "\"";
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
private Map<String, CommandExecutor> commands = new HashMap<>();
|
||||
@@ -72,7 +73,11 @@ public class CommanderChatProcessor extends SimpleChatProcessor {
|
||||
String[] args = message.substring(idx).split(" ");
|
||||
commands.get(command).execute(player, args);
|
||||
} else {
|
||||
player.getChannel().sendChatMessage(String.format(UNKNOW_COMMAND_MSG, command));
|
||||
Text msg = Text.builder(TextColor.RED, "Unknown command \"")
|
||||
.append(Text.of(TextColor.WHITE, command))
|
||||
.append(Text.of("\""))
|
||||
.build();
|
||||
player.getChannel().sendChatMessage(msg);
|
||||
}
|
||||
} else {
|
||||
super.process(player, message);
|
||||
|
||||
@@ -7,6 +7,8 @@ package mc.core.chat;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.player.Player;
|
||||
import mc.core.player.PlayerManager;
|
||||
import mc.core.text.Text;
|
||||
import mc.core.text.TextColor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
@Slf4j
|
||||
@@ -16,11 +18,12 @@ public class SimpleChatProcessor extends ChatProcessor {
|
||||
|
||||
@Override
|
||||
public void process(Player player, String message) {
|
||||
log.info(CHAT_MARKER, "<{}> {}", player.getName(), ChatStyle.escapeStyle(message));
|
||||
log.info(CHAT_MARKER, "<{}> {}", player.getName(), message);
|
||||
playerManager.getBroadcastChannel().sendChatMessage(
|
||||
ChatStyle.GOLD + player.getName()
|
||||
+ ChatStyle.GRAY + ": "
|
||||
+ ChatStyle.WHITE + message
|
||||
Text.builder(TextColor.GOLD, player.getName())
|
||||
.append(Text.of(TextColor.GRAY, ": "))
|
||||
.append(Text.of(TextColor.WHITE, message))
|
||||
.build()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ package mc.core.network;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import mc.core.player.Player;
|
||||
import mc.core.text.Text;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -24,8 +25,8 @@ public class BroadcastNetChannel implements NetChannel {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendChatMessage(final String message) {
|
||||
playerStream.forEach(player -> player.getChannel().sendChatMessage(message));
|
||||
public void sendChatMessage(final Text text) {
|
||||
playerStream.forEach(player -> player.getChannel().sendChatMessage(text));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,10 +4,12 @@
|
||||
*/
|
||||
package mc.core.network;
|
||||
|
||||
import mc.core.text.Text;
|
||||
|
||||
public interface NetChannel {
|
||||
void sendKeepAlive();
|
||||
void sendTimeUpdate(long value);
|
||||
void sendChatMessage(String message);
|
||||
void sendChatMessage(Text text);
|
||||
|
||||
void writeAndFlush(SCPacket pkt);
|
||||
void write(SCPacket pkt);
|
||||
|
||||
@@ -5,14 +5,19 @@
|
||||
package mc.core.text;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
@Getter
|
||||
public class Text {
|
||||
@Setter
|
||||
private String string;
|
||||
@Setter
|
||||
private TextColor color;
|
||||
@Setter
|
||||
private TextStyle style;
|
||||
private List<Text> childs;
|
||||
|
||||
@@ -26,15 +31,27 @@ public class Text {
|
||||
return of(string, null, null);
|
||||
}
|
||||
|
||||
public static Text of(String string, TextColor color) {
|
||||
return of(string, color, null);
|
||||
}
|
||||
|
||||
public static Text of(String string, TextColor color, TextStyle style) {
|
||||
public static Text of(Object... objects) {
|
||||
Text text = new Text();
|
||||
text.string = string;
|
||||
text.color = color;
|
||||
text.style = style;
|
||||
|
||||
for(Object obj : objects) {
|
||||
if (obj instanceof String) {
|
||||
if (text.string == null) {
|
||||
text.string = (String) obj;
|
||||
} else {
|
||||
text.string = text.string.concat((String) obj);
|
||||
}
|
||||
} else if (obj instanceof TextStyle) {
|
||||
if (text.style == null) {
|
||||
text.style = (TextStyle) obj;
|
||||
} else {
|
||||
text.style.concat((TextStyle) obj);
|
||||
}
|
||||
} else if (obj instanceof TextColor) {
|
||||
text.color = (TextColor) obj;
|
||||
}
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
@@ -46,46 +63,73 @@ public class Text {
|
||||
return new Builder(string);
|
||||
}
|
||||
|
||||
public static Builder builder(Object... objects) {
|
||||
return new Builder(objects);
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private Text currentText;
|
||||
private Text text;
|
||||
|
||||
private Builder() {
|
||||
this.currentText = new Text();
|
||||
this.text = new Text();
|
||||
}
|
||||
|
||||
private Builder(String string) {
|
||||
this.currentText = new Text();
|
||||
this.currentText.string = string;
|
||||
this.text = new Text();
|
||||
this.text.string = string;
|
||||
}
|
||||
|
||||
private Builder(Text text) {
|
||||
this.currentText = text;
|
||||
private Builder(Object... objects) {
|
||||
this.text = Text.of(objects);
|
||||
}
|
||||
|
||||
public Builder color(TextColor color) {
|
||||
this.currentText.color = color;
|
||||
this.text.color = color;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder style(TextStyle style) {
|
||||
if (this.currentText.style == null) {
|
||||
this.currentText.style = new TextStyle(null,null,null,null,null);
|
||||
if (this.text.style == null) {
|
||||
this.text.style = new TextStyle(null,null,null,null,null);
|
||||
}
|
||||
this.currentText.style.concat(style);
|
||||
this.text.style.concat(style);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder append(Text text) {
|
||||
if (this.currentText.childs == null) {
|
||||
this.currentText.childs = new ArrayList<>();
|
||||
if (this.text.childs == null) {
|
||||
this.text.childs = new ArrayList<>();
|
||||
}
|
||||
|
||||
this.currentText.childs.add(text);
|
||||
this.text.childs.add(text);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Text build() {
|
||||
return this.currentText;
|
||||
return this.text;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
boolean result = string.isEmpty();
|
||||
|
||||
if (childs != null) {
|
||||
for(Text child : childs) {
|
||||
result = child.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public String toPlainText() {
|
||||
if (childs != null) {
|
||||
final StringJoiner sj = new StringJoiner("");
|
||||
sj.add(string);
|
||||
childs.forEach(child -> sj.add(child.toPlainText()));
|
||||
return sj.toString();
|
||||
} else {
|
||||
return string;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,11 @@
|
||||
package mc.commands;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.chat.ChatStyle;
|
||||
import mc.core.chat.CommandExecutor;
|
||||
import mc.core.chat.CommanderChatProcessor;
|
||||
import mc.core.player.Player;
|
||||
import mc.core.text.Text;
|
||||
import mc.core.text.TextColor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
@@ -46,17 +47,23 @@ public class HelpCommand implements CommandExecutor {
|
||||
commanderChatProcessor = applicationContext.getBean(CommanderChatProcessor.class);
|
||||
if (commanderChatProcessor == null) {
|
||||
log.error("Error get bean of type \"CommanderChatProcessor\". WTF?!");
|
||||
sender.getChannel().sendChatMessage(ChatStyle.RED + "!!-Server error-!!");
|
||||
sender.getChannel().sendChatMessage(Text.of(TextColor.RED, "!!-Server error-!!"));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
final String messageFormat = ChatStyle.RED + "%s " + ChatStyle.GRAY + "- " + ChatStyle.WHITE + "%s";
|
||||
Text commandNameText = Text.of(TextColor.RED);
|
||||
Text descriptionText = Text.of(TextColor.WHITE);
|
||||
Text messageText = Text.builder()
|
||||
.append(commandNameText)
|
||||
.append(Text.of(TextColor.GRAY, " - "))
|
||||
.append(descriptionText)
|
||||
.build();
|
||||
|
||||
commanderChatProcessor.getAllCommands().forEach(commandExecutor -> {
|
||||
sender.getChannel().sendChatMessage(String.format(messageFormat,
|
||||
commandExecutor.getUsage().orElse(commandExecutor.getName()),
|
||||
commandExecutor.getDescription()
|
||||
));
|
||||
commandNameText.setString(commandExecutor.getUsage().orElse(commandExecutor.getName()));
|
||||
descriptionText.setString(commandExecutor.getDescription());
|
||||
sender.getChannel().sendChatMessage(messageText);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,10 +4,11 @@
|
||||
*/
|
||||
package mc.commands;
|
||||
|
||||
import mc.core.chat.ChatStyle;
|
||||
import mc.core.chat.CommandExecutor;
|
||||
import mc.core.player.Player;
|
||||
import mc.core.player.PlayerManager;
|
||||
import mc.core.text.Text;
|
||||
import mc.core.text.TextColor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.Optional;
|
||||
@@ -42,8 +43,9 @@ public class ListCommand implements CommandExecutor {
|
||||
StringJoiner sj = new StringJoiner(", ");
|
||||
playerManager.getPlayers().forEach(pl -> sj.add(pl.getName()));
|
||||
|
||||
sender.getChannel().sendChatMessage(
|
||||
ChatStyle.GREEN + "Online(" + playerManager.getCountOnlinePlayers() + "): "
|
||||
+ ChatStyle.DARK_GREEN + sj.toString());
|
||||
Text message = Text.builder(TextColor.GREEN, "Online(" + playerManager.getCountOnlinePlayers() + "): ")
|
||||
.append(Text.of(TextColor.DARK_GREEN, sj.toString()))
|
||||
.build();
|
||||
sender.getChannel().sendChatMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user