Skip to content

Commit b4d9003

Browse files
Bypass everbook size limit in singleplayer (#185)
2 parents 301756e + 1bde71a commit b4d9003

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

Common/src/main/java/ram/talia/hexal/common/network/MsgSendEverbookC2S.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import ram.talia.hexal.xplat.IXplatAbstractions
1818
data class MsgSendEverbookC2S(val everbook: Everbook) : IMessage {
1919
private var wasTooBig = false
2020
private var somethingWeirdHappened = false
21+
private var amountOverLimit = -1L
2122
override fun serialize(buf: FriendlyByteBuf) {
2223
val bookNbt = everbook.serialiseToNBT()
2324
val bytes = bookNbt.toCompressedBytes()
@@ -34,9 +35,10 @@ data class MsgSendEverbookC2S(val everbook: Everbook) : IMessage {
3435
if (wasTooBig){
3536
HexalAPI.LOGGER.info("Player ${sender.displayName.string}'s everbook data exceeded the configured size limit of ${HexalConfig.server.everbookMaxSize}")
3637
sender.sendSystemMessage(Component.translatable("hexal.everbook.sizewarning", HexalConfig.server.everbookMaxSize))
37-
}
38-
if (somethingWeirdHappened){
38+
} else if (somethingWeirdHappened){
3939
sender.sendSystemMessage(Component.translatable("hexal.everbook.unexpectederror"))
40+
} else if (isSingleplayer && amountOverLimit > 0 && server.playerCount <= 1){
41+
sender.sendSystemMessage(Component.translatable("hexal.everbook.overlimitsingleplayer", HexalConfig.server.everbookMaxSize, amountOverLimit))
4042
}
4143
}
4244
}
@@ -45,16 +47,24 @@ data class MsgSendEverbookC2S(val everbook: Everbook) : IMessage {
4547
@JvmField
4648
val ID: ResourceLocation = HexalAPI.modLoc("sendever")
4749

50+
@JvmField var isSingleplayer = false
51+
4852
@JvmStatic
4953
fun deserialise(buffer: ByteBuf): MsgSendEverbookC2S {
5054
val buf = FriendlyByteBuf(buffer)
5155
val uuid = buf.readUUID()
5256
val bytes = buf.readByteArray()
5357
var packet = MsgSendEverbookC2S(Everbook(uuid, mutableMapOf(), listOf()))
58+
val maxSize = if (isSingleplayer) {
59+
Long.MAX_VALUE
60+
} else {
61+
HexalConfig.server.everbookMaxSize
62+
}
5463
try {
55-
val decompressed = bytes.decompressToNBT(HexalConfig.server.everbookMaxSize)
64+
val decompressed = bytes.decompressToNBT(maxSize)
5665
HexalAPI.LOGGER.info("Deserializing everbook data of size ${bytes.size} decompressed to size ${decompressed.sizeInBytes()}")
5766
packet = MsgSendEverbookC2S(Everbook.fromNbt(decompressed))
67+
packet.amountOverLimit = decompressed.sizeInBytes() - HexalConfig.server.everbookMaxSize
5868
} catch (_ : NbtSizeException){
5969
packet.wasTooBig = true
6070
} catch (e : Exception){

Common/src/main/resources/assets/hexal/lang/en_us.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,5 +418,6 @@
418418
"text.autoconfig.hexal.option.server.greatSpells.tickRandomTickIProb.@Tooltip[2]": "Bound from 600 to 2100.",
419419

420420
"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.",
421-
"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."
421+
"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.",
422+
"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."
422423
}

Fabric/src/main/java/ram/talia/hexal/fabric/FabricHexalInitializer.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ import net.fabricmc.api.ModInitializer
99
import net.fabricmc.fabric.api.biome.v1.BiomeModifications
1010
import net.fabricmc.fabric.api.biome.v1.BiomeSelectors
1111
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents
12+
import net.fabricmc.fabric.api.networking.v1.PacketSender
13+
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents
1214
import net.fabricmc.fabric.api.tag.convention.v1.ConventionalBiomeTags
1315
import net.minecraft.core.Registry
1416
import net.minecraft.core.registries.BuiltInRegistries
1517
import net.minecraft.core.registries.Registries
18+
import net.minecraft.gametest.framework.GameTestServer
1619
import net.minecraft.resources.ResourceKey
1720
import net.minecraft.resources.ResourceLocation
21+
import net.minecraft.server.MinecraftServer
22+
import net.minecraft.server.dedicated.DedicatedServer
23+
import net.minecraft.server.network.ServerGamePacketListenerImpl
1824
import net.minecraft.world.level.levelgen.GenerationStep
1925
import ram.talia.hexal.api.HexalAPI
2026
import ram.talia.hexal.api.gates.GateManager
@@ -24,6 +30,7 @@ import ram.talia.hexal.common.lib.HexalFeatures
2430
import ram.talia.hexal.common.lib.hex.HexalActions
2531
import ram.talia.hexal.common.lib.hex.HexalArithmetics
2632
import ram.talia.hexal.common.lib.hex.HexalIotaTypes
33+
import ram.talia.hexal.common.network.MsgSendEverbookC2S
2734
import ram.talia.hexal.common.recipe.HexalRecipeSerializers
2835
import ram.talia.hexal.common.recipe.HexalRecipeTypes
2936
import ram.talia.hexal.fabric.interop.phantom.OpPhaseBlock
@@ -54,12 +61,20 @@ object FabricHexalInitializer : ModInitializer {
5461
ServerLifecycleEvents.SERVER_STARTED.register {
5562
val savedData = it.overworld().dataStorage.computeIfAbsent(::GateSavedData, ::GateSavedData, FILE_GATE_MANAGER)
5663
savedData.setDirty()
64+
MsgSendEverbookC2S.isSingleplayer = !(it is DedicatedServer || it is GameTestServer)
5765
}
5866
ServerLifecycleEvents.SERVER_STOPPING.register {
5967
val savedData = it.overworld().dataStorage.computeIfAbsent(::GateSavedData, ::GateSavedData, FILE_GATE_MANAGER)
6068
GateManager.shouldClearOnWrite = true
6169
savedData.setDirty()
6270
}
71+
ServerPlayConnectionEvents.JOIN.register(this::onPlayerJoin)
72+
}
73+
74+
private fun onPlayerJoin(impl : ServerGamePacketListenerImpl, sender : PacketSender, server : MinecraftServer){
75+
if (server.playerCount > 1) {
76+
MsgSendEverbookC2S.isSingleplayer = false //for handling everbook size restrictions if someone joins a LAN world
77+
}
6378
}
6479

6580
private fun initRegistries() {

Forge/src/main/java/ram/talia/hexal/forge/ForgeHexalInitializer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import at.petrak.hexcasting.common.lib.HexRegistries;
44
import net.minecraft.core.Registry;
55
import net.minecraft.core.registries.Registries;
6+
import net.minecraft.gametest.framework.GameTestServer;
67
import net.minecraft.resources.ResourceKey;
78
import net.minecraft.resources.ResourceLocation;
9+
import net.minecraft.server.dedicated.DedicatedServer;
810
import net.minecraftforge.common.ForgeConfigSpec;
911
import net.minecraftforge.common.MinecraftForge;
12+
import net.minecraftforge.event.server.ServerStartedEvent;
1013
import net.minecraftforge.eventbus.api.IEventBus;
1114
import net.minecraftforge.fml.ModLoadingContext;
1215
import net.minecraftforge.fml.common.Mod;
@@ -21,6 +24,7 @@
2124
import ram.talia.hexal.common.lib.HexalFeatures;
2225
import ram.talia.hexal.common.lib.hex.HexalArithmetics;
2326
import ram.talia.hexal.common.lib.hex.HexalIotaTypes;
27+
import ram.talia.hexal.common.network.MsgSendEverbookC2S;
2428
import ram.talia.hexal.common.recipe.HexalRecipeSerializers;
2529
import ram.talia.hexal.common.recipe.HexalRecipeTypes;
2630
import ram.talia.hexal.forge.datagen.HexalForgeDataGenerators;
@@ -83,6 +87,11 @@ private static void initListeners () {
8387
//noinspection Convert2MethodRef
8488
ForgePacketHandler.init();
8589
}));
90+
91+
evBus.addListener((ServerStartedEvent event)->{
92+
//rule them out like this so that it doesn't try to access client-only stuff on a dedicated server
93+
MsgSendEverbookC2S.isSingleplayer = !(event.getServer() instanceof DedicatedServer || event.getServer() instanceof GameTestServer);
94+
});
8695

8796

8897
// We have to do these at some point when the registries are still open

Forge/src/main/java/ram/talia/hexal/forge/eventhandlers/EverbookEventHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public static void toggleMacro (ServerPlayer player, HexPattern key) {
8080
@SubscribeEvent
8181
public static void playerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
8282
ServerPlayer player = (ServerPlayer) event.getEntity();
83+
84+
if (player.server.getPlayerCount() > 1){
85+
MsgSendEverbookC2S.isSingleplayer = false; //for handling everbook size restrictions if someone joins a LAN world
86+
}
8387

8488
if (!everbooks.containsKey(player.getUUID()))
8589
everbooks.put(player.getUUID(), new Everbook(player.getUUID()));

0 commit comments

Comments
 (0)