aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/events
diff options
context:
space:
mode:
authorBrandon <brandon.wamboldt@gmail.com>2023-09-07 08:03:26 -0300
committerGitHub <noreply@github.com>2023-09-07 13:03:26 +0200
commit6be7873d9475686fdc404212cebd45f22edbff87 (patch)
tree2de35d9806c73e1ca361cbac704f84c09e844540 /src/main/java/at/hannibal2/skyhanni/events
parent5d2f4ea1989b1a3077a26fa7f9fd2935daf9d1bb (diff)
downloadskyhanni-6be7873d9475686fdc404212cebd45f22edbff87.tar.gz
skyhanni-6be7873d9475686fdc404212cebd45f22edbff87.tar.bz2
skyhanni-6be7873d9475686fdc404212cebd45f22edbff87.zip
Add highlight and outline feature for rare sea creatures (#439)
Add highlight and outline feature for rare sea creatures #439
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/events')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/events/RenderEntityOutlineEvent.kt118
1 files changed, 118 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/events/RenderEntityOutlineEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/RenderEntityOutlineEvent.kt
new file mode 100644
index 000000000..5efc7cd94
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/events/RenderEntityOutlineEvent.kt
@@ -0,0 +1,118 @@
+package at.hannibal2.skyhanni.events
+
+import net.minecraft.client.Minecraft
+import net.minecraft.entity.Entity
+import net.minecraft.entity.item.EntityArmorStand
+import net.minecraft.entity.item.EntityItemFrame
+import java.util.function.Consumer
+
+class RenderEntityOutlineEvent(theType: Type?, potentialEntities: HashSet<Entity>?) :
+ LorenzEvent() {
+
+ /**
+ * The phase of the event (see [Type]
+ */
+ var type: Type? = null
+
+ /**
+ * The entities to outline. This is progressively cumulated from [.entitiesToChooseFrom]
+ */
+ var entitiesToOutline: HashMap<Entity, Int>? = null
+
+ /**
+ * The entities we can outline. Note that this set and [.entitiesToOutline] are disjoint at all times.
+ */
+ var entitiesToChooseFrom: HashSet<Entity>? = null
+
+ /**
+ * Constructs the event, given the type and optional entities to outline.
+ *
+ *
+ * This will modify {@param potentialEntities} internally, so make a copy before passing it if necessary.
+ *
+ * @param theType of the event (see [Type]
+ */
+ init {
+ type = theType
+ entitiesToChooseFrom = potentialEntities
+ if (potentialEntities != null) {
+ entitiesToOutline = HashMap(potentialEntities.size)
+ }
+ }
+
+ /**
+ * Conditionally queue entities around which to render entities
+ * Selects from the pool of [.entitiesToChooseFrom] to speed up the predicate testing on subsequent calls.
+ * Is more efficient (theoretically) than calling [.queueEntityToOutline] for each entity because lists are handled internally.
+ *
+ *
+ * This function loops through all entities and so is not very efficient.
+ * It's advisable to encapsulate calls to this function with global checks (those not dependent on an individual entity) for efficiency purposes.
+ *
+ * @param outlineColor a function to test
+ */
+ fun queueEntitiesToOutline(outlineColor: ((entity: Entity) -> Int?)? = null) {
+ if (outlineColor == null) {
+ return
+ }
+ if (entitiesToChooseFrom == null) {
+ computeAndCacheEntitiesToChooseFrom()
+ }
+ val itr: MutableIterator<Entity> = entitiesToChooseFrom!!.iterator()
+ while (itr.hasNext()) {
+ val e: Entity = itr.next()
+ val i: Int? = outlineColor(e)
+ if (i != null) {
+ entitiesToOutline!![e] = i
+ itr.remove()
+ }
+ }
+ }
+
+ /**
+ * Adds a single entity to the list of the entities to outline
+ *
+ * @param entity the entity to add
+ * @param outlineColor the color with which to outline
+ */
+ fun queueEntityToOutline(entity: Entity?, outlineColor: Int) {
+ if (entity == null) {
+ return
+ }
+ if (entitiesToChooseFrom == null) {
+ computeAndCacheEntitiesToChooseFrom()
+ }
+ if (!entitiesToChooseFrom!!.contains(entity)) {
+ return
+ }
+ entitiesToOutline!![entity] = outlineColor
+ entitiesToChooseFrom!!.remove(entity)
+ }
+
+ /**
+ * Used for on-the-fly generation of entities. Driven by event handlers in a decentralized fashion
+ */
+ private fun computeAndCacheEntitiesToChooseFrom() {
+ val entities: List<Entity> = Minecraft.getMinecraft().theWorld.getLoadedEntityList()
+ // Only render outlines around non-null entities within the camera frustum
+ entitiesToChooseFrom = HashSet(entities.size)
+ // Only consider entities that aren't invisible armorstands to increase FPS significantly
+ entities.forEach(Consumer<Entity> { e: Entity? ->
+ if (e != null && !(e is EntityArmorStand && e.isInvisible()) && e !is EntityItemFrame) {
+ entitiesToChooseFrom!!.add(e)
+ }
+ })
+ entitiesToOutline = HashMap(entitiesToChooseFrom!!.size)
+ }
+
+ /**
+ * The phase of the event.
+ * [.XRAY] means that this directly precedes entities whose outlines are rendered through walls (Vanilla 1.9+)
+ * [.NO_XRAY] means that this directly precedes entities whose outlines are rendered only when visible to the client
+ */
+ enum class Type {
+ XRAY,
+ NO_XRAY
+ }
+
+} \ No newline at end of file