Skip to content

Commit

Permalink
Complete requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatGravyBoat committed Sep 9, 2024
1 parent 8e0d180 commit 4f44473
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package me.partlysanestudios.partlysaneskies

import com.google.gson.JsonObject
import com.google.gson.JsonParser
import me.partlysanestudios.partlysaneskies.api.events.PssEvents
import me.partlysanestudios.partlysaneskies.api.events.PSSEvents
import me.partlysanestudios.partlysaneskies.config.Keybinds
import me.partlysanestudios.partlysaneskies.config.OneConfigScreen
import me.partlysanestudios.partlysaneskies.config.psconfig.Config
Expand Down Expand Up @@ -380,7 +380,7 @@ class PartlySaneSkies {
private fun registerEvent(obj: Any) {
runCatching { EVENT_BUS.register(obj) }.onFailure { it.printStackTrace() }
runCatching { EventManager.register(obj) }.onFailure { it.printStackTrace() }
runCatching { PssEvents.register(obj) }.onFailure { it.printStackTrace() }
runCatching { PSSEvents.register(obj) }.onFailure { it.printStackTrace() }
}

// Method runs every tick
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
//
// Written by ThatGravyBoat.
// See LICENSE for copyright and license notices.
//

package me.partlysanestudios.partlysaneskies.api.events

import org.apache.logging.log4j.Logger
Expand All @@ -7,12 +12,12 @@ import java.lang.invoke.MethodType
import java.lang.reflect.Method
import java.util.function.Consumer

class EventHandler<T : PssEvent>(private val logger: Logger) {
class EventHandler<T : PSSEvent>(private val logger: Logger) {

private val listeners: MutableList<Listener> = mutableListOf()
private var lastCancellableIndex: Int = -1

fun register(method: Method, instance: Any, options: PssEvent.Subscribe) {
fun register(method: Method, instance: Any, options: PSSEvent.Subscribe) {
listeners.add(Listener(options, createEventConsumer(instance, method)))
if (options.receiveCancelled) lastCancellableIndex = listeners.size - 1
}
Expand Down Expand Up @@ -55,5 +60,5 @@ class EventHandler<T : PssEvent>(private val logger: Logger) {

}

private data class Listener(val options: PssEvent.Subscribe, val consumer: Consumer<Any>)
private data class Listener(val options: PSSEvent.Subscribe, val consumer: Consumer<Any>)
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
//
// Written by ThatGravyBoat.
// See LICENSE for copyright and license notices.
//

package me.partlysanestudios.partlysaneskies.api.events

abstract class PssEvent protected constructor() {
abstract class PSSEvent protected constructor() {

var isCancelled = false
private set

fun post() {
PssEvents.post(this)
PSSEvents.post(this)
}

interface Cancellable {

fun cancel() {
(this as PssEvent).isCancelled = true
(this as PSSEvent).isCancelled = true
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
//
// Written by ThatGravyBoat.
// See LICENSE for copyright and license notices.
//

package me.partlysanestudios.partlysaneskies.api.events

import org.apache.logging.log4j.LogManager
import java.lang.reflect.Method


object PssEvents {
object PSSEvents {

private val handlers: MutableMap<Class<*>, EventHandler<*>> = mutableMapOf()
private val logger = LogManager.getLogger("Partly Sane Skies Events")

@Suppress("UNCHECKED_CAST")
private fun <T : PssEvent> getHandler(event: Class<T>): EventHandler<T> {
private fun <T : PSSEvent> getHandler(event: Class<T>): EventHandler<T> {
return handlers.getOrPut(event) { EventHandler<T>(logger) } as EventHandler<T>
}

@Suppress("UNCHECKED_CAST")
private fun register(method: Method, instance: Any): SubscribeResponse? {
val subscribe = method.getAnnotation(PssEvent.Subscribe::class.java) ?: return null
val subscribe = method.getAnnotation(PSSEvent.Subscribe::class.java) ?: return null
if (method.parameterCount == 0) return SubscribeResponse.NO_PARAMETERS
if (method.parameterCount > 1) return SubscribeResponse.TOO_MANY_PARAMETERS
val eventClass = method.parameterTypes[0]
if (!PssEvent::class.java.isAssignableFrom(eventClass)) return SubscribeResponse.PARAMETER_NOT_EVENT
getHandler(eventClass as Class<PssEvent>).register(method, instance, subscribe)
if (!PSSEvent::class.java.isAssignableFrom(eventClass)) return SubscribeResponse.PARAMETER_NOT_EVENT
getHandler(eventClass as Class<PSSEvent>).register(method, instance, subscribe)
return SubscribeResponse.SUCCESS
}

Expand All @@ -35,7 +39,7 @@ object PssEvents {
logger.warn("Event subscription on ${method.name} has an incorrect number of parameters (${method.parameterCount})")
}
SubscribeResponse.PARAMETER_NOT_EVENT -> {
logger.warn("Event subscription on ${method.name} does not have a parameter that extends ${PssEvent::class.java.name}")
logger.warn("Event subscription on ${method.name} does not have a parameter that extends ${PSSEvent::class.java.name}")
}
SubscribeResponse.SUCCESS -> {}
}
Expand All @@ -45,7 +49,7 @@ object PssEvents {
}
}

fun post(event: PssEvent) {
fun post(event: PSSEvent) {
getHandler(event.javaClass).post(event)
}

Expand Down

0 comments on commit 4f44473

Please sign in to comment.