Skip to content

Commit 07eb825

Browse files
authored
Merge pull request #20 from Gikkman/dev-0.6
up-to-0.6
2 parents ea1136a + bbbeb04 commit 07eb825

13 files changed

Lines changed: 222 additions & 150 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,6 @@ $RECYCLE.BIN/
132132
# Windows shortcuts
133133
*.lnk
134134
.idea/
135+
136+
# IntelliJ Idea
137+
/twirc.iml

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,25 @@ Include the following in your pom.xml
2222
<dependency>
2323
<groupId>com.github.gikkman</groupId>
2424
<artifactId>Java-Twirk</artifactId>
25-
<version>0.5</version>
25+
<version>0.6</version>
2626
</dependency>
2727
</dependencies>
2828
```
2929
Or simply download the latest version of the library jar from the release page.
3030

3131
## Changes
32-
There has been some new features in version 0.5, but nothing that is breaking backwards compability. New features include:
33-
* Whispers (This was removed in 0.4, but is now back. Twitch said they'd remove it , but they haven't so far so...)
34-
* Support for new sub-streak logic
35-
* Updated the example
36-
* Added even more tests
37-
* Touched up on some JavaDoc
32+
There has only been minor changes between 0.5 and 0.6. Nothing that should break backwards compatibility. Fixes include:
33+
* Fixed NumberFormatException on modified emotes
34+
* Twitch changed the emote IDs from always being a number to sometimes being a number_string -.-' Its fixed now
35+
* Fixed SockerClosedException stacktrace printing on some locales
36+
* I think I fixed it at least, but this one is hard to test since there are so many locales.
37+
* Updated the emotes parse for a safer and faster implementation.
38+
* This deprecates a previously public method (`EmoteParse.parseEmote(String, String)`), and the new method is package private. There isn't really any need to call these methods from outside the library
39+
* Twirk will not only show the "User X was not online" or "User X was already online", when in verbose mode.
40+
* This happened when we see a leave/part message but didn't track the user correctly. I felt like it was not needed unless you want the verbose output
41+
* Updated the example a bit
42+
* Started to move towards proper JUnit tests
43+
* My home rolled test setup wasn't very user friendly, so now I started moving to user regular `@Test` tests. I'll eventually convert all tests to this format
3844

3945
And probably some more...
4046

pom.xml

Lines changed: 1 addition & 1 deletion
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.5.0</version>
7+
<version>0.6.0</version>
88
<packaging>jar</packaging>
99

1010
<name>twirc</name>

src/example/java/com/gikk/twirk/BotExample.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public static void main(String[] args) throws IOException, InterruptedException{
3333
twirk.addIrcListener( getOnDisconnectListener(twirk) );
3434
twirk.addIrcListener( new PatternCommandExample(twirk) );
3535
twirk.addIrcListener( new PrefixCommandExample(twirk) );
36-
36+
37+
System.out.println("\nTo exit this example, type .quit and press Enter\n");
38+
3739
twirk.connect(); //Connect to Twitch
3840

3941
//As long as we don't type .quit into the command prompt, send everything we type as a message to twitch

src/example/java/com/gikk/twirk/commands/CommandExampleBase.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
import java.util.Set;
1010

1111
public abstract class CommandExampleBase implements TwirkListener {
12+
/**
13+
* A PREFIX_COMMAND are the classical commands that starts with a certain pattern (e.g. !time)
14+
* A CONTENT_COMMAND is a command that looks for a certain pattern in the message (e.g. this is !tick)
15+
*/
1216
public enum CommandType{ PREFIX_COMMAND, CONTENT_COMMAND };
1317

1418
//***********************************************************************************************

src/example/java/com/gikk/twirk/commands/PatternCommandExample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.gikk.twirk.types.users.TwitchUser;
77

88
public class PatternCommandExample extends CommandExampleBase{
9-
private static String PATTERN = "tick";
9+
private static String PATTERN = "!tick";
1010

1111
private final Twirk twirk;
1212

@@ -27,6 +27,6 @@ protected USER_TYPE getMinUserPrevilidge() {
2727

2828
@Override
2929
protected void performCommand(String command, TwitchUser sender, TwitchMessage message) {
30-
twirk.channelMessage("Tock");
30+
twirk.channelMessage("tock");
3131
}
3232
}

src/main/java/com/gikk/twirk/InputThread.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ public void run() {
6363
}
6464
catch (IOException e) {
6565
//This probably means we force closed the socket. In case something else occurred, we print the StackTrace
66-
String message = e.getMessage();
67-
if( (message.contains("Socket Closed")) ){
66+
String message = e.getMessage().toLowerCase();
67+
if( (message.toLowerCase().contains("socket closed")) ){
6868
//Ignore
69-
} else if ( message.contains("Connection reset") || message.contains("Stream closed")) {
69+
} else if ( message.contains("connection reset") || message.contains("stream closed")) {
7070
System.err.println( message );
7171
}
7272
else {

src/main/java/com/gikk/twirk/TwirkMaintainanceListener.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ public void onAnything(String line) {
2929
@Override
3030
public void onJoin(String joinedNick) {
3131
if( !instance.online.add( joinedNick ) ) {
32-
System.out.println(" was already listed as online...." + "\tUser " + joinedNick);
32+
if(instance.verboseMode)
33+
System.out.println("\tUser " + joinedNick + " was already listed as online....");
3334
}
3435
}
3536

3637
@Override
3738
public void onPart(String partedNick) {
3839
if( !instance.online.remove( partedNick ) ) {
39-
System.out.println("\tUser " + partedNick + " was not listed as online....");
40+
if(instance.verboseMode)
41+
System.out.println("\tUser " + partedNick + " was not listed as online....");
4042
}
4143
}
4244

Lines changed: 17 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,22 @@
11
package com.gikk.twirk.types.emote;
22

3-
import java.util.LinkedList;
4-
import java.util.List;
5-
6-
public class EmoteParser {
7-
private static final String EMOTES_IDENTIFIER = "emotes=";
8-
9-
public static List<Emote> parseEmotes(String content, String tag) {
10-
/* Emotes come in sets formated like this:
11-
*
12-
* emotes=15614:0-6/4685:8-13,15-21 (Message = tmrToad tmrHat tmrHat)
13-
*
14-
* The first number indicates the emotes ID. The emote ID is follower by a :
15-
* The numbers separated by a - indicates the indices in the message that makes up the emote.
16-
* If an emote appears several times in a message, the different index ranges are separated by a ,
17-
*
18-
* So we find the begining of the emotes section and the end of the section.
19-
* Then, check that the message actually contains an emotes section and that
20-
* the emote section actually contains data.
21-
*/
22-
List<Emote> emotes = new LinkedList<>();
23-
24-
int begin = tag.indexOf( EMOTES_IDENTIFIER );
25-
int end = tag.indexOf(';', begin );
26-
if( begin == -1 || begin + EMOTES_IDENTIFIER.length() == end){
27-
return emotes;
28-
}
29-
30-
/* This segment is a head ache, but here is a basic idea of what it is doing:
31-
* Iterate through the emotes-section.
32-
* The first part will be the emote ID. it is terminated by a :
33-
* The second part is the emote's start index. It is terminated by a -
34-
* The third part is the emote's end index. It is terminated EITHER by a / or a , or by the segment ending
35-
* If terminated by a , the emote is present more than once in the message. So the next part will be a new begin index
36-
* If terminated by a / there is another emote in the message. The next part is a new emote ID
37-
* If terminated by the segment ending we simply finish the last begin-end pair and we are done
38-
*/
39-
String emotesString = tag.substring( begin + EMOTES_IDENTIFIER.length(), end);
3+
import com.gikk.twirk.types.TagMap;
404

41-
EmoteImpl emote = new EmoteImpl();
42-
StringBuilder str = new StringBuilder();
43-
String emoteID = "", beginIndex = "";
44-
for( char c : emotesString.toCharArray() ){
45-
switch(c) {
46-
case ':':
47-
emoteID = str.toString();
48-
str.setLength(0);
49-
break;
50-
case '-' :
51-
beginIndex = str.toString();
52-
str.setLength(0);
53-
break;
54-
case ',':
55-
addIndices(emote, beginIndex, str.toString() );
56-
str.setLength(0);
57-
break;
58-
case '/':
59-
finalizeEmote(content, emotes, emote, emoteID, beginIndex, str.toString() );
60-
emote = new EmoteImpl();
61-
str.setLength(0);
62-
break;
63-
default:
64-
str.append(c);
65-
}
66-
}
67-
//The emotes segment ended, so we finish the current emote's begin-end pair, then we're done
68-
finalizeEmote(content, emotes, emote, emoteID, beginIndex, str.toString());
69-
70-
return emotes;
71-
}
72-
73-
private static void finalizeEmote(String content, List<Emote> emotes, EmoteImpl emote, String emoteID, String beginIndex, String endIndex) {
74-
int begin = Integer.parseInt( beginIndex );
75-
int end = Integer.parseInt( endIndex ) + 1; //The end index we receive from Twitch is inclusive, but Java is almost always exclusive
76-
emote.addIndices(begin, end);
77-
78-
emote.setEmoteID( Integer.parseInt( emoteID ) );
79-
emote.setPattern( content.substring(begin, end) );
80-
emotes.add(emote);
81-
}
5+
import java.util.List;
826

83-
private static void addIndices(EmoteImpl emote, String beginIndex, String endIndex){
84-
int begin = Integer.parseInt( beginIndex );
85-
int end = Integer.parseInt( endIndex ) + 1;
86-
emote.addIndices(begin, end);
87-
}
7+
public interface EmoteParser {
8+
/**
9+
* @deprecated Use {{@link #parseEmotes(TagMap, String)} instead. This will be removed in a future release}
10+
*/
11+
@Deprecated
12+
static List<Emote> parseEmotes(String content, String tag) {
13+
TagMap tagMap = TagMap.getDefault(tag);
14+
return new EmoteParserImpl().parseEmotes(tagMap, content);
15+
}
16+
17+
static EmoteParser getDefaultImplementation() {
18+
return new EmoteParserImpl();
19+
}
20+
21+
List<Emote> parseEmotes(TagMap tagMap, String content);
8822
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.gikk.twirk.types.emote;
2+
3+
import com.gikk.twirk.types.TagMap;
4+
import com.gikk.twirk.types.TwitchTags;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class EmoteParserImpl implements EmoteParser {
10+
11+
@Override
12+
public List<Emote> parseEmotes(TagMap tagMap, String content) {
13+
/* Emotes come in sets formatted like this:
14+
*
15+
* emotes=15614:0-6/4685:8-13,15-21 (Message = tmrToad tmrHat tmrHat)
16+
*
17+
* The first number indicates the emotes ID. The emote ID is follower by a :
18+
* The numbers separated by a - indicates the indices in the message that makes up the emote.
19+
* If an emote appears several times in a message, the different index ranges are separated by a ,
20+
*
21+
* Thankfully the tagmap does the extraction for us, but this piece of information is left here for reference
22+
*/
23+
24+
/* This segment is a head ache, but here is a basic idea of what it is doing:
25+
* Iterate through the emotes-section.
26+
* The first part will be the emote ID. it is terminated by a :
27+
* The second part is the emote's start index. It is terminated by a -
28+
* The third part is the emote's end index. It is terminated EITHER by a / or a , or by the segment ending
29+
* If terminated by a , the emote is present more than once in the message. So the next part will be a new begin index
30+
* If terminated by a / there is another emote in the message. The next part is a new emote ID
31+
* If terminated by the segment ending we simply finish the last begin-end pair and we are done
32+
*/
33+
List<Emote> emotes = new ArrayList<>();
34+
String emotesString = tagMap.getAsString(TwitchTags.EMOTES);
35+
if(emotesString == null || emotesString.isEmpty()) return emotes;
36+
37+
EmoteImpl emote = new EmoteImpl();
38+
StringBuilder str = new StringBuilder();
39+
String emoteID = "", beginIndex = "";
40+
for( char c : emotesString.toCharArray() ){
41+
switch(c) {
42+
case ':':
43+
emoteID = str.toString();
44+
str.setLength(0);
45+
break;
46+
case '-' :
47+
beginIndex = str.toString();
48+
str.setLength(0);
49+
break;
50+
case ',':
51+
addIndices(emote, beginIndex, str.toString() );
52+
str.setLength(0);
53+
break;
54+
case '/':
55+
finalizeEmote(content, emotes, emote, emoteID, beginIndex, str.toString() );
56+
emote = new EmoteImpl();
57+
str.setLength(0);
58+
break;
59+
default:
60+
str.append(c);
61+
}
62+
}
63+
//The emotes segment ended, so we finish the current emote's begin-end pair, then we're done
64+
finalizeEmote(content, emotes, emote, emoteID, beginIndex, str.toString());
65+
66+
return emotes;
67+
}
68+
69+
private static void finalizeEmote(String content, List<Emote> emotes, EmoteImpl emote, String emoteID, String beginIndex, String endIndex) {
70+
int begin = Integer.parseInt( beginIndex );
71+
int end = Integer.parseInt( endIndex ) + 1; //The end index we receive from Twitch is inclusive, but Java is almost always exclusive
72+
emote.addIndices(begin, end);
73+
74+
if(emoteID.contains("_")) emoteID = emoteID.substring(0, emoteID.indexOf("_"));
75+
emote.setEmoteID( Integer.parseInt( emoteID ) );
76+
emote.setPattern( content.substring(begin, end) );
77+
emotes.add(emote);
78+
}
79+
80+
private static void addIndices(EmoteImpl emote, String beginIndex, String endIndex){
81+
int begin = Integer.parseInt( beginIndex );
82+
int end = Integer.parseInt( endIndex ) + 1;
83+
emote.addIndices(begin, end);
84+
}
85+
}

0 commit comments

Comments
 (0)