forked from hannibal002/SkyHanni
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'beta' into backend/command-register-event
# Conflicts: # src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt
- Loading branch information
Showing
39 changed files
with
396 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/at/hannibal2/skyhanni/api/event/EventListeners.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package at.hannibal2.skyhanni.api.event | ||
|
||
import at.hannibal2.skyhanni.data.IslandType | ||
import java.lang.invoke.LambdaMetafactory | ||
import java.lang.invoke.MethodHandles | ||
import java.lang.invoke.MethodType | ||
import java.lang.reflect.Method | ||
import java.lang.reflect.ParameterizedType | ||
import java.util.function.Consumer | ||
|
||
class EventListeners private constructor(val name: String, private val isGeneric: Boolean) { | ||
|
||
private val listeners: MutableList<Listener> = mutableListOf() | ||
|
||
constructor(event: Class<*>) : this( | ||
(event.name.split(".").lastOrNull() ?: event.name).replace("$", "."), | ||
GenericSkyHanniEvent::class.java.isAssignableFrom(event) | ||
) | ||
|
||
fun addListener(method: Method, instance: Any, options: HandleEvent) { | ||
val generic: Class<*>? = if (isGeneric) { | ||
method.genericParameterTypes | ||
.firstNotNullOfOrNull { it as? ParameterizedType } | ||
?.let { it.actualTypeArguments.firstOrNull() as? Class<*> } | ||
?: throw IllegalArgumentException("Generic event handler must have a generic type") | ||
} else { | ||
null | ||
} | ||
val name = "${method.declaringClass.name}.${method.name}${ | ||
method.parameterTypes.joinTo( | ||
StringBuilder(), | ||
prefix = "(", | ||
postfix = ")", | ||
separator = ", ", | ||
transform = Class<*>::getTypeName | ||
) | ||
}" | ||
listeners.add(Listener(name, createEventConsumer(name, instance, method), options, generic)) | ||
} | ||
|
||
/** | ||
* Creates a consumer using LambdaMetafactory, this is the most efficient way to reflectively call | ||
* a method from within code. | ||
*/ | ||
@Suppress("UNCHECKED_CAST") | ||
private fun createEventConsumer(name: String, instance: Any, method: Method): Consumer<Any> { | ||
try { | ||
val handle = MethodHandles.lookup().unreflect(method) | ||
return LambdaMetafactory.metafactory( | ||
MethodHandles.lookup(), | ||
"accept", | ||
MethodType.methodType(Consumer::class.java, instance::class.java), | ||
MethodType.methodType(Nothing::class.javaPrimitiveType, Object::class.java), | ||
handle, | ||
MethodType.methodType(Nothing::class.javaPrimitiveType, method.parameterTypes[0]) | ||
).target.bindTo(instance).invokeExact() as Consumer<Any> | ||
} catch (e: Throwable) { | ||
throw IllegalArgumentException("Method $name is not a valid consumer", e) | ||
} | ||
} | ||
|
||
fun getListeners(): List<Listener> = listeners | ||
|
||
class Listener( | ||
val name: String, | ||
val invoker: Consumer<Any>, | ||
val options: HandleEvent, | ||
val generic: Class<*>?, | ||
) { | ||
val onlyOnIslandTypes: Set<IslandType> = getIslands(options) | ||
|
||
companion object { | ||
private fun getIslands(options: HandleEvent): Set<IslandType> = | ||
if (options.onlyOnIslands.isEmpty()) setOf(options.onlyOnIsland) | ||
else options.onlyOnIslands.toSet() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/at/hannibal2/skyhanni/config/features/mining/CrystalHighlighterConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package at.hannibal2.skyhanni.config.features.mining; | ||
|
||
import at.hannibal2.skyhanni.config.FeatureToggle; | ||
import com.google.gson.annotations.Expose; | ||
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorBoolean; | ||
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorSlider; | ||
import io.github.notenoughupdates.moulconfig.annotations.ConfigOption; | ||
|
||
public class CrystalHighlighterConfig { | ||
|
||
@Expose | ||
@ConfigOption( | ||
name = "Highlight Crystal Nucleus barriers", | ||
desc = "Draw visible bounding boxes around the Crystal Nucleus crystal barrier blocks." | ||
) | ||
@ConfigEditorBoolean | ||
@FeatureToggle | ||
public boolean enabled = true; | ||
|
||
@Expose | ||
@ConfigOption( | ||
name = "Highlight Opacity", | ||
desc = "Set the opacity of the highlighted boxes.\n§70§8: §7Transparent\n§7100§8: §7Solid" | ||
) | ||
@ConfigEditorSlider(minValue = 0, maxValue = 100, minStep = 1) | ||
public int opacity = 60; | ||
|
||
@Expose | ||
@ConfigOption( | ||
name = "Only Show During Hoppity's", | ||
desc = "Only show the highlighted boxes during Hoppity's Hunt." | ||
) | ||
@ConfigEditorBoolean | ||
public boolean onlyDuringHoppity = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.