├── .gitignore
├── README.md
├── pom.xml
└── src
└── main
├── java
└── tv
│ └── banko
│ └── twitchwhitelist
│ ├── TwitchWhitelist.java
│ ├── commands
│ ├── WhitelistCommand.java
│ └── WhitelistSettingsCommand.java
│ ├── config
│ └── TwitchConfig.java
│ ├── listeners
│ └── PlayerLogin.java
│ ├── twitch
│ └── Bot.java
│ ├── utils
│ └── MojangRequest.java
│ └── whitelist
│ ├── Whitelist.java
│ ├── WhitelistType.java
│ └── WhitelistUser.java
└── resources
└── plugin.yml
/.gitignore:
--------------------------------------------------------------------------------
1 | # User-specific stuff
2 | .idea/
3 |
4 | *.iml
5 | *.ipr
6 | *.iws
7 |
8 | # IntelliJ
9 | out/
10 |
11 | # Compiled class file
12 | *.class
13 |
14 | # Log file
15 | *.log
16 |
17 | # BlueJ files
18 | *.ctxt
19 |
20 | # Package Files #
21 | *.jar
22 | *.war
23 | *.nar
24 | *.ear
25 | *.zip
26 | *.tar.gz
27 | *.rar
28 |
29 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
30 | hs_err_pid*
31 |
32 | *~
33 |
34 | # temporary files which can be created if a process still has a handle open of a deleted file
35 | .fuse_hidden*
36 |
37 | # KDE directory preferences
38 | .directory
39 |
40 | # Linux trash folder which might appear on any partition or disk
41 | .Trash-*
42 |
43 | # .nfs files are created when an open file is removed but is still being accessed
44 | .nfs*
45 |
46 | # General
47 | .DS_Store
48 | .AppleDouble
49 | .LSOverride
50 |
51 | # Icon must end with two \r
52 | Icon
53 |
54 | # Thumbnails
55 | ._*
56 |
57 | # Files that might appear in the root of a volume
58 | .DocumentRevisions-V100
59 | .fseventsd
60 | .Spotlight-V100
61 | .TemporaryItems
62 | .Trashes
63 | .VolumeIcon.icns
64 | .com.apple.timemachine.donotpresent
65 |
66 | # Directories potentially created on remote AFP share
67 | .AppleDB
68 | .AppleDesktop
69 | Network Trash Folder
70 | Temporary Items
71 | .apdisk
72 |
73 | # Windows thumbnail cache files
74 | Thumbs.db
75 | Thumbs.db:encryptable
76 | ehthumbs.db
77 | ehthumbs_vista.db
78 |
79 | # Dump file
80 | *.stackdump
81 |
82 | # Folder config file
83 | [Dd]esktop.ini
84 |
85 | # Recycle Bin used on file shares
86 | $RECYCLE.BIN/
87 |
88 | # Windows Installer files
89 | *.cab
90 | *.msi
91 | *.msix
92 | *.msm
93 | *.msp
94 |
95 | # Windows shortcuts
96 | *.lnk
97 |
98 | target/
99 |
100 | pom.xml.tag
101 | pom.xml.releaseBackup
102 | pom.xml.versionsBackup
103 | pom.xml.next
104 |
105 | release.properties
106 | dependency-reduced-pom.xml
107 | buildNumber.properties
108 | .mvn/timing.properties
109 | .mvn/wrapper/maven-wrapper.jar
110 | .flattened-pom.xml
111 |
112 | # Common working directory
113 | run/
114 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # TwitchWhitelist
2 |
3 | This plugin connects your **Minecraft Server** with your **Twitch Chat**.
4 | You can **whitelist** players by using following functions:
5 | * **Channel Points Redemptions** → Enter the username in a specific redemption
6 | * **Using a Command** → Enter `! ` in the twitch chat
7 |
8 | Minecraft Server Version Support: **1.8 or above**
9 |
10 | # Setup
11 |
12 | 1) Download the plugin via [the releases](https://github.com/DerBanko/TwitchWhitelist/releases).
13 | 2) Setup your minecraft server *(many tutorials on the internet)* and place the jar file in the `/plugins/` directory.
14 | 3) Start the server and connect on it.
15 | 4) Check for the permissions the player should have to use the commands.
16 |
17 | # Commands
18 |
19 | * /whitelist (alias: `/wl`, required permission: `twl.whitelist`)
20 | * `/whitelist (help)` - shows the help of the command
21 | * `/whitelist list` - shows the whitelisted players
22 | * `/whitelist add ` - add the player to the whitelist
23 | * `/whitelist remove ` - remove the player from the whitelist
24 | * `/whitelist user ` - shows the information of the command
25 |
26 | * /settings (required permission: `twl.settings`)
27 | * `/settings (help)` - shows the help of the command
28 | * `/settings token ` - set the access token of the twitch account ([get it here](https://twitchtokengenerator.com/))
29 | * `/settings cpname ` - set the name of the channel points reward
30 | * `/settings command ` - set the command which will be listened in twitch chat
31 | * `/settings users ` - set the users in which the bot will connect
32 | * `/settings togglecp ` - set if the players will be able to whitelist with channel points
33 | * `/settings togglecommand ` - set if the players will be able to whitelist with the command
34 | * `/settings reconnect` - reconnect the twitch bot
35 |
36 | # Credits
37 |
38 | The plugin uses the [twitch4j](https://github.com/twitch4j/twitch4j) API.
39 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | tv.banko
8 | twitchwhitelist
9 | 1.0.1
10 | jar
11 |
12 | TwitchWhitelist
13 |
14 | Twitch Whitelist with command or channel points
15 |
16 | 13.0.2
17 | UTF-8
18 |
19 | https://banko.tv
20 |
21 |
22 | ../../Export/Public/TwitchWhitelist/
23 |
24 |
25 | org.apache.maven.plugins
26 | maven-compiler-plugin
27 | 3.8.1
28 |
29 | 11
30 | 11
31 |
32 |
33 |
34 | org.apache.maven.plugins
35 | maven-shade-plugin
36 | 3.2.4
37 |
38 |
39 | package
40 |
41 | shade
42 |
43 |
44 | false
45 | TwitchWhitelist-${project.version}
46 | ../../Export/Public/TwitchWhitelist/
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 | src/main/resources
55 | true
56 |
57 |
58 |
59 |
60 |
61 |
62 | spigotmc-repo
63 | https://hub.spigotmc.org/nexus/content/repositories/snapshots/
64 |
65 |
66 | sonatype
67 | https://oss.sonatype.org/content/groups/public/
68 |
69 |
70 | jcenter
71 | https://jcenter.bintray.com/
72 |
73 |
74 |
75 |
76 |
77 | org.spigotmc
78 | spigot-api
79 | 1.8.8-R0.1-SNAPSHOT
80 | provided
81 |
82 |
83 | com.github.twitch4j
84 | twitch4j
85 | 1.5.1
86 | compile
87 |
88 |
89 |
90 |
--------------------------------------------------------------------------------
/src/main/java/tv/banko/twitchwhitelist/TwitchWhitelist.java:
--------------------------------------------------------------------------------
1 | package tv.banko.twitchwhitelist;
2 |
3 | import org.bukkit.Bukkit;
4 | import org.bukkit.plugin.java.JavaPlugin;
5 | import tv.banko.twitchwhitelist.commands.WhitelistCommand;
6 | import tv.banko.twitchwhitelist.commands.WhitelistSettingsCommand;
7 | import tv.banko.twitchwhitelist.listeners.PlayerLogin;
8 | import tv.banko.twitchwhitelist.twitch.Bot;
9 | import tv.banko.twitchwhitelist.utils.MojangRequest;
10 | import tv.banko.twitchwhitelist.whitelist.Whitelist;
11 |
12 | public final class TwitchWhitelist extends JavaPlugin {
13 |
14 | private static TwitchWhitelist instance;
15 |
16 | private Whitelist whitelist;
17 | private Bot bot;
18 |
19 | private MojangRequest request;
20 |
21 | public TwitchWhitelist() {
22 | instance = this;
23 | }
24 |
25 | @Override
26 | public void onEnable() {
27 | this.whitelist = new Whitelist();
28 | this.bot = new Bot();
29 | this.request = new MojangRequest();
30 |
31 | Bukkit.getPluginManager().registerEvents(new PlayerLogin(), this);
32 | getCommand("wl").setExecutor(new WhitelistCommand());
33 | getCommand("settings").setExecutor(new WhitelistSettingsCommand());
34 | }
35 |
36 | @Override
37 | public void onDisable() {
38 | whitelist.save();
39 | bot.stop();
40 | }
41 |
42 | public static String getPrefix() {
43 | return "§8[§5Twitch§lWL§8] §7";
44 | }
45 |
46 | public Whitelist getWhitelist() {
47 | return whitelist;
48 | }
49 |
50 | public Bot getBot() {
51 | return bot;
52 | }
53 |
54 | public MojangRequest getRequest() {
55 | return request;
56 | }
57 |
58 | public static TwitchWhitelist getInstance() {
59 | return instance;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/src/main/java/tv/banko/twitchwhitelist/commands/WhitelistCommand.java:
--------------------------------------------------------------------------------
1 | package tv.banko.twitchwhitelist.commands;
2 |
3 | import org.bukkit.Bukkit;
4 | import org.bukkit.OfflinePlayer;
5 | import org.bukkit.command.Command;
6 | import org.bukkit.command.CommandExecutor;
7 | import org.bukkit.command.CommandSender;
8 | import tv.banko.twitchwhitelist.TwitchWhitelist;
9 | import tv.banko.twitchwhitelist.whitelist.WhitelistType;
10 | import tv.banko.twitchwhitelist.whitelist.WhitelistUser;
11 |
12 | import java.util.Date;
13 | import java.util.UUID;
14 |
15 | public class WhitelistCommand implements CommandExecutor {
16 |
17 | @Override
18 | public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
19 |
20 | if(!sender.hasPermission("twl.whitelist")) {
21 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cYou are not allowed to use this command.");
22 | return true;
23 | }
24 |
25 | if(args.length < 1) {
26 | sendHelp(sender);
27 | return true;
28 | }
29 |
30 | switch (args[0].toLowerCase()) {
31 | case "list": {
32 |
33 | if(args.length != 1) {
34 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cUsage§8: §7/wl list");
35 | break;
36 | }
37 |
38 | int playersWhitelisted = TwitchWhitelist.getInstance().getWhitelist().getUUIDs().size();
39 |
40 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§7There " + (playersWhitelisted == 1 ? "is" : "are") +
41 | " currently §6" + playersWhitelisted +
42 | " player" + (playersWhitelisted == 1 ? "" : "s") + " §7whitelisted.");
43 |
44 | StringBuilder s = new StringBuilder(TwitchWhitelist.getPrefix() + "§6Players§8: ");
45 | boolean comma = false;
46 |
47 | for(UUID uuid : TwitchWhitelist.getInstance().getWhitelist().getUUIDs()) {
48 | OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(uuid);
49 |
50 | s.append("§7").append(offlinePlayer.getName());
51 |
52 | if(comma) {
53 | s.append("§8, ");
54 | }
55 |
56 | comma = true;
57 | }
58 |
59 | sender.sendMessage(s.toString());
60 | break;
61 | }
62 | case "add": {
63 | if(args.length != 2) {
64 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cUsage§8: §7/wl add ");
65 | break;
66 | }
67 |
68 | UUID uuid = TwitchWhitelist.getInstance().getRequest().getUUID(args[1]);
69 |
70 | if(uuid == null) {
71 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cThe player does not exist or too many requests have been made.");
72 | break;
73 | }
74 |
75 | if(TwitchWhitelist.getInstance().getWhitelist().isWhitelisted(uuid)) {
76 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cThe player is already whitelisted.");
77 | break;
78 | }
79 |
80 | TwitchWhitelist.getInstance().getWhitelist().addWhitelist(new WhitelistUser(uuid, "", "", System.currentTimeMillis(), WhitelistType.INGAME));
81 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§7The player §6" + args[1] + " §7has been successfully whitelisted.");
82 | break;
83 | }
84 | case "remove": {
85 | if(args.length != 2) {
86 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cUsage§8: §7/wl remove ");
87 | break;
88 | }
89 |
90 | UUID uuid = TwitchWhitelist.getInstance().getRequest().getUUID(args[1]);
91 |
92 | if(uuid == null) {
93 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cThe player does not exist or too many requests have been made.");
94 | break;
95 | }
96 |
97 | if(!TwitchWhitelist.getInstance().getWhitelist().isWhitelisted(uuid)) {
98 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cThe player is not whitelisted.");
99 | break;
100 | }
101 |
102 | TwitchWhitelist.getInstance().getWhitelist().removeWhitelist(uuid);
103 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§7The player §6" + args[1] + " §7has been successfully removed from whitelist.");
104 | break;
105 | }
106 | case "user": {
107 | if(args.length != 2) {
108 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cUsage§8: §7/wl user ");
109 | break;
110 | }
111 |
112 | UUID uuid = TwitchWhitelist.getInstance().getRequest().getUUID(args[1]);
113 |
114 | if(uuid == null) {
115 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cThe player does not exist or too many requests have been made.");
116 | break;
117 | }
118 |
119 | if(!TwitchWhitelist.getInstance().getWhitelist().isWhitelisted(uuid)) {
120 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cThe player is not whitelisted.");
121 | break;
122 | }
123 |
124 | WhitelistUser user = TwitchWhitelist.getInstance().getWhitelist().getWhitelistUser(uuid);
125 |
126 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§7Data of §6" + args[1] + "§8:");
127 | sender.sendMessage("§8- §7UUID§8: §6" + user.getUUID());
128 | sender.sendMessage("§8- §7Twitch Name§8: §6" + user.getTwitchName());
129 | sender.sendMessage("§8- §7Twitch ID§8: §6" + user.getID());
130 | sender.sendMessage("§8- §7Whitelisted on§8: §6" + new Date(user.getTimestamp()));
131 | sender.sendMessage("§8- §7Whitelist type§8: §6" + user.getType().name());
132 | break;
133 | }
134 | default: {
135 | sendHelp(sender);
136 | break;
137 | }
138 | }
139 | return true;
140 | }
141 |
142 | private void sendHelp(CommandSender sender) {
143 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§6/wl [help]§8: §fShows help");
144 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§6/wl list§8: §fShows the whitelisted players");
145 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§6/wl add §8: §fAdd player to whitelist");
146 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§6/wl remove §8: §fRemove player from whitelist");
147 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§6/wl user §8: §fShows the data of player");
148 | }
149 | }
150 |
--------------------------------------------------------------------------------
/src/main/java/tv/banko/twitchwhitelist/commands/WhitelistSettingsCommand.java:
--------------------------------------------------------------------------------
1 | package tv.banko.twitchwhitelist.commands;
2 |
3 | import org.bukkit.command.Command;
4 | import org.bukkit.command.CommandExecutor;
5 | import org.bukkit.command.CommandSender;
6 | import tv.banko.twitchwhitelist.TwitchWhitelist;
7 |
8 | public class WhitelistSettingsCommand implements CommandExecutor {
9 |
10 | @Override
11 | public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
12 |
13 | if (!sender.hasPermission("twl.settings")) {
14 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cYou are not allowed to use this command.");
15 | return true;
16 | }
17 |
18 | if (args.length < 1) {
19 | sendHelp(sender);
20 | return true;
21 | }
22 |
23 | switch (args[0].toLowerCase()) {
24 | case "cpname": {
25 |
26 | if (args.length < 2) {
27 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cUsage§8: §7/settings cpname ");
28 | break;
29 | }
30 |
31 | StringBuilder name = new StringBuilder();
32 |
33 | for (int i = 1; i < args.length; i++) {
34 | if (i != 1) {
35 | name.append(" ");
36 | }
37 | name.append(args[i]);
38 | }
39 |
40 | TwitchWhitelist.getInstance().getBot().getConfig().setChannelPointsName(name.toString());
41 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§7The name of the channel points redemtion has been set to §6" + name + "§7.");
42 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§aBot is reconnecting...");
43 | TwitchWhitelist.getInstance().getBot().reconnect();
44 | break;
45 | }
46 | case "token": {
47 |
48 | if (args.length != 2) {
49 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cUsage§8: §7/settings token ");
50 | break;
51 | }
52 |
53 | StringBuilder token = new StringBuilder();
54 |
55 | for (int i = 1; i < args.length; i++) {
56 | token.append(args[i]);
57 | }
58 |
59 | TwitchWhitelist.getInstance().getBot().getConfig().setAccessToken(token.toString());
60 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§7The access token has been set to §6§k" + token + "§7.");
61 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§aBot is reconnecting...");
62 | TwitchWhitelist.getInstance().getBot().setCredential();
63 | TwitchWhitelist.getInstance().getBot().reconnect();
64 | break;
65 | }
66 | case "command": {
67 |
68 | if (args.length != 2) {
69 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cUsage§8: §7/settings command ");
70 | break;
71 | }
72 |
73 | StringBuilder cmd = new StringBuilder();
74 |
75 | for (int i = 1; i < args.length; i++) {
76 | cmd.append(args[i]);
77 | }
78 |
79 | TwitchWhitelist.getInstance().getBot().getConfig().setCommand(cmd.toString());
80 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§7The command has been set to §6" + cmd + "§7.");
81 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§aBot is reconnecting...");
82 | TwitchWhitelist.getInstance().getBot().reconnect();
83 | break;
84 | }
85 | case "users": {
86 |
87 | if (args.length < 2) {
88 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cUsage§8: §7/settings users ");
89 | break;
90 | }
91 |
92 | StringBuilder users = new StringBuilder();
93 |
94 | for (int i = 1; i < args.length; i++) {
95 | if (i != 1) {
96 | users.append(" ");
97 | }
98 | users.append(args[i]);
99 | }
100 |
101 | TwitchWhitelist.getInstance().getBot().getConfig().setUsers(users.toString().split(" "));
102 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§7The users has been set to §6" + users.toString().replace(" ", "§8, §6") + "§7.");
103 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§aBot is reconnecting...");
104 | TwitchWhitelist.getInstance().getBot().reconnect();
105 | break;
106 | }
107 | case "togglecp": {
108 |
109 | if (args.length != 2) {
110 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cUsage§8: §7/settings togglecp ");
111 | break;
112 | }
113 |
114 | if (!args[1].equalsIgnoreCase("true") && !args[1].equalsIgnoreCase("false")) {
115 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cUsage§8: §7/settings togglecp ");
116 | break;
117 | }
118 |
119 | boolean b = Boolean.parseBoolean(args[1]);
120 |
121 | TwitchWhitelist.getInstance().getBot().getConfig().setWhitelistWithChannelPoints(b);
122 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§7The whitelist with channel points has been set to §6" + b + "§7.");
123 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§aBot is reconnecting...");
124 | TwitchWhitelist.getInstance().getBot().reconnect();
125 | break;
126 | }
127 | case "togglecommand": {
128 |
129 | if (args.length != 2) {
130 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cUsage§8: §7/settings togglecommand ");
131 | break;
132 | }
133 |
134 | if (!args[1].equalsIgnoreCase("true") && !args[1].equalsIgnoreCase("false")) {
135 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cUsage§8: §7/settings togglecommand ");
136 | break;
137 | }
138 |
139 | boolean b = Boolean.parseBoolean(args[1]);
140 |
141 | TwitchWhitelist.getInstance().getBot().getConfig().setWhitelistWithCommand(b);
142 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§7The whitelist with command has been set to §6" + b + "§7.");
143 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§aBot is reconnecting...");
144 | TwitchWhitelist.getInstance().getBot().reconnect();
145 | break;
146 | }
147 | case "reconnect": {
148 |
149 | if (args.length != 1) {
150 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§cUsage§8: §7/settings reconnect");
151 | break;
152 | }
153 |
154 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§aBot is reconnecting...");
155 | TwitchWhitelist.getInstance().getBot().reconnect();
156 | break;
157 | }
158 | default: {
159 | sendHelp(sender);
160 | break;
161 | }
162 | }
163 | return true;
164 | }
165 |
166 | private void sendHelp(CommandSender sender) {
167 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§6/settings [help]§8: §fShows help");
168 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§6/settings cpname §8: §fSet name of channel points reward");
169 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§6/settings token §8: §fSet access token of twitch account");
170 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§6/settings command §8: §fSet command for whitelist in twitch chat");
171 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§6/settings users §8: §fSet twitch channels in which the bot should connect");
172 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§6/settings togglecp §8: §fSet if players can whitelist with channel points");
173 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§6/settings togglecommand §8: §fSet if players can whitelist with the command");
174 | sender.sendMessage(TwitchWhitelist.getPrefix() + "§6/settings reconnect§8: §fReconnect the bot");
175 | }
176 | }
177 |
--------------------------------------------------------------------------------
/src/main/java/tv/banko/twitchwhitelist/config/TwitchConfig.java:
--------------------------------------------------------------------------------
1 | package tv.banko.twitchwhitelist.config;
2 |
3 | import org.bukkit.configuration.file.YamlConfiguration;
4 |
5 | import java.io.File;
6 | import java.io.IOException;
7 |
8 | public class TwitchConfig {
9 |
10 | private final File file;
11 | private final YamlConfiguration config;
12 |
13 | private String accessToken;
14 | private String[] users;
15 | private String command;
16 | private String channelPointsName;
17 |
18 | private boolean whitelistWithCommand;
19 | private boolean whitelistWithChannelPoints;
20 |
21 | public TwitchConfig() {
22 | File dir = new File("./plugins/TwitchWhitelist/");
23 |
24 | if(!dir.exists()) {
25 | dir.mkdirs();
26 | }
27 |
28 | file = new File(dir, "config.yml");
29 |
30 | if(!file.exists()) {
31 | try {
32 | file.createNewFile();
33 | } catch (IOException e) {
34 | e.printStackTrace();
35 | }
36 | }
37 |
38 | config = YamlConfiguration.loadConfiguration(file);
39 |
40 | load();
41 | }
42 |
43 | public String getAccessToken() {
44 | return accessToken;
45 | }
46 |
47 | public void setAccessToken(String accessToken) {
48 | this.accessToken = accessToken;
49 | }
50 |
51 | public String[] getUsers() {
52 | return users;
53 | }
54 |
55 | public void setUsers(String[] users) {
56 | this.users = users;
57 | }
58 |
59 | public String getCommand() {
60 | return command;
61 | }
62 |
63 | public void setCommand(String command) {
64 | this.command = command;
65 | }
66 |
67 | public String getChannelPointsName() {
68 | return channelPointsName;
69 | }
70 |
71 | public void setChannelPointsName(String channelPointsName) {
72 | this.channelPointsName = channelPointsName;
73 | }
74 |
75 | public boolean isWhitelistWithCommand() {
76 | return whitelistWithCommand;
77 | }
78 |
79 | public void setWhitelistWithCommand(boolean whitelistWithCommand) {
80 | this.whitelistWithCommand = whitelistWithCommand;
81 | }
82 |
83 | public boolean isWhitelistWithChannelPoints() {
84 | return whitelistWithChannelPoints;
85 | }
86 |
87 | public void setWhitelistWithChannelPoints(boolean whitelistWithChannelPoints) {
88 | this.whitelistWithChannelPoints = whitelistWithChannelPoints;
89 | }
90 |
91 | private void load() {
92 | if(config.contains("twitch.accessToken")) {
93 | accessToken = config.getString("twitch.accessToken");
94 | } else {
95 | accessToken = "";
96 | }
97 |
98 | if(config.contains("twitch.users")) {
99 | users = config.getStringList("twitch.users").toArray(new String[]{});
100 | } else {
101 | users = new String[] { };
102 | }
103 |
104 | if(config.contains("twitch.command")) {
105 | command = config.getString("twitch.command");
106 | } else {
107 | command = "!whitelist";
108 | }
109 |
110 | if(config.contains("twitch.channelPointsName")) {
111 | channelPointsName = config.getString("twitch.channelPointsName");
112 | } else {
113 | channelPointsName = "Whitelist";
114 | }
115 |
116 | if(config.contains("twitch.whitelistWithCommand")) {
117 | whitelistWithCommand = config.getBoolean("twitch.whitelistWithCommand");
118 | } else {
119 | whitelistWithCommand = true;
120 | }
121 |
122 | if(config.contains("twitch.whitelistWithChannelPoints")) {
123 | whitelistWithChannelPoints = config.getBoolean("twitch.whitelistWithChannelPoints");
124 | } else {
125 | whitelistWithChannelPoints = false;
126 | }
127 | }
128 |
129 | public void save() {
130 | config.set("twitch.accessToken", accessToken);
131 | config.set("twitch.users", users);
132 | config.set("twitch.command", command);
133 | config.set("twitch.channelPointsName", channelPointsName);
134 | config.set("twitch.whitelistWithCommand", whitelistWithCommand);
135 | config.set("twitch.whitelistWithChannelPoints", whitelistWithChannelPoints);
136 |
137 | try {
138 | config.save(file);
139 | } catch (IOException e) {
140 | e.printStackTrace();
141 | }
142 | }
143 | }
144 |
--------------------------------------------------------------------------------
/src/main/java/tv/banko/twitchwhitelist/listeners/PlayerLogin.java:
--------------------------------------------------------------------------------
1 | package tv.banko.twitchwhitelist.listeners;
2 |
3 | import org.bukkit.event.EventHandler;
4 | import org.bukkit.event.Listener;
5 | import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
6 | import tv.banko.twitchwhitelist.TwitchWhitelist;
7 |
8 | public class PlayerLogin implements Listener {
9 |
10 | @EventHandler
11 | public void onLogin(AsyncPlayerPreLoginEvent event) {
12 |
13 | if(event.getUniqueId() == null) {
14 | event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_WHITELIST, "§cCould not verify your account. §7Try again");
15 | return;
16 | }
17 |
18 | if(!TwitchWhitelist.getInstance().getWhitelist().isWhitelisted(event.getUniqueId())) {
19 | event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_WHITELIST, "§cYou are not whitelisted.");
20 | return;
21 | }
22 |
23 | event.allow();
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/tv/banko/twitchwhitelist/twitch/Bot.java:
--------------------------------------------------------------------------------
1 | package tv.banko.twitchwhitelist.twitch;
2 |
3 | import com.github.philippheuer.credentialmanager.domain.OAuth2Credential;
4 | import com.github.philippheuer.events4j.simple.SimpleEventHandler;
5 | import com.github.twitch4j.TwitchClient;
6 | import com.github.twitch4j.TwitchClientBuilder;
7 | import com.github.twitch4j.chat.events.channel.ChannelMessageEvent;
8 | import com.github.twitch4j.helix.domain.UserList;
9 | import com.github.twitch4j.pubsub.events.RewardRedeemedEvent;
10 | import org.bukkit.Bukkit;
11 | import tv.banko.twitchwhitelist.TwitchWhitelist;
12 | import tv.banko.twitchwhitelist.config.TwitchConfig;
13 | import tv.banko.twitchwhitelist.whitelist.WhitelistType;
14 | import tv.banko.twitchwhitelist.whitelist.WhitelistUser;
15 |
16 | import java.util.Arrays;
17 | import java.util.Objects;
18 | import java.util.UUID;
19 | import java.util.logging.Level;
20 |
21 | public class Bot {
22 |
23 | private TwitchClient twitchClient;
24 | private OAuth2Credential credential;
25 | private final String[] users;
26 |
27 | private final TwitchConfig config;
28 |
29 | public Bot() {
30 |
31 | config = new TwitchConfig();
32 | users = config.getUsers();
33 |
34 | setCredential();
35 | connect();
36 | }
37 |
38 | public void setCredential() {
39 | if(Objects.equals(config.getAccessToken(), "")) {
40 | TwitchWhitelist.getInstance().getLogger().warning("There is no access token set. Please set it to use the plugin.");
41 | return;
42 | }
43 |
44 | credential = new OAuth2Credential("twitch", config.getAccessToken());
45 | }
46 |
47 | public void connect() {
48 |
49 | if(credential == null) {
50 | setCredential();
51 | }
52 |
53 | if(twitchClient != null) {
54 | return;
55 | }
56 |
57 | twitchClient = TwitchClientBuilder.builder()
58 | .withEnableChat(true)
59 | .withEnablePubSub(true)
60 | .withEnableHelix(true)
61 | .withDefaultEventHandler(SimpleEventHandler.class)
62 | .withChatAccount(credential)
63 | .build();
64 |
65 | for (String user : users) {
66 | twitchClient.getChat().joinChannel(user);
67 | }
68 |
69 | registerListeners();
70 | }
71 |
72 | public void disconnect() {
73 |
74 | if(twitchClient == null) {
75 | return;
76 | }
77 |
78 | twitchClient.close();
79 | twitchClient = null;
80 | }
81 |
82 | public void reconnect() {
83 |
84 | if(twitchClient == null) {
85 | return;
86 | }
87 |
88 | disconnect();
89 | connect();
90 | }
91 |
92 | private void registerListeners() {
93 |
94 | if(twitchClient == null) {
95 | return;
96 | }
97 |
98 | try {
99 | UserList resultList = twitchClient.getHelix().getUsers(config.getAccessToken(), null, Arrays.asList(users)).execute();
100 | resultList.getUsers().forEach(user -> twitchClient.getPubSub().listenForChannelPointsRedemptionEvents(credential, user.getId()));
101 |
102 | if (config.isWhitelistWithChannelPoints()) {
103 | twitchClient.getEventManager().onEvent(RewardRedeemedEvent.class, (rewardRedeemedEvent -> {
104 |
105 | if (!rewardRedeemedEvent.getRedemption().getReward().getTitle().equalsIgnoreCase(config.getChannelPointsName())) {
106 | return;
107 | }
108 |
109 | if (rewardRedeemedEvent.getRedemption().getUserInput() == null) {
110 | return;
111 | }
112 |
113 | String user = rewardRedeemedEvent.getRedemption().getUser().getDisplayName();
114 | String id = rewardRedeemedEvent.getRedemption().getUser().getId();
115 | String playerName = rewardRedeemedEvent.getRedemption().getUserInput();
116 |
117 | UUID uuid = TwitchWhitelist.getInstance().getRequest().getUUID(playerName);
118 |
119 | if (uuid == null) {
120 | twitchClient.getChat().sendPrivateMessage(user, "The Minecraft Account \"" + playerName + "\" does not exist!");
121 | Bukkit.getLogger().log(Level.INFO, "(CHANNELPOINTS) Twitch User " + user + " [ID: " + id + "] whitelisted \"" +
122 | playerName + "\" but does not exist.");
123 | return;
124 | }
125 |
126 | if(TwitchWhitelist.getInstance().getWhitelist().hasTwitchWhitelist(id)) {
127 | twitchClient.getChat().sendPrivateMessage(user, "You have already whitelisted an account. " +
128 | "Please tell the administration to unwhitelist your minecraft account.");
129 | Bukkit.getLogger().log(Level.INFO, "(CHANNELPOINTS) Twitch User " + user + " [ID: " + id + "] has already whitelisted " +
130 | "an account.");
131 | return;
132 | }
133 |
134 | TwitchWhitelist.getInstance().getWhitelist().addWhitelist(new WhitelistUser(uuid, user, id, System.currentTimeMillis(), WhitelistType.CHANNEL_POINTS));
135 | twitchClient.getChat().sendPrivateMessage(user, "You have been successfully whitelisted with \"" + playerName + "\"!");
136 | Bukkit.getLogger().log(Level.INFO, "(CHANNELPOINTS) Twitch User " + user + " [ID: " + id + "] successfully whitelisted \"" +
137 | playerName + "\".");
138 | }));
139 | }
140 |
141 | if (config.isWhitelistWithCommand()) {
142 | twitchClient.getEventManager().onEvent(ChannelMessageEvent.class, event -> {
143 |
144 | String[] args = event.getMessage().split(" ");
145 |
146 | if (!args[0].equalsIgnoreCase(config.getCommand())) {
147 | return;
148 | }
149 |
150 | if (args.length != 2) {
151 | return;
152 | }
153 |
154 | String user = event.getUser().getName();
155 | String id = event.getUser().getId();
156 | String playerName = args[1];
157 |
158 | UUID uuid = TwitchWhitelist.getInstance().getRequest().getUUID(playerName);
159 |
160 | if (uuid == null) {
161 | twitchClient.getChat().sendPrivateMessage(user, "The Minecraft Account \"" + playerName + "\" does not exist!");
162 | Bukkit.getLogger().log(Level.INFO, "(COMMAND) Twitch User " + user + " [ID: " + id + "] whitelisted \"" +
163 | playerName + "\" but does not exist.");
164 | return;
165 | }
166 |
167 | if(TwitchWhitelist.getInstance().getWhitelist().hasTwitchWhitelist(id)) {
168 | twitchClient.getChat().sendPrivateMessage(user, "You have already whitelisted an account. " +
169 | "Please tell the administration to unwhitelist your minecraft account.");
170 | Bukkit.getLogger().log(Level.INFO, "(COMMAND) Twitch User " + user + " [ID: " + id + "] has already whitelisted " +
171 | "an account.");
172 | return;
173 | }
174 |
175 | TwitchWhitelist.getInstance().getWhitelist().addWhitelist(new WhitelistUser(uuid, user, id, System.currentTimeMillis(), WhitelistType.COMMAND));
176 | twitchClient.getChat().sendPrivateMessage(user, "You have been successfully whitelisted with \"" + playerName + "\"!");
177 | Bukkit.getLogger().log(Level.INFO, "(COMMAND) Twitch User " + user + " [ID: " + id + "] successfully whitelisted \"" +
178 | playerName + "\".");
179 | });
180 | }
181 | } catch (Exception e) {
182 | e.printStackTrace();
183 | }
184 | }
185 |
186 | public void stop() {
187 | config.save();
188 | disconnect();
189 | }
190 |
191 | public TwitchConfig getConfig() {
192 | return config;
193 | }
194 | }
--------------------------------------------------------------------------------
/src/main/java/tv/banko/twitchwhitelist/utils/MojangRequest.java:
--------------------------------------------------------------------------------
1 | package tv.banko.twitchwhitelist.utils;
2 |
3 | import com.google.gson.JsonElement;
4 | import com.google.gson.JsonObject;
5 | import com.google.gson.JsonParser;
6 | import org.apache.commons.io.IOUtils;
7 | import org.bukkit.Bukkit;
8 |
9 | import java.net.URL;
10 | import java.nio.charset.StandardCharsets;
11 | import java.util.UUID;
12 | import java.util.logging.Level;
13 |
14 | public class MojangRequest {
15 |
16 | private int requests;
17 | private long resetIn;
18 |
19 | public MojangRequest() {
20 | this.requests = 0;
21 | this.resetIn = System.currentTimeMillis() + (10 * 60 * 1000);
22 | }
23 |
24 | public UUID getUUID(String name) {
25 | String url = "https://api.mojang.com/users/profiles/minecraft/" + name;
26 |
27 | if (resetIn <= System.currentTimeMillis()) {
28 | requests = 0;
29 | resetIn = System.currentTimeMillis() + (10 * 60 * 1000);
30 | }
31 |
32 | if (requests > 400) {
33 | Bukkit.getLogger().log(Level.WARNING, "Could not manage mojang request of " + name + "! Too many requests in 10 Minutes.");
34 | return null;
35 | }
36 |
37 | try {
38 | String json = IOUtils.toString(new URL(url), StandardCharsets.UTF_8);
39 |
40 | requests++;
41 |
42 | if (json.isEmpty()) return null;
43 |
44 | JsonParser parser = new JsonParser();
45 | JsonElement jsonTree = parser.parse(json);
46 |
47 | JsonObject jsonObject = jsonTree.getAsJsonObject();
48 |
49 | String uuidString = jsonObject.get("id").toString().replace("\"", "");
50 | return fromString(uuidString);
51 | } catch (Exception e) {
52 | e.printStackTrace();
53 | return null;
54 | }
55 | }
56 |
57 | private UUID fromString(String uuid) {
58 | try {
59 | return UUID.fromString(uuid);
60 | } catch (IllegalArgumentException e) {
61 |
62 | StringBuilder newUUID = new StringBuilder();
63 |
64 | for (int i = 0; i < uuid.length(); i++) {
65 | newUUID.append(uuid.charAt(i));
66 |
67 | switch (i) {
68 | case 7:
69 | case 11:
70 | case 15:
71 | case 19:
72 | newUUID.append("-");
73 | break;
74 | }
75 | }
76 |
77 | return UUID.fromString(newUUID.toString());
78 | }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/src/main/java/tv/banko/twitchwhitelist/whitelist/Whitelist.java:
--------------------------------------------------------------------------------
1 | package tv.banko.twitchwhitelist.whitelist;
2 |
3 | import org.bukkit.Bukkit;
4 | import org.bukkit.configuration.file.YamlConfiguration;
5 | import org.bukkit.entity.Player;
6 |
7 | import java.io.File;
8 | import java.io.IOException;
9 | import java.util.*;
10 |
11 | public class Whitelist {
12 |
13 | private final File file;
14 | private final YamlConfiguration config;
15 |
16 | private final Map map;
17 |
18 | public Whitelist() {
19 | File dir = new File("./plugins/TwitchWhitelist/");
20 |
21 | if(!dir.exists()) {
22 | dir.mkdirs();
23 | }
24 |
25 | file = new File(dir, "whitelist.yml");
26 |
27 | if(!file.exists()) {
28 | try {
29 | file.createNewFile();
30 | } catch (IOException e) {
31 | e.printStackTrace();
32 | }
33 | }
34 |
35 | config = YamlConfiguration.loadConfiguration(file);
36 |
37 | map = new HashMap<>();
38 |
39 | load();
40 | }
41 |
42 | public void addWhitelist(WhitelistUser user) {
43 | if(map.containsKey(user.getUUID())) {
44 | return;
45 | }
46 |
47 | map.put(user.getUUID(), user);
48 | }
49 |
50 | public void removeWhitelist(UUID uuid) {
51 | if(!map.containsKey(uuid)) {
52 | return;
53 | }
54 |
55 | Player player = Bukkit.getPlayer(uuid);
56 |
57 | if(player != null) {
58 | player.kickPlayer("§cYou are no longer whitelisted.");
59 | }
60 |
61 | map.remove(uuid);
62 | }
63 |
64 | public boolean isWhitelisted(UUID uuid) {
65 | return map.containsKey(uuid);
66 | }
67 |
68 | public List getUUIDs() {
69 | return new ArrayList<>(map.keySet());
70 | }
71 |
72 | public WhitelistUser getWhitelistUser(UUID uuid) {
73 | return map.getOrDefault(uuid, null);
74 | }
75 |
76 | public boolean hasTwitchWhitelist(String id) {
77 | for(WhitelistUser user : map.values()) {
78 | if(user.getID().equalsIgnoreCase(id)) {
79 | return true;
80 | }
81 | }
82 | return false;
83 | }
84 |
85 | public void load() {
86 | List uuids = new ArrayList<>();
87 |
88 | if(config.contains("uuids")) {
89 | uuids.addAll(config.getStringList("uuids"));
90 | }
91 |
92 | for(String uuidString : uuids) {
93 | try {
94 | String id = config.getString(uuidString + ".id");
95 | long timestamp = config.getLong(uuidString + ".timestamp");
96 | String twitch = config.getString(uuidString + ".twitch");
97 | WhitelistType type = WhitelistType.valueOf(config.getString(uuidString + ".type"));
98 | UUID uuid = UUID.fromString(uuidString);
99 |
100 | map.put(uuid, new WhitelistUser(uuid, twitch, id, timestamp, type));
101 | } catch (Exception ignored) { }
102 | }
103 | }
104 |
105 | public void save() {
106 | for(UUID uuid : map.keySet()) {
107 |
108 | WhitelistUser user = map.get(uuid);
109 |
110 | config.set(uuid + ".id", user.getID());
111 | config.set(uuid + ".timestamp", user.getTimestamp());
112 | config.set(uuid + ".twitch", user.getTwitchName());
113 | config.set(uuid + ".type", user.getType().name());
114 | }
115 |
116 | List uuids = new ArrayList<>();
117 |
118 | for(UUID uuid : map.keySet()) {
119 | uuids.add(uuid.toString());
120 | }
121 |
122 | config.set("uuids", uuids);
123 |
124 | try {
125 | config.save(file);
126 | } catch (IOException e) {
127 | e.printStackTrace();
128 | }
129 | }
130 |
131 | }
132 |
--------------------------------------------------------------------------------
/src/main/java/tv/banko/twitchwhitelist/whitelist/WhitelistType.java:
--------------------------------------------------------------------------------
1 | package tv.banko.twitchwhitelist.whitelist;
2 |
3 | public enum WhitelistType {
4 |
5 | CHANNEL_POINTS,
6 | COMMAND,
7 | INGAME;
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/java/tv/banko/twitchwhitelist/whitelist/WhitelistUser.java:
--------------------------------------------------------------------------------
1 | package tv.banko.twitchwhitelist.whitelist;
2 |
3 | import java.util.UUID;
4 |
5 | public class WhitelistUser {
6 |
7 | private final UUID uuid;
8 | private final String twitchName;
9 | private final String id;
10 | private final long timestamp;
11 | private final WhitelistType type;
12 |
13 | public WhitelistUser(UUID uuid, String twitchName, String id, long timestamp, WhitelistType type) {
14 | this.uuid = uuid;
15 | this.twitchName = twitchName;
16 | this.id = id;
17 | this.timestamp = timestamp;
18 | this.type = type;
19 | }
20 |
21 | public UUID getUUID() {
22 | return uuid;
23 | }
24 |
25 | public String getTwitchName() {
26 | return twitchName;
27 | }
28 |
29 | public String getID() {
30 | return id;
31 | }
32 |
33 | public long getTimestamp() {
34 | return timestamp;
35 | }
36 |
37 | public WhitelistType getType() {
38 | return type;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/resources/plugin.yml:
--------------------------------------------------------------------------------
1 | name: TwitchWhitelist
2 | version: ${project.version}
3 | main: tv.banko.twitchwhitelist.TwitchWhitelist
4 | authors: [ DerBanko ]
5 | description: Twitch Whitelist with command or channel points
6 | website: https://banko.tv
7 |
8 | commands:
9 | wl:
10 | description: Whitelist Command of TwitchWhitelist
11 | aliases: [whitelist]
12 | settings:
13 | description: Settings of TwitchWhitelist
--------------------------------------------------------------------------------