mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 08:27:43 +01:00
Merge branches 'experimental' and 'master' of https://github.com/Minestom/Minestom
This commit is contained in:
commit
ffe5240a8d
@ -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'
|
||||
|
||||
|
@ -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;
|
||||
@ -215,7 +216,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");
|
||||
@ -238,11 +238,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());
|
||||
@ -275,22 +277,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"));
|
||||
|
||||
});
|
||||
|
||||
|
@ -1,18 +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);
|
||||
}
|
||||
}
|
@ -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 + "}";
|
||||
}
|
||||
}
|
||||
|
75
src/main/java/net/minestom/server/chat/ChatParser.java
Normal file
75
src/main/java/net/minestom/server/chat/ChatParser.java
Normal 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();
|
||||
}
|
||||
}
|
@ -28,9 +28,10 @@ public class SerializableData extends Data {
|
||||
|
||||
@Override
|
||||
public Data clone() {
|
||||
SerializableData cloned = (SerializableData) super.clone();
|
||||
cloned.dataType = new ConcurrentHashMap<>(dataType);
|
||||
return super.clone();
|
||||
SerializableData data = new SerializableData();
|
||||
data.data = new ConcurrentHashMap<>(this.data);
|
||||
data.dataType = new ConcurrentHashMap<>(this.dataType);
|
||||
return data;
|
||||
}
|
||||
|
||||
public byte[] getSerializedData() throws IOException {
|
||||
|
@ -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;
|
||||
@ -652,12 +652,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());
|
||||
}
|
||||
|
||||
|
@ -214,7 +214,7 @@ public final class Chunk implements Viewable {
|
||||
refreshBlockValue(x, y, z, blockId, customBlockId);
|
||||
}
|
||||
|
||||
public Data getData(int x, byte y, int z) {
|
||||
public Data getData(int x, int y, int z) {
|
||||
int index = getBlockIndex(x, y, z);
|
||||
return getData(index);
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -17,13 +17,16 @@ import java.util.UUID;
|
||||
|
||||
public class LoginStartPacket implements ClientPreplayPacket {
|
||||
|
||||
private static final String ALREADY_CONNECTED_JSON =
|
||||
ColoredText.of(ChatColor.RED, "You are already on this server").toString();
|
||||
|
||||
public String username;
|
||||
|
||||
@Override
|
||||
public void process(PlayerConnection connection, ConnectionManager connectionManager) {
|
||||
if (MojangAuth.isUsingMojangAuth()) {
|
||||
if (connectionManager.getPlayer(username) != null) {
|
||||
connection.sendPacket(new LoginDisconnect(ColoredText.of(ChatColor.RED, "You are already on this server").toString()));
|
||||
connection.sendPacket(new LoginDisconnect(ALREADY_CONNECTED_JSON));
|
||||
connection.disconnect();
|
||||
return;
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ public class EntityEquipmentPacket implements ServerPacket {
|
||||
@Override
|
||||
public void write(PacketWriter writer) {
|
||||
writer.writeVarInt(entityId);
|
||||
// TODO multiple equipments
|
||||
writer.writeByte((byte) slot.ordinal());
|
||||
writer.writeItemStack(itemStack);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
@ -210,10 +209,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;
|
||||
@ -228,10 +226,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);
|
||||
}
|
||||
|
@ -96,4 +96,7 @@ public class NbtWriter {
|
||||
packet.writeShortSizedString(name);
|
||||
}
|
||||
|
||||
public PacketWriter getPacketWriter() {
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user