Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 40dac62

Browse files
committed
Merge branch 'api-rework'
2 parents d81d4b0 + 774dadc commit 40dac62

20 files changed

+239
-437
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ lazy val deploy = TaskKey[Unit]("deploy", "Prepares the environment for deployme
8484
lazy val gui = TaskKey[Unit]("gui", "Installs GUI dependencies and builds it using npm.")
8585

8686
pluginBuildFileName := "plugins.sbt"
87-
pluginFolderNames := List("plugins-public")
87+
pluginFolderNames := List("plugins-public", "plugins-private")
8888
pluginTargetFolderNames := List("plugins", s"target/scala-$scalaMajorVersion/plugins")
8989
apiProjectPath := "api"
9090
guiProjectPath := "gui"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.codeoverflow.chatoverflow.requirement.impl
2+
3+
import java.util.function.Consumer
4+
5+
import org.codeoverflow.chatoverflow.api.io.event.Event
6+
7+
private[impl] case class EventHandler[T <: Event](consumer: Consumer[T], clazz: Class[T])
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.codeoverflow.chatoverflow.requirement.impl
2+
3+
import java.util.function.Consumer
4+
5+
import org.codeoverflow.chatoverflow.api.io.event.Event
6+
import org.codeoverflow.chatoverflow.api.io.input.event.EventInput
7+
import org.codeoverflow.chatoverflow.connector.Connector
8+
9+
import scala.collection.mutable.ListBuffer
10+
import scala.reflect.ClassTag
11+
12+
/**
13+
* Default implementation for all inputs that provide events.
14+
*
15+
* The integrated event registry allows registering new Event handlers at any time.
16+
* @tparam T the event interface that all events for this EventInput share
17+
* @tparam C the connector to which this input belongs
18+
*/
19+
abstract class EventInputImpl[T <: Event, C <: Connector](implicit ctc: ClassTag[C]) extends InputImpl[C] with EventInput[T] {
20+
21+
protected val handlers: ListBuffer[EventHandler[_ <: T]] = ListBuffer[EventHandler[_ <: T]]()
22+
23+
/**
24+
* Register a new event handler that listens for a specific event
25+
*
26+
* @param eventHandler consumer for which `accept()` is called if the event is fired
27+
* @param eventClass class of the events for which this listener should listen
28+
*/
29+
override def registerEventHandler[S <: T](eventHandler: Consumer[S], eventClass: Class[S]): Unit = {
30+
handlers += EventHandler[S](eventHandler, eventClass)
31+
}
32+
33+
/**
34+
* Use this methods to fire an event.
35+
* All eventHandlers listening for that event will be triggered.
36+
* @param event the event that should be fired
37+
*/
38+
protected def call[S <: T](event: S)(implicit cts: ClassTag[S]): Unit = {
39+
handlers.filter(handler => handler.clazz == cts.runtimeClass)
40+
.foreach(handler => handler.consumer.asInstanceOf[Consumer[S]].accept(event))
41+
}
42+
}

src/main/scala/org/codeoverflow/chatoverflow/requirement/InputImpl.scala renamed to src/main/scala/org/codeoverflow/chatoverflow/requirement/impl/InputImpl.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package org.codeoverflow.chatoverflow.requirement
1+
package org.codeoverflow.chatoverflow.requirement.impl
22

33
import org.codeoverflow.chatoverflow.WithLogger
44
import org.codeoverflow.chatoverflow.api.io.input.Input
55
import org.codeoverflow.chatoverflow.connector.Connector
6+
import org.codeoverflow.chatoverflow.requirement.Connection
67

78
import scala.reflect.ClassTag
89

src/main/scala/org/codeoverflow/chatoverflow/requirement/OutputImpl.scala renamed to src/main/scala/org/codeoverflow/chatoverflow/requirement/impl/OutputImpl.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package org.codeoverflow.chatoverflow.requirement
1+
package org.codeoverflow.chatoverflow.requirement.impl
22

33
import org.codeoverflow.chatoverflow.WithLogger
44
import org.codeoverflow.chatoverflow.api.io.output.Output
55
import org.codeoverflow.chatoverflow.connector.Connector
6+
import org.codeoverflow.chatoverflow.requirement.Connection
67

78
import scala.reflect.ClassTag
89

src/main/scala/org/codeoverflow/chatoverflow/requirement/service/discord/DiscordChatConnector.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class DiscordChatConnector(override val sourceIdentifier: String) extends Connec
101101
case None =>
102102
Option(validJDA.getPrivateChannelById(channelId)) match {
103103
case Some(channel) => channel.getMessageById(messageId)
104-
case None => throw new IllegalArgumentException(s"Channel with id $channelId not found")
104+
case None => throw new IllegalArgumentException(s"TextChannel with id $channelId not found")
105105
}
106106
}
107107
}
@@ -123,7 +123,7 @@ class DiscordChatConnector(override val sourceIdentifier: String) extends Connec
123123
def sendChatMessage(channelId: String, chatMessage: String): Unit = {
124124
Option(validJDA.getTextChannelById(channelId)) match {
125125
case Some(channel) => channel.sendMessage(chatMessage).queue(null, defaultFailureHandler)
126-
case None => throw new IllegalArgumentException(s"Channel with id $channelId not found")
126+
case None => throw new IllegalArgumentException(s"TextChannel with id $channelId not found")
127127
}
128128
}
129129

@@ -136,7 +136,7 @@ class DiscordChatConnector(override val sourceIdentifier: String) extends Connec
136136
def sendChatMessage(channelId: String, embed: MessageEmbed): Unit = {
137137
Option(validJDA.getTextChannelById(channelId)) match {
138138
case Some(channel) => channel.sendMessage(embed).queue(null, defaultFailureHandler)
139-
case None => throw new IllegalArgumentException(s"Channel with id $channelId not found")
139+
case None => throw new IllegalArgumentException(s"TextChannel with id $channelId not found")
140140
}
141141
}
142142

@@ -157,7 +157,7 @@ class DiscordChatConnector(override val sourceIdentifier: String) extends Connec
157157
case Some(m) => channel.sendFile(fileIn.get.get, fileName, new MessageBuilder(m).build()).queue(null, defaultFailureHandler)
158158
case None => channel.sendFile(fileIn.get.get, fileName).queue(null, defaultFailureHandler)
159159
}
160-
case None => throw new IllegalArgumentException(s"Channel with id $channelId not found")
160+
case None => throw new IllegalArgumentException(s"TextChannel with id $channelId not found")
161161
}
162162
} else {
163163
logger warn s"Could not load file '$file'"

0 commit comments

Comments
 (0)