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

150
pom.xml
View File

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

View File

@ -13,11 +13,12 @@ import org.bukkit.scheduler.BukkitRunnable;
import com.fpghoti.bcraftbot.bot.ServerBot;
import com.fpghoti.bcraftbot.command.AddExempt;
import com.fpghoti.bcraftbot.command.BCWhitelist;
import com.fpghoti.bcraftbot.listener.PlayerListener;
import com.fpghoti.bcraftbot.sql.MySQLConnection;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.User;
public class Main extends JavaPlugin {
@ -65,6 +66,7 @@ public class Main extends JavaPlugin {
bot = new ServerBot(this);
bot.runBot();
getCommand("AddExempt").setExecutor(new AddExempt(this));
getCommand("BCWhitelist").setExecutor(new BCWhitelist(this));
}
public void onDisable() {
@ -183,6 +185,10 @@ public class Main extends JavaPlugin {
config.createSection("MainRecordKeeper");
config.set("MainRecordKeeper", true);
}
if (config.get("Whitelist") == null){
config.createSection("Whitelist");
config.set("Whitelist", true);
}
this.saveConfig();
}
@ -229,6 +235,22 @@ public class Main extends JavaPlugin {
}
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() {
return bot;

View File

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

View File

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

View File

@ -4,21 +4,23 @@ import java.util.HashMap;
import java.util.logging.Level;
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.util.CommandParser;
import com.fpghoti.bcraftbot.sql.MySQLConnection;
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.requests.GatewayIntent;
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 {
private Main plugin;
private JDA jda;
private static ShardManager shardm;
private CommandParser parser;
@ -34,7 +36,19 @@ public class ServerBot {
String token = plugin.getBotToken();
plugin.log(Level.INFO, "Connecting bot to Discord...");
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.setAutoReconnect(true);
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.ServerBot;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public class AddMeCommand implements Command{
@ -19,7 +19,6 @@ public class AddMeCommand implements Command{
@Override
public boolean called(String[] args, MessageReceivedEvent event) {
return true;
}

View File

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

View File

@ -32,7 +32,7 @@ public class AddExempt implements CommandExecutor{
}
}else {
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.");
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 net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.Role;
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.User;
public class PlayerListener implements Listener {
@ -28,19 +28,20 @@ public class PlayerListener implements Listener {
if(player.hasPermission("bcraftbot.bypasscheck")) {
return;
}
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;
}
if(plugin.checkRole() && !plugin.isExempt(player.getName())) {
boolean allow = false;
for(Guild guild : plugin.getBot().getJDA().getGuilds()) {
for(String rolename : plugin.getRequiredRole().split(",")) {
Role role = null;
for(Role r : guild.getRoles()) {
if(r.getName().equalsIgnoreCase(rolename)) {
@ -51,6 +52,12 @@ public class PlayerListener implements Listener {
User user = plugin.getDiscordUser(player);
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(mem.getRoles().contains(role)) {
allow = true;
@ -80,7 +87,7 @@ public class PlayerListener implements Listener {
Member mem = guild.getMember(user);
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
version: 1.1
version: 1.2
api-version: 1.13
name: BCraftBot
descripion: Discord-based whitelisting system for Minecraft
@ -7,4 +7,7 @@ author: Ghoti_Mayo
commands:
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.
usage: /addexempt <username>
usage: /addexempt <username>
BCWhitelist:
description: Toggles plugin whitelist.
usage: /bcwhitelist <on/off>