Fix inventory + added our own chat parser

This commit is contained in:
Felix Cravic 2020-07-01 01:17:58 +02:00
parent 151dedca99
commit f873f495ea
10 changed files with 104 additions and 59 deletions

View File

@ -63,12 +63,8 @@ dependencies {
// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl
api group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.13.2'
api 'org.mongodb.morphia:morphia:1.3.2'
api 'org.mongodb:mongo-java-driver:3.12.5'
api 'net.kyori:text-serializer-legacy:3.0.3'
api 'net.kyori:text-serializer-gson:3.0.3'
api 'net.kyori:text-serializer-plain:3.0.3'
api 'com.mojang:authlib:1.5.21'
api 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'

View File

@ -6,7 +6,8 @@ import net.minestom.server.MinecraftServer;
import net.minestom.server.attribute.Attribute;
import net.minestom.server.benchmark.BenchmarkManager;
import net.minestom.server.benchmark.ThreadResult;
import net.minestom.server.chat.*;
import net.minestom.server.chat.ChatColor;
import net.minestom.server.chat.ColoredText;
import net.minestom.server.entity.*;
import net.minestom.server.entity.damage.DamageType;
import net.minestom.server.entity.type.EntityZombie;
@ -212,7 +213,6 @@ public class PlayerInit {
player.getInventory().addInventoryCondition((p, slot, clickType, inventoryConditionResult) -> {
player.sendMessage("CLICK PLAYER INVENTORY");
System.out.println("slot player: " + slot);
p.closeInventory();
});
/*Sidebar scoreboard = new Sidebar("Scoreboard Title");
@ -235,11 +235,13 @@ public class PlayerInit {
ItemStack item = new ItemStack(Material.STONE_SWORD, (byte) 1);
item.setDisplayName(ColoredText.of(ChatColor.BLUE + "Item name"));
item.getLore().add(ColoredText.of(ChatColor.RED + "a lore line"));
item.getLore().add(ColoredText.of(ChatColor.RED + "a lore line " + ChatColor.BLACK + " BLACK"));
item.addItemFlags(ItemFlag.HIDE_ENCHANTS);
item.setEnchantment(Enchantment.SHARPNESS, (short) 50);
player.getInventory().addItemStack(item);
player.getInventory().addItemStack(new ItemStack(Material.STONE, (byte) 127));
//player.setHelmet(new ItemStack(Material.DIAMOND_HELMET, (byte) 1));
inventory.addItemStack(item.clone());
@ -272,22 +274,8 @@ public class PlayerInit {
setBelowNameScoreboard(belowNameScoreboard);
belowNameScoreboard.updateScore(this, 50);*/
ColoredText coloredText1 = ColoredText.of(ChatColor.BLUE, "I am colored")
.append(ChatColor.BLUE, "I am the next")
.appendFormat("I am {#blue}here");
ColoredText coloredText2 =
ColoredText.ofFormat(
"I am {#green}colo{#red}red {#white}{&key.jump} keybind, {@attack.fall} translatable");
RichMessage richMessage1 = RichMessage.of(coloredText1)
.setClickEvent(ChatClickEvent.runCommand("/test"))
.setHoverEvent(ChatHoverEvent.showText("I'm a text"))
.append(coloredText2)
.setHoverEvent(ChatHoverEvent.showText("I'm a second text"));
player.sendMessage(richMessage1);
player.sendLegacyMessage("&aIm &bHere", '&');
player.sendMessage(ColoredText.of("{#ff55ff}test"));
});

View File

@ -1,19 +0,0 @@
package net.minestom.server.chat;
import net.kyori.text.Component;
import net.kyori.text.serializer.gson.GsonComponentSerializer;
import net.kyori.text.serializer.legacy.LegacyComponentSerializer;
public class Chat {
public static final char COLOR_CHAR = (char) 0xA7; // Represent the character '§'
public static Component fromJsonString(String json) {
return GsonComponentSerializer.INSTANCE.deserialize(json);
}
public static String toLegacyText(Component component) {
return LegacyComponentSerializer.legacyLinking().serialize(component);
}
}

View File

@ -123,8 +123,17 @@ public class ChatColor {
return "";
String redH = Integer.toHexString(red);
if (redH.length() == 1)
redH = "0" + redH;
String greenH = Integer.toHexString(green);
if (greenH.length() == 1)
greenH = "0" + greenH;
String blueH = Integer.toHexString(blue);
if (blueH.length() == 1)
blueH = "0" + blueH;
return "{#" + redH + greenH + blueH + "}";
}
}

View File

@ -0,0 +1,75 @@
package net.minestom.server.chat;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
* Class used to convert JSON string to proper chat message representation
*/
public class ChatParser {
public static final char COLOR_CHAR = (char) 0xA7; // Represent the character '§'
/**
* Convert a simple colored message json (text/color) to a {@link ColoredText}
*
* @param json the json containing the text & color
* @return a {@link ColoredText} representing the text
*/
public static ColoredText toColoredText(String json) {
StringBuilder builder = new StringBuilder();
final JsonObject object = JsonParser.parseString(json).getAsJsonObject();
builder.append(parseText(object));
final boolean hasExtra = object.has("extra");
if (hasExtra) {
JsonArray extraArray = object.get("extra").getAsJsonArray();
for (JsonElement element : extraArray) {
JsonObject extraObject = element.getAsJsonObject();
builder.append(parseText(extraObject));
}
}
return ColoredText.of(builder.toString());
}
/**
* Get the format representing of a single text component (text + color key)
*
* @param textObject the text component to parse
* @return the colored text format of the text component
*/
private static String parseText(JsonObject textObject) {
final boolean hasText = textObject.has("text");
if (!hasText)
return "";
final boolean hasColor = textObject.has("color");
StringBuilder builder = new StringBuilder();
// Add color
if (hasColor) {
String colorString = textObject.get("color").getAsString();
if (colorString.startsWith("#")) {
// RGB format
builder.append("{" + colorString + "}");
} else {
// Color simple name
ChatColor color = ChatColor.fromName(colorString);
builder.append(color);
}
}
// Add text
String text = textObject.get("text").getAsString();
builder.append(text);
return builder.toString();
}
}

View File

@ -30,7 +30,7 @@ public class SerializableData extends Data {
public Data clone() {
SerializableData data = new SerializableData();
data.data = new ConcurrentHashMap<>(this.data);
data.dataType = new ConcurrentHashMap<>(dataType);
data.dataType = new ConcurrentHashMap<>(this.dataType);
return data;
}

View File

@ -3,7 +3,7 @@ package net.minestom.server.entity;
import net.minestom.server.MinecraftServer;
import net.minestom.server.attribute.Attribute;
import net.minestom.server.bossbar.BossBar;
import net.minestom.server.chat.Chat;
import net.minestom.server.chat.ChatParser;
import net.minestom.server.chat.ColoredText;
import net.minestom.server.chat.RichMessage;
import net.minestom.server.collision.BoundingBox;
@ -643,12 +643,12 @@ public class Player extends LivingEntity implements CommandSender {
}
/**
* Send a legacy message with the default color char {@link Chat#COLOR_CHAR}
* Send a legacy message with the default color char {@link ChatParser#COLOR_CHAR}
*
* @param text the text with the legacy color formatting
*/
public void sendLegacyMessage(String text) {
ColoredText coloredText = ColoredText.ofLegacy(text, Chat.COLOR_CHAR);
ColoredText coloredText = ColoredText.ofLegacy(text, ChatParser.COLOR_CHAR);
sendJsonMessage(coloredText.toString());
}

View File

@ -277,8 +277,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
}
// Refresh slot
update();
//refreshSlot(slot); seems to break things concerning +64 stacks
refreshSlot(slot); // Use #update() if any problem occurs
}
/**

View File

@ -1,7 +1,7 @@
package net.minestom.server.scoreboard;
import net.minestom.server.Viewable;
import net.minestom.server.chat.Chat;
import net.minestom.server.chat.ChatParser;
import net.minestom.server.chat.ColoredText;
import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.server.play.DisplayScoreboardPacket;
@ -208,7 +208,7 @@ public class Sidebar implements Viewable {
}
private void createTeam() {
this.entityName = Chat.COLOR_CHAR + Integer.toHexString(colorName);
this.entityName = ChatParser.COLOR_CHAR + Integer.toHexString(colorName);
this.sidebarTeam = new SidebarTeam(teamName, content, ColoredText.of(""), entityName);
}

View File

@ -1,9 +1,8 @@
package net.minestom.server.utils.item;
import net.kyori.text.Component;
import net.minestom.server.attribute.Attribute;
import net.minestom.server.attribute.AttributeOperation;
import net.minestom.server.chat.Chat;
import net.minestom.server.chat.ChatParser;
import net.minestom.server.chat.ColoredText;
import net.minestom.server.item.Enchantment;
import net.minestom.server.item.ItemStack;
@ -211,10 +210,9 @@ public class NbtReaderUtils {
if (stringName.equals("Name")) {
String jsonDisplayName = reader.readShortSizedString();
Component textObject = Chat.fromJsonString(jsonDisplayName);
String displayName = Chat.toLegacyText(textObject);
ColoredText displayName = ChatParser.toColoredText(jsonDisplayName);
item.setDisplayName(ColoredText.of(displayName));
item.setDisplayName(displayName);
readItemStackDisplayNBT(reader, item);
}
break;
@ -229,10 +227,9 @@ public class NbtReaderUtils {
ArrayList<ColoredText> lore = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
String string = reader.readShortSizedString();
Component textObject = Chat.fromJsonString(string);
String line = Chat.toLegacyText(textObject);
ColoredText text = ChatParser.toColoredText(string);
lore.add(ColoredText.of(line));
lore.add(text);
if (lore.size() == size) {
item.setLore(lore);
}