Skip encoding not-implemented item data components

This commit is contained in:
LOOHP 2024-05-27 00:17:28 +01:00
parent c260a9cc1f
commit af380214ef
7 changed files with 68 additions and 44 deletions

View File

@ -53,6 +53,8 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
@ -72,23 +74,23 @@ public class Console implements CommandSender {
protected final static String ERROR_RED = "\u001B[31;1m"; protected final static String ERROR_RED = "\u001B[31;1m";
protected final static String RESET_COLOR = "\u001B[0m"; protected final static String RESET_COLOR = "\u001B[0m";
private Terminal terminal; private final Terminal terminal;
private LineReader tabReader; private final LineReader tabReader;
private ConsoleReader reader; private final ConsoleReader reader;
private InputStream in; private final InputStream in;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private PrintStream out; private final PrintStream out;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private PrintStream err; private final PrintStream err;
protected PrintStream logs; protected final PrintStream logs;
public Console(InputStream in, PrintStream out, PrintStream err) throws IOException { public Console(InputStream in, PrintStream out, PrintStream err) throws IOException {
String fileName = new SimpleDateFormat("yyyy'-'MM'-'dd'_'HH'-'mm'-'ss'_'zzz'.log'").format(new Date()); String fileName = new SimpleDateFormat("yyyy'-'MM'-'dd'_'HH'-'mm'-'ss'_'zzz'.log'").format(new Date());
File dir = new File("logs"); File dir = new File("logs");
dir.mkdirs(); dir.mkdirs();
File logs = new File(dir, fileName); File logs = new File(dir, fileName);
this.logs = new PrintStream(logs); this.logs = new PrintStream(Files.newOutputStream(logs.toPath()), true, StandardCharsets.UTF_8.toString());
if (in != null) { if (in != null) {
System.setIn(in); System.setIn(in);
@ -256,7 +258,7 @@ public class Console implements CommandSender {
while (true) { while (true) {
try { try {
String command = tabReader.readLine(PROMPT).trim(); String command = tabReader.readLine(PROMPT).trim();
if (command.length() > 0) { if (!command.isEmpty()) {
String[] input = CustomStringUtils.splitStringToArgs(command); String[] input = CustomStringUtils.splitStringToArgs(command);
new Thread(() -> Limbo.getInstance().dispatchCommand(this, input)).start(); new Thread(() -> Limbo.getInstance().dispatchCommand(this, input)).start();
} }
@ -317,8 +319,8 @@ public class Console implements CommandSender {
public static class ConsoleOutputStream extends PrintStream { public static class ConsoleOutputStream extends PrintStream {
private PrintStream logs; private final PrintStream logs;
private Console console; private final Console console;
public ConsoleOutputStream(Console console, OutputStream out, PrintStream logs) { public ConsoleOutputStream(Console console, OutputStream out, PrintStream logs) {
super(out); super(out);
@ -326,7 +328,6 @@ public class Console implements CommandSender {
this.console = console; this.console = console;
} }
@SuppressWarnings("resource")
@Override @Override
public PrintStream printf(Locale l, String format, Object... args) { public PrintStream printf(Locale l, String format, Object... args) {
console.stashLine(); console.stashLine();
@ -338,7 +339,6 @@ public class Console implements CommandSender {
return stream; return stream;
} }
@SuppressWarnings("resource")
@Override @Override
public PrintStream printf(String format, Object... args) { public PrintStream printf(String format, Object... args) {
console.stashLine(); console.stashLine();
@ -453,8 +453,8 @@ public class Console implements CommandSender {
public static class ConsoleErrorStream extends PrintStream { public static class ConsoleErrorStream extends PrintStream {
private PrintStream logs; private final PrintStream logs;
private Console console; private final Console console;
public ConsoleErrorStream(Console console, OutputStream out, PrintStream logs) { public ConsoleErrorStream(Console console, OutputStream out, PrintStream logs) {
super(out); super(out);

View File

@ -19,7 +19,7 @@
package com.loohp.limbo.inventory; package com.loohp.limbo.inventory;
import com.loohp.limbo.registry.DataComponentTypes; import com.loohp.limbo.registry.DataComponentType;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.querz.nbt.tag.Tag; import net.querz.nbt.tag.Tag;
@ -81,11 +81,11 @@ public class ItemStack implements Cloneable {
return new ItemStack(material, amount, components); return new ItemStack(material, amount, components);
} }
public <T> T component(DataComponentTypes<T> type) { public <T> T component(DataComponentType<T> type) {
return type.getCodec().decode(components.get(type.getKey())); return type.getCodec().decode(components.get(type.getKey()));
} }
public <T> ItemStack component(DataComponentTypes<T> type, T value) { public <T> ItemStack component(DataComponentType<T> type, T value) {
Map<Key, Tag<?>> components = components(); Map<Key, Tag<?>> components = components();
components.put(type.getKey(), type.getCodec().encode(value)); components.put(type.getKey(), type.getCodec().encode(value));
return components(components); return components(components);
@ -96,7 +96,7 @@ public class ItemStack implements Cloneable {
return null; return null;
} }
try { try {
return component(DataComponentTypes.CUSTOM_NAME); return component(DataComponentType.CUSTOM_NAME);
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }
@ -106,7 +106,7 @@ public class ItemStack implements Cloneable {
if (type().equals(AIR.type())) { if (type().equals(AIR.type())) {
return this; return this;
} }
return component(DataComponentTypes.CUSTOM_NAME, component); return component(DataComponentType.CUSTOM_NAME, component);
} }
public int getMaxStackSize() { public int getMaxStackSize() {

View File

@ -27,8 +27,8 @@ import java.io.IOException;
public class PacketPlayInSetCreativeSlot extends PacketIn { public class PacketPlayInSetCreativeSlot extends PacketIn {
private int slotNumber; private final int slotNumber;
private ItemStack itemStack; private final ItemStack itemStack;
public PacketPlayInSetCreativeSlot(int slotNumber, ItemStack itemStack) { public PacketPlayInSetCreativeSlot(int slotNumber, ItemStack itemStack) {
this.slotNumber = slotNumber; this.slotNumber = slotNumber;

View File

@ -31,13 +31,13 @@ import java.util.Map;
public class PacketPlayInWindowClick extends PacketIn { public class PacketPlayInWindowClick extends PacketIn {
private int containerId; private final int containerId;
private int stateId; private final int stateId;
private int slotNum; private final int slotNum;
private int buttonNum; private final int buttonNum;
private InventoryClickType clickType; private final InventoryClickType clickType;
private Map<Integer, ItemStack> changedSlots; private final Map<Integer, ItemStack> changedSlots;
private ItemStack carriedItem; private final ItemStack carriedItem;
public PacketPlayInWindowClick(int containerId, int stateId, int slotNum, int buttonNum, InventoryClickType clickType, Map<Integer, ItemStack> changedSlots, ItemStack carriedItem) { public PacketPlayInWindowClick(int containerId, int stateId, int slotNum, int buttonNum, InventoryClickType clickType, Map<Integer, ItemStack> changedSlots, ItemStack carriedItem) {
this.containerId = containerId; this.containerId = containerId;

View File

@ -28,10 +28,10 @@ import java.io.IOException;
public class PacketPlayOutSetSlot extends PacketOut { public class PacketPlayOutSetSlot extends PacketOut {
private int containerId; private final int containerId;
private int stateId; private final int stateId;
private int slot; private final int slot;
private ItemStack itemStack; private final ItemStack itemStack;
public PacketPlayOutSetSlot(int containerId, int stateId, int slot, ItemStack itemStack) { public PacketPlayOutSetSlot(int containerId, int stateId, int slot, ItemStack itemStack) {
this.containerId = containerId; this.containerId = containerId;

View File

@ -26,27 +26,40 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.querz.nbt.tag.Tag; import net.querz.nbt.tag.Tag;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
public class DataComponentTypes<T> { public class DataComponentType<T> {
public static final DataComponentTypes<Component> CUSTOM_NAME = new DataComponentTypes<>("custom_name", new DataComponentCodec<>(component -> { private static final Map<Key, DataComponentType<?>> REGISTERED_TYPES = new HashMap<>();
public static final DataComponentType<Component> CUSTOM_NAME = register(new DataComponentType<>("custom_name", new DataComponentCodec<>(component -> {
JsonElement element = GsonComponentSerializer.gson().serializeToTree(component); JsonElement element = GsonComponentSerializer.gson().serializeToTree(component);
return NbtComponentSerializer.jsonComponentToTag(element); return NbtComponentSerializer.jsonComponentToTag(element);
}, tag -> { }, tag -> {
JsonElement element = NbtComponentSerializer.tagComponentToJson(tag); JsonElement element = NbtComponentSerializer.tagComponentToJson(tag);
return GsonComponentSerializer.gson().deserializeFromTree(element); return GsonComponentSerializer.gson().deserializeFromTree(element);
})); })));
public static <T> DataComponentType<T> register(DataComponentType<T> type) {
REGISTERED_TYPES.put(type.getKey(), type);
return type;
}
public static boolean isKnownType(Key key) {
return REGISTERED_TYPES.containsKey(key);
}
private final Key key; private final Key key;
private final DataComponentCodec<T> codec; private final DataComponentCodec<T> codec;
@SuppressWarnings("PatternValidation") @SuppressWarnings("PatternValidation")
public DataComponentTypes(String key, DataComponentCodec<T> codec) { public DataComponentType(String key, DataComponentCodec<T> codec) {
this(Key.key(key), codec); this(Key.key(key), codec);
} }
public DataComponentTypes(Key key, DataComponentCodec<T> codec) { public DataComponentType(Key key, DataComponentCodec<T> codec) {
this.key = key; this.key = key;
this.codec = codec; this.codec = codec;
} }
@ -69,7 +82,6 @@ public class DataComponentTypes<T> {
this.decode = decode; this.decode = decode;
} }
@SuppressWarnings("unchecked")
public Tag<?> encode(T t) { public Tag<?> encode(T t) {
return encode.apply((T) t); return encode.apply((T) t);
} }

View File

@ -25,6 +25,7 @@ import com.loohp.limbo.location.BlockFace;
import com.loohp.limbo.location.MovingObjectPositionBlock; import com.loohp.limbo.location.MovingObjectPositionBlock;
import com.loohp.limbo.location.Vector; import com.loohp.limbo.location.Vector;
import com.loohp.limbo.registry.BuiltInRegistries; import com.loohp.limbo.registry.BuiltInRegistries;
import com.loohp.limbo.registry.DataComponentType;
import com.loohp.limbo.world.BlockPosition; import com.loohp.limbo.world.BlockPosition;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -56,12 +57,21 @@ public class DataTypeIO {
DataTypeIO.writeVarInt(out, itemstack.amount()); DataTypeIO.writeVarInt(out, itemstack.amount());
writeVarInt(out, BuiltInRegistries.ITEM_REGISTRY.getId(itemstack.type())); writeVarInt(out, BuiltInRegistries.ITEM_REGISTRY.getId(itemstack.type()));
Map<Key, Tag<?>> components = itemstack.components(); Map<Key, Tag<?>> components = itemstack.components();
DataTypeIO.writeVarInt(out, components.size()); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataTypeIO.writeVarInt(out, 0); DataOutputStream componentOut = new DataOutputStream(buffer);
int componentSize = 0;
for (Map.Entry<Key, Tag<?>> entry : components.entrySet()) { for (Map.Entry<Key, Tag<?>> entry : components.entrySet()) {
DataTypeIO.writeVarInt(out, BuiltInRegistries.DATA_COMPONENT_TYPE.getId(entry.getKey())); Key componentKey = entry.getKey();
DataTypeIO.writeTag(out, entry.getValue()); int typeId = BuiltInRegistries.DATA_COMPONENT_TYPE.getId(componentKey);
if (typeId >= 0 && DataComponentType.isKnownType(componentKey)) {
DataTypeIO.writeVarInt(componentOut, typeId);
DataTypeIO.writeTag(componentOut, entry.getValue());
componentSize++;
}
} }
DataTypeIO.writeVarInt(out, componentSize);
DataTypeIO.writeVarInt(out, 0);
out.write(buffer.toByteArray());
} }
} }
@ -77,7 +87,9 @@ public class DataTypeIO {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
Key componentKey = BuiltInRegistries.DATA_COMPONENT_TYPE.fromId(DataTypeIO.readVarInt(in)); Key componentKey = BuiltInRegistries.DATA_COMPONENT_TYPE.fromId(DataTypeIO.readVarInt(in));
Tag<?> component = readTag(in, Tag.class); Tag<?> component = readTag(in, Tag.class);
components.put(componentKey, component); if (componentKey != null && DataComponentType.isKnownType(componentKey)) {
components.put(componentKey, component);
}
} }
return new ItemStack(key, amount, components); return new ItemStack(key, amount, components);
} }