Skip to content

Commit b7f5cbe

Browse files
committed
Optimize join regex & more comments
1 parent 9fca3b1 commit b7f5cbe

File tree

5 files changed

+54
-61
lines changed

5 files changed

+54
-61
lines changed

src/main/java/com/udu3324/hytools/Command.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@ public String getCommandUsage(ICommandSender sender) {
2424

2525
@Override
2626
public void processCommand(ICommandSender sender, String[] args) throws CommandException {
27+
//put the commands in the collection
2728
collection.put("partyguess", "1");
2829
collection.put("partyguessfriends", "2");
2930
collection.put("partyguessguilds", "3");
3031
collection.put("nickalert", "4");
3132
collection.put("nickalerthypixelapi", "5");
3233

34+
// /hytools
3335
if (args.length < 1) {
3436
sender.addChatMessage(new ChatComponentText("\n" + EnumChatFormatting.GOLD + "[+]= Hytools v" + Reference.VERSION + " by udu3324 =[+]\n"
3537
+ EnumChatFormatting.GOLD + "Check out my GitHub! https://github.com/udu3324 \n\n"
36-
+ EnumChatFormatting.GREEN + "/fcheck (player1) (player2) - checks if player 1 is friends with player 2\n"
38+
+ EnumChatFormatting.GREEN + "/fcheck (player1) (player2) - checks player's friend\n"
3739
+ EnumChatFormatting.DARK_AQUA + "/hytools partyguess [toggle/on/off] [toggled|" + Config.getPartyGuess() + "]\n"
3840
+ EnumChatFormatting.DARK_GREEN + "/hytools partyguessfriends [toggle/on/off] [toggled|" + Config.getPartyGuessFriend() +"] \n"
3941
+ EnumChatFormatting.GOLD + "/hytools partyguessguilds [toggle/on/off] [toggled|" + Config.getPartyGuessGuild() + "]\n"
@@ -43,40 +45,36 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE
4345
return;
4446
}
4547

48+
// /hytools ??? (get tool toggled)
4649
if (args.length == 1) {
4750
String command = args[0].toUpperCase();
4851
String data;
4952

5053
if (command.equals("PARTYGUESS")) {
51-
if (Config.getPartyGuess()) {
54+
if (Config.getPartyGuess())
5255
data = "ON";
53-
} else {
56+
else
5457
data = "OFF";
55-
}
5658
} else if (command.equals("PARTYGUESSFRIENDS")) {
57-
if (Config.getPartyGuessFriend()) {
59+
if (Config.getPartyGuessFriend())
5860
data = "ON";
59-
} else {
61+
else
6062
data = "OFF";
61-
}
6263
} else if (command.equals("PARTYGUESSGUILDS")) {
63-
if (Config.getPartyGuessGuild()) {
64+
if (Config.getPartyGuessGuild())
6465
data = "ON";
65-
} else {
66+
else
6667
data = "OFF";
67-
}
6868
} else if (command.equals("NICKALERT")) {
69-
if (Config.getNickAlert()) {
69+
if (Config.getNickAlert())
7070
data = "ON";
71-
} else {
71+
else
7272
data = "OFF";
73-
}
7473
} else if (command.equals("NICKALERTHYPIXELAPI")) {
75-
if (Config.getNickAlertHypixelAPI()) {
74+
if (Config.getNickAlertHypixelAPI())
7675
data = "ON";
77-
} else {
76+
else
7877
data = "OFF";
79-
}
8078
} else {
8179
sender.addChatMessage(new ChatComponentText("\n" + EnumChatFormatting.RED + command +
8280
" doesn't exist in Hytools! "));
@@ -88,7 +86,7 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE
8886
return;
8987
}
9088

91-
89+
// /hytools ??? ??? (toggle a tool)
9290
if (args.length == 2) {
9391
String command = args[0].toLowerCase();
9492
String data = args[1].toLowerCase();
@@ -185,6 +183,7 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE
185183
EnumChatFormatting.GREEN + command + " is now set to " + data + "."));
186184
}
187185

186+
// /hytools ??? ??? ??? (invalid command)
188187
if (args.length >= 3) {
189188
sender.addChatMessage(new ChatComponentText("\n" + EnumChatFormatting.RED + "Too many arguments! You probably have a typo."));
190189
return;

src/main/java/com/udu3324/hytools/Config.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
public class Config {
1313
public static File configFile = new File(Loader.instance().getConfigDir(), "hytools.cfg");
1414

15+
//returns if config is not the current version
1516
public static Boolean isNotCurrentVersion() throws IOException {
1617
BufferedReader brTest = new BufferedReader(new FileReader(configFile));
1718
String text = brTest.readLine();
@@ -20,11 +21,9 @@ public static Boolean isNotCurrentVersion() throws IOException {
2021
if (!text.contains(Reference.VERSION)) return true;
2122
return false;
2223
}
23-
24+
25+
//example: getValueFromConfig("api-key") returns "thisIsTheApiKey"
2426
public static String getValueFromConfig(String value) {
25-
//example:
26-
//getValueFromConfig("api-key") returns qergijoiwequjfboqwiuefnweiofnwe
27-
2827
String data = null;
2928
try {
3029
BufferedReader file = new BufferedReader(new FileReader(configFile));
@@ -47,11 +46,9 @@ public static String getValueFromConfig(String value) {
4746

4847
return data;
4948
}
50-
49+
50+
//example: setValueFromConfig("api-key", "thisIsTheApiKey")
5151
public static void setValueFromConfig(String value, String data) {
52-
//example:
53-
//setValueFromConfig("api-key", "qergijoiwequjfboqwiuefnweiofnwe")
54-
5552
try {
5653
// input the (modified) file content to the StringBuffer "input"
5754
BufferedReader file = new BufferedReader(new FileReader(configFile));
@@ -75,16 +72,16 @@ public static void setValueFromConfig(String value, String data) {
7572
Hytools.log.info("Problem writing file.");
7673
}
7774
}
78-
75+
76+
//delete the config file
7977
public static void delete() {
80-
//deletes the config file
81-
if (configFile.delete()) {
78+
if (configFile.delete())
8279
Hytools.log.info("Config file has been succesfully deleted.");
83-
} else {
80+
else
8481
Hytools.log.info("Error! Config file couldn't be deleted!");
85-
}
8682
}
8783

84+
//create a config file
8885
public static void create() {
8986
try {
9087
if (configFile.createNewFile()) {
@@ -128,7 +125,7 @@ public static void create() {
128125
}
129126
}
130127

131-
// premade methods to do the ones above automatically
128+
// hypixel api key
132129
public static String getStoredAPIKey() {
133130
return getValueFromConfig("api-key");
134131
}
@@ -137,6 +134,7 @@ public static void setStoredAPIKey(String key) {
137134
setValueFromConfig("api-key", key);
138135
}
139136

137+
// party guess
140138
public static Boolean getPartyGuess() {
141139
String str = getValueFromConfig("party-guess_toggled");
142140
if (str.equals("true")) {
@@ -150,6 +148,7 @@ public static void setPartyGuess(Boolean bool) {
150148
setValueFromConfig("party-guess_toggled", String.valueOf(bool));
151149
}
152150

151+
// party guess friend
153152
public static Boolean getPartyGuessFriend() {
154153
String str = getValueFromConfig("party-guess-friendmsg_toggled");
155154
if (str.equals("true")) {
@@ -158,44 +157,44 @@ public static Boolean getPartyGuessFriend() {
158157
return false;
159158
}
160159
}
161-
160+
162161
public static void setPartyGuessFriend(Boolean bool) {
163162
setValueFromConfig("party-guess-friendmsg_toggled", String.valueOf(bool));
164163
}
165164

165+
// party guess guild
166166
public static Boolean getPartyGuessGuild() {
167167
String str = getValueFromConfig("party-guess-guildcheck_toggled");
168-
if (str.equals("true")) {
168+
if (str.equals("true"))
169169
return true;
170-
} else {
170+
else
171171
return false;
172-
}
173172
}
174173

175174
public static void setPartyGuessGuild(Boolean bool) {
176175
setValueFromConfig("party-guess-guildcheck_toggled", String.valueOf(bool));
177176
}
178-
177+
178+
// nick alert
179179
public static Boolean getNickAlert() {
180180
String str = getValueFromConfig("nick-alert_toggled");
181-
if (str.equals("true")) {
181+
if (str.equals("true"))
182182
return true;
183-
} else {
183+
else
184184
return false;
185-
}
186185
}
187186

188187
public static void setNickAlert(Boolean bool) {
189188
setValueFromConfig("nick-alert_toggled", String.valueOf(bool));
190189
}
191190

191+
// nick alert hypixel api
192192
public static Boolean getNickAlertHypixelAPI() {
193193
String str = getValueFromConfig("nick-alert-hypixel-api_toggled");
194-
if (str.equals("true")) {
194+
if (str.equals("true"))
195195
return true;
196-
} else {
196+
else
197197
return false;
198-
}
199198
}
200199

201200
public static void setNickAlertHypixelAPI(Boolean bool) {

src/main/java/com/udu3324/hytools/FCheck.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.udu3324.hytools;
22

33
import java.util.ArrayList;
4-
import java.util.HashMap;
54

65
import com.udu3324.hytools.hyapi.FriendsOfUUID;
76
import com.udu3324.hytools.hyapi.RankOfUUID;
@@ -28,12 +27,14 @@ public String getCommandUsage(ICommandSender sender) {
2827

2928
@Override
3029
public void processCommand(ICommandSender sender, String[] args) throws CommandException {
30+
// /fcheck (invalid)
3131
if (args.length < 2) {
3232
sender.addChatMessage(new ChatComponentText(
3333
"\n" + EnumChatFormatting.RED + "Not enough arguments! You probably have a typo."));
3434
return;
3535
}
3636

37+
// /fcheck ???? ????
3738
if (args.length == 2) {
3839
final String player1 = args[0];
3940
final String player2 = args[1];
@@ -65,11 +66,10 @@ public void run() {
6566
}
6667

6768
// return result of player2 is found in friends list of player1
68-
if (listOfFriends.contains(uuid2)) {
69+
if (listOfFriends.contains(uuid2))
6970
Hytools.sendMessage("\u00A7A" + RankOfUUID.get(uuid1) + IGN.get(uuid1) + "\u00A7A is friends with " + RankOfUUID.get(uuid2) + IGN.get(uuid2) + "\u00A7A.");
70-
} else {
71+
else
7172
Hytools.sendMessage("\u00A7C" + RankOfUUID.get(uuid1) + IGN.get(uuid1) + "\u00A7C is not friends with " + RankOfUUID.get(uuid2) + IGN.get(uuid2) + "\u00A7C.");
72-
}
7373
} catch (Exception e) {
7474
// TODO Auto-generated catch block
7575
e.printStackTrace();

src/main/java/com/udu3324/hytools/Hytools.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,27 +133,24 @@ public void onPlayerChat(ClientChatReceivedEvent event) {
133133
//check chat messages for regex
134134
String filterSpaces = filtered.replaceAll(" ", "");
135135

136-
Matcher hytillities = Pattern.compile("(?=.*^\\+ \\()(?=.*\\) )").matcher(filtered);
136+
// ^\+ \([0-9]+\/[0-9]+\) [a-zA-Z0-9_]{1,16}$ | "+ (23/24) NintendoOS"
137+
Matcher hytillitiesJoined = Pattern.compile("^\\+ \\([0-9]+\\/[0-9]+\\) [a-zA-Z0-9_]{1,16}$").matcher(filtered);
137138

138-
Matcher joined = Pattern.compile("(?=.* has joined \\()(?=.*\\)!$)").matcher(filtered);
139-
Matcher noFrontSpace = Pattern.compile("^ ").matcher(filtered);
139+
// ^[a-zA-Z0-9_]{1,16} has joined \([0-9]+/[0-9]+\)!$ | "NintendoOS has joined (1/16)!"
140+
Matcher joined = Pattern.compile("^[a-zA-Z0-9_]{1,16} has joined \\([0-9]+/[0-9]+\\)!$").matcher(filtered);
140141
Matcher duels = Pattern.compile("^Opponent:").matcher(filterSpaces);
141142

142-
int countOfSpaces = filtered.length() - filtered.replace(" ", "").length();
143-
144-
if (joined.find() && !noFrontSpace.find() && countOfSpaces == 3) {
143+
if (joined.find()) {
145144
runTools(filtered, false);
146-
} else if (hytillities.find() && !noFrontSpace.find() && countOfSpaces == 2) {
147-
//player is using hytillities! adjust for it
145+
} else if (hytillitiesJoined.find()) {
148146
runTools(filtered, true);
149147
} else if (duels.find()) {
150148
//remove opponent
151149
filterSpaces = filterSpaces.substring(9);
152150

153151
//remove rank prefix (if it has one)
154-
if (filterSpaces.indexOf(']') != -1) {
152+
if (filterSpaces.indexOf(']') != -1)
155153
filterSpaces = filterSpaces.substring(filterSpaces.indexOf(']') + 1);
156-
}
157154

158155
//pass it on to nickalert
159156
runTools(filterSpaces, false);

src/main/resources/mcmod.info

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
[
2-
{
3-
"modid": "HT",
1+
[{
2+
"modid": "Hytools",
43
"name": "Hytools",
54
"description": "A mod that has useful tools for Hypixel.",
65
"version": "0",
@@ -12,5 +11,4 @@
1211
"logoFile": "logo.png",
1312
"screenshots": [],
1413
"dependencies": []
15-
}
16-
]
14+
}]

0 commit comments

Comments
 (0)