Skip to content

Commit

Permalink
Skip encoding not-implemented item data components
Browse files Browse the repository at this point in the history
  • Loading branch information
LOOHP committed May 26, 2024
1 parent c260a9c commit af38021
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 44 deletions.
30 changes: 15 additions & 15 deletions src/main/java/com/loohp/limbo/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
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;
Expand All @@ -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);
Expand Down Expand Up @@ -256,7 +258,7 @@ protected void run() {
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();
}
Expand Down Expand Up @@ -317,16 +319,15 @@ protected static String translateToConsole(String str) {

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);
this.logs = logs;
this.console = console;
}

@SuppressWarnings("resource")
@Override
public PrintStream printf(Locale l, String format, Object... args) {
console.stashLine();
Expand All @@ -338,7 +339,6 @@ public PrintStream printf(Locale l, String format, Object... args) {
return stream;
}

@SuppressWarnings("resource")
@Override
public PrintStream printf(String format, Object... args) {
console.stashLine();
Expand Down Expand Up @@ -453,8 +453,8 @@ public void println(String string) {

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);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/loohp/limbo/inventory/ItemStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,11 +81,11 @@ public ItemStack components(Map<Key, Tag<?>> 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()));
}

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);
Expand All @@ -96,7 +96,7 @@ public Component displayName() {
return null;
}
try {
return component(DataComponentTypes.CUSTOM_NAME);
return component(DataComponentType.CUSTOM_NAME);
} catch (Exception e) {
return null;
}
Expand All @@ -106,7 +106,7 @@ public ItemStack displayName(Component component) {
if (type().equals(AIR.type())) {
return this;
}
return component(DataComponentTypes.CUSTOM_NAME, component);
return component(DataComponentType.CUSTOM_NAME, component);
}

public int getMaxStackSize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,40 @@
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;
}
Expand All @@ -69,7 +82,6 @@ public DataComponentCodec(Function<T, Tag<?>> encode, Function<Tag<?>, T> decode
this.decode = decode;
}

@SuppressWarnings("unchecked")
public Tag<?> encode(T t) {
return encode.apply((T) t);
}
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/com/loohp/limbo/utils/DataTypeIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -56,12 +57,21 @@ public static void writeItemStack(DataOutputStream out, ItemStack itemstack) thr
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());
}
}

Expand All @@ -77,7 +87,9 @@ public static ItemStack readItemStack(DataInputStream in) throws IOException {
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);
}
Expand Down

0 comments on commit af38021

Please sign in to comment.