add writeBytes to displayLinkPeripheral to allow write utf8 from CC:T

resolve cc-tweaked/CC-Tweaked#2035
This commit is contained in:
zyxkad 2024-12-21 11:13:09 -07:00
parent d48a504486
commit a2f7a8eb58
Failed to generate hash of commit

View file

@ -1,5 +1,7 @@
package com.simibubi.create.compat.computercraft.implementation.peripherals;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.jetbrains.annotations.NotNull;
@ -8,7 +10,11 @@ import com.simibubi.create.content.redstone.displayLink.DisplayLinkBlockEntity;
import com.simibubi.create.content.redstone.displayLink.DisplayLinkContext;
import com.simibubi.create.content.redstone.displayLink.target.DisplayTargetStats;
import dan200.computercraft.api.lua.IArguments;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.LuaValues;
import dan200.computercraft.api.lua.ObjectLuaTable;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;
@ -52,6 +58,28 @@ public class DisplayLinkPeripheral extends SyncedPeripheral<DisplayLinkBlockEnti
@LuaFunction
public final void write(String text) {
writeImpl(text);
}
@LuaFunction
public final void writeBytes(IArguments args) throws LuaException {
Object data = args.get(0);
byte[] bytes;
if (data instanceof String str) {
bytes = str.getBytes(StandardCharsets.US_ASCII);
} else if (data instanceof Map<?, ?> map) {
ObjectLuaTable table = new ObjectLuaTable(map);
bytes = new byte[table.length()];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) (table.getInt(i + 1) & 0xff);
}
} else {
throw LuaValues.badArgumentOf(args, 0, "string or table");
}
writeImpl(new String(bytes, StandardCharsets.UTF_8));
}
protected final void writeImpl(String text) {
ListTag tag = blockEntity.getSourceConfig().getList(TAG_KEY, Tag.TAG_STRING);
int x = cursorX.get();