Skip to content

Commit

Permalink
Prevent crash with duplicate stat ids, instead return the old one.
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinSVK12 committed Jun 12, 2024
1 parent 5b3b01e commit 5d47518
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ loader_version=0.15.6-babric.6-bta
mod_menu_version=2.0.6

# Mod
mod_version=4.0.7
mod_version=4.0.8
mod_group=turniplabs
mod_name=halplibe

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package turniplabs.halplibe.mixin.accessors;

import net.minecraft.core.achievement.stat.Stat;
import net.minecraft.core.achievement.stat.StatList;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import java.util.Map;

@Mixin(value = StatList.class,remap = false)
public interface StatListAccessor {

@Accessor
static Map<Integer, Stat> getStatMap(){
throw new AssertionError();
}
}
27 changes: 27 additions & 0 deletions src/main/java/turniplabs/halplibe/mixin/mixins/StatMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package turniplabs.halplibe.mixin.mixins;

import net.minecraft.core.achievement.stat.Stat;
import net.minecraft.core.achievement.stat.StatList;
import org.spongepowered.asm.mixin.Final;
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.CallbackInfoReturnable;
import turniplabs.halplibe.mixin.accessors.StatListAccessor;

@Mixin(value = Stat.class,remap = false)
public class StatMixin{

@Shadow @Final public int statId;

@Shadow @Final String statName;

@Inject(method = "registerStat",at = @At("HEAD"), cancellable = true)
public void registerStat(CallbackInfoReturnable<Stat> cir) {
if (StatListAccessor.getStatMap().containsKey(this.statId)) {
System.err.println("Duplicate stat id: \"" + (StatListAccessor.getStatMap().get(this.statId)).getStatName() + "\" and \"" + this.statName + "\" at id " + this.statId);
cir.setReturnValue(StatListAccessor.getStatMap().get(this.statId));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public void postInit(CallbackInfo ci) {
m.invoke(null);
m.setAccessible(false);
I18n.initialize("en_US");
StatList.init();
StatList.onBlockInit();
StatList.onItemInit();
} catch (Throwable err) {
throw new RuntimeException(err);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/halplibe.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
"accessors.EntityFXAccessor",
"accessors.LanguageAccessor",
"accessors.RecipeSymbolAccessor",
"accessors.StatListAccessor",
"accessors.TileEntityAccessor",
"accessors.WeightedRandomBagAccessor",
"accessors.WeightedRandomBagEntryAccessor",
"mixins.BlockSoundDispatcherMixin",
"mixins.ContainerPlayerCreativeMixin",
"mixins.I18nMixin",
"mixins.StatMixin",
"mixins.commands.CommandsCoreMixin",
"mixins.network.PacketMixin",
"mixins.registry.BlockMixin",
Expand Down

0 comments on commit 5d47518

Please sign in to comment.