Skip to content

Commit 4c717a3

Browse files
committed
Refactor name validation and update formatting logic
Unified and simplified name length validation across server and world screens, replacing custom logic with a single isTooLong method. Refactored ComponentU to streamline color/formatting code parsing. Updated mixin methods and interfaces for consistency, and bumped Minecraft and Fabric Loader versions in gradle.properties and fabric.mod.json.
1 parent 059a7b4 commit 4c717a3

12 files changed

+108
-215
lines changed

gradle.properties

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
# Done to increase the memory available to gradle.
21
org.gradle.jvmargs=-Xmx1G
32

4-
# Fabric Properties
5-
# check these on https://fabricmc.net/develop
6-
minecraft_version=1.20.6
7-
yarn_mappings=1.20.6+build.3
8-
loader_version=0.15.11
9-
# Mod Properties
3+
minecraft_version=1.21.6
4+
loader_version=0.16.1
105
mod_version = 1.0.5
116
maven_group = com.github.voxxin
127
archives_base_name = ColourMyServers
Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package com.github.voxxin.colourmyservers.mixin.server;
22

3-
import com.github.voxxin.colourmyservers.util.ComponentU;
43
import com.github.voxxin.colourmyservers.util.ErrorCatching;
5-
import net.minecraft.client.Minecraft;
64
import net.minecraft.client.gui.components.Button;
75
import net.minecraft.client.gui.components.EditBox;
8-
import net.minecraft.client.gui.components.toasts.SystemToast;
96
import net.minecraft.client.gui.screens.EditServerScreen;
10-
import net.minecraft.client.multiplayer.resolver.ServerAddress;
11-
import net.minecraft.network.chat.Component;
127
import org.spongepowered.asm.mixin.Mixin;
138
import org.spongepowered.asm.mixin.Shadow;
149
import org.spongepowered.asm.mixin.injection.At;
@@ -17,32 +12,20 @@
1712

1813
@Mixin(EditServerScreen.class)
1914
public class EditServerScreenMixin {
20-
2115
@Shadow private EditBox nameEdit;
22-
2316
@Shadow private Button addButton;
2417

2518
@Inject(at = @At("TAIL"), method = "init")
2619
public void init(CallbackInfo ci) {
27-
if (!nameEdit.getValue().isEmpty()) nameEdit.setMaxLength(9999999);
28-
}
29-
30-
@Inject(at = @At("HEAD"), method = "onAdd", cancellable = true)
31-
public void onAdd(CallbackInfo ci) {
32-
if (ErrorCatching.stringTooLong(this.nameEdit.getValue())) {
33-
ErrorCatching.nameTooLongToast();
34-
ci.cancel();
35-
}
20+
nameEdit.setMaxLength(Integer.MAX_VALUE);
3621
}
3722

38-
@Inject(at = @At("HEAD"), method = "updateAddButtonStatus", cancellable = true)
39-
public void updateAddButtonStatus(CallbackInfo ci) {
40-
if (ErrorCatching.stringTooLong(this.nameEdit.getValue())) {
41-
if (this.addButton.active) {
42-
ErrorCatching.nameTooLongToast();
43-
}
44-
this.addButton.active = false;
23+
@Inject(at = @At("HEAD"), method = {"onAdd", "updateAddButtonStatus"}, cancellable = true)
24+
public void handleValidation(CallbackInfo ci) {
25+
if (ErrorCatching.isTooLong(nameEdit.getValue())) {
26+
if (addButton.active) ErrorCatching.showToast();
27+
addButton.active = false;
4528
ci.cancel();
4629
}
4730
}
48-
}
31+
}

src/main/java/com/github/voxxin/colourmyservers/mixin/server/ServerSelectionListMixin.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ public class ServerSelectionListMixin {
1414
method = "render",
1515
at = @At(
1616
value = "INVOKE",
17-
target = "Lnet/minecraft/client/gui/GuiGraphics;drawString(Lnet/minecraft/client/gui/Font;Ljava/lang/String;IIIZ)I", ordinal = 0
17+
target = "Lnet/minecraft/client/gui/GuiGraphics;drawString(Lnet/minecraft/client/gui/Font;Ljava/lang/String;III)V", ordinal = 0
1818
)
1919
)
20-
private int render(GuiGraphics instance, Font font, String string, int i, int j, int k, boolean bl) {
21-
net.minecraft.network.chat.Component component = new ComponentU(string).getComponent();
22-
return instance.drawString(font, component, i, j, k, bl);
20+
private void render(GuiGraphics instance, Font font, String string, int i, int j, int k) {
21+
net.minecraft.network.chat.Component component = new ComponentU(string).parse();
22+
instance.drawString(font, component, i, j, k);
2323
}
2424
}
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.github.voxxin.colourmyservers.mixin.world;
22

3-
import com.github.voxxin.colourmyservers.util.ComponentU;
43
import com.github.voxxin.colourmyservers.util.CreateWorldScreenExt;
54
import net.minecraft.client.gui.components.EditBox;
65
import net.minecraft.client.gui.screens.worldselection.CreateWorldScreen;
@@ -10,19 +9,14 @@
109
import org.spongepowered.asm.mixin.injection.At;
1110
import org.spongepowered.asm.mixin.injection.Inject;
1211
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
13-
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
14-
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
1512

1613
@Mixin(CreateWorldScreen.GameTab.class)
1714
public class CreateWorldScreenGameTabMixin {
18-
1915
@Shadow @Final private EditBox nameEdit;
2016

21-
@Inject(at = @At(value = "TAIL"), method = "<init>", locals = LocalCapture.CAPTURE_FAILHARD)
17+
@Inject(at = @At("TAIL"), method = "<init>")
2218
private void init(CreateWorldScreen createWorldScreen, CallbackInfo ci) {
23-
if (!nameEdit.getValue().isEmpty()) nameEdit.setMaxLength(9999999);
24-
nameEdit.setResponder(w -> {
25-
((CreateWorldScreenExt)createWorldScreen).cms$updateFinalizeButton(w);
26-
});
19+
nameEdit.setMaxLength(Integer.MAX_VALUE);
20+
nameEdit.setResponder(w -> ((CreateWorldScreenExt) createWorldScreen).updateFinalizeButton(w));
2721
}
28-
}
22+
}
Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package com.github.voxxin.colourmyservers.mixin.world;
22

3-
import com.github.voxxin.colourmyservers.util.ComponentU;
43
import com.github.voxxin.colourmyservers.util.CreateWorldScreenExt;
54
import com.github.voxxin.colourmyservers.util.ErrorCatching;
6-
import net.minecraft.client.Minecraft;
5+
import com.llamalad7.mixinextras.sugar.Local;
76
import net.minecraft.client.gui.components.Button;
8-
import net.minecraft.client.gui.components.EditBox;
9-
import net.minecraft.client.gui.components.toasts.SystemToast;
107
import net.minecraft.client.gui.layouts.LinearLayout;
118
import net.minecraft.client.gui.screens.worldselection.CreateWorldScreen;
129
import net.minecraft.client.gui.screens.worldselection.WorldCreationUiState;
@@ -18,37 +15,27 @@
1815
import org.spongepowered.asm.mixin.injection.At;
1916
import org.spongepowered.asm.mixin.injection.Inject;
2017
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
21-
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
2218

2319
@Mixin(CreateWorldScreen.class)
2420
public class CreateWorldScreenMixin implements CreateWorldScreenExt {
25-
2621
@Shadow @Final
2722
WorldCreationUiState uiState;
23+
@Unique private LinearLayout layout;
2824

29-
@Unique
30-
private LinearLayout layout;
31-
32-
@Inject(method = "init", at = @At("TAIL"), locals = LocalCapture.CAPTURE_FAILHARD)
33-
private void init(CallbackInfo ci, LinearLayout linearLayout) {
25+
@Inject(method = "init", at = @At("TAIL"))
26+
private void init(CallbackInfo ci, @Local LinearLayout linearLayout) {
3427
this.layout = linearLayout;
3528
}
3629

37-
3830
@Override
39-
public void cms$updateFinalizeButton(String name) {
40-
this.layout.visitWidgets((w) -> {
31+
public void updateFinalizeButton(String name) {
32+
this.layout.visitWidgets(w -> {
4133
if (w instanceof Button button && button.getMessage().equals(Component.translatable("selectWorld.create"))) {
42-
if (ErrorCatching.stringTooLong(name)) {
43-
if (button.active) {
44-
ErrorCatching.nameTooLongToast();
45-
}
46-
button.active = false;
47-
} else {
48-
this.uiState.setName(name);
49-
button.active = true;
50-
}
34+
boolean valid = !ErrorCatching.isTooLong(name);
35+
if (!valid && button.active) ErrorCatching.showToast();
36+
button.active = valid;
37+
if (valid) uiState.setName(name);
5138
}
5239
});
5340
}
54-
}
41+
}
Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,26 @@
11
package com.github.voxxin.colourmyservers.mixin.world;
22

33
import com.github.voxxin.colourmyservers.util.ErrorCatching;
4-
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
5-
import net.minecraft.client.Minecraft;
6-
import net.minecraft.client.gui.Font;
4+
import com.llamalad7.mixinextras.sugar.Local;
75
import net.minecraft.client.gui.components.Button;
86
import net.minecraft.client.gui.components.EditBox;
9-
import net.minecraft.client.gui.layouts.LinearLayout;
107
import net.minecraft.client.gui.screens.worldselection.EditWorldScreen;
11-
import net.minecraft.world.level.storage.LevelStorageSource;
128
import org.spongepowered.asm.mixin.Final;
139
import org.spongepowered.asm.mixin.Mixin;
1410
import org.spongepowered.asm.mixin.Shadow;
1511
import org.spongepowered.asm.mixin.injection.At;
1612
import org.spongepowered.asm.mixin.injection.Inject;
1713
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
18-
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
1914

2015
@Mixin(EditWorldScreen.class)
2116
public class EditWorldScreenMixin {
22-
2317
@Shadow @Final private EditBox nameEdit;
2418

25-
@Inject(method = "<init>", at = @At("TAIL"), locals = LocalCapture.CAPTURE_FAILHARD)
26-
public void init(Minecraft minecraft, LevelStorageSource.LevelStorageAccess levelStorageAccess, String string, BooleanConsumer booleanConsumer, CallbackInfo ci, Font font, LinearLayout linearLayout, Button button) {
27-
if (nameEdit.getValue().isEmpty()) nameEdit.setMaxLength(9999999);
19+
@Inject(method = "<init>", at = @At("TAIL"))
20+
public void init(CallbackInfo ci, @Local Button button) {
21+
nameEdit.setMaxLength(Integer.MAX_VALUE);
2822
nameEdit.setResponder(w -> {
29-
if (ErrorCatching.stringTooLong(w)) {
30-
if (button.active) {
31-
ErrorCatching.nameTooLongToast();
32-
}
33-
button.active = false;
34-
} else {
35-
button.active = true;
36-
}
37-
38-
if (w.isEmpty()) {
39-
button.active = false;
40-
}
23+
button.active = !w.isEmpty() && !ErrorCatching.isTooLong(w);
4124
});
4225
}
43-
}
26+
}

src/main/java/com/github/voxxin/colourmyservers/mixin/world/WorldCreationUiStateMixin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
@Mixin(WorldCreationUiState.class)
1111
public class WorldCreationUiStateMixin {
1212

13-
@Inject(at = @At(value = "INVOKE_ASSIGN", target = "Ljava/lang/String;trim()Ljava/lang/String;"), method = "findResultFolder", cancellable = true)
13+
@Inject(at = @At(value = "HEAD"), method = "findResultFolder")
1414
private void findResultFolder(String string, CallbackInfoReturnable<String> cir) {
15-
cir.setReturnValue(new ComponentU(string).getComponent().getString());
15+
string = new ComponentU(string).parse().getString();
1616
}
1717
}

src/main/java/com/github/voxxin/colourmyservers/mixin/world/WorldSelectionListMixin.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ public class WorldSelectionListMixin {
1515
method = "render",
1616
at = @At(
1717
value = "INVOKE",
18-
target = "Lnet/minecraft/client/gui/GuiGraphics;drawString(Lnet/minecraft/client/gui/Font;Ljava/lang/String;IIIZ)I", ordinal = 0
18+
target = "Lnet/minecraft/client/gui/GuiGraphics;drawString(Lnet/minecraft/client/gui/Font;Ljava/lang/String;III)V", ordinal = 0
1919
)
2020
)
21-
private int render(GuiGraphics instance, Font font, String string, int i, int j, int k, boolean bl) {
22-
net.minecraft.network.chat.Component component = new ComponentU(string).getComponent();
23-
return instance.drawString(font, component, i, j, k, bl);
21+
private void render(GuiGraphics instance, Font font, String string, int i, int j, int k) {
22+
net.minecraft.network.chat.Component component = new ComponentU(string).parse();
23+
instance.drawString(font, component, i, j, k);
2424
}
2525
}

0 commit comments

Comments
 (0)