Skip to content
Snippets Groups Projects
Commit edde392c authored by Sargun Vohra's avatar Sargun Vohra
Browse files

add failsafe for data pack silent failures, fix #21

parent e7d05338
No related branches found
Tags 4.3.0
No related merge requests found
......@@ -2,3 +2,4 @@
* added advancements for first and max hp levels
* changed `/leveluphp set*` commands to not implicitly target self
* changed `/leveluphp config` command to not take a player argument
* added failsafe to force a crash in case data loading silently fails
package me.sargunvohra.mcmods.leveluphp.mixin;
import com.mojang.authlib.GameProfile;
import me.sargunvohra.mcmods.leveluphp.LevelUpHp;
import me.sargunvohra.mcmods.leveluphp.UtilKt;
import me.sargunvohra.mcmods.leveluphp.level.HpLevelHandler;
import net.minecraft.entity.Entity;
......@@ -46,16 +47,23 @@ public abstract class ServerPlayerEntityMixin extends PlayerEntity {
@Inject(method = "writeCustomDataToTag", at = @At("HEAD"))
private void onWriteCustomDataToTag(CompoundTag tag, CallbackInfo ci) {
leveluphp_ensureDataLoaded();
HpLevelHandler handler = UtilKt.getHpLevelHandler(this);
tag.put("leveluphp", handler.writeToTag());
}
@Inject(method = "readCustomDataFromTag", at = @At("HEAD"))
private void onReadCustomDataFromTag(CompoundTag tag, CallbackInfo ci) {
leveluphp_ensureDataLoaded();
HpLevelHandler handler = UtilKt.getHpLevelHandler(this);
Tag data = tag.getTag("leveluphp");
if (data != null) {
handler.readFromTag(data);
}
}
private void leveluphp_ensureDataLoaded() {
if (!LevelUpHp.INSTANCE.getReloadListener().getSuccessfullyLoadedDataPack())
throw new RuntimeException("One of your mods broke data loading; forcing a crash to preserve your levels!");
}
}
......@@ -14,14 +14,14 @@ import net.minecraft.util.registry.Registry
@Suppress("unused")
object LevelUpHp : ModInitializer {
val LEVEL_UP_SOUND = SoundEvent(id("levelup"))
val RELOAD_LISTENER = ReloadListener()
val levelUpSound = SoundEvent(id("levelup"))
val reloadListener = ReloadListener()
override fun onInitialize() {
Registry.register(Registry.SOUND_EVENT, id("levelup"), LEVEL_UP_SOUND)
Registry.register(Registry.SOUND_EVENT, id("levelup"), levelUpSound)
Registry.register(Registry.ITEM, id("heart_container"), HeartContainerItem())
ServerStartCallback.EVENT.register(CommandLoader)
ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(RELOAD_LISTENER)
ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(reloadListener)
}
fun id(name: String) = Identifier("leveluphp", name)
......
......@@ -68,7 +68,7 @@ fun buildLevelUpHpCommand(): LiteralArgumentBuilder<ServerCommandSource> {
literal("config")
.executes {
it.source.sendFeedback(
TextComponent(LevelUpHp.RELOAD_LISTENER.config.toString()),
TextComponent(LevelUpHp.reloadListener.config.toString()),
false
)
return@executes 0
......
......@@ -25,7 +25,7 @@ data class LevellingConfig(
levelPenaltyFunction.validate()
primaryXpValues.validate()
overrides.forEach { entityId, xp ->
overrides.forEach { (entityId, xp) ->
check(entityId.isNotEmpty())
check(xp >= 0)
}
......
......@@ -13,6 +13,10 @@ class ReloadListener : SimpleSynchronousResourceReloadListener {
private val gson = Gson()
var config: LevellingConfig = LevellingConfig()
// failsafe to allow us to force a crash in case data loading silently fails
var successfullyLoadedDataPack = false
private set
init {
config.validate()
}
......@@ -39,6 +43,7 @@ class ReloadListener : SimpleSynchronousResourceReloadListener {
config.validate()
this.config = config
successfullyLoadedDataPack = true
}
override fun getFabricId() = LevelUpHp.id("reload_listener")
......
......@@ -49,7 +49,7 @@ class HpLevelHandler {
val currentXpTarget get() = config.xpTargetFunction(level)
val isMaxedOut get() = level >= config.maximumLevel
val config get() = LevelUpHp.RELOAD_LISTENER.config
val config get() = LevelUpHp.reloadListener.config
fun applyKill(killed: Entity) {
val typeId = Registry.ENTITY_TYPE.getId(killed.type)
......@@ -134,7 +134,7 @@ class HpLevelHandler {
player.world.playSound(
null,
player.x, player.y, player.z,
LevelUpHp.LEVEL_UP_SOUND,
LevelUpHp.levelUpSound,
SoundCategory.PLAYERS,
1f, 1f
)
......
......@@ -34,7 +34,7 @@ class SyncPacketConsumer : PacketConsumer {
context.taskQueue.execute {
try {
config.validate()
LevelUpHp.RELOAD_LISTENER.config = config
LevelUpHp.reloadListener.config = config
} catch (e: IllegalStateException) {
e.printStackTrace()
}
......@@ -57,7 +57,7 @@ class SyncPacketConsumer : PacketConsumer {
tag.write(output)
// write config data (shitty way to do it but ¯\_(ツ)_/¯)
val configJson = gson.toJson(LevelUpHp.RELOAD_LISTENER.config)
val configJson = gson.toJson(LevelUpHp.reloadListener.config)
output.writeInt(configJson.length)
output.writeChars(configJson)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment