Skip to content

Commit

Permalink
make player leave world after 3 crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Wyvest committed Nov 23, 2023
1 parent 3e2cba7 commit d3b60b3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
39 changes: 26 additions & 13 deletions src/main/java/org/polyfrost/crashpatch/mixin/MixinMinecraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import cc.polyfrost.oneconfig.events.event.RenderEvent;
import cc.polyfrost.oneconfig.events.event.Stage;
import cc.polyfrost.oneconfig.utils.gui.GuiUtils;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.client.network.NetHandlerPlayClient;
import net.minecraft.client.renderer.EntityRenderer;
import net.minecraft.util.ChatComponentText;
import org.polyfrost.crashpatch.config.CrashPatchConfig;
import org.polyfrost.crashpatch.crashes.StateManager;
import org.polyfrost.crashpatch.gui.CrashGui;
Expand Down Expand Up @@ -48,6 +52,8 @@

import java.awt.*;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.FutureTask;

@Mixin(value = Minecraft.class, priority = -9000)
public abstract class MixinMinecraft implements MinecraftHook {
Expand Down Expand Up @@ -121,6 +127,13 @@ public abstract class MixinMinecraft implements MinecraftHook {

@Shadow public abstract void displayCrashReport(CrashReport crashReportIn);
@Shadow private int leftClickCounter;

@Shadow public abstract NetHandlerPlayClient getNetHandler();

@Shadow public abstract void loadWorld(WorldClient worldClientIn);

@Shadow public EntityRenderer entityRenderer;
@Shadow @Final private Queue<FutureTask<?>> scheduledTasks;
@Unique
private int crashpatch$clientCrashCount = 0;
@Unique
Expand Down Expand Up @@ -198,7 +211,7 @@ private void onGUIDisplay(GuiScreen i, CallbackInfo ci) {
crashpatch$letDie = true;
}
if ((crashpatch$clientCrashCount >= CrashPatchConfig.INSTANCE.getCrashLimit() || crashpatch$serverCrashCount >= CrashPatchConfig.INSTANCE.getCrashLimit())) {
logger.error("Crash limit reached, exiting");
logger.error("Crash limit reached, exiting game");
crashpatch$letDie = true;
}
displayCrashReport(report);
Expand All @@ -208,7 +221,6 @@ private void onGUIDisplay(GuiScreen i, CallbackInfo ci) {
// Reset hasCrashed, debugCrashKeyPressTime, and crashIntegratedServerNextTick
hasCrashed = false;
debugCrashKeyPressTime = -1;
// crashIntegratedServerNextTick = false;

// Vanilla does this when switching to main menu but not our custom crash screen
// nor the out of memory screen (see https://bugs.mojang.com/browse/MC-128953)
Expand Down Expand Up @@ -247,20 +259,21 @@ private void onGUIDisplay(GuiScreen i, CallbackInfo ci) {

StateManager.INSTANCE.resetStates();

/*/
if (getNetHandler() != null) {
getNetHandler().getNetworkManager().closeChannel(new ChatComponentText("[CrashPatch] Client crashed"));
}
loadWorld(null);
if (entityRenderer.isShaderActive()) {
entityRenderer.stopUseShader();
}
if (crashpatch$clientCrashCount >= CrashPatchConfig.INSTANCE.getLeaveLimit() || crashpatch$serverCrashCount >= CrashPatchConfig.INSTANCE.getLeaveLimit()) {
logger.error("Crash limit reached, exiting world");
CrashGui.Companion.setLeaveWorldCrash$CrashPatch_1_8_9_forge(true);
if (getNetHandler() != null) {
getNetHandler().getNetworkManager().closeChannel(new ChatComponentText("[CrashPatch] Client crashed"));
}
loadWorld(null);

scheduledTasks.clear(); // TODO: Figure out why this isn't necessary for vanilla disconnect
if (entityRenderer.isShaderActive()) {
entityRenderer.stopUseShader();
}

scheduledTasks.clear(); // TODO: Figure out why this isn't necessary for vanilla disconnect
}

*/
crashpatch$resetState();

if (originalMemoryReserveSize != -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ object CrashPatchConfig : Config(Mod("CrashPatch", ModType.UTIL_QOL, "/assets/cr

// Limits
@Info(
text = "It's recommended to restart your game after every few crashes, to avoid severe instability",
text = "It's recommended to leave the world after a few crashes, and outright quit the game if there are more; this is to avoid severe instability",
type = InfoType.WARNING,
size = 2,
subcategory = "Limits"
)
var ignored: Boolean = false

@Slider(
name = "World Leave Limit",
min = 1f,
max = 20f,
step = 1,
subcategory = "Limits"
)
var leaveLimit = 3

@Slider(
name = "Crash Limit",
Expand Down
28 changes: 24 additions & 4 deletions src/main/kotlin/org/polyfrost/crashpatch/gui/CrashGui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import cc.polyfrost.oneconfig.gui.animations.EaseInOutQuad
import cc.polyfrost.oneconfig.gui.animations.EaseOutQuad
import cc.polyfrost.oneconfig.gui.elements.BasicButton
import cc.polyfrost.oneconfig.libs.universal.UDesktop
import cc.polyfrost.oneconfig.libs.universal.UMatrixStack
import cc.polyfrost.oneconfig.libs.universal.UResolution.windowHeight
import cc.polyfrost.oneconfig.libs.universal.UResolution.windowWidth
import cc.polyfrost.oneconfig.libs.universal.UScreen
import cc.polyfrost.oneconfig.platform.Platform
import cc.polyfrost.oneconfig.renderer.NanoVGHelper
import cc.polyfrost.oneconfig.renderer.asset.Icon
Expand All @@ -23,13 +25,12 @@ import cc.polyfrost.oneconfig.utils.Notifications
import cc.polyfrost.oneconfig.utils.color.ColorPalette
import cc.polyfrost.oneconfig.utils.color.ColorUtils
import cc.polyfrost.oneconfig.utils.dsl.*
import cc.polyfrost.oneconfig.utils.gui.OneUIScreen
import net.minecraft.crash.CrashReport
import org.polyfrost.crashpatch.CrashPatch
import org.polyfrost.crashpatch.crashes.CrashHelper
import org.polyfrost.crashpatch.crashes.CrashScan
import org.polyfrost.crashpatch.hooks.CrashReportHook
import org.polyfrost.crashpatch.utils.InternetUtils
import net.minecraft.crash.CrashReport
import java.io.File
import java.net.URI

Expand All @@ -39,7 +40,7 @@ class CrashGui @JvmOverloads constructor(
private val susThing: String,
private val type: GuiType = GuiType.NORMAL,
val throwable: Throwable? = null
) : OneUIScreen() {
) : UScreen(false) {
@JvmOverloads
constructor(report: CrashReport, type: GuiType = GuiType.NORMAL) : this(
report.completeReport,
Expand Down Expand Up @@ -171,7 +172,22 @@ class CrashGui @JvmOverloads constructor(

private var vg = -1L

override fun draw(vg: Long, partialTicks: Float, inputHandler: InputHandler) {
private val inputHandler = InputHandler()

override fun onDrawScreen(matrixStack: UMatrixStack, mouseX: Int, mouseY: Int, partialTicks: Float) {
super.onDrawScreen(matrixStack, mouseX, mouseY, partialTicks)
if (mc.theWorld == null && leaveWorldCrash) {
drawDefaultBackground()
}
NanoVGHelper.INSTANCE.setupAndDraw { draw(it, inputHandler) }
}

override fun onScreenClose() {
super.onScreenClose()
leaveWorldCrash = false
}

fun draw(vg: Long, inputHandler: InputHandler) {
this.vg = vg
nanoVG(vg) {
FontHelper.INSTANCE.loadFont(vg, JETBRAINS_MONO)
Expand Down Expand Up @@ -431,4 +447,8 @@ class CrashGui @JvmOverloads constructor(
enum class GuiType {
INIT, NORMAL, DISCONNECT
}

companion object {
internal var leaveWorldCrash = false
}
}

0 comments on commit d3b60b3

Please sign in to comment.