Skip to content
This repository has been archived by the owner on May 14, 2022. It is now read-only.

Commit

Permalink
支持库 v1.1.0 更新: 包名调整和部分代码调整
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonLake committed Nov 26, 2017
1 parent 9769614 commit 0bf0e2b
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 38 deletions.
19 changes: 5 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.minecraft.moonlake</groupId>
<groupId>com.mcmoonlake</groupId>
<artifactId>MoonLakeChat</artifactId>
<packaging>jar</packaging>
<version>1.0.3</version>
<version>1.1.0</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--<libraries>${project.basedir}/lib</libraries>-->
</properties>

<repositories>
Expand All @@ -32,12 +31,6 @@
<version>1.0</version>
<scope>compile</scope> <!-- 将 Validate 支持库合并到 MoonLakeChat 内 -->
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
<scope>provided</scope> <!-- 运行时依赖, 如果依赖此 MoonLakeChat 需要单独 Guava 支持库 -->
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>Gson</artifactId>
Expand All @@ -60,7 +53,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
Expand All @@ -69,7 +62,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<version>3.0.2</version>
<configuration>
<finalName>${project.artifactId}-${project.version}</finalName>
</configuration>
Expand All @@ -93,7 +86,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -110,7 +103,6 @@
<directory>src/main/resources/</directory>
<filtering>true</filtering>
</resource>
<!-- 这个支持库暂时不需要这些资源打包在一起
<resource>
<directory>${project.basedir}/</directory>
<includes>
Expand All @@ -119,7 +111,6 @@
</includes>
<filtering>true</filtering>
</resource>
-->
</resources>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/


package com.minecraft.moonlake.chat;
package com.mcmoonlake.chat;

/**
* <h1>ChatClickEvent</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
*/


package com.minecraft.moonlake.chat;
package com.mcmoonlake.chat;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
* <h1>ChatColor</h1>
Expand Down Expand Up @@ -119,6 +123,16 @@ public enum ChatColor {

private final char code;
private final boolean format;
private final static char CHAR_COLOR = '§';
private final static String STRING_COLOR = "" + CHAR_COLOR;
private final static Map<Character, ChatColor> CHAR_MAP;
private final static Pattern STRIP_COLOR = Pattern.compile("(?i)" + CHAR_COLOR + "[0-9A-FK-OR]");

static {
CHAR_MAP = new HashMap<>();
for(ChatColor color : values())
CHAR_MAP.put(color.code, color);
}

/**
* 聊天组件颜色构造函数
Expand Down Expand Up @@ -158,6 +172,26 @@ public boolean isFormat() {
return format;
}

@Override
public String toString() {
return STRING_COLOR + code;
}

public static String stripColor(String input) {
return STRIP_COLOR.matcher(input).replaceAll("");
}

public static String translateAlternateColorCodes(char altColorChar, String textToTranslate) {
char[] chars = textToTranslate.toCharArray();
for(int i = 0; i < chars.length; i++) {
if(chars[i] == altColorChar && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(chars[i + 1]) > -1) {
chars[i] = CHAR_COLOR;
chars[i + 1] = Character.toLowerCase(chars[i + 1]);
}
}
return new String(chars);
}

/**
* 从名称返回聊天组件颜色
*
Expand All @@ -167,4 +201,8 @@ public boolean isFormat() {
public static ChatColor fromName(String name) {
return ChatColor.valueOf(name.toUpperCase());
}

public static ChatColor fromCode(Character code) {
return CHAR_MAP.getOrDefault(code, WHITE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/


package com.minecraft.moonlake.chat;
package com.mcmoonlake.chat;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/


package com.minecraft.moonlake.chat;
package com.mcmoonlake.chat;

import com.minecraft.moonlake.validate.Validate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/


package com.minecraft.moonlake.chat;
package com.mcmoonlake.chat;

import com.minecraft.moonlake.validate.Validate;

Expand Down Expand Up @@ -258,7 +258,7 @@ public ChatComponentFancy tooltipTexts(Collection<? extends String> collection)
*/
public ChatComponentFancy tooltipItem(String item) {
Validate.notNull(item, "The item object is null.");
getLast().getStyle().setHoverEvent(new ChatHoverEvent(ChatHoverEvent.Action.SHOW_ITEM, new ChatComponentText(item)));
getLast().getStyle().setHoverEvent(new ChatHoverEvent(ChatHoverEvent.Action.SHOW_ITEM, new ChatSerializer.ChatComponentRaw(item)));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/


package com.minecraft.moonlake.chat;
package com.mcmoonlake.chat;

/**
* <h1>ChatComponentKeybind</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/


package com.minecraft.moonlake.chat;
package com.mcmoonlake.chat;

/**
* <h1>ChatComponentScore</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/


package com.minecraft.moonlake.chat;
package com.mcmoonlake.chat;

import com.minecraft.moonlake.validate.Validate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/


package com.minecraft.moonlake.chat;
package com.mcmoonlake.chat;

import com.minecraft.moonlake.validate.Validate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/


package com.minecraft.moonlake.chat;
package com.mcmoonlake.chat;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/


package com.minecraft.moonlake.chat;
package com.mcmoonlake.chat;

/**
* <h1>ChatHoverEvent</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/


package com.minecraft.moonlake.chat;
package com.mcmoonlake.chat;

import com.google.gson.*;
import com.google.gson.stream.JsonReader;
Expand All @@ -26,6 +26,8 @@
import java.io.StringReader;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* <h1>ChatSerializer</h1>
Expand All @@ -51,6 +53,85 @@ public final class ChatSerializer {
private ChatSerializer() {
}

public static ChatComponent fromRaw(String raw) {
if(raw == null || raw.isEmpty())
return new ChatComponentText("");
return new RawMessage(raw).get();
}

public static ChatComponent fromRawOrNull(String raw) {
return raw == null ? null : fromRaw(raw);
}

private static class RawMessage {

private final static Pattern PATTERN_RAW = Pattern.compile("(§[0-9a-fk-or])", Pattern.CASE_INSENSITIVE);
private ChatComponent currentComponent;
private ChatStyle style;
private int currentIndex;
private final String raw;

private RawMessage(String raw) {
this.currentComponent = new ChatComponentText("");
this.style = new ChatStyle();
this.raw = raw;
if(raw != null) {
Matcher matcher = PATTERN_RAW.matcher(raw);
int groupId;
for(String match; matcher.find(); currentIndex = matcher.end(groupId)) {
groupId = 0;
do {
++groupId;
} while ((match = matcher.group(groupId)) == null);
append(matcher.start(groupId));
switch (groupId) {
case 1:
ChatColor color = ChatColor.fromCode(match.toLowerCase().charAt(1));
if(color == ChatColor.RESET) {
style = new ChatStyle();
} else if(color.isFormat()) {
switch (color) {
case OBFUSCATED:
style.setObfuscated(true); break;
case BOLD:
style.setBold(true); break;
case STRIKETHROUGH:
style.setStrikethrough(true); break;
case UNDERLINE:
style.setUnderlined(true); break;
case ITALIC:
style.setItalic(true); break;
default:
throw new AssertionError("Error chat color format: " + color);
}
} else {
style = new ChatStyle().setColor(color);
}
break;
}
currentIndex = matcher.end(groupId);
}
if(currentIndex < raw.length())
append(raw.length());
}
}

private void append(int index) {
if(index > currentIndex) {
ChatComponent extra = new ChatComponentText(raw.substring(currentIndex, index)).setStyle(style);
currentIndex = index;
style = style.clone();
if(currentComponent == null)
currentComponent = new ChatComponentText("");
currentComponent.append(extra);
}
}

private ChatComponent get() {
return currentComponent == null ? new ChatComponentText("") : currentComponent;
}
}

/**
* 将指定聊天组件转换为源文本内容
*
Expand Down Expand Up @@ -97,7 +178,7 @@ private static void toRaw0(ChatComponent chatComponent, boolean color, StringBui
}

private static void appendColor(StringBuilder builder, ChatColor color) {
builder.append('\u00A7').append(color.getCode());
builder.append(color.toString());
}

/**
Expand Down Expand Up @@ -182,9 +263,7 @@ public ChatStyle deserialize(JsonElement json, Type typeOfT, JsonDeserialization
if(jsonObjectHoverEvent != null) {
ChatHoverEvent.Action action = ChatHoverEvent.Action.fromName(jsonObjectHoverEvent.get("action").getAsString());
JsonElement value = jsonObjectHoverEvent.get("value");
if(value.isJsonPrimitive())
style.hoverEvent = new ChatHoverEvent(action, new ChatComponentText(value.getAsString()));
else
if(action != null && value != null)
style.hoverEvent = new ChatHoverEvent(action, context.deserialize(value, ChatComponent.class));
}
}
Expand Down Expand Up @@ -221,8 +300,8 @@ public JsonElement serialize(ChatStyle src, Type typeOfSrc, JsonSerializationCon
if(src.hoverEvent != null) {
JsonObject jsonObjectHoverEvent = new JsonObject();
jsonObjectHoverEvent.addProperty("action", src.hoverEvent.getAction().toString().toLowerCase());
if(src.hoverEvent.getValue() instanceof ChatComponentText)
jsonObjectHoverEvent.addProperty("value", ((ChatComponentText) src.hoverEvent.getValue()).getText());
if(src.hoverEvent.getValue() instanceof ChatComponentRaw)
jsonObjectHoverEvent.addProperty("value", ((ChatComponentRaw) src.hoverEvent.getValue()).getText());
else
jsonObjectHoverEvent.add("value", context.serialize(src.hoverEvent.getValue()));
jsonObject.add("hoverEvent", jsonObjectHoverEvent);
Expand Down Expand Up @@ -343,4 +422,10 @@ public JsonElement serialize(ChatComponent src, Type typeOfSrc, JsonSerializatio
return jsonObject;
}
}

static class ChatComponentRaw extends ChatComponentText {
ChatComponentRaw(String text) {
super(text);
}
}
}
Loading

0 comments on commit 0bf0e2b

Please sign in to comment.