Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ram.talia.hexal.xplat.IXplatAbstractions
data class MsgSendEverbookC2S(val everbook: Everbook) : IMessage {
private var wasTooBig = false
private var somethingWeirdHappened = false
private var amountOverLimit = -1L
override fun serialize(buf: FriendlyByteBuf) {
val bookNbt = everbook.serialiseToNBT()
val bytes = bookNbt.toCompressedBytes()
Expand All @@ -34,9 +35,10 @@ data class MsgSendEverbookC2S(val everbook: Everbook) : IMessage {
if (wasTooBig){
HexalAPI.LOGGER.info("Player ${sender.displayName.string}'s everbook data exceeded the configured size limit of ${HexalConfig.server.everbookMaxSize}")
sender.sendSystemMessage(Component.translatable("hexal.everbook.sizewarning", HexalConfig.server.everbookMaxSize))
}
if (somethingWeirdHappened){
} else if (somethingWeirdHappened){
sender.sendSystemMessage(Component.translatable("hexal.everbook.unexpectederror"))
} else if (isSingleplayer && amountOverLimit > 0 && server.playerCount <= 1){
sender.sendSystemMessage(Component.translatable("hexal.everbook.overlimitsingleplayer", HexalConfig.server.everbookMaxSize, amountOverLimit))
}
}
}
Expand All @@ -45,16 +47,24 @@ data class MsgSendEverbookC2S(val everbook: Everbook) : IMessage {
@JvmField
val ID: ResourceLocation = HexalAPI.modLoc("sendever")

@JvmField var isSingleplayer = false

@JvmStatic
fun deserialise(buffer: ByteBuf): MsgSendEverbookC2S {
val buf = FriendlyByteBuf(buffer)
val uuid = buf.readUUID()
val bytes = buf.readByteArray()
var packet = MsgSendEverbookC2S(Everbook(uuid, mutableMapOf(), listOf()))
val maxSize = if (isSingleplayer) {
Long.MAX_VALUE
} else {
HexalConfig.server.everbookMaxSize
}
try {
val decompressed = bytes.decompressToNBT(HexalConfig.server.everbookMaxSize)
val decompressed = bytes.decompressToNBT(maxSize)
HexalAPI.LOGGER.info("Deserializing everbook data of size ${bytes.size} decompressed to size ${decompressed.sizeInBytes()}")
packet = MsgSendEverbookC2S(Everbook.fromNbt(decompressed))
packet.amountOverLimit = decompressed.sizeInBytes() - HexalConfig.server.everbookMaxSize
} catch (_ : NbtSizeException){
packet.wasTooBig = true
} catch (e : Exception){
Expand Down
3 changes: 2 additions & 1 deletion Common/src/main/resources/assets/hexal/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -418,5 +418,6 @@
"text.autoconfig.hexal.option.server.greatSpells.tickRandomTickIProb.@Tooltip[2]": "Bound from 600 to 2100.",

"hexal.everbook.sizewarning": "Your everbook data was larger than the server's limit of %s bytes. You will not be able to use it until you rejoin with a small enough everbook.",
"hexal.everbook.unexpectederror": "An unexpected error occurred while attempting to process your everbook data, and you will not have access to it until the problem is resolved. Please contact your server administrator(s) for more information."
"hexal.everbook.unexpectederror": "An unexpected error occurred while attempting to process your everbook data, and you will not have access to it until the problem is resolved. Please contact your server administrator(s) for more information.",
"hexal.everbook.overlimitsingleplayer": "Your everbook data was over the configured limit of %s bytes, but as this is a singleplayer world, you will still have access to it. You will have to remove %s bytes of data before being able to use it on servers with the same limit."
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.biome.v1.BiomeModifications
import net.fabricmc.fabric.api.biome.v1.BiomeSelectors
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents
import net.fabricmc.fabric.api.networking.v1.PacketSender
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents
import net.fabricmc.fabric.api.tag.convention.v1.ConventionalBiomeTags
import net.minecraft.core.Registry
import net.minecraft.core.registries.BuiltInRegistries
import net.minecraft.core.registries.Registries
import net.minecraft.gametest.framework.GameTestServer
import net.minecraft.resources.ResourceKey
import net.minecraft.resources.ResourceLocation
import net.minecraft.server.MinecraftServer
import net.minecraft.server.dedicated.DedicatedServer
import net.minecraft.server.network.ServerGamePacketListenerImpl
import net.minecraft.world.level.levelgen.GenerationStep
import ram.talia.hexal.api.HexalAPI
import ram.talia.hexal.api.gates.GateManager
Expand All @@ -24,6 +30,7 @@ import ram.talia.hexal.common.lib.HexalFeatures
import ram.talia.hexal.common.lib.hex.HexalActions
import ram.talia.hexal.common.lib.hex.HexalArithmetics
import ram.talia.hexal.common.lib.hex.HexalIotaTypes
import ram.talia.hexal.common.network.MsgSendEverbookC2S
import ram.talia.hexal.common.recipe.HexalRecipeSerializers
import ram.talia.hexal.common.recipe.HexalRecipeTypes
import ram.talia.hexal.fabric.interop.phantom.OpPhaseBlock
Expand Down Expand Up @@ -54,12 +61,20 @@ object FabricHexalInitializer : ModInitializer {
ServerLifecycleEvents.SERVER_STARTED.register {
val savedData = it.overworld().dataStorage.computeIfAbsent(::GateSavedData, ::GateSavedData, FILE_GATE_MANAGER)
savedData.setDirty()
MsgSendEverbookC2S.isSingleplayer = !(it is DedicatedServer || it is GameTestServer)
}
ServerLifecycleEvents.SERVER_STOPPING.register {
val savedData = it.overworld().dataStorage.computeIfAbsent(::GateSavedData, ::GateSavedData, FILE_GATE_MANAGER)
GateManager.shouldClearOnWrite = true
savedData.setDirty()
}
ServerPlayConnectionEvents.JOIN.register(this::onPlayerJoin)
}

private fun onPlayerJoin(impl : ServerGamePacketListenerImpl, sender : PacketSender, server : MinecraftServer){
if (server.playerCount > 1) {
MsgSendEverbookC2S.isSingleplayer = false //for handling everbook size restrictions if someone joins a LAN world
}
}

private fun initRegistries() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import at.petrak.hexcasting.common.lib.HexRegistries;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.gametest.framework.GameTestServer;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.dedicated.DedicatedServer;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.server.ServerStartedEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
Expand All @@ -21,6 +24,7 @@
import ram.talia.hexal.common.lib.HexalFeatures;
import ram.talia.hexal.common.lib.hex.HexalArithmetics;
import ram.talia.hexal.common.lib.hex.HexalIotaTypes;
import ram.talia.hexal.common.network.MsgSendEverbookC2S;
import ram.talia.hexal.common.recipe.HexalRecipeSerializers;
import ram.talia.hexal.common.recipe.HexalRecipeTypes;
import ram.talia.hexal.forge.datagen.HexalForgeDataGenerators;
Expand Down Expand Up @@ -83,6 +87,11 @@ private static void initListeners () {
//noinspection Convert2MethodRef
ForgePacketHandler.init();
}));

evBus.addListener((ServerStartedEvent event)->{
//rule them out like this so that it doesn't try to access client-only stuff on a dedicated server
MsgSendEverbookC2S.isSingleplayer = !(event.getServer() instanceof DedicatedServer || event.getServer() instanceof GameTestServer);
});


// We have to do these at some point when the registries are still open
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public static void toggleMacro (ServerPlayer player, HexPattern key) {
@SubscribeEvent
public static void playerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
ServerPlayer player = (ServerPlayer) event.getEntity();

if (player.server.getPlayerCount() > 1){
MsgSendEverbookC2S.isSingleplayer = false; //for handling everbook size restrictions if someone joins a LAN world
}

if (!everbooks.containsKey(player.getUUID()))
everbooks.put(player.getUUID(), new Everbook(player.getUUID()));
Expand Down
Loading