Make whitelist changes

This commit is contained in:
Ghoti 2022-04-29 21:01:11 -05:00
parent 87eac4114f
commit 22b43a93a5
12 changed files with 238 additions and 110 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.fpghoti</groupId> <groupId>com.fpghoti</groupId>
<artifactId>BCraftBot</artifactId> <artifactId>BCraftBot</artifactId>
<version>1.1</version> <version>1.2</version>
<build> <build>
<resources> <resources>
<resource> <resource>
@ -43,22 +43,22 @@
<name>jcenter-bintray</name> <name>jcenter-bintray</name>
<url>http://jcenter.bintray.com</url> <url>http://jcenter.bintray.com</url>
</repository> </repository>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.bukkit</groupId> <groupId>org.spigotmc</groupId>
<artifactId>bukkit</artifactId> <artifactId>spigot-api</artifactId>
<version>1.13.2-R0.1-SNAPSHOT</version> <version>1.18.2-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
<exclusions> <exclusions>
<exclusion> <exclusion>
<artifactId>commons-lang</artifactId> <artifactId>commons-lang</artifactId>
<groupId>commons-lang</groupId> <groupId>commons-lang</groupId>
</exclusion> </exclusion>
<exclusion>
<artifactId>json-simple</artifactId>
<groupId>com.googlecode.json-simple</groupId>
</exclusion>
<exclusion> <exclusion>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
@ -67,6 +67,10 @@
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
</exclusion> </exclusion>
<exclusion>
<artifactId>bungeecord-chat</artifactId>
<groupId>net.md-5</groupId>
</exclusion>
<exclusion> <exclusion>
<artifactId>snakeyaml</artifactId> <artifactId>snakeyaml</artifactId>
<groupId>org.yaml</groupId> <groupId>org.yaml</groupId>

150
pom.xml
View File

@ -1,74 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0"
<modelVersion>4.0.0</modelVersion> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<groupId>com.fpghoti</groupId> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>BCraftBot</artifactId> <modelVersion>4.0.0</modelVersion>
<version>1.1</version> <groupId>com.fpghoti</groupId>
<repositories> <artifactId>BCraftBot</artifactId>
<repository> <version>1.2</version>
<id>jcenter</id> <repositories>
<name>jcenter-bintray</name> <repository>
<url>http://jcenter.bintray.com</url> <id>jcenter</id>
</repository> <name>jcenter-bintray</name>
</repositories> <url>http://jcenter.bintray.com</url>
<build> </repository>
<resources> <repository>
<resource> <id>spigot-repo</id>
<directory>src/main/resources</directory> <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
<filtering>true</filtering> </repository>
</resource> </repositories>
</resources> <build>
<plugins> <resources>
<plugin> <resource>
<groupId>org.apache.maven.plugins</groupId> <directory>src/main/resources</directory>
<artifactId>maven-compiler-plugin</artifactId> <filtering>true</filtering>
<version>3.5.1</version> </resource>
<configuration> </resources>
<source>1.8</source> <plugins>
<target>1.8</target> <plugin>
</configuration> <groupId>org.apache.maven.plugins</groupId>
</plugin> <artifactId>maven-compiler-plugin</artifactId>
<plugin> <version>3.5.1</version>
<groupId>org.apache.maven.plugins</groupId> <configuration>
<artifactId>maven-shade-plugin</artifactId> <source>1.8</source>
<version>3.1.0</version> <target>1.8</target>
<executions> </configuration>
<execution> </plugin>
<phase>package</phase> <plugin>
<goals> <groupId>org.apache.maven.plugins</groupId>
<goal>shade</goal> <artifactId>maven-shade-plugin</artifactId>
</goals> <version>3.1.0</version>
<configuration> <executions>
<minimizeJar>true</minimizeJar> <execution>
</configuration> <phase>package</phase>
</execution> <goals>
</executions> <goal>shade</goal>
</plugin> </goals>
</plugins> <configuration>
</build> <minimizeJar>true</minimizeJar>
<dependencies> </configuration>
<dependency> </execution>
<groupId>net.dv8tion</groupId> </executions>
<artifactId>JDA</artifactId> </plugin>
<version>3.5.0_327</version> </plugins>
</dependency> </build>
<dependency> <dependencies>
<groupId>org.bukkit</groupId> <dependency>
<artifactId>bukkit</artifactId> <groupId>net.dv8tion</groupId>
<version>1.13.2-R0.1-SNAPSHOT</version> <artifactId>JDA</artifactId>
<scope>provided</scope> <version>5.0.0-alpha.9</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.spigotmc</groupId>
<artifactId>commons-collections4</artifactId> <artifactId>spigot-api</artifactId>
<version>4.1</version> <version>1.18.2-R0.1-SNAPSHOT</version>
<type>pom</type> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-collections4</artifactId>
<version>3.6</version> <version>4.1</version>
<type>pom</type> <type>pom</type>
</dependency> </dependency>
</dependencies> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
<type>pom</type>
</dependency>
</dependencies>
</project> </project>

View File

@ -13,11 +13,12 @@ import org.bukkit.scheduler.BukkitRunnable;
import com.fpghoti.bcraftbot.bot.ServerBot; import com.fpghoti.bcraftbot.bot.ServerBot;
import com.fpghoti.bcraftbot.command.AddExempt; import com.fpghoti.bcraftbot.command.AddExempt;
import com.fpghoti.bcraftbot.command.BCWhitelist;
import com.fpghoti.bcraftbot.listener.PlayerListener; import com.fpghoti.bcraftbot.listener.PlayerListener;
import com.fpghoti.bcraftbot.sql.MySQLConnection; import com.fpghoti.bcraftbot.sql.MySQLConnection;
import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.core.entities.User; import net.dv8tion.jda.api.entities.User;
public class Main extends JavaPlugin { public class Main extends JavaPlugin {
@ -65,6 +66,7 @@ public class Main extends JavaPlugin {
bot = new ServerBot(this); bot = new ServerBot(this);
bot.runBot(); bot.runBot();
getCommand("AddExempt").setExecutor(new AddExempt(this)); getCommand("AddExempt").setExecutor(new AddExempt(this));
getCommand("BCWhitelist").setExecutor(new BCWhitelist(this));
} }
public void onDisable() { public void onDisable() {
@ -183,6 +185,10 @@ public class Main extends JavaPlugin {
config.createSection("MainRecordKeeper"); config.createSection("MainRecordKeeper");
config.set("MainRecordKeeper", true); config.set("MainRecordKeeper", true);
} }
if (config.get("Whitelist") == null){
config.createSection("Whitelist");
config.set("Whitelist", true);
}
this.saveConfig(); this.saveConfig();
} }
@ -230,6 +236,22 @@ public class Main extends JavaPlugin {
setExemptList(updated); setExemptList(updated);
} }
public void setBCWhitelist(Boolean b) {
if (config.get("Whitelist") != null){
config.set("Whitelist", b);
this.saveConfig();
}else {
log.severe("BCraftBot failed to update the whitelist in the config!");
}
}
public boolean getBCWhitelist() {
if (config.get("Whitelist") != null){
return config.getBoolean("Whitelist");
}
return false;
}
public ServerBot getBot() { public ServerBot getBot() {
return bot; return bot;
} }

View File

@ -2,12 +2,10 @@ package com.fpghoti.bcraftbot.bot;
import java.util.logging.Level; import java.util.logging.Level;
import com.fpghoti.bcraftbot.bot.ServerBot; import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.events.ReadyEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
public class BotListener extends ListenerAdapter{ public class BotListener extends ListenerAdapter{

View File

@ -1,6 +1,6 @@
package com.fpghoti.bcraftbot.bot; package com.fpghoti.bcraftbot.bot;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public interface Command { public interface Command {

View File

@ -4,21 +4,23 @@ import java.util.HashMap;
import java.util.logging.Level; import java.util.logging.Level;
import com.fpghoti.bcraftbot.Main; import com.fpghoti.bcraftbot.Main;
import com.fpghoti.bcraftbot.bot.BotListener;
import com.fpghoti.bcraftbot.bot.Command;
import com.fpghoti.bcraftbot.bot.command.AddMeCommand; import com.fpghoti.bcraftbot.bot.command.AddMeCommand;
import com.fpghoti.bcraftbot.bot.util.CommandParser; import com.fpghoti.bcraftbot.bot.util.CommandParser;
import com.fpghoti.bcraftbot.sql.MySQLConnection; import com.fpghoti.bcraftbot.sql.MySQLConnection;
import net.dv8tion.jda.core.AccountType; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.core.JDA; import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.core.JDABuilder; import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder;
import net.dv8tion.jda.api.sharding.ShardManager;
import net.dv8tion.jda.api.utils.ChunkingFilter;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
public class ServerBot { public class ServerBot {
private Main plugin; private Main plugin;
private JDA jda; private JDA jda;
private static ShardManager shardm;
private CommandParser parser; private CommandParser parser;
@ -34,7 +36,19 @@ public class ServerBot {
String token = plugin.getBotToken(); String token = plugin.getBotToken();
plugin.log(Level.INFO, "Connecting bot to Discord..."); plugin.log(Level.INFO, "Connecting bot to Discord...");
try{ try{
jda = new JDABuilder(AccountType.BOT).setToken(token).buildBlocking(); shardm = DefaultShardManagerBuilder.createDefault(token)
.setChunkingFilter(ChunkingFilter.ALL)
.setMemberCachePolicy(MemberCachePolicy.ALL)
.enableIntents(GatewayIntent.getIntents(GatewayIntent.DEFAULT))
.enableIntents(GatewayIntent.GUILD_MEMBERS)
.enableIntents(GatewayIntent.GUILD_MESSAGES)
.build();
jda = shardm.getShardById(0);
try {
jda.awaitReady();
} catch (InterruptedException e) {
e.printStackTrace();
}
jda.addEventListener(new BotListener(this)); jda.addEventListener(new BotListener(this));
jda.setAutoReconnect(true); jda.setAutoReconnect(true);
String link = "https://discordapp.com/oauth2/authorize?&client_id=" + jda.getSelfUser().getId(); String link = "https://discordapp.com/oauth2/authorize?&client_id=" + jda.getSelfUser().getId();

View File

@ -5,7 +5,7 @@ import java.util.logging.Level;
import com.fpghoti.bcraftbot.bot.Command; import com.fpghoti.bcraftbot.bot.Command;
import com.fpghoti.bcraftbot.bot.ServerBot; import com.fpghoti.bcraftbot.bot.ServerBot;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public class AddMeCommand implements Command{ public class AddMeCommand implements Command{
@ -19,7 +19,6 @@ public class AddMeCommand implements Command{
@Override @Override
public boolean called(String[] args, MessageReceivedEvent event) { public boolean called(String[] args, MessageReceivedEvent event) {
return true; return true;
} }

View File

@ -2,7 +2,7 @@ package com.fpghoti.bcraftbot.bot.util;
import java.util.ArrayList; import java.util.ArrayList;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public class CommandParser { public class CommandParser {
public CommandContainer parse(String rw, MessageReceivedEvent e){ public CommandContainer parse(String rw, MessageReceivedEvent e){

View File

@ -32,7 +32,7 @@ public class AddExempt implements CommandExecutor{
} }
}else { }else {
if(args.length != 1) { if(args.length != 1) {
plugin.log(Level.INFO, "Usage: addexempt <username>"); plugin.log(Level.INFO, "Usage: /addexempt <username>");
plugin.log(Level.INFO, "There was an issue with your syntax."); plugin.log(Level.INFO, "There was an issue with your syntax.");
return true; return true;
} }

View File

@ -0,0 +1,75 @@
package com.fpghoti.bcraftbot.command;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.fpghoti.bcraftbot.Main;
public class BCWhitelist implements CommandExecutor{
private Main plugin;
public BCWhitelist(Main plugin) {
this.plugin = plugin;
}
public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
if(label.equalsIgnoreCase("bcwhitelist")){
if(sender instanceof Player) {
if(!((Player)sender).hasPermission("bcraftbot.whitelist")) {
((Player)sender).sendMessage(ChatColor.RED + "[BCraftBot] You do not have permission to do this!");
return true;
}
if(args.length != 1) {
((Player)sender).sendMessage(ChatColor.GOLD + "[BCraftBot] Usage: /bcwhitelist <on/off>");
((Player)sender).sendMessage(ChatColor.RED + "[BCraftBot] There was an issue with your syntax.");
return true;
}
}else {
if(args.length != 1) {
plugin.log(Level.INFO, "Usage: /bcwhitelist <on/off>");
plugin.log(Level.INFO, "There was an issue with your syntax.");
return true;
}
}
String arg = args[0].toLowerCase();
int status = 0;
if(arg.equals("on") || arg.equals("true") || arg.equals("enable")) {
status = 1;
plugin.setBCWhitelist(true);
}else if(arg.equals("off") || arg.equals("false") || arg.equals("disable")) {
status = 2;
plugin.setBCWhitelist(false);
}
if(sender instanceof Player) {
if(status == 1) {
((Player)sender).sendMessage(ChatColor.GREEN + "[BCraftBot] Whitelist enabled.");
}else if(status == 2) {
((Player)sender).sendMessage(ChatColor.GREEN + "[BCraftBot] Whitelist disabled.");
}else {
((Player)sender).sendMessage(ChatColor.GOLD + "[BCraftBot] Usage: /bcwhitelist <on/off>");
((Player)sender).sendMessage(ChatColor.RED + "[BCraftBot] There was an issue with your syntax.");
}
}else {
if(status == 1) {
plugin.log(Level.INFO, "[BCraftBot] Whitelist enabled.");
}else if(status == 2) {
plugin.log(Level.INFO, "[BCraftBot] Whitelist disabled.");
}else {
plugin.log(Level.INFO, "[BCraftBot] Usage: /bcwhitelist <on/off>");
plugin.log(Level.INFO, "[BCraftBot] There was an issue with your syntax.");
}
}
}
return true;
}
}

View File

@ -8,10 +8,10 @@ import org.bukkit.event.player.PlayerLoginEvent.Result;
import com.fpghoti.bcraftbot.Main; import com.fpghoti.bcraftbot.Main;
import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.core.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.core.entities.Role; import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.core.entities.User; import net.dv8tion.jda.api.entities.User;
public class PlayerListener implements Listener { public class PlayerListener implements Listener {
@ -30,7 +30,9 @@ public class PlayerListener implements Listener {
} }
if(!plugin.isMember(player) && !plugin.isExempt(player.getName())) { if(!plugin.isMember(player) && !plugin.isExempt(player.getName())) {
event.disallow(Result.KICK_OTHER, plugin.getKickMessage()); if(!player.isWhitelisted() && plugin.getBCWhitelist()) {
event.disallow(Result.KICK_OTHER, plugin.getKickMessage());
}
return; return;
} }
@ -40,7 +42,6 @@ public class PlayerListener implements Listener {
for(Guild guild : plugin.getBot().getJDA().getGuilds()) { for(Guild guild : plugin.getBot().getJDA().getGuilds()) {
for(String rolename : plugin.getRequiredRole().split(",")) { for(String rolename : plugin.getRequiredRole().split(",")) {
Role role = null; Role role = null;
for(Role r : guild.getRoles()) { for(Role r : guild.getRoles()) {
if(r.getName().equalsIgnoreCase(rolename)) { if(r.getName().equalsIgnoreCase(rolename)) {
@ -51,6 +52,12 @@ public class PlayerListener implements Listener {
User user = plugin.getDiscordUser(player); User user = plugin.getDiscordUser(player);
Member mem = guild.getMember(user); Member mem = guild.getMember(user);
if(!plugin.getBCWhitelist()) {
allow = true;
}
if(player.isWhitelisted()) {
allow = true;
}
if(user != null && user.getMutualGuilds().contains(guild) && role != null) { if(user != null && user.getMutualGuilds().contains(guild) && role != null) {
if(mem.getRoles().contains(role)) { if(mem.getRoles().contains(role)) {
allow = true; allow = true;
@ -80,7 +87,7 @@ public class PlayerListener implements Listener {
Member mem = guild.getMember(user); Member mem = guild.getMember(user);
if(user != null && user.getMutualGuilds().contains(guild) && role != null) { if(user != null && user.getMutualGuilds().contains(guild) && role != null) {
guild.getController().addRolesToMember(mem, role).queue(); guild.addRoleToMember(mem.getUser().getId(), role).queue();
} }
} }

View File

@ -1,5 +1,5 @@
main: com.fpghoti.bcraftbot.Main main: com.fpghoti.bcraftbot.Main
version: 1.1 version: 1.2
api-version: 1.13 api-version: 1.13
name: BCraftBot name: BCraftBot
descripion: Discord-based whitelisting system for Minecraft descripion: Discord-based whitelisting system for Minecraft
@ -8,3 +8,6 @@ commands:
AddExempt: AddExempt:
description: Add a player to the exempt list. Exempted users are permanently allowed to join, as long as they remain in the exempted list. description: Add a player to the exempt list. Exempted users are permanently allowed to join, as long as they remain in the exempted list.
usage: /addexempt <username> usage: /addexempt <username>
BCWhitelist:
description: Toggles plugin whitelist.
usage: /bcwhitelist <on/off>