From 191e24ef1d95f2af571c917237817f8e1648ea0b Mon Sep 17 00:00:00 2001 From: Empa <42304516+ItsEmpa@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:11:16 +0200 Subject: Improvement: Contributor nametag (#1687) Co-authored-by: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Co-authored-by: Cal Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> --- src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt | 2 -- .../skyhanni/config/features/dev/DevConfig.java | 8 ++++++++ .../java/at/hannibal2/skyhanni/data/EntityData.kt | 22 +++++++++++++++++++++- .../events/entity/EntityDisplayNameEvent.kt | 7 +++++++ .../skyhanni/features/misc/ContributorManager.kt | 16 +++++++++++++++- .../misc/compacttablist/AdvancedPlayerList.kt | 2 +- .../skyhanni/mixins/transformers/MixinEntity.java | 18 ++++++++++++++++++ .../mixins/transformers/MixinEntityPlayer.java | 18 ++++++++++++++++++ 8 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/events/entity/EntityDisplayNameEvent.kt create mode 100644 src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinEntity.java create mode 100644 src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinEntityPlayer.java (limited to 'src') diff --git a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt index 53a3863b9..b7e6d7417 100644 --- a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt +++ b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt @@ -15,7 +15,6 @@ import at.hannibal2.skyhanni.data.BitsAPI import at.hannibal2.skyhanni.data.BlockData import at.hannibal2.skyhanni.data.BossbarData import at.hannibal2.skyhanni.data.CropAccessoryData -import at.hannibal2.skyhanni.data.EntityData import at.hannibal2.skyhanni.data.EntityMovementData import at.hannibal2.skyhanni.data.EventCounter import at.hannibal2.skyhanni.data.FameRanks @@ -524,7 +523,6 @@ class SkyHanniMod { loadModule(ScoreboardData()) loadModule(SeaCreatureFeatures()) loadModule(SeaCreatureManager()) - loadModule(EntityData()) loadModule(MobData()) loadModule(MobDetection()) loadModule(EntityMovementData) diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/dev/DevConfig.java b/src/main/java/at/hannibal2/skyhanni/config/features/dev/DevConfig.java index a53cc4e72..e99fb656c 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/features/dev/DevConfig.java +++ b/src/main/java/at/hannibal2/skyhanni/config/features/dev/DevConfig.java @@ -69,6 +69,14 @@ public class DevConfig { @ConfigEditorBoolean public boolean fancyContributors = true; + @Expose + @ConfigOption( + name = "Contributor Nametags", + desc = "Makes SkyHanni contributors' nametags fancy too. " + ) + @ConfigEditorBoolean + public boolean contributorNametags = true; + @Expose @ConfigOption( name = "Flip Contributors", diff --git a/src/main/java/at/hannibal2/skyhanni/data/EntityData.kt b/src/main/java/at/hannibal2/skyhanni/data/EntityData.kt index 4cc5f840d..318ac465a 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/EntityData.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/EntityData.kt @@ -6,11 +6,15 @@ import at.hannibal2.skyhanni.events.LorenzTickEvent import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent import at.hannibal2.skyhanni.events.PacketEvent import at.hannibal2.skyhanni.events.SecondPassedEvent +import at.hannibal2.skyhanni.events.entity.EntityDisplayNameEvent +import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule import at.hannibal2.skyhanni.utils.EntityUtils import at.hannibal2.skyhanni.utils.LorenzUtils.baseMaxHealth import at.hannibal2.skyhanni.utils.LorenzUtils.derpy +import at.hannibal2.skyhanni.utils.TimeLimitedCache import net.minecraft.client.entity.EntityOtherPlayerMP import net.minecraft.client.entity.EntityPlayerSP +import net.minecraft.entity.Entity import net.minecraft.entity.EntityLivingBase import net.minecraft.entity.boss.EntityWither import net.minecraft.entity.item.EntityArmorStand @@ -18,11 +22,16 @@ import net.minecraft.entity.item.EntityItem import net.minecraft.entity.item.EntityItemFrame import net.minecraft.entity.item.EntityXPOrb import net.minecraft.network.play.server.S1CPacketEntityMetadata +import net.minecraft.util.IChatComponent import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable +import kotlin.time.Duration.Companion.milliseconds -class EntityData { +@SkyHanniModule +object EntityData { private val maxHealthMap = mutableMapOf() + private val nametagCache = TimeLimitedCache(50.milliseconds) @SubscribeEvent fun onTick(event: LorenzTickEvent) { @@ -83,4 +92,15 @@ class EntityData { } } } + + @JvmStatic + fun getDisplayName(entity: Entity, ci: CallbackInfoReturnable) { + ci.returnValue = postRenderNametag(entity, ci.returnValue) + } + + private fun postRenderNametag(entity: Entity, chatComponent: IChatComponent) = nametagCache.getOrPut(entity) { + val event = EntityDisplayNameEvent(entity, chatComponent) + event.postAndCatch() + event.chatComponent + } } diff --git a/src/main/java/at/hannibal2/skyhanni/events/entity/EntityDisplayNameEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/entity/EntityDisplayNameEvent.kt new file mode 100644 index 000000000..1a3bcfed4 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/events/entity/EntityDisplayNameEvent.kt @@ -0,0 +1,7 @@ +package at.hannibal2.skyhanni.events.entity + +import at.hannibal2.skyhanni.events.LorenzEvent +import net.minecraft.entity.Entity +import net.minecraft.util.IChatComponent + +class EntityDisplayNameEvent(val entity: Entity, var chatComponent: IChatComponent) : LorenzEvent() diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/ContributorManager.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/ContributorManager.kt index 2ad7d6446..10153cca3 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/ContributorManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/ContributorManager.kt @@ -3,7 +3,11 @@ package at.hannibal2.skyhanni.features.misc import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.data.jsonobjects.repo.ContributorJsonEntry import at.hannibal2.skyhanni.data.jsonobjects.repo.ContributorsJson +import at.hannibal2.skyhanni.data.mob.MobFilter.isRealPlayer import at.hannibal2.skyhanni.events.RepositoryReloadEvent +import at.hannibal2.skyhanni.events.entity.EntityDisplayNameEvent +import net.minecraft.entity.player.EntityPlayer +import net.minecraft.util.ChatComponentText import net.minecraftforge.fml.common.eventhandler.SubscribeEvent object ContributorManager { @@ -16,7 +20,17 @@ object ContributorManager { contributors = event.getConstant("Contributors").contributors.mapKeys { it.key.lowercase() } } - fun getTabListSuffix(username: String): String? = getContributor(username)?.suffix + @SubscribeEvent + fun onRenderNametag(event: EntityDisplayNameEvent) { + if (!config.contributorNametags) return + (event.entity as? EntityPlayer)?.let { player -> + if (player.isRealPlayer()) getSuffix(event.entity.name)?.let { + event.chatComponent.appendSibling(ChatComponentText(" $it")) + } + } + } + + fun getSuffix(username: String): String? = getContributor(username)?.suffix fun shouldSpin(username: String): Boolean = getContributor(username)?.spinny ?: false fun shouldBeUpsideDown(username: String): Boolean = getContributor(username)?.upsideDown ?: false diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/compacttablist/AdvancedPlayerList.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/compacttablist/AdvancedPlayerList.kt index 99fc14b43..ba53ee57d 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/compacttablist/AdvancedPlayerList.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/compacttablist/AdvancedPlayerList.kt @@ -197,7 +197,7 @@ object AdvancedPlayerList { if (config.markSpecialPersons) { suffix += " ${getSocialIcon(data.name).icon()}" } - ContributorManager.getTabListSuffix(data.name)?.let { + ContributorManager.getSuffix(data.name)?.let { suffix += " $it" } diff --git a/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinEntity.java b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinEntity.java new file mode 100644 index 000000000..951e65e27 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinEntity.java @@ -0,0 +1,18 @@ +package at.hannibal2.skyhanni.mixins.transformers; + +import at.hannibal2.skyhanni.data.EntityData; +import net.minecraft.entity.Entity; +import net.minecraft.util.IChatComponent; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(Entity.class) +public abstract class MixinEntity { + + @Inject(method = "getDisplayName", at = @At("RETURN"), cancellable = true) + public void getDisplayName(CallbackInfoReturnable ci) { + EntityData.getDisplayName((Entity) (Object) this, ci); + } +} diff --git a/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinEntityPlayer.java b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinEntityPlayer.java new file mode 100644 index 000000000..4a0d4d4a3 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/mixins/transformers/MixinEntityPlayer.java @@ -0,0 +1,18 @@ +package at.hannibal2.skyhanni.mixins.transformers; + +import at.hannibal2.skyhanni.data.EntityData; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.util.IChatComponent; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(EntityPlayer.class) +public class MixinEntityPlayer { + + @Inject(method = "getDisplayName", at = @At("RETURN"), cancellable = true) + public void getDisplayName(CallbackInfoReturnable ci) { + EntityData.getDisplayName((EntityPlayer) (Object) this, ci); + } +} -- cgit