Skip to content

Commit 1c8b363

Browse files
authored
Merge pull request #12 from Gikkman/dev-0.4
Dev 0.4
2 parents 68851c0 + cd45b75 commit 1c8b363

125 files changed

Lines changed: 4007 additions & 2096 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Small, basic library for creating an IRC connection to the Twitch chat.
77

88
The library is intended to make communication via Twitch chat as easy as possible, and uses Java objects to represent most events that can occur in Twitch chat.
99

10-
Java 6 compatible.
10+
Java 8 compatible.
1111

1212
## Installation
1313
Include the following in your pom.xml
@@ -30,14 +30,18 @@ Include the following in your pom.xml
3030
Or simply download the latest version of the library jar from the release page.
3131

3232
## Changes
33-
There has been some changes between Java-Twirk 0.2 and Java-Twirk 0.3:
34-
* TwitchUser.getUserId() now returns long (was int)
35-
* TwitchUser.getName() is now called TwitchUser.getDisplayName()
36-
* TwitchUser.getUserName() added (retrieves the users login name)
37-
* USER_TYPE now has type SUBSCRIBER
38-
* USER_TYPE.value changed for most types
39-
* TwirkListener.onWhisper is now depricated (Twitch uses a new whisper system)
40-
* TwirkListener.onMode is now depricated (User TwitchUser.isMod() instead)
33+
There has beenmajor changes from Java-Twirk 0.3 to Java-Twirk 0.4. I am sorry for the hassle, but it was a necessity. Changes include (but is not limited to)
34+
* Added support for Subscription Gift
35+
* Added support for Raid
36+
* Encapsulated all default factories (previously all public under GikkDefault_<NAME HERE>)
37+
* TwirkListenerBaseImpl has been removed, please implement TwirkListener instead
38+
* Mode-events has been depricated (please use the TwitchUser.isMod() instead)
39+
* Subscriber-event has been removed. It is now found under Usernotice-event instead (Twitch has changed their grouping)
40+
* USER_TYPES now has category SUBSCRIBER, which is given to regular subs and turbo subs.
41+
* USER_TYPES now has ranks, which allows you to do if( user.getUserType().value >= USER_TYPE.SUBSCRIBER.value )
42+
* Added the isOwner() status on TwichUser
43+
44+
And probably some more. Thankfully, Java is a strongly typed language :D
4145

4246
# Usage
4347
#### Basic usage
@@ -51,7 +55,7 @@ There has been some changes between Java-Twirk 0.2 and Java-Twirk 0.3:
5155
All events which can be reacted to are listed in [TwirkListener.java](https://github.com/Gikkman/Java-Twirk/blob/master/twirc/src/main/java/com/gikk/twirk/events/TwirkListener.java) This snippet will make your bot respond to any channel
5256
message with a "pong USER_NAME".
5357
```Java
54-
twirk.addIrcListener( new TwirkListenerBaseImpl() {
58+
twirk.addIrcListener( new TwirkListener() {
5559
public void onPrivMsg( TwitchUser sender, TwitchMessage message) {
5660
twirk.channelMessage("pong " + sender.getDisplayName() );
5761
}
@@ -64,10 +68,10 @@ For a more complex example, which shows how to connect properly and how to write
6468
You can make Twirk use your own implementation of all event types by using custom builder classes. By extending the types Builder interface, and then passing an instance of your custom builder to the TwirkBuilder, you can use your own custom implementation of whichever type you want.
6569
```Java
6670
final Twirk twirk = new TwirkBuilder(channel, SETTINGS.MY_NICK, SETTINGS.MY_PASS)
67-
.setSubscriberEventBuilder( new MySubscriberEventBuilder() )
71+
.setClearChatBuilder( new MyClearChatBuilder() )
6872
.build();
6973
```
70-
This will make the Twirk instance build instances of your custom implementation of `SubscriberEvent`
74+
This will make the Twirk instance build instances of your custom implementation of `ClearChat` events
7175

7276
# Contribute
7377
If you find any issues, or have suggestions for features (which does not clutter the library), feel free to submit an [Issue](https://github.com/Gikkman/Java-Twirk/issues) or make a pull request. You can also reach me on [Twitter](https://twitter.com/gikkman) or on [Twitch](http://twitch.com/gikkman)

twirc/pom.xml renamed to pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.gikk</groupId>
66
<artifactId>twirc</artifactId>
7-
<version>0.3</version>
7+
<version>0.4</version>
88
<packaging>jar</packaging>
99

1010
<name>twirc</name>
@@ -19,8 +19,8 @@
1919

2020
<properties>
2121
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22-
<maven.compiler.source>1.6</maven.compiler.source>
23-
<maven.compiler.target>1.6</maven.compiler.target>
22+
<maven.compiler.source>1.8</maven.compiler.source>
23+
<maven.compiler.target>1.8</maven.compiler.target>
2424
</properties>
2525

2626
<build>
@@ -32,13 +32,13 @@
3232
</resources>
3333

3434
<plugins>
35-
<plugin> <!-- Compile java 6 compatible bytecode -->
35+
<plugin> <!-- Compile java 8 compatible bytecode -->
3636
<groupId>org.apache.maven.plugins</groupId>
3737
<artifactId>maven-compiler-plugin</artifactId>
3838
<version>3.2</version>
3939
<configuration>
40-
<source>1.6</source>
41-
<target>1.6</target>
40+
<source>1.8</source>
41+
<target>1.8</target>
4242
</configuration>
4343
</plugin>
4444
<plugin> <!-- Create sources.jar -->
File renamed without changes.
File renamed without changes.

twirc/src/example/java/com/gikk/twirk/commands/CommandExampleBase.java renamed to src/example/java/com/gikk/twirk/commands/CommandExampleBase.java

File renamed without changes.

twirc/src/example/java/com/gikk/twirk/commands/PatternCommandExample.java renamed to src/example/java/com/gikk/twirk/commands/PatternCommandExample.java

File renamed without changes.

twirc/src/example/java/com/gikk/twirk/commands/PrefixCommandExample.java renamed to src/example/java/com/gikk/twirk/commands/PrefixCommandExample.java

File renamed without changes.
File renamed without changes.

twirc/src/main/java/com/gikk/twirk/OutputQueue.java renamed to src/main/java/com/gikk/twirk/OutputQueue.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@
33
import java.util.LinkedList;
44

55
/**We need a specialized messaging queue to be able to handle <ul>
6-
* <li>A) one consumer/multiple producers and
6+
* <li>A) one consumer/multiple producers and
77
* <li>B) being able to put messages to the front and back of the queue.
88
*</ul>
9-
* We also want the {@link #next()} method to block until there is anything to send to the IRC server in the
9+
* We also want the {@link #next()} method to block until there is anything to send to the IRC server in the
1010
* queue.<br><br>
11-
*
11+
*
1212
* Due to these reasons, we cannot use a normal queue. Thus we use this thread safe and blocking implementation.
13-
*
13+
*
1414
* @author Gikkman
1515
*
1616
*/
1717
class OutputQueue {
1818
//***********************************************************************************************
1919
// VARIABLES
2020
//***********************************************************************************************
21-
private final LinkedList<String> queue = new LinkedList<String>();
22-
21+
private final LinkedList<String> queue = new LinkedList<>();
22+
2323
//***********************************************************************************************
2424
// PUBLIC
2525
//***********************************************************************************************
2626
/**Adds a message to the back of the output queue
27-
*
27+
*
2828
* @param s The message to add to the queue
2929
*/
3030
public void add(String s){
@@ -33,10 +33,10 @@ public void add(String s){
3333
queue.notify();
3434
}
3535
}
36-
36+
3737
/**Adds a message to the front of the output queue.<br>
3838
* Can be useful for prioritized messages
39-
*
39+
*
4040
* @param s The message to add to the queue
4141
*/
4242
public void addFirst(String s){
@@ -45,42 +45,43 @@ public void addFirst(String s){
4545
queue.notify();
4646
}
4747
}
48-
48+
4949
/**A <b>blocking</b> call that retrieves the next message from the queue.
5050
* If no message is currently in the queue, this method will block until a message appears.
51-
*
51+
*
5252
* @return The next message OR <code>null</code>(if we were interrupted and there were no message in the queue)
5353
*/
5454
public String next(){
5555
synchronized (queue) {
5656
if( !hasNext() ){
57-
try { queue.wait(); }
58-
catch (InterruptedException e) {
57+
try { queue.wait(); }
58+
catch (InterruptedException e) {
5959
/* Being interrupted either means that there now is an element in the queue or
6060
* that the application is shutting down.
6161
* Anyway, we don't need to care about being interrupted, we simply proceed as
62-
* usual and let the thread waiting for input handle the potential null return */
62+
* usual and let the thread waiting for input handle the potential null return */
6363
}
64-
}
65-
if( !hasNext() )
66-
return null;
67-
64+
}
65+
if( !hasNext() ) {
66+
return null;
67+
}
68+
6869
String message = queue.getFirst();
6970
queue.removeFirst();
7071
return message;
7172
}
7273
}
73-
74+
7475
/**Checks if there are any elements currently in the queue
75-
*
76+
*
7677
* @return {@code true} if there are any messages in the queue
7778
*/
7879
public boolean hasNext(){
7980
synchronized (queue) {
8081
return queue.size() > 0;
8182
}
8283
}
83-
84+
8485
/**This will cause all threads waiting for new content in the {@code queue} to wake up. <br>
8586
* If there is no content when this call is issued, waiting threads will return {@code null}
8687
*/

twirc/src/main/java/com/gikk/twirk/OutputThread.java renamed to src/main/java/com/gikk/twirk/OutputThread.java

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import java.net.SocketException;
77

88
/**This class handles all outgoing IRC traffic.<br><br>
9-
*
10-
* The implementation is intended to be thread safe and handle all potential errors (<u>keyword: INTENDED</u>).
9+
*
10+
* The implementation is intended to be thread safe and handle all potential errors (<u>keyword: INTENDED</u>).
1111
* That means that we can have multiple threads feeding the message queue safely and still operate without any trouble.
12-
*
12+
*
1313
* @author Simon
1414
*
1515
*/
@@ -20,22 +20,22 @@ class OutputThread extends Thread{
2020
private final Twirk connection;
2121
private final BufferedWriter writer;
2222
private final OutputQueue queue;
23-
23+
2424
private boolean isConnected = true;
25-
26-
private int MESSAGE_GAP_MILLIS = 1500; //We may not send more than 20 messages to the Twitch server / 30 seconds
27-
25+
26+
private int messageGapMillis = 1500; //We may not send more than 20 messages to the Twitch server / 30 seconds
27+
2828
//***********************************************************************************************
2929
// CONSTRUCTOR
3030
//***********************************************************************************************
3131
public OutputThread(Twirk connection, OutputQueue queue, BufferedReader reader, BufferedWriter writer){
3232
this.connection = connection;
3333
this.queue = queue;
3434
this.writer = writer;
35-
35+
3636
this.setName("Twirk-OutputThread");
3737
}
38-
38+
3939
@Override
4040
public void run(){
4141
String line;
@@ -48,74 +48,91 @@ public void run(){
4848
else {
4949
//If we get a null line from the queue, it might mean that the application interrupted the thread
5050
// and wants us to shut down.
51-
isConnected = connection.isConnected();
52-
}
53-
Thread.sleep(MESSAGE_GAP_MILLIS); }
54-
catch (Exception ignored) {
51+
isConnected = connection.isConnected();
52+
}
53+
Thread.sleep(messageGapMillis); }
54+
catch (Exception ignored) {
5555
/* Being interrupted probably means that we are about to shut down.
5656
* If the socket is closed, it also means that we are about to shut down.
57-
*
57+
*
5858
* If we are about to close down, isConnected will be set to false so we can just go back
59-
* to the loop and automatically terminate from there. */
59+
* to the loop and automatically terminate from there. */
6060
}
6161
}
6262
}
63-
63+
6464
//***********************************************************************************************
6565
// PUBLIC
6666
//***********************************************************************************************
67-
67+
6868
/**Circumvents the message queue completely and attempts to send the message at once. Should only be used for sending
6969
* PING responses.
70-
*
70+
*
7171
* @param message
7272
*/
7373
public void quickSend(String message) {
7474
try {
7575
sendLine(message);
7676
} catch (SocketException e) {
7777
System.err.println("Could not QuickSend message. Socket was closed (OutputThread @ Twirk)");
78-
}
78+
}
7979
}
80-
80+
8181
/**Tells the thread to stop execution. Future messages written to the {@code OutputQueue} will
8282
* not be sent by this {@code OutputThread} unless it is started again.
8383
*/
8484
public void end() {
8585
isConnected = false;
8686
this.queue.releaseWaitingThreads();
8787
}
88-
88+
89+
//***********************************************************************************************
90+
// PACKAGE
91+
//***********************************************************************************************
92+
93+
/**Tells the OutputThread how long we should wait, at minimum, in between
94+
* each message sent to Twitch.
95+
*
96+
* @param millis the delay
97+
*/
98+
void setMessageDelay(int millis){
99+
this.messageGapMillis = millis;
100+
}
101+
89102
//***********************************************************************************************
90103
// PRIVATE
91104
//***********************************************************************************************
92105

93106
/**Sends a message AS IS, without modifying it in any way. Users of this method are responsible for
94-
* formating the string correctly:
107+
* formating the string correctly:
95108
* <br>
96109
* That means, who ever uses this method has to manually assign channel data and the similar to the
97110
* message.
98-
*
111+
*
99112
* @param message The message to write to out BufferedWriter
100113
*/
101114
private void sendLine(String message) throws SocketException{
102115
if( !isConnected ){
103116
System.err.println("Twirk is not connected! Sending messages will not succeed!");
104117
}
118+
119+
if(connection.verboseMode) {
120+
System.out.println("OUT " + message);
121+
}
122+
105123
/**An IRC message may not be longer than 512 characters. Also, they must end with \r\n,
106124
* so if the supplied message is longer than 510 characters, we have to cut it short.
107-
*
125+
*
108126
* While it might be an alternative to split the message and send it in different batches,
109-
* that would mean that we lose potential commands and such. Hence, instead we just drop
127+
* that would mean that we lose potential commands and such. Hence, instead we just drop
110128
* everything beyond the 510th character.
111129
*/
112130
if( message.length() > 510 ){
113131
message = message.substring(0, 511);
114-
}
115-
132+
}
133+
116134
try{
117-
System.out.println("OUT " + message);
118-
synchronized (writer) {
135+
synchronized (writer) {
119136
writer.write(message + "\r\n");
120137
writer.flush();
121138
}

0 commit comments

Comments
 (0)