diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/android/AndroidResearchManager.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/android/AndroidResearchManager.kt index 7f1672933..7062117f1 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/android/AndroidResearchManager.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/android/AndroidResearchManager.kt @@ -11,10 +11,8 @@ import net.minecraft.resources.ResourceLocation import net.minecraft.server.packs.resources.ResourceManager import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener import net.minecraft.util.profiling.ProfilerFiller -import net.minecraftforge.common.MinecraftForge import net.minecraftforge.event.AddReloadListenerEvent import net.minecraftforge.event.OnDatapackSyncEvent -import net.minecraftforge.eventbus.api.Event import net.minecraftforge.network.NetworkEvent import net.minecraftforge.network.PacketDistributor import org.apache.logging.log4j.LogManager @@ -26,10 +24,7 @@ import ru.dbotthepony.mc.otm.client.minecraft import ru.dbotthepony.mc.otm.core.fromJsonStrict import ru.dbotthepony.mc.otm.core.fromNetwork import ru.dbotthepony.mc.otm.core.set -import ru.dbotthepony.mc.otm.core.toJsonStrict import ru.dbotthepony.mc.otm.core.toNetwork -import ru.dbotthepony.mc.otm.core.util.readJson -import ru.dbotthepony.mc.otm.matter.MatterManager import ru.dbotthepony.mc.otm.network.MatteryPacket import ru.dbotthepony.mc.otm.network.RegistryNetworkChannel import ru.dbotthepony.mc.otm.network.enqueueWork diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/UnOverengineering.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/UnOverengineering.kt index f643eb1a9..3cc18df17 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/UnOverengineering.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/UnOverengineering.kt @@ -4,7 +4,6 @@ import com.google.gson.JsonElement import com.google.gson.JsonSyntaxException import com.mojang.serialization.Codec import com.mojang.serialization.JsonOps -import net.minecraft.core.BlockPos import net.minecraft.core.Holder import net.minecraft.core.RegistryAccess import net.minecraft.core.registries.Registries @@ -23,11 +22,10 @@ import net.minecraft.world.damagesource.DamageType import net.minecraft.world.item.Item import net.minecraft.world.level.block.Block import net.minecraft.world.level.material.Fluid -import net.minecraft.world.phys.Vec3 import net.minecraftforge.registries.ForgeRegistries import net.minecraftforge.registries.IForgeRegistry -import ru.dbotthepony.mc.otm.core.util.readJson -import ru.dbotthepony.mc.otm.core.util.writeJson +import ru.dbotthepony.mc.otm.core.util.readBinaryJson +import ru.dbotthepony.mc.otm.core.util.writeBinaryJson // because doing it inline is ugly fun Codec.fromJson(value: JsonElement): V? { @@ -63,11 +61,11 @@ fun Codec.toNbtStrict(value: V, prefix: Tag = NbtOps.INSTANCE.empty } fun Codec.toNetwork(buff: FriendlyByteBuf, value: V) { - buff.writeJson(toJsonStrict(value)) + buff.writeBinaryJson(toJsonStrict(value)) } fun Codec.fromNetwork(buff: FriendlyByteBuf): V { - return fromJsonStrict(buff.readJson()) + return fromJsonStrict(buff.readBinaryJson()) } // 1.19 being 1.19 diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/nbt/CompoundTagExt.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/nbt/CompoundTagExt.kt index 140619739..538382561 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/nbt/CompoundTagExt.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/nbt/CompoundTagExt.kt @@ -20,8 +20,8 @@ import net.minecraft.nbt.ShortTag import net.minecraft.nbt.StringTag import net.minecraft.nbt.Tag import net.minecraft.world.item.ItemStack -import ru.dbotthepony.mc.otm.core.util.readJson -import ru.dbotthepony.mc.otm.core.util.writeJson +import ru.dbotthepony.mc.otm.core.util.readBinaryJson +import ru.dbotthepony.mc.otm.core.util.writeBinaryJson import java.util.UUID operator fun CompoundTag.set(index: String, value: Tag) = put(index, value) @@ -108,7 +108,7 @@ fun CompoundTag.getUUIDSafe(key: String): UUID? { fun CompoundTag.putJson(key: String, json: JsonElement) { val bytes = FastByteArrayOutputStream() - bytes.writeJson(json) + bytes.writeBinaryJson(json) putByteArray(key, bytes.array.copyOfRange(0, bytes.length)) } @@ -118,7 +118,7 @@ fun CompoundTag.getJson(key: String, sizeLimit: NbtAccounter = NbtAccounter.UNLI if (bytes.isEmpty()) return null - return FastByteArrayInputStream(bytes).readJson(sizeLimit) + return FastByteArrayInputStream(bytes).readBinaryJson(sizeLimit) } fun CompoundTag.getBoolean(index: String, orElse: Boolean): Boolean { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/FriendlyStreams.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/FriendlyStreams.kt index c4e5ffbc5..308563fa2 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/FriendlyStreams.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/FriendlyStreams.kt @@ -24,7 +24,6 @@ import net.minecraftforge.registries.ForgeRegistry import java.io.* import java.math.BigDecimal import java.math.BigInteger -import kotlin.math.absoluteValue // But seriously, Mojang, why would you need to derive from ByteBuf directly, when you can implement // your own InputStream and OutputStream, since ByteBuf is meant to be operated on most time like a stream anyway? @@ -154,21 +153,21 @@ private const val TYPE_OBJECT = 0x07 * * just copy pasted this code from my another project because i was lazy */ -fun OutputStream.writeJson(element: JsonElement) { +fun OutputStream.writeBinaryJson(element: JsonElement) { if (element is JsonObject) { write(TYPE_OBJECT) writeVarIntLE(element.size()) for ((k, v) in element.entrySet()) { writeBinaryString(k) - writeJson(v) + writeBinaryJson(v) } } else if (element is JsonArray) { write(TYPE_ARRAY) writeVarIntLE(element.size()) for (v in element) { - writeJson(v) + writeBinaryJson(v) } } else if (element is JsonPrimitive) { if (element.isNumber) { @@ -204,7 +203,7 @@ fun OutputStream.writeJson(element: JsonElement) { * * just copy pasted this code from my another project because i was lazy */ -fun InputStream.readJson(sizeLimit: NbtAccounter = NbtAccounter(1L shl 18 /* 256 KiB */)): JsonElement { +fun InputStream.readBinaryJson(sizeLimit: NbtAccounter = NbtAccounter(1L shl 18 /* 256 KiB */)): JsonElement { sizeLimit.accountBytes(1L) return when (val id = read()) { @@ -226,7 +225,7 @@ fun InputStream.readJson(sizeLimit: NbtAccounter = NbtAccounter(1L shl 18 /* 256 if (values < 0) throw JsonSyntaxException("Tried to read json array with $values elements in it") val build = JsonArray(values) - for (i in 0 until values) build.add(readJson(sizeLimit)) + for (i in 0 until values) build.add(readBinaryJson(sizeLimit)) return build } TYPE_OBJECT -> { @@ -246,7 +245,7 @@ fun InputStream.readJson(sizeLimit: NbtAccounter = NbtAccounter(1L shl 18 /* 256 } try { - build.add(key, readJson(sizeLimit)) + build.add(key, readBinaryJson(sizeLimit)) } catch(err: Throwable) { throw JsonSyntaxException("Reading json object at $i with name $key", err) } @@ -259,27 +258,27 @@ fun InputStream.readJson(sizeLimit: NbtAccounter = NbtAccounter(1L shl 18 /* 256 } fun InputStream.readBinaryComponent(): Component? { - return Component.Serializer.fromJson(readJson()) + return Component.Serializer.fromJson(readBinaryJson()) } fun OutputStream.writeBinaryComponent(component: Component) { - writeJson(Component.Serializer.toJsonTree(component)) + writeBinaryJson(Component.Serializer.toJsonTree(component)) } -fun FriendlyByteBuf.readJson(sizeLimit: NbtAccounter = NbtAccounter(1L shl 18)): JsonElement { - return ByteBufInputStream(this).readJson(sizeLimit) +fun FriendlyByteBuf.readBinaryJson(sizeLimit: NbtAccounter = NbtAccounter(1L shl 18)): JsonElement { + return ByteBufInputStream(this).readBinaryJson(sizeLimit) } -fun FriendlyByteBuf.writeJson(value: JsonElement) { - ByteBufOutputStream(this).writeJson(value) +fun FriendlyByteBuf.writeBinaryJson(value: JsonElement) { + ByteBufOutputStream(this).writeBinaryJson(value) } fun FriendlyByteBuf.readBinaryComponent(): Component { - return Component.Serializer.fromJson(readJson()) ?: throw NullPointerException("Received null component") + return Component.Serializer.fromJson(readBinaryJson()) ?: throw NullPointerException("Received null component") } fun FriendlyByteBuf.writeBinaryComponent(component: Component) { - writeJson(Component.Serializer.toJsonTree(component)) + writeBinaryJson(Component.Serializer.toJsonTree(component)) } fun S.writeCollection(collection: Collection, writer: S.(V) -> Unit) { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PlatePressRecipe.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PlatePressRecipe.kt index f471b091f..783ee71d5 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PlatePressRecipe.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PlatePressRecipe.kt @@ -17,15 +17,14 @@ import net.minecraft.world.item.crafting.RecipeType import net.minecraft.world.level.Level import org.apache.logging.log4j.LogManager import ru.dbotthepony.mc.otm.OverdriveThatMatters -import ru.dbotthepony.mc.otm.block.entity.tech.PlatePressBlockEntity import ru.dbotthepony.mc.otm.container.get import ru.dbotthepony.mc.otm.core.fromJsonStrict import ru.dbotthepony.mc.otm.core.isActuallyEmpty import ru.dbotthepony.mc.otm.registry.MRecipes import ru.dbotthepony.mc.otm.core.registryName import ru.dbotthepony.mc.otm.core.toJsonStrict -import ru.dbotthepony.mc.otm.core.util.readJson -import ru.dbotthepony.mc.otm.core.util.writeJson +import ru.dbotthepony.mc.otm.core.util.readBinaryJson +import ru.dbotthepony.mc.otm.core.util.writeBinaryJson class PlatePressRecipe( private val id: ResourceLocation, @@ -122,7 +121,7 @@ class PlatePressRecipe( } override fun fromNetwork(loc: ResourceLocation, buff: FriendlyByteBuf): PlatePressRecipe { - return PlatePressRecipe(loc, Ingredient.fromNetwork(buff), Ingredient.fromNetwork(buff), buff.readInt(), buff.readInt(), FloatProvider.CODEC.fromJsonStrict(buff.readJson())) + return PlatePressRecipe(loc, Ingredient.fromNetwork(buff), Ingredient.fromNetwork(buff), buff.readInt(), buff.readInt(), FloatProvider.CODEC.fromJsonStrict(buff.readBinaryJson())) } override fun toNetwork(buff: FriendlyByteBuf, recipe: PlatePressRecipe) { @@ -130,7 +129,7 @@ class PlatePressRecipe( recipe.output.toNetwork(buff) buff.writeInt(recipe.count) buff.writeInt(recipe.workTime) - buff.writeJson(FloatProvider.CODEC.toJsonStrict(recipe.experience)) + buff.writeBinaryJson(FloatProvider.CODEC.toJsonStrict(recipe.experience)) } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/UpgradeRecipe.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/UpgradeRecipe.kt index 224186c4e..71ae269d2 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/UpgradeRecipe.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/UpgradeRecipe.kt @@ -14,7 +14,6 @@ import net.minecraft.world.item.ItemStack import net.minecraft.world.item.crafting.CraftingBookCategory import net.minecraft.world.item.crafting.CraftingRecipe import net.minecraft.world.item.crafting.Ingredient -import net.minecraft.world.item.crafting.Recipe import net.minecraft.world.item.crafting.RecipeSerializer import net.minecraft.world.item.crafting.RecipeType import net.minecraft.world.item.crafting.ShapedRecipe @@ -25,8 +24,8 @@ import ru.dbotthepony.mc.otm.core.nbt.set import ru.dbotthepony.mc.otm.core.registryName import ru.dbotthepony.mc.otm.core.set import ru.dbotthepony.mc.otm.core.tagNotNull -import ru.dbotthepony.mc.otm.core.util.readJson -import ru.dbotthepony.mc.otm.core.util.writeJson +import ru.dbotthepony.mc.otm.core.util.readBinaryJson +import ru.dbotthepony.mc.otm.core.util.writeBinaryJson import ru.dbotthepony.mc.otm.core.collect.stream import java.util.stream.Stream @@ -241,14 +240,14 @@ class UpgradeRecipe( return UpgradeRecipe( recipe, - buff.readCollection({ ArrayList(it) }, { deserializeOp(it.readJson() as JsonObject) }).stream(), + buff.readCollection({ ArrayList(it) }, { deserializeOp(it.readBinaryJson() as JsonObject) }).stream(), buff.readResourceLocation() ) } override fun toNetwork(buff: FriendlyByteBuf, value: UpgradeRecipe) { ShapedRecipe.Serializer.SHAPED_RECIPE.toNetwork(buff, value.parent) - buff.writeCollection(value.copyPaths) { it, v -> it.writeJson(v.serialize()) } + buff.writeCollection(value.copyPaths) { it, v -> it.writeBinaryJson(v.serialize()) } buff.writeResourceLocation(value.source) } }