Skip to content

Commit d3b60b3

Browse files
committed
make player leave world after 3 crashes
1 parent 3e2cba7 commit d3b60b3

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

src/main/java/org/polyfrost/crashpatch/mixin/MixinMinecraft.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import cc.polyfrost.oneconfig.events.event.RenderEvent;
55
import cc.polyfrost.oneconfig.events.event.Stage;
66
import cc.polyfrost.oneconfig.utils.gui.GuiUtils;
7+
import net.minecraft.client.multiplayer.WorldClient;
8+
import net.minecraft.client.network.NetHandlerPlayClient;
9+
import net.minecraft.client.renderer.EntityRenderer;
10+
import net.minecraft.util.ChatComponentText;
711
import org.polyfrost.crashpatch.config.CrashPatchConfig;
812
import org.polyfrost.crashpatch.crashes.StateManager;
913
import org.polyfrost.crashpatch.gui.CrashGui;
@@ -48,6 +52,8 @@
4852

4953
import java.awt.*;
5054
import java.util.Objects;
55+
import java.util.Queue;
56+
import java.util.concurrent.FutureTask;
5157

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

122128
@Shadow public abstract void displayCrashReport(CrashReport crashReportIn);
123129
@Shadow private int leftClickCounter;
130+
131+
@Shadow public abstract NetHandlerPlayClient getNetHandler();
132+
133+
@Shadow public abstract void loadWorld(WorldClient worldClientIn);
134+
135+
@Shadow public EntityRenderer entityRenderer;
136+
@Shadow @Final private Queue<FutureTask<?>> scheduledTasks;
124137
@Unique
125138
private int crashpatch$clientCrashCount = 0;
126139
@Unique
@@ -198,7 +211,7 @@ private void onGUIDisplay(GuiScreen i, CallbackInfo ci) {
198211
crashpatch$letDie = true;
199212
}
200213
if ((crashpatch$clientCrashCount >= CrashPatchConfig.INSTANCE.getCrashLimit() || crashpatch$serverCrashCount >= CrashPatchConfig.INSTANCE.getCrashLimit())) {
201-
logger.error("Crash limit reached, exiting");
214+
logger.error("Crash limit reached, exiting game");
202215
crashpatch$letDie = true;
203216
}
204217
displayCrashReport(report);
@@ -208,7 +221,6 @@ private void onGUIDisplay(GuiScreen i, CallbackInfo ci) {
208221
// Reset hasCrashed, debugCrashKeyPressTime, and crashIntegratedServerNextTick
209222
hasCrashed = false;
210223
debugCrashKeyPressTime = -1;
211-
// crashIntegratedServerNextTick = false;
212224

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

248260
StateManager.INSTANCE.resetStates();
249261

250-
/*/
251-
if (getNetHandler() != null) {
252-
getNetHandler().getNetworkManager().closeChannel(new ChatComponentText("[CrashPatch] Client crashed"));
253-
}
254-
loadWorld(null);
255-
256-
if (entityRenderer.isShaderActive()) {
257-
entityRenderer.stopUseShader();
258-
}
262+
if (crashpatch$clientCrashCount >= CrashPatchConfig.INSTANCE.getLeaveLimit() || crashpatch$serverCrashCount >= CrashPatchConfig.INSTANCE.getLeaveLimit()) {
263+
logger.error("Crash limit reached, exiting world");
264+
CrashGui.Companion.setLeaveWorldCrash$CrashPatch_1_8_9_forge(true);
265+
if (getNetHandler() != null) {
266+
getNetHandler().getNetworkManager().closeChannel(new ChatComponentText("[CrashPatch] Client crashed"));
267+
}
268+
loadWorld(null);
259269

260-
scheduledTasks.clear(); // TODO: Figure out why this isn't necessary for vanilla disconnect
270+
if (entityRenderer.isShaderActive()) {
271+
entityRenderer.stopUseShader();
272+
}
261273

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

263-
*/
264277
crashpatch$resetState();
265278

266279
if (originalMemoryReserveSize != -1) {

src/main/kotlin/org/polyfrost/crashpatch/config/CrashPatchConfig.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,21 @@ object CrashPatchConfig : Config(Mod("CrashPatch", ModType.UTIL_QOL, "/assets/cr
3535

3636
// Limits
3737
@Info(
38-
text = "It's recommended to restart your game after every few crashes, to avoid severe instability",
38+
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",
3939
type = InfoType.WARNING,
4040
size = 2,
4141
subcategory = "Limits"
4242
)
4343
var ignored: Boolean = false
4444

45+
@Slider(
46+
name = "World Leave Limit",
47+
min = 1f,
48+
max = 20f,
49+
step = 1,
50+
subcategory = "Limits"
51+
)
52+
var leaveLimit = 3
4553

4654
@Slider(
4755
name = "Crash Limit",

src/main/kotlin/org/polyfrost/crashpatch/gui/CrashGui.kt

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import cc.polyfrost.oneconfig.gui.animations.EaseInOutQuad
77
import cc.polyfrost.oneconfig.gui.animations.EaseOutQuad
88
import cc.polyfrost.oneconfig.gui.elements.BasicButton
99
import cc.polyfrost.oneconfig.libs.universal.UDesktop
10+
import cc.polyfrost.oneconfig.libs.universal.UMatrixStack
1011
import cc.polyfrost.oneconfig.libs.universal.UResolution.windowHeight
1112
import cc.polyfrost.oneconfig.libs.universal.UResolution.windowWidth
13+
import cc.polyfrost.oneconfig.libs.universal.UScreen
1214
import cc.polyfrost.oneconfig.platform.Platform
1315
import cc.polyfrost.oneconfig.renderer.NanoVGHelper
1416
import cc.polyfrost.oneconfig.renderer.asset.Icon
@@ -23,13 +25,12 @@ import cc.polyfrost.oneconfig.utils.Notifications
2325
import cc.polyfrost.oneconfig.utils.color.ColorPalette
2426
import cc.polyfrost.oneconfig.utils.color.ColorUtils
2527
import cc.polyfrost.oneconfig.utils.dsl.*
26-
import cc.polyfrost.oneconfig.utils.gui.OneUIScreen
28+
import net.minecraft.crash.CrashReport
2729
import org.polyfrost.crashpatch.CrashPatch
2830
import org.polyfrost.crashpatch.crashes.CrashHelper
2931
import org.polyfrost.crashpatch.crashes.CrashScan
3032
import org.polyfrost.crashpatch.hooks.CrashReportHook
3133
import org.polyfrost.crashpatch.utils.InternetUtils
32-
import net.minecraft.crash.CrashReport
3334
import java.io.File
3435
import java.net.URI
3536

@@ -39,7 +40,7 @@ class CrashGui @JvmOverloads constructor(
3940
private val susThing: String,
4041
private val type: GuiType = GuiType.NORMAL,
4142
val throwable: Throwable? = null
42-
) : OneUIScreen() {
43+
) : UScreen(false) {
4344
@JvmOverloads
4445
constructor(report: CrashReport, type: GuiType = GuiType.NORMAL) : this(
4546
report.completeReport,
@@ -171,7 +172,22 @@ class CrashGui @JvmOverloads constructor(
171172

172173
private var vg = -1L
173174

174-
override fun draw(vg: Long, partialTicks: Float, inputHandler: InputHandler) {
175+
private val inputHandler = InputHandler()
176+
177+
override fun onDrawScreen(matrixStack: UMatrixStack, mouseX: Int, mouseY: Int, partialTicks: Float) {
178+
super.onDrawScreen(matrixStack, mouseX, mouseY, partialTicks)
179+
if (mc.theWorld == null && leaveWorldCrash) {
180+
drawDefaultBackground()
181+
}
182+
NanoVGHelper.INSTANCE.setupAndDraw { draw(it, inputHandler) }
183+
}
184+
185+
override fun onScreenClose() {
186+
super.onScreenClose()
187+
leaveWorldCrash = false
188+
}
189+
190+
fun draw(vg: Long, inputHandler: InputHandler) {
175191
this.vg = vg
176192
nanoVG(vg) {
177193
FontHelper.INSTANCE.loadFont(vg, JETBRAINS_MONO)
@@ -431,4 +447,8 @@ class CrashGui @JvmOverloads constructor(
431447
enum class GuiType {
432448
INIT, NORMAL, DISCONNECT
433449
}
450+
451+
companion object {
452+
internal var leaveWorldCrash = false
453+
}
434454
}

0 commit comments

Comments
 (0)