Skip to content

Commit fad75e5

Browse files
committed
added fix with pricing in itemName, and advertisement for ltmods server
1 parent bfd2e4d commit fad75e5

7 files changed

Lines changed: 155 additions & 8 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ yarn_mappings=1.20.4+build.3
88
loader_version=0.16.10
99

1010
# Mod Properties
11-
mod_version=1.5.3-Alpha+1.20.4
11+
mod_version=1.5.4-Alpha+1.20.4
1212
maven_group=pl.lordtricker
1313
archives_base_name=ltrynek
1414

src/client/java/pl/lordtricker/ltrynek/client/config/ConfigLoader.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public static ServersConfig loadConfig() {
4040
if (config == null) {
4141
config = createDefaultConfig();
4242
}
43+
if (config.adsEnabled == null) {
44+
config.adsEnabled = true;
45+
// Persist the new default into the main config file
46+
saveAllConfigs(config);
47+
}
4348
} catch (IOException e) {
4449
e.printStackTrace();
4550
config = createDefaultConfig();
@@ -114,6 +119,7 @@ public static void saveAllConfigs(ServersConfig config) {
114119
ServersConfig mainConfig = new ServersConfig();
115120
mainConfig.defaultProfile = config.defaultProfile;
116121
mainConfig.soundsEnabled = config.soundsEnabled;
122+
mainConfig.adsEnabled = (config.adsEnabled == null) ? Boolean.TRUE : config.adsEnabled;
117123
mainConfig.servers = mainServers;
118124

119125
Path mainConfigFile = MOD_CONFIG_DIR.resolve(MAIN_CONFIG_FILE_NAME);
@@ -140,6 +146,7 @@ public static void saveAllConfigs(ServersConfig config) {
140146
private static ServersConfig createDefaultConfig() {
141147
ServersConfig cfg = new ServersConfig();
142148
cfg.defaultProfile = "default";
149+
cfg.adsEnabled = true;
143150

144151
ServerEntry server1 = new ServerEntry();
145152
server1.domains = List.of("minestar.pl");

src/client/java/pl/lordtricker/ltrynek/client/config/ServersConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public class ServersConfig {
77
public String defaultProfile = "default";
88
public List<ServerEntry> servers = new ArrayList<>();
99
public boolean soundsEnabled = false;
10+
public Boolean adsEnabled;
1011
}

src/client/java/pl/lordtricker/ltrynek/client/mixin/HandledScreenMixin.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,18 @@ private boolean processSlot(DrawContext context, Slot slot) {
8282
ItemStack stack = slot.getStack();
8383
if (stack.isEmpty()) return false;
8484

85+
String displayName = stack.getName().getString();
86+
String noColorName = ColorStripUtils.stripAllColorsAndFormats(displayName);
87+
8588
PlayerEntity player = MinecraftClient.getInstance().player;
8689
List<Text> tooltip = stack.getTooltip(player, TooltipContext.BASIC);
87-
8890
List<String> loreLines = new ArrayList<>();
89-
for (Text line : tooltip) {
90-
String plain = line.getString();
91+
92+
for (int i = 0; i < tooltip.size(); i++) {
93+
// Skip the first line which is the item name; we only want lore
94+
if (i == 0) continue;
95+
Text textLine = tooltip.get(i);
96+
String plain = textLine.getString();
9197
String noColorLine = ColorStripUtils.stripAllColorsAndFormats(plain);
9298
loreLines.add(noColorLine);
9399
}
@@ -161,8 +167,6 @@ private boolean processSlot(DrawContext context, Slot slot) {
161167

162168
Identifier id = Registries.ITEM.getId(stack.getItem());
163169
String materialId = id.toString();
164-
String displayName = stack.getName().getString();
165-
String noColorName = ColorStripUtils.stripAllColorsAndFormats(displayName);
166170

167171
int stackSize = stack.getCount();
168172
boolean isStack = stackSize > 1;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package pl.lordtricker.ltrynek.client.mixin;
2+
3+
import net.minecraft.client.network.ServerInfo;
4+
import net.minecraft.client.option.ServerList;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.gen.Accessor;
7+
8+
import java.util.List;
9+
10+
@Mixin(ServerList.class)
11+
public interface ServerListAccessor {
12+
@Accessor("servers")
13+
List<ServerInfo> getServers();
14+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package pl.lordtricker.ltrynek.client.mixin;
2+
3+
import net.minecraft.client.network.ServerInfo;
4+
import net.minecraft.client.option.ServerList;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.Unique;
7+
import org.spongepowered.asm.mixin.injection.At;
8+
import org.spongepowered.asm.mixin.injection.Inject;
9+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
10+
import pl.lordtricker.ltrynek.client.LtrynekClient;
11+
12+
import java.util.List;
13+
14+
@Mixin(ServerList.class)
15+
public class ServerListMixin {
16+
17+
@Inject(method = "loadFile", at = @At("TAIL"))
18+
private void ltrynek$afterLoadFile(CallbackInfo ci) {
19+
ltrynek$injectOrMove();
20+
}
21+
22+
// Fallback for name variations in different mappings
23+
@Inject(method = "load", at = @At("TAIL"), cancellable = false, require = 0)
24+
private void ltrynek$afterLoad(CallbackInfo ci) {
25+
ltrynek$injectOrMove();
26+
}
27+
28+
@Unique
29+
private void ltrynek$injectOrMove() {
30+
if (LtrynekClient.serversConfig == null) return;
31+
Boolean enabled = LtrynekClient.serversConfig.adsEnabled;
32+
if (enabled != null && !enabled) return;
33+
34+
List<ServerInfo> list = ((ServerListAccessor) (Object) this).getServers();
35+
if (list == null) return;
36+
37+
final String targetAddress = "pvpstar.pl";
38+
39+
int existingIndex = -1;
40+
for (int i = 0; i < list.size(); i++) {
41+
ServerInfo info = list.get(i);
42+
if (info != null && info.address != null && info.address.equalsIgnoreCase(targetAddress)) {
43+
existingIndex = i;
44+
break;
45+
}
46+
}
47+
48+
if (existingIndex >= 0 && existingIndex < 5) {
49+
// Already within top 5; ensure display name is correct, keep position
50+
ServerInfo existing = list.get(existingIndex);
51+
if (existing != null) existing.name = "Serwer LT-Mods";
52+
return;
53+
}
54+
55+
ServerInfo targetInfo;
56+
if (existingIndex >= 0) {
57+
// Move existing entry to the desired position and ensure display name
58+
targetInfo = list.remove(existingIndex);
59+
targetInfo.name = "Serwer LT-Mods";
60+
} else {
61+
// Create a new entry (handle signature differences across MC versions)
62+
targetInfo = createServerInfo("Serwer LT-Mods", targetAddress);
63+
if (targetInfo == null) {
64+
return;
65+
}
66+
}
67+
// Always place at the very top (index 0)
68+
list.add(0, targetInfo);
69+
}
70+
71+
private static ServerInfo createServerInfo(String name, String address) {
72+
try {
73+
java.lang.reflect.Constructor<?>[] ctors = ServerInfo.class.getConstructors();
74+
for (java.lang.reflect.Constructor<?> c : ctors) {
75+
Class<?>[] pts = c.getParameterTypes();
76+
Object[] args = new Object[pts.length];
77+
int stringCount = 0;
78+
boolean unknown = false;
79+
for (int i = 0; i < pts.length; i++) {
80+
Class<?> t = pts[i];
81+
if (t == String.class) {
82+
args[i] = (stringCount == 0) ? name : address;
83+
stringCount++;
84+
} else if (t == boolean.class || t == Boolean.class) {
85+
args[i] = Boolean.FALSE;
86+
} else if (t.isEnum()) {
87+
Object[] constants = t.getEnumConstants();
88+
if (constants != null && constants.length > 0) {
89+
args[i] = constants[0];
90+
} else {
91+
unknown = true; break;
92+
}
93+
} else {
94+
// Try Text.of(String)
95+
try {
96+
if ("net.minecraft.text.Text".equals(t.getName())) {
97+
Class<?> textClass = Class.forName("net.minecraft.text.Text");
98+
java.lang.reflect.Method ofMethod = textClass.getMethod("of", String.class);
99+
args[i] = ofMethod.invoke(null, name);
100+
} else {
101+
unknown = true; break;
102+
}
103+
} catch (Throwable ex) {
104+
unknown = true; break;
105+
}
106+
}
107+
}
108+
if (unknown) continue;
109+
try {
110+
Object inst = c.newInstance(args);
111+
return (ServerInfo) inst;
112+
} catch (Throwable ignored) {}
113+
}
114+
} catch (Throwable ignored) {}
115+
return null;
116+
}
117+
}

src/client/resources/ltrynek.client.mixins.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
"compatibilityLevel": "JAVA_17",
55
"mixins": [
66
"HandledScreenMixin",
7-
"ScreenHandlerAccessor"
7+
"ScreenHandlerAccessor",
8+
"ServerListMixin",
9+
"ServerListAccessor"
810
],
911
"client": [
1012
"HandledScreenMixin",
11-
"ScreenHandlerAccessor"
13+
"ScreenHandlerAccessor",
14+
"ServerListMixin",
15+
"ServerListAccessor"
1216
],
1317
"injectors": {
1418
"defaultRequire": 1

0 commit comments

Comments
 (0)