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

Commit

Permalink
[Add] 聊天组件支持库基础内容完成
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonLake committed Jun 15, 2017
1 parent 26a4c71 commit 1230a08
Show file tree
Hide file tree
Showing 15 changed files with 1,546 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.minecraft.moonlake</groupId>
<artifactId>Validate</artifactId>
<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 Down
78 changes: 78 additions & 0 deletions src/main/java/com/minecraft/moonlake/chat/ChatClickEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (C) 2017 The MoonLake Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


package com.minecraft.moonlake.chat;

public class ChatClickEvent {

private final Action action;
private final String value;

public ChatClickEvent(Action action, String value) {
this.action = action;
this.value = value;
}

public Action getAction() {
return action;
}

public String getValue() {
return value;
}

@Override
public boolean equals(Object obj) {
if(obj == this)
return true;
if(obj instanceof ChatClickEvent) {
ChatClickEvent other = (ChatClickEvent) obj;
return action == other.action && value != null ? value.equals(other.value) : other.value == null;
}
return false;
}

@Override
public int hashCode() {
int result = action != null ? action.hashCode() : 0;
result = 31 * result + (value != null ? value.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "ChatClickEvent{" +
"action=" + action +
", value='" + value + '\'' +
'}';
}

public enum Action {

OPEN_URL,
OPEN_FILE,
RUN_COMMAND,
SUGGEST_COMMAND,
CHANGE_PAGE,
;

public static Action fromName(String name) {
return Action.valueOf(name.toUpperCase());
}
}
}
70 changes: 70 additions & 0 deletions src/main/java/com/minecraft/moonlake/chat/ChatColor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (C) 2017 The MoonLake Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


package com.minecraft.moonlake.chat;

public enum ChatColor {

BLACK('0'),
DARK_BLUE('1'),
DARK_GREEN('2'),
DARK_AQUA('3'),
DARK_RED('4'),
DARK_PURPLE('5'),
GOLD('6'),
GRAY('7'),
DARK_GRAY('8'),
BLUE('9'),
GREEN('a'),
AQUA('b'),
RED('c'),
LIGHT_PURPLE('d'),
YELLOW('e'),
WHITE('f'),
OBFUSCATED('k', true),
BOLD('l', true),
STRIKETHROUGH('m', true),
UNDERLINE('n', true),
ITALIC('o', true),
RESET('r'),
;

private final char code;
private final boolean format;

ChatColor(char code) {
this(code, false);
}

ChatColor(char code, boolean format) {
this.code = code;
this.format = format;
}

public char getCode() {
return code;
}

public boolean isFormat() {
return format;
}

public static ChatColor fromName(String name) {
return ChatColor.valueOf(name.toUpperCase());
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/minecraft/moonlake/chat/ChatComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2017 The MoonLake Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


package com.minecraft.moonlake.chat;

import java.util.List;

public interface ChatComponent {

ChatStyle getStyle();

ChatComponent setStyle(ChatStyle style);

List<ChatComponent> getExtras();

int getExtraSize();

ChatComponent append(String text);

ChatComponent append(ChatComponent extra);

String toJson();

String toRaw();

String toRaw(boolean color);
}
118 changes: 118 additions & 0 deletions src/main/java/com/minecraft/moonlake/chat/ChatComponentBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright (C) 2017 The MoonLake Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


package com.minecraft.moonlake.chat;

import com.minecraft.moonlake.validate.Validate;

import java.util.ArrayList;
import java.util.List;

public abstract class ChatComponentBase implements ChatComponent {

private ChatStyle style;
private List<ChatComponent> extras;

ChatComponentBase() {
this.extras = new ArrayList<>();
}

@Override
public ChatComponent setStyle(ChatStyle style) {
Validate.notNull(style, "The chat style object is null.");
this.style = style;
for(ChatComponent component : extras)
component.getStyle().setParent(getStyle());
return this;
}

@Override
public ChatStyle getStyle() {
if(style == null) {
this.style = new ChatStyle();
for(ChatComponent component : extras)
component.getStyle().setParent(style);
}
return style;
}

@Override
public final List<ChatComponent> getExtras() {
return extras;
}

@Override
public final int getExtraSize() {
return extras.size();
}

@Override
public ChatComponent append(String text) {
Validate.notNull(text, "The text object is null.");
return append(new ChatComponentText(text));
}

@Override
public ChatComponent append(ChatComponent extra) {
Validate.notNull(extra, "The extra component object is null.");
extra.getStyle().setParent(getStyle());
extras.add(extra);
return this;
}

@Override
public String toJson() {
return ChatSerializer.toJson(this);
}

@Override
public String toRaw() {
return ChatSerializer.toRaw(this);
}

@Override
public String toRaw(boolean color) {
return ChatSerializer.toRaw(this, color);
}

@Override
public boolean equals(Object obj) {
if(obj == this)
return true;
if(obj instanceof ChatComponentBase) {
ChatComponentBase other = (ChatComponentBase) obj;
return getStyle().equals(other.getStyle()) && extras.equals(other.extras);
}
return false;
}

@Override
public int hashCode() {
int result = style != null ? style.hashCode() : 0;
result = 31 * result + (extras != null ? extras.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "ChatComponentBase{" +
"style=" + style +
", extras=" + extras +
'}';
}
}
Loading

0 comments on commit 1230a08

Please sign in to comment.