diff options
author | Empa <42304516+ItsEmpa@users.noreply.github.com> | 2024-06-02 10:11:16 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-02 10:11:16 +0200 |
commit | 191e24ef1d95f2af571c917237817f8e1648ea0b (patch) | |
tree | 941e36445db5f8d1aa3a672d4b6434cdc98df4d7 /src/main | |
parent | 4c838a148544a862b7a52ba68347d0d5715e7b5e (diff) | |
download | skyhanni-191e24ef1d95f2af571c917237817f8e1648ea0b.tar.gz skyhanni-191e24ef1d95f2af571c917237817f8e1648ea0b.tar.bz2 skyhanni-191e24ef1d95f2af571c917237817f8e1648ea0b.zip |
Improvement: Contributor nametag (#1687)
Co-authored-by: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com>
Co-authored-by: Cal <cwolfson58@gmail.com>
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Diffstat (limited to 'src/main')
8 files changed, 88 insertions, 5 deletions
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 @@ -71,6 +71,14 @@ public class DevConfig { @Expose @ConfigOption( + name = "Contributor Nametags", + desc = "Makes SkyHanni contributors' nametags fancy too. " + ) + @ConfigEditorBoolean + public boolean contributorNametags = true; + + @Expose + @ConfigOption( name = "Flip Contributors", desc = "Make SkyHanni contributors appear upside down in the world.") @ConfigEditorBoolean 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<EntityLivingBase, Int>() + private val nametagCache = TimeLimitedCache<Entity, IChatComponent>(50.milliseconds) @SubscribeEvent fun onTick(event: LorenzTickEvent) { @@ -83,4 +92,15 @@ class EntityData { } } } + + @JvmStatic + fun getDisplayName(entity: Entity, ci: CallbackInfoReturnable<IChatComponent>) { + 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<ContributorsJson>("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<IChatComponent> 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<IChatComponent> ci) { + EntityData.getDisplayName((EntityPlayer) (Object) this, ci); + } +} |