forked from BLOCKFANTASY/LOOHP-Limbo
Skip encoding not-implemented item data components
This commit is contained in:
parent
c260a9cc1f
commit
af380214ef
|
|
@ -53,6 +53,8 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
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 RESET_COLOR = "\u001B[0m";
|
||||
|
||||
private Terminal terminal;
|
||||
private LineReader tabReader;
|
||||
private ConsoleReader reader;
|
||||
private final Terminal terminal;
|
||||
private final LineReader tabReader;
|
||||
private final ConsoleReader reader;
|
||||
|
||||
private InputStream in;
|
||||
private final InputStream in;
|
||||
@SuppressWarnings("unused")
|
||||
private PrintStream out;
|
||||
private final PrintStream out;
|
||||
@SuppressWarnings("unused")
|
||||
private PrintStream err;
|
||||
protected PrintStream logs;
|
||||
private final PrintStream err;
|
||||
protected final PrintStream logs;
|
||||
|
||||
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());
|
||||
File dir = new File("logs");
|
||||
dir.mkdirs();
|
||||
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) {
|
||||
System.setIn(in);
|
||||
|
|
@ -256,7 +258,7 @@ public class Console implements CommandSender {
|
|||
while (true) {
|
||||
try {
|
||||
String command = tabReader.readLine(PROMPT).trim();
|
||||
if (command.length() > 0) {
|
||||
if (!command.isEmpty()) {
|
||||
String[] input = CustomStringUtils.splitStringToArgs(command);
|
||||
new Thread(() -> Limbo.getInstance().dispatchCommand(this, input)).start();
|
||||
}
|
||||
|
|
@ -317,8 +319,8 @@ public class Console implements CommandSender {
|
|||
|
||||
public static class ConsoleOutputStream extends PrintStream {
|
||||
|
||||
private PrintStream logs;
|
||||
private Console console;
|
||||
private final PrintStream logs;
|
||||
private final Console console;
|
||||
|
||||
public ConsoleOutputStream(Console console, OutputStream out, PrintStream logs) {
|
||||
super(out);
|
||||
|
|
@ -326,7 +328,6 @@ public class Console implements CommandSender {
|
|||
this.console = console;
|
||||
}
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
@Override
|
||||
public PrintStream printf(Locale l, String format, Object... args) {
|
||||
console.stashLine();
|
||||
|
|
@ -338,7 +339,6 @@ public class Console implements CommandSender {
|
|||
return stream;
|
||||
}
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
@Override
|
||||
public PrintStream printf(String format, Object... args) {
|
||||
console.stashLine();
|
||||
|
|
@ -453,8 +453,8 @@ public class Console implements CommandSender {
|
|||
|
||||
public static class ConsoleErrorStream extends PrintStream {
|
||||
|
||||
private PrintStream logs;
|
||||
private Console console;
|
||||
private final PrintStream logs;
|
||||
private final Console console;
|
||||
|
||||
public ConsoleErrorStream(Console console, OutputStream out, PrintStream logs) {
|
||||
super(out);
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
|
||||
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.text.Component;
|
||||
import net.querz.nbt.tag.Tag;
|
||||
|
|
@ -81,11 +81,11 @@ public class ItemStack implements Cloneable {
|
|||
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()));
|
||||
}
|
||||
|
||||
public <T> ItemStack component(DataComponentTypes<T> type, T value) {
|
||||
public <T> ItemStack component(DataComponentType<T> type, T value) {
|
||||
Map<Key, Tag<?>> components = components();
|
||||
components.put(type.getKey(), type.getCodec().encode(value));
|
||||
return components(components);
|
||||
|
|
@ -96,7 +96,7 @@ public class ItemStack implements Cloneable {
|
|||
return null;
|
||||
}
|
||||
try {
|
||||
return component(DataComponentTypes.CUSTOM_NAME);
|
||||
return component(DataComponentType.CUSTOM_NAME);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -106,7 +106,7 @@ public class ItemStack implements Cloneable {
|
|||
if (type().equals(AIR.type())) {
|
||||
return this;
|
||||
}
|
||||
return component(DataComponentTypes.CUSTOM_NAME, component);
|
||||
return component(DataComponentType.CUSTOM_NAME, component);
|
||||
}
|
||||
|
||||
public int getMaxStackSize() {
|
||||
|
|
|
|||
|
|
@ -27,8 +27,8 @@ import java.io.IOException;
|
|||
|
||||
public class PacketPlayInSetCreativeSlot extends PacketIn {
|
||||
|
||||
private int slotNumber;
|
||||
private ItemStack itemStack;
|
||||
private final int slotNumber;
|
||||
private final ItemStack itemStack;
|
||||
|
||||
public PacketPlayInSetCreativeSlot(int slotNumber, ItemStack itemStack) {
|
||||
this.slotNumber = slotNumber;
|
||||
|
|
|
|||
|
|
@ -31,13 +31,13 @@ import java.util.Map;
|
|||
|
||||
public class PacketPlayInWindowClick extends PacketIn {
|
||||
|
||||
private int containerId;
|
||||
private int stateId;
|
||||
private int slotNum;
|
||||
private int buttonNum;
|
||||
private InventoryClickType clickType;
|
||||
private Map<Integer, ItemStack> changedSlots;
|
||||
private ItemStack carriedItem;
|
||||
private final int containerId;
|
||||
private final int stateId;
|
||||
private final int slotNum;
|
||||
private final int buttonNum;
|
||||
private final InventoryClickType clickType;
|
||||
private final Map<Integer, ItemStack> changedSlots;
|
||||
private final ItemStack carriedItem;
|
||||
|
||||
public PacketPlayInWindowClick(int containerId, int stateId, int slotNum, int buttonNum, InventoryClickType clickType, Map<Integer, ItemStack> changedSlots, ItemStack carriedItem) {
|
||||
this.containerId = containerId;
|
||||
|
|
|
|||
|
|
@ -28,10 +28,10 @@ import java.io.IOException;
|
|||
|
||||
public class PacketPlayOutSetSlot extends PacketOut {
|
||||
|
||||
private int containerId;
|
||||
private int stateId;
|
||||
private int slot;
|
||||
private ItemStack itemStack;
|
||||
private final int containerId;
|
||||
private final int stateId;
|
||||
private final int slot;
|
||||
private final ItemStack itemStack;
|
||||
|
||||
public PacketPlayOutSetSlot(int containerId, int stateId, int slot, ItemStack itemStack) {
|
||||
this.containerId = containerId;
|
||||
|
|
|
|||
|
|
@ -26,27 +26,40 @@ import net.kyori.adventure.text.Component;
|
|||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import net.querz.nbt.tag.Tag;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
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);
|
||||
return NbtComponentSerializer.jsonComponentToTag(element);
|
||||
}, tag -> {
|
||||
JsonElement element = NbtComponentSerializer.tagComponentToJson(tag);
|
||||
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 DataComponentCodec<T> codec;
|
||||
|
||||
@SuppressWarnings("PatternValidation")
|
||||
public DataComponentTypes(String key, DataComponentCodec<T> codec) {
|
||||
public DataComponentType(String key, DataComponentCodec<T> codec) {
|
||||
this(Key.key(key), codec);
|
||||
}
|
||||
|
||||
public DataComponentTypes(Key key, DataComponentCodec<T> codec) {
|
||||
public DataComponentType(Key key, DataComponentCodec<T> codec) {
|
||||
this.key = key;
|
||||
this.codec = codec;
|
||||
}
|
||||
|
|
@ -69,7 +82,6 @@ public class DataComponentTypes<T> {
|
|||
this.decode = decode;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Tag<?> encode(T t) {
|
||||
return encode.apply((T) t);
|
||||
}
|
||||
|
|
@ -25,6 +25,7 @@ import com.loohp.limbo.location.BlockFace;
|
|||
import com.loohp.limbo.location.MovingObjectPositionBlock;
|
||||
import com.loohp.limbo.location.Vector;
|
||||
import com.loohp.limbo.registry.BuiltInRegistries;
|
||||
import com.loohp.limbo.registry.DataComponentType;
|
||||
import com.loohp.limbo.world.BlockPosition;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
|
@ -56,12 +57,21 @@ public class DataTypeIO {
|
|||
DataTypeIO.writeVarInt(out, itemstack.amount());
|
||||
writeVarInt(out, BuiltInRegistries.ITEM_REGISTRY.getId(itemstack.type()));
|
||||
Map<Key, Tag<?>> components = itemstack.components();
|
||||
DataTypeIO.writeVarInt(out, components.size());
|
||||
DataTypeIO.writeVarInt(out, 0);
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
DataOutputStream componentOut = new DataOutputStream(buffer);
|
||||
int componentSize = 0;
|
||||
for (Map.Entry<Key, Tag<?>> entry : components.entrySet()) {
|
||||
DataTypeIO.writeVarInt(out, BuiltInRegistries.DATA_COMPONENT_TYPE.getId(entry.getKey()));
|
||||
DataTypeIO.writeTag(out, entry.getValue());
|
||||
Key componentKey = entry.getKey();
|
||||
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++) {
|
||||
Key componentKey = BuiltInRegistries.DATA_COMPONENT_TYPE.fromId(DataTypeIO.readVarInt(in));
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue