Skip to content

Commit

Permalink
EntityDisplayNameEvent & EntityCustomNameUpdateEvent & DataWatcherUpd…
Browse files Browse the repository at this point in the history
…atedEvent
  • Loading branch information
CalMWolfs committed Oct 18, 2024
1 parent 99e7dbe commit 6ba4e5d
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 28 deletions.
9 changes: 5 additions & 4 deletions src/main/java/at/hannibal2/skyhanni/api/DataWatcherAPI.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package at.hannibal2.skyhanni.api

import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.events.DataWatcherUpdatedEvent
import at.hannibal2.skyhanni.events.EntityCustomNameUpdateEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraft.entity.Entity

@SkyHanniModule
object DataWatcherAPI {

private const val DATA_VALUE_CUSTOM_NAME = 2

@SubscribeEvent
fun onDataWatcherUpdate(event: DataWatcherUpdatedEvent) {
@HandleEvent
fun onDataWatcherUpdate(event: DataWatcherUpdatedEvent<Entity>) {
for (updatedEntry in event.updatedEntries) {
if (updatedEntry.dataValueId == DATA_VALUE_CUSTOM_NAME) {
EntityCustomNameUpdateEvent(event.entity.customNameTag, event.entity).postAndCatch()
EntityCustomNameUpdateEvent( event.entity, event.entity.customNameTag).post()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/at/hannibal2/skyhanni/data/EntityData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ object EntityData {

private fun postRenderNametag(entity: Entity, chatComponent: ChatComponentText) = nametagCache.getOrPut(entity) {
val event = EntityDisplayNameEvent(entity, chatComponent)
event.postAndCatch()
event.post()
event.chatComponent
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package at.hannibal2.skyhanni.events

import at.hannibal2.skyhanni.api.event.GenericSkyHanniEvent
import net.minecraft.entity.DataWatcher
import net.minecraft.entity.Entity

data class DataWatcherUpdatedEvent(
val entity: Entity,
data class DataWatcherUpdatedEvent<T : Entity>(
val entity: T,
val updatedEntries: List<DataWatcher.WatchableObject>,
) : LorenzEvent()
) : GenericSkyHanniEvent<T>(entity.javaClass)
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package at.hannibal2.skyhanni.events

import at.hannibal2.skyhanni.api.event.GenericSkyHanniEvent
import net.minecraft.entity.Entity

data class EntityCustomNameUpdateEvent(
data class EntityCustomNameUpdateEvent<T : Entity>(
val entity: T,
val newName: String?,
val entity: Entity,
) : LorenzEvent()
) : GenericSkyHanniEvent<T>(entity.javaClass)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package at.hannibal2.skyhanni.events.entity

import at.hannibal2.skyhanni.events.LorenzEvent
import at.hannibal2.skyhanni.api.event.GenericSkyHanniEvent
import net.minecraft.entity.Entity
import net.minecraft.util.ChatComponentText

class EntityDisplayNameEvent(val entity: Entity, var chatComponent: ChatComponentText) : LorenzEvent()
class EntityDisplayNameEvent<T : Entity>(val entity: T, var chatComponent: ChatComponentText) : GenericSkyHanniEvent<T>(entity.javaClass)
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ object UniqueGiftingOpportunitiesFeatures {
addGiftedPlayer(matchedPlayer.name)
}

@SubscribeEvent
fun onEntityChangeName(event: EntityCustomNameUpdateEvent) {
val entity = event.entity as? EntityArmorStand ?: return
analyzeArmorStand(entity)
@HandleEvent(onlyOnSkyblock = true)
fun onEntityChangeName(event: EntityCustomNameUpdateEvent<EntityArmorStand>) {
analyzeArmorStand(event.entity)
}

@HandleEvent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package at.hannibal2.skyhanni.features.misc

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.data.jsonobjects.repo.ContributorJsonEntry
import at.hannibal2.skyhanni.data.jsonobjects.repo.ContributorsJson
import at.hannibal2.skyhanni.data.mob.MobFilter.isRealPlayer
Expand All @@ -22,13 +23,11 @@ object ContributorManager {
contributors = event.getConstant<ContributorsJson>("Contributors").contributors.mapKeys { it.key.lowercase() }
}

@SubscribeEvent
fun onRenderNametag(event: EntityDisplayNameEvent) {
@HandleEvent
fun onRenderNametag(event: EntityDisplayNameEvent<EntityPlayer>) {
if (!config.contributorNametags) return
(event.entity as? EntityPlayer)?.let { player ->
if (player.isRealPlayer()) getSuffix(event.entity.name)?.let {
event.chatComponent.appendSibling(ChatComponentText(" $it"))
}
if (event.entity.isRealPlayer()) getSuffix(event.entity.name)?.let {
event.chatComponent.appendSibling(ChatComponentText(" $it"))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package at.hannibal2.skyhanni.features.misc.pets

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.events.entity.EntityDisplayNameEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.LorenzUtils
Expand All @@ -10,7 +11,6 @@ import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.chat.Text.asComponent
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraft.entity.item.EntityArmorStand
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

@SkyHanniModule
object PetNametag {
Expand All @@ -26,10 +26,9 @@ object PetNametag {
"(?<start>§8\\[§7Lv(?<lvl>\\d+)§8]) (?<rarity>§.)(?<pet>[\\w\\s]+)(?<skin>§. ✦)?",
)

@SubscribeEvent
fun onNameTagRender(event: EntityDisplayNameEvent) {
@HandleEvent
fun onNameTagRender(event: EntityDisplayNameEvent<EntityArmorStand>) {
if (!isEnabled()) return
if (event.entity !is EntityArmorStand) return

petNametagPattern.matchMatcher(event.chatComponent.unformattedText) {
val start = group("start")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public class UpdateDataWatcherEventPatch {

@Inject(method = "updateWatchedObjectsFromList", at = @At("TAIL"))
public void onWhatever(List<DataWatcher.WatchableObject> list, CallbackInfo ci) {
new DataWatcherUpdatedEvent(owner, list).postAndCatch();
new DataWatcherUpdatedEvent(owner, list).post();
}
}

0 comments on commit 6ba4e5d

Please sign in to comment.