Skip to content

Commit

Permalink
Fix stats syncing not working on 1.1.0 and 1.2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Nov 11, 2023
1 parent 780a38b commit cbca379
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,72 +1,53 @@
package net.lostluma.server_stats.mixin.client;

import net.lostluma.server_stats.types.OverridableStats;
import net.lostluma.server_stats.utils.Serialization;
import net.minecraft.stat.PlayerStats;
import net.minecraft.stat.Stat;
import net.minecraft.stat.Stats;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.regex.Pattern;

@Mixin(PlayerStats.class)
public class PlayerStatsMixin implements OverridableStats {
@Shadow

private Map<Stat, Integer> stats;

private static final Pattern STAT_PATTERN = Pattern.compile("^(?<type>stat.(?:breakItem|craftItem|mineBlock|useItem).)(?<id>\\d+)$");

@Inject(method = "<init>", at = @At("TAIL"))
private void onInit(CallbackInfo callbackInfo) {
this.stats = Collections.synchronizedMap(this.stats);
}

@Override
public void player_stats$override(Map<String, Integer> override) {
try {
this.player_stats$override0(override);
} catch (IOException e) {
System.out.println("Failed to override local stats " + e.getMessage());
}
}

private void player_stats$override0(Map<String, Integer> override) throws IOException {
// Reset all stats for the case where the world joined has some stats not set
this.stats.keySet().removeAll(
this.stats.keySet().stream().filter(integer -> !integer.isAchievement()).toList()
);

var ids = Serialization.getFromAssets("stat_ids");
var prefixes = Serialization.getFromAssets("stat_id_prefixes");

override.forEach((key, value) -> {
Stat stat = null;
var match = STAT_PATTERN.matcher(key);

if (ids.containsKey(key)) {
stat = Stats.byKey(ids.get(key));
} else if (match.matches()) {
var id = match.group("id");
var type = match.group("type");

if (prefixes.containsKey(type)) {
stat = Stats.byKey(Integer.parseInt(id) + prefixes.get(type));
}
}
var stat = this.player_stats$getVanillaStat(key);

if (stat != null) {
this.stats.put(stat, value);
} else {
System.out.println("No client-side stat found for key " + key);
System.out.println("No client-side stat found for key " + key + ".");
}
});
}
}

private @Nullable Stat player_stats$getVanillaStat(String key) {
var stat = net.lostluma.server_stats.stats.Stats.byKey(key);

if (stat == null || stat.vanillaId == null) {
return null;
} else {
return net.minecraft.stat.Stats.byKey(stat.vanillaId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ public interface OverridableStats {
public default void player_stats$override(Map<String, Integer> override) {
throw new RuntimeException("No implementation for player_stats$override found.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,72 +1,53 @@
package net.lostluma.server_stats.mixin.client;

import net.lostluma.server_stats.types.OverridableStats;
import net.lostluma.server_stats.utils.Serialization;
import net.minecraft.stat.PlayerStats;
import net.minecraft.stat.Stat;
import net.minecraft.stat.Stats;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.regex.Pattern;

@Mixin(PlayerStats.class)
public class PlayerStatsMixin implements OverridableStats {
@Shadow

private Map<Stat, Integer> stats;

private static final Pattern STAT_PATTERN = Pattern.compile("^(?<type>stat.(?:breakItem|craftItem|mineBlock|useItem).)(?<id>\\d+)$");

@Inject(method = "<init>", at = @At("TAIL"))
private void onInit(CallbackInfo callbackInfo) {
this.stats = Collections.synchronizedMap(this.stats);
}

@Override
public void player_stats$override(Map<String, Integer> override) {
try {
this.player_stats$override0(override);
} catch (IOException e) {
System.out.println("Failed to override local stats " + e.getMessage());
}
}

private void player_stats$override0(Map<String, Integer> override) throws IOException {
// Reset all stats for the case where the world joined has some stats not set
this.stats.keySet().removeAll(
this.stats.keySet().stream().filter(integer -> !integer.isAchievement()).toList()
);

var ids = Serialization.getFromAssets("stat_ids");
var prefixes = Serialization.getFromAssets("stat_id_prefixes");

override.forEach((key, value) -> {
Stat stat = null;
var match = STAT_PATTERN.matcher(key);

if (ids.containsKey(key)) {
stat = Stats.byKey(ids.get(key));
} else if (match.matches()) {
var id = match.group("id");
var type = match.group("type");

if (prefixes.containsKey(type)) {
stat = Stats.byKey(Integer.parseInt(id) + prefixes.get(type));
}
}
var stat = this.player_stats$getVanillaStat(key);

if (stat != null) {
this.stats.put(stat, value);
} else {
System.out.println("No client-side stat found for key " + key);
System.out.println("No client-side stat found for key " + key + ".");
}
});
}
}

private @Nullable Stat player_stats$getVanillaStat(String key) {
var stat = net.lostluma.server_stats.stats.Stats.byKey(key);

if (stat == null || stat.vanillaId == null) {
return null;
} else {
return net.minecraft.stat.Stats.byKey(stat.vanillaId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ public interface OverridableStats {
public default void player_stats$override(Map<String, Integer> override) {
throw new RuntimeException("No implementation for player_stats$override found.");
}
}
}

0 comments on commit cbca379

Please sign in to comment.