Skip to content

Commit cbca379

Browse files
committed
Fix stats syncing not working on 1.1.0 and 1.2.5
1 parent 780a38b commit cbca379

File tree

4 files changed

+30
-68
lines changed

4 files changed

+30
-68
lines changed
Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,53 @@
11
package net.lostluma.server_stats.mixin.client;
22

33
import net.lostluma.server_stats.types.OverridableStats;
4-
import net.lostluma.server_stats.utils.Serialization;
54
import net.minecraft.stat.PlayerStats;
65
import net.minecraft.stat.Stat;
7-
import net.minecraft.stat.Stats;
6+
import org.jetbrains.annotations.Nullable;
87
import org.spongepowered.asm.mixin.Mixin;
98
import org.spongepowered.asm.mixin.Shadow;
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;
1312

14-
import java.io.IOException;
1513
import java.util.Collections;
1614
import java.util.Map;
17-
import java.util.regex.Pattern;
1815

1916
@Mixin(PlayerStats.class)
2017
public class PlayerStatsMixin implements OverridableStats {
2118
@Shadow
22-
2319
private Map<Stat, Integer> stats;
2420

25-
private static final Pattern STAT_PATTERN = Pattern.compile("^(?<type>stat.(?:breakItem|craftItem|mineBlock|useItem).)(?<id>\\d+)$");
26-
2721
@Inject(method = "<init>", at = @At("TAIL"))
2822
private void onInit(CallbackInfo callbackInfo) {
2923
this.stats = Collections.synchronizedMap(this.stats);
3024
}
3125

3226
@Override
3327
public void player_stats$override(Map<String, Integer> override) {
34-
try {
35-
this.player_stats$override0(override);
36-
} catch (IOException e) {
37-
System.out.println("Failed to override local stats " + e.getMessage());
38-
}
39-
}
40-
41-
private void player_stats$override0(Map<String, Integer> override) throws IOException {
4228
// Reset all stats for the case where the world joined has some stats not set
4329
this.stats.keySet().removeAll(
4430
this.stats.keySet().stream().filter(integer -> !integer.isAchievement()).toList()
4531
);
4632

47-
var ids = Serialization.getFromAssets("stat_ids");
48-
var prefixes = Serialization.getFromAssets("stat_id_prefixes");
49-
5033
override.forEach((key, value) -> {
51-
Stat stat = null;
52-
var match = STAT_PATTERN.matcher(key);
53-
54-
if (ids.containsKey(key)) {
55-
stat = Stats.byKey(ids.get(key));
56-
} else if (match.matches()) {
57-
var id = match.group("id");
58-
var type = match.group("type");
59-
60-
if (prefixes.containsKey(type)) {
61-
stat = Stats.byKey(Integer.parseInt(id) + prefixes.get(type));
62-
}
63-
}
34+
var stat = this.player_stats$getVanillaStat(key);
6435

6536
if (stat != null) {
6637
this.stats.put(stat, value);
6738
} else {
68-
System.out.println("No client-side stat found for key " + key);
39+
System.out.println("No client-side stat found for key " + key + ".");
6940
}
7041
});
7142
}
72-
}
43+
44+
private @Nullable Stat player_stats$getVanillaStat(String key) {
45+
var stat = net.lostluma.server_stats.stats.Stats.byKey(key);
46+
47+
if (stat == null || stat.vanillaId == null) {
48+
return null;
49+
} else {
50+
return net.minecraft.stat.Stats.byKey(stat.vanillaId);
51+
}
52+
}
53+
}

versions/1.1.0-client/src/main/java/net/lostluma/server_stats/types/OverridableStats.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ public interface OverridableStats {
66
public default void player_stats$override(Map<String, Integer> override) {
77
throw new RuntimeException("No implementation for player_stats$override found.");
88
}
9-
}
9+
}
Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,53 @@
11
package net.lostluma.server_stats.mixin.client;
22

33
import net.lostluma.server_stats.types.OverridableStats;
4-
import net.lostluma.server_stats.utils.Serialization;
54
import net.minecraft.stat.PlayerStats;
65
import net.minecraft.stat.Stat;
7-
import net.minecraft.stat.Stats;
6+
import org.jetbrains.annotations.Nullable;
87
import org.spongepowered.asm.mixin.Mixin;
98
import org.spongepowered.asm.mixin.Shadow;
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;
1312

14-
import java.io.IOException;
1513
import java.util.Collections;
1614
import java.util.Map;
17-
import java.util.regex.Pattern;
1815

1916
@Mixin(PlayerStats.class)
2017
public class PlayerStatsMixin implements OverridableStats {
2118
@Shadow
22-
2319
private Map<Stat, Integer> stats;
2420

25-
private static final Pattern STAT_PATTERN = Pattern.compile("^(?<type>stat.(?:breakItem|craftItem|mineBlock|useItem).)(?<id>\\d+)$");
26-
2721
@Inject(method = "<init>", at = @At("TAIL"))
2822
private void onInit(CallbackInfo callbackInfo) {
2923
this.stats = Collections.synchronizedMap(this.stats);
3024
}
3125

3226
@Override
3327
public void player_stats$override(Map<String, Integer> override) {
34-
try {
35-
this.player_stats$override0(override);
36-
} catch (IOException e) {
37-
System.out.println("Failed to override local stats " + e.getMessage());
38-
}
39-
}
40-
41-
private void player_stats$override0(Map<String, Integer> override) throws IOException {
4228
// Reset all stats for the case where the world joined has some stats not set
4329
this.stats.keySet().removeAll(
4430
this.stats.keySet().stream().filter(integer -> !integer.isAchievement()).toList()
4531
);
4632

47-
var ids = Serialization.getFromAssets("stat_ids");
48-
var prefixes = Serialization.getFromAssets("stat_id_prefixes");
49-
5033
override.forEach((key, value) -> {
51-
Stat stat = null;
52-
var match = STAT_PATTERN.matcher(key);
53-
54-
if (ids.containsKey(key)) {
55-
stat = Stats.byKey(ids.get(key));
56-
} else if (match.matches()) {
57-
var id = match.group("id");
58-
var type = match.group("type");
59-
60-
if (prefixes.containsKey(type)) {
61-
stat = Stats.byKey(Integer.parseInt(id) + prefixes.get(type));
62-
}
63-
}
34+
var stat = this.player_stats$getVanillaStat(key);
6435

6536
if (stat != null) {
6637
this.stats.put(stat, value);
6738
} else {
68-
System.out.println("No client-side stat found for key " + key);
39+
System.out.println("No client-side stat found for key " + key + ".");
6940
}
7041
});
7142
}
72-
}
43+
44+
private @Nullable Stat player_stats$getVanillaStat(String key) {
45+
var stat = net.lostluma.server_stats.stats.Stats.byKey(key);
46+
47+
if (stat == null || stat.vanillaId == null) {
48+
return null;
49+
} else {
50+
return net.minecraft.stat.Stats.byKey(stat.vanillaId);
51+
}
52+
}
53+
}

versions/1.2.5-client/src/main/java/net/lostluma/server_stats/types/OverridableStats.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ public interface OverridableStats {
66
public default void player_stats$override(Map<String, Integer> override) {
77
throw new RuntimeException("No implementation for player_stats$override found.");
88
}
9-
}
9+
}

0 commit comments

Comments
 (0)