Skip to content

Commit 77009c0

Browse files
authored
Large Refactor
use less intrusive mixins (replaced some redirects with modifyconstant/-variable) fix the "still saving the last world" screen never actually rendering fix savelock not applying on old worlds make savelock apply later for faster resets remove/replace unused and unnecessary code dont render menu.quitworld in multiplayer more open dependencies more vanilla / less copypaste code and some more stuff
2 parents e758d0a + b8d8233 commit 77009c0

File tree

14 files changed

+163
-234
lines changed

14 files changed

+163
-234
lines changed

build.gradle

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,11 @@ dependencies {
2525

2626
processResources {
2727
inputs.property "version", project.version
28+
filteringCharset "UTF-8"
2829

29-
// from(sourceSets.main.resources.srcDirs) {
30-
// include "fabric.mod.json"
31-
// expand "version": project.version
32-
// }
33-
//
34-
// from(sourceSets.main.resources.srcDirs) {
35-
// exclude "fabric.mod.json"
36-
// }
30+
filesMatching("fabric.mod.json") {
31+
expand "version": project.version
32+
}
3733
}
3834

3935
// ensure that the encoding is set to UTF-8, no matter what the system default is

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ minecraft_version=1.16.1
66
yarn_mappings=1.16.1+build.21
77
loader_version=0.11.3
88
# Mod Properties
9-
mod_version=1.4.1
9+
mod_version=1.5
1010
maven_group=fast-reset-1.16.1-v
1111
archives_base_name=fast-reset-1.16.1-v

src/main/java/fast_reset/client/Client.java

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package fast_reset.client;
2+
3+
import net.fabricmc.api.ClientModInitializer;
4+
import net.fabricmc.loader.api.FabricLoader;
5+
import org.apache.logging.log4j.LogManager;
6+
import org.apache.logging.log4j.Logger;
7+
8+
import java.io.*;
9+
import java.nio.file.Files;
10+
11+
public class FastReset implements ClientModInitializer {
12+
13+
public static final Logger LOGGER = LogManager.getLogger();
14+
private static final File configurationFile = FabricLoader.getInstance().getConfigDir().resolve("fastReset").resolve("settings.txt").toFile();
15+
16+
public static boolean saveOnQuit = true;
17+
public static Boolean saving = false;
18+
public static final Object saveLock = new Object();
19+
20+
public static int buttonLocation = 0;
21+
22+
public static void updateButtonLocation() {
23+
buttonLocation = (buttonLocation + 1) % 3;
24+
save();
25+
}
26+
27+
private static void save() {
28+
try {
29+
Files.write(configurationFile.toPath(), String.valueOf(buttonLocation).getBytes());
30+
} catch (IOException e) {
31+
LOGGER.error("Failed to save FastReset config", e);
32+
}
33+
}
34+
35+
@Override
36+
public void onInitializeClient() {
37+
LOGGER.info("Using Fast Reset");
38+
39+
if (!configurationFile.exists()) {
40+
try {
41+
if (!configurationFile.getParentFile().mkdirs()) {
42+
throw new IOException("couldn't make config folder");
43+
}
44+
if (!configurationFile.createNewFile()) {
45+
throw new IOException("couldn't make config file");
46+
}
47+
save();
48+
} catch (IOException e) {
49+
LOGGER.error("Failed to create FastReset config", e);
50+
}
51+
return;
52+
}
53+
54+
try {
55+
BufferedReader reader = new BufferedReader(new FileReader(configurationFile));
56+
buttonLocation = Integer.parseInt(reader.readLine()) % 3;
57+
reader.close();
58+
} catch (IOException | NumberFormatException e) {
59+
LOGGER.error("Failed to load FastReset config", e);
60+
}
61+
}
62+
}

src/main/java/fast_reset/client/client/Client.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/main/java/fast_reset/client/client/SaveWorldScreen.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/main/java/fast_reset/client/mixin/ClientMixin.java

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
package fast_reset.client.mixin;
22

3-
import fast_reset.client.Client;
3+
import fast_reset.client.FastReset;
44
import net.minecraft.client.gui.screen.GameMenuScreen;
5-
import net.minecraft.client.gui.screen.SaveLevelScreen;
65
import net.minecraft.client.gui.screen.Screen;
7-
import net.minecraft.client.gui.screen.TitleScreen;
8-
import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen;
96
import net.minecraft.client.gui.widget.ButtonWidget;
10-
import net.minecraft.realms.RealmsBridge;
117
import net.minecraft.text.Text;
128
import net.minecraft.text.TranslatableText;
139
import org.spongepowered.asm.mixin.Mixin;
1410
import org.spongepowered.asm.mixin.injection.At;
15-
import org.spongepowered.asm.mixin.injection.Inject;
16-
import org.spongepowered.asm.mixin.injection.Redirect;
17-
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
11+
import org.spongepowered.asm.mixin.injection.ModifyVariable;
1812

1913
@Mixin(GameMenuScreen.class)
2014
public class GameMenuMixin extends Screen {
@@ -23,26 +17,17 @@ protected GameMenuMixin(Text title) {
2317
super(title);
2418
}
2519

26-
private static final int bottomRightWidth = 102;
27-
28-
// kill save on the shutdown
29-
@Redirect(method = "initWidgets", at = @At(value = "NEW", target = "net/minecraft/client/gui/widget/ButtonWidget", ordinal=7))
30-
private ButtonWidget createExitButton(int defaultX, int defaultY, int defaultWidth, int height, Text message, ButtonWidget.PressAction onPress){
31-
int x = Client.buttonLocation == 2 ? (int) (this.width - (bottomRightWidth * 1.5) - 4) : defaultX;
32-
int y = Client.buttonLocation == 2 ? this.height - 24 : defaultY;
33-
int width = Client.buttonLocation == 2 ? (int) (bottomRightWidth * 1.5) : defaultWidth;
34-
35-
return new ButtonWidget(x, y, width, height, message, onPress);
36-
}
20+
@ModifyVariable(method = "initWidgets", at = @At(value = "STORE", ordinal = 1))
21+
private ButtonWidget createExitButton(ButtonWidget saveButton) {
22+
if (!this.client.isInSingleplayer()) {
23+
return saveButton;
24+
}
3725

38-
@Inject(method = "initWidgets", at=@At(value ="TAIL"))
39-
private void createSaveButton(CallbackInfo ci){
4026
int height = 20;
41-
4227
int width;
4328
int x;
4429
int y;
45-
switch(Client.buttonLocation){
30+
switch (FastReset.buttonLocation){
4631
// bottom right build
4732
case 0:
4833
width = 102;
@@ -52,40 +37,27 @@ private void createSaveButton(CallbackInfo ci){
5237
// center build
5338
case 1:
5439
width = 204;
55-
x = this.width / 2 - width/2;
40+
x = this.width / 2 - width / 2;
5641
y = this.height / 4 + 148 - height;
5742
break;
43+
// replace s&q build
5844
case 2:
5945
default:
6046
width = 204;
61-
x = this.width / 2 - width/2;
47+
x = this.width / 2 - width / 2;
6248
y = this.height / 4 + 124 - height;
49+
50+
saveButton.x = (int) (this.width - (102 * 1.5) - 4);
51+
saveButton.y = this.height - 24;
52+
saveButton.setWidth((int) (102 * 1.5));
6353
break;
6454
}
6555

66-
6756
this.addButton(new ButtonWidget(x, y, width, height, new TranslatableText("menu.quitWorld"), (buttonWidgetX) -> {
68-
Client.saveOnQuit = false;
69-
70-
boolean bl = this.client.isInSingleplayer();
71-
boolean bl2 = this.client.isConnectedToRealms();
72-
buttonWidgetX.active = false;
73-
this.client.world.disconnect();
74-
if (bl) {
75-
this.client.disconnect(new SaveLevelScreen(new TranslatableText("menu.savingLevel")));
76-
} else {
77-
this.client.disconnect();
78-
}
79-
80-
if (bl) {
81-
this.client.openScreen(new TitleScreen());
82-
} else if (bl2) {
83-
RealmsBridge realmsBridge = new RealmsBridge();
84-
realmsBridge.switchToRealms(new TitleScreen());
85-
} else {
86-
this.client.openScreen(new MultiplayerScreen(new TitleScreen()));
87-
}
88-
Client.saveOnQuit = true;
57+
FastReset.saveOnQuit = false;
58+
saveButton.onPress();
59+
FastReset.saveOnQuit = true;
8960
}));
61+
return saveButton;
9062
}
9163
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package fast_reset.client.mixin;
2+
3+
import fast_reset.client.FastReset;
4+
import net.minecraft.client.MinecraftClient;
5+
import net.minecraft.client.gui.screen.SaveLevelScreen;
6+
import net.minecraft.client.gui.screen.Screen;
7+
import net.minecraft.text.LiteralText;
8+
import org.spongepowered.asm.mixin.Mixin;
9+
import org.spongepowered.asm.mixin.Shadow;
10+
import org.spongepowered.asm.mixin.injection.At;
11+
import org.spongepowered.asm.mixin.injection.Inject;
12+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
13+
14+
@Mixin(MinecraftClient.class)
15+
public abstract class MinecraftClientMixin {
16+
17+
@Shadow public abstract void method_29970(Screen screen);
18+
19+
@Inject(method = "startIntegratedServer(Ljava/lang/String;Lnet/minecraft/util/registry/RegistryTracker$Modifiable;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function4;ZLnet/minecraft/client/MinecraftClient$WorldLoadAction;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;startServer(Ljava/util/function/Function;)Lnet/minecraft/server/MinecraftServer;"))
20+
private void worldWait(CallbackInfo ci) {
21+
synchronized (FastReset.saving) {
22+
if (!FastReset.saving) {
23+
FastReset.LOGGER.info("no save lock active");
24+
return;
25+
}
26+
}
27+
28+
this.method_29970(new SaveLevelScreen(new LiteralText("still saving the last world")));
29+
30+
synchronized(FastReset.saveLock) {
31+
FastReset.LOGGER.info("done waiting for save lock");
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)