mirror of
https://github.com/WiIIiam278/HuskSync.git
synced 2025-12-19 14:59:21 +00:00
feat: skip unserializable items on Fabric
This commit is contained in:
@@ -39,6 +39,7 @@ import org.jetbrains.annotations.ApiStatus;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -173,6 +174,7 @@ public abstract class FabricSerializer {
|
|||||||
container.putInt("size", items.length);
|
container.putInt("size", items.length);
|
||||||
final NbtList itemList = new NbtList();
|
final NbtList itemList = new NbtList();
|
||||||
final DynamicRegistryManager registryManager = plugin.getMinecraftServer().getRegistryManager();
|
final DynamicRegistryManager registryManager = plugin.getMinecraftServer().getRegistryManager();
|
||||||
|
final List<ItemStack> skipped = new ArrayList<>();
|
||||||
for (int i = 0; i < items.length; i++) {
|
for (int i = 0; i < items.length; i++) {
|
||||||
final ItemStack item = items[i];
|
final ItemStack item = items[i];
|
||||||
if (item == null || item.isEmpty() || item.getCount() < 1 || item.getCount() > 99) {
|
if (item == null || item.isEmpty() || item.getCount() < 1 || item.getCount() > 99) {
|
||||||
@@ -180,9 +182,16 @@ public abstract class FabricSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final NbtCompound entry = encodeNbt(item, registryManager);
|
final NbtCompound entry = encodeNbt(item, registryManager);
|
||||||
|
if (entry == null) {
|
||||||
|
skipped.add(item);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
entry.putInt("Slot", i);
|
entry.putInt("Slot", i);
|
||||||
itemList.add(entry);
|
itemList.add(entry);
|
||||||
}
|
}
|
||||||
|
if (!skipped.isEmpty()) {
|
||||||
|
plugin.debug("Skipped serializing items in array: %s".formatted(Arrays.toString(skipped.toArray())));
|
||||||
|
}
|
||||||
container.put(ITEMS_TAG, itemList);
|
container.put(ITEMS_TAG, itemList);
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
@@ -216,17 +225,21 @@ public abstract class FabricSerializer {
|
|||||||
).getValue();
|
).getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@Nullable
|
||||||
private NbtCompound encodeNbt(@NotNull ItemStack item, @NotNull DynamicRegistryManager registryManager) {
|
private NbtCompound encodeNbt(@NotNull ItemStack item, @NotNull DynamicRegistryManager registryManager) {
|
||||||
//#if MC==12104
|
try {
|
||||||
return (NbtCompound) item.toNbt(registryManager);
|
//#if MC==12104
|
||||||
//#elseif MC==12101
|
return (NbtCompound) item.toNbt(registryManager);
|
||||||
//$$ return (NbtCompound) item.encode(registryManager);
|
//#elseif MC==12101
|
||||||
//#elseif MC==12001
|
//$$ return (NbtCompound) item.encode(registryManager);
|
||||||
//$$ final NbtCompound compound = new NbtCompound();
|
//#elseif MC==12001
|
||||||
//$$ item.writeNbt(compound);
|
//$$ final NbtCompound compound = new NbtCompound();
|
||||||
//$$ return compound;
|
//$$ item.writeNbt(compound);
|
||||||
//#endif
|
//$$ return compound;
|
||||||
|
//#endif
|
||||||
|
} catch (Throwable e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
Reference in New Issue
Block a user