diff --git a/gradle.properties b/gradle.properties index 2759f38..3732e0d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,7 +12,7 @@ fabric_version=0.68.1+1.19.3 # Mod Properties - mod_version = 2.1.3+1.19.3 + mod_version = 2.1.4+1.19.3 maven_group = eu.pb4 archives_base_name = styled-chat diff --git a/src/main/java/eu/pb4/styledchat/mixin/PlayerEntityMixin.java b/src/main/java/eu/pb4/styledchat/mixin/PlayerEntityMixin.java index f23fa89..937af7b 100644 --- a/src/main/java/eu/pb4/styledchat/mixin/PlayerEntityMixin.java +++ b/src/main/java/eu/pb4/styledchat/mixin/PlayerEntityMixin.java @@ -1,6 +1,7 @@ package eu.pb4.styledchat.mixin; import eu.pb4.styledchat.StyledChatStyles; +import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; @@ -11,16 +12,30 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(PlayerEntity.class) -public class PlayerEntityMixin { +public abstract class PlayerEntityMixin { @Unique - private boolean ignoreNextCalls = false; + private Text styledChat$cachedName = Text.empty(); + + @Unique + private int styledChat$cachedAge = -1234568; + + @Unique + private boolean styledChat$ignoreNextCalls = false; @Inject(method = "getDisplayName", at = @At("RETURN"), cancellable = true) private void styledChat_replaceDisplayName(CallbackInfoReturnable cir) { - if (!this.ignoreNextCalls && ((Object) this) instanceof ServerPlayerEntity player) { - this.ignoreNextCalls = true; - cir.setReturnValue(StyledChatStyles.getDisplayName(player, cir.getReturnValue())); - this.ignoreNextCalls = false; + if (!this.styledChat$ignoreNextCalls && ((Object) this).getClass() == ServerPlayerEntity.class) { + if (this.styledChat$cachedAge == ((Entity) (Object) this).age) { + cir.setReturnValue(this.styledChat$cachedName); + return; + } + + this.styledChat$ignoreNextCalls = true; + var name = StyledChatStyles.getDisplayName((ServerPlayerEntity) (Object) this, cir.getReturnValue()); + this.styledChat$ignoreNextCalls = false; + this.styledChat$cachedName = name; + this.styledChat$cachedAge = ((Entity) (Object) this).age; + cir.setReturnValue(name); } } }