Skip to content
This repository has been archived by the owner on Oct 21, 2021. It is now read-only.

Commit

Permalink
Add whitelist and compress data.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucky-fish authored and lucky-fish committed Aug 18, 2020
1 parent fdf3d73 commit 6e99b42
Show file tree
Hide file tree
Showing 18 changed files with 489 additions and 50 deletions.
15 changes: 13 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cn.mcres.luckyfish</groupId>
<artifactId>AntiLeakAccount</artifactId>
<version>1.0.0-BETA</version>
<version>1.1.0-BETA</version>
<packaging>jar</packaging>

<name>AntiLeakAccount</name>
Expand Down Expand Up @@ -66,6 +66,12 @@
<id>bungeecord-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
<repository>
<id>luckyfish-repo</id>
<url>
https://raw.githubusercontent.com/Lucky-fish/mvn-repo/master/
</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -76,7 +82,7 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.nanohttpd</groupId> <!-- <groupId>com.nanohttpd</groupId> for 2.1.0 and earlier -->
<groupId>org.nanohttpd</groupId>
<artifactId>nanohttpd</artifactId>
<version>2.2.0</version>
</dependency>
Expand Down Expand Up @@ -104,5 +110,10 @@
<type>javadoc</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>cn.mcres.luckyfish</groupId>
<artifactId>PluginCommons</artifactId>
<version>1.2-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package cn.mcres.luckyfish.antileakaccount;

import cn.mcres.luckyfish.antileakaccount.api.ApiServer;
import cn.mcres.luckyfish.antileakaccount.command.WhiteListAddCommand;
import cn.mcres.luckyfish.antileakaccount.command.WhiteListListCommand;
import cn.mcres.luckyfish.antileakaccount.command.WhiteListRemoveCommand;
import cn.mcres.luckyfish.antileakaccount.http.HttpServer;
import cn.mcres.luckyfish.antileakaccount.listener.PlayerListener;
import cn.mcres.luckyfish.antileakaccount.mojang.MojangApiHelper;
import cn.mcres.luckyfish.antileakaccount.task.SpamTask;
import cn.mcres.luckyfish.antileakaccount.verify.VerifyManager;
import cn.mcres.luckyfish.antileakaccount.whitelist.WhiteListStorage;
import cn.mcres.luckyfish.plugincommons.commands.CommonCommand;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.IOException;
Expand All @@ -15,6 +20,7 @@ public final class AntiLeakAccount extends JavaPlugin {

private ConfigHolder configHolder;
private VerifyManager verifyManager;
private WhiteListStorage whiteListStorage = null;
@Override
public void onEnable() {
// Plugin startup logic
Expand All @@ -30,6 +36,10 @@ public void onEnable() {
MojangApiHelper.setUserCache(getDataFolder());
verifyManager = new VerifyManager();

if (!configHolder.bungeeMode) {
whiteListStorage = new WhiteListStorage(getDataFolder());
}

if (configHolder.httpdEnabled) {
HttpServer hs = new HttpServer();
try {
Expand All @@ -41,6 +51,16 @@ public void onEnable() {
if (configHolder.apiEnabled) {
new ApiServer();
}

if (!configHolder.bungeeMode) {
CommonCommand whiteListCommand = new CommonCommand();
whiteListCommand.registerCommand(new WhiteListAddCommand());
whiteListCommand.registerCommand(new WhiteListRemoveCommand());
whiteListCommand.registerCommand(new WhiteListListCommand());
getCommand("alawhitelist").setExecutor(whiteListCommand);
getCommand("alawhitelist").setTabCompleter(whiteListCommand);
}

getServer().getPluginManager().registerEvents(new PlayerListener(), this);
getServer().getScheduler().runTaskTimer(this, new SpamTask(), configHolder.spamInterval, configHolder.spamInterval);
}
Expand All @@ -61,4 +81,8 @@ public ConfigHolder getConfigHolder() {
public VerifyManager getVerifyManager() {
return verifyManager;
}

public WhiteListStorage getWhiteListStorage() {
return whiteListStorage;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package cn.mcres.luckyfish.antileakaccount.bungee;

import cn.mcres.luckyfish.antileakaccount.bungee.command.WhiteListCommand;
import cn.mcres.luckyfish.antileakaccount.bungee.listener.PlayerListener;
import cn.mcres.luckyfish.antileakaccount.bungee.listener.PluginListener;
import cn.mcres.luckyfish.antileakaccount.bungee.storage.PlayerStorage;
import cn.mcres.luckyfish.antileakaccount.mojang.MojangApiHelper;
import cn.mcres.luckyfish.antileakaccount.whitelist.WhiteListStorage;
import net.md_5.bungee.api.plugin.Plugin;

public class AntiLeakAccount extends Plugin {
private static AntiLeakAccount instance;
private PlayerStorage ps;
private WhiteListStorage whiteListStorage;
@Override
public void onEnable() {
instance = this;
Expand All @@ -18,11 +21,14 @@ public void onEnable() {
}

ps = new PlayerStorage();
whiteListStorage = new WhiteListStorage(getDataFolder());

getProxy().registerChannel("ala:message");
MojangApiHelper.setUserCache(getDataFolder());
getProxy().getPluginManager().registerListener(this, new PlayerListener());
getProxy().getPluginManager().registerListener(this, new PluginListener());
getProxy().getPluginManager().registerCommand(this, new WhiteListCommand());

}

public PlayerStorage getPlayerStorage() {
Expand All @@ -32,4 +38,8 @@ public PlayerStorage getPlayerStorage() {
public static AntiLeakAccount getInstance() {
return instance;
}

public WhiteListStorage getWhiteListStorage() {
return whiteListStorage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cn.mcres.luckyfish.antileakaccount.bungee.command;

import cn.mcres.luckyfish.antileakaccount.bungee.AntiLeakAccount;
import cn.mcres.luckyfish.antileakaccount.mojang.MojangApiHelper;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.plugin.Command;

import java.util.List;
import java.util.UUID;

public class WhiteListCommand extends Command {
public WhiteListCommand() {
super("alawhitelist", "antileakaccount.whitelist", "awhitelist", "aw");
}

@Override
public void execute(CommandSender sender, String[] args) {
switch (args[0]) {
case "add": {
if (args.length != 2) {
sender.sendMessage(new TextComponent("用法:/alawhitelist add <玩家名>"));
break;
}

if (AntiLeakAccount.getInstance().getWhiteListStorage().addWhitelistPlayer(args[1])) {
sender.sendMessage(new TextComponent(ChatColor.GREEN + "已将" + args[1] + "加入白名单,该玩家不再需要接受黑卡验证"));
} else {
sender.sendMessage(new TextComponent(ChatColor.RED + "你说的" + args[1] + ",她长什么样?"));
}

break;
}
case "remove": {
if (args.length != 2) {
sender.sendMessage(new TextComponent("用法:/alawhitelist remove <玩家名>"));
break;
}

if (AntiLeakAccount.getInstance().getWhiteListStorage().removeWhitelistPlayer(args[1])) {
sender.sendMessage(new TextComponent(ChatColor.GREEN + "已将" + args[1] + "移除白名单,该玩家再次需要接受黑卡验证"));
} else {
sender.sendMessage(new TextComponent(ChatColor.RED + "你说的" + args[1] + ",她长什么样?"));
}
break;
}
case "list": {
List<UUID> whiteList = AntiLeakAccount.getInstance().getWhiteListStorage().getWhiteList();;
for (UUID uid : whiteList) {
sender.sendMessage(new TextComponent(ChatColor.YELLOW + "玩家 " + ChatColor.GREEN + MojangApiHelper.getMinecraftNameByUuid(uid) + " - " + uid));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.mcres.luckyfish.antileakaccount.bungee.AntiLeakAccount;
import cn.mcres.luckyfish.antileakaccount.mojang.MojangApiHelper;
import cn.mcres.luckyfish.antileakaccount.util.UuidHelper;
import net.md_5.bungee.api.connection.ProxiedPlayer;

import java.io.*;
Expand All @@ -22,20 +23,11 @@ public PlayerStorage() {
}
}

try (DataInputStream dis = new DataInputStream(new FileInputStream(storageFile))) {
int amount = dis.readInt();
for (int i = 0; i < amount; i ++) {
verifiedUuids.add(new UUID(dis.readLong(), dis.readLong()));
}
} catch (EOFException e) {

} catch (Exception e) {
e.printStackTrace();
}
UuidHelper.readUuidListFromFile(storageFile, verifiedUuids);
}

public boolean isPlayerVerified(ProxiedPlayer player) {
return verifiedUuids.contains(player.getUniqueId()) || !player.getName().equals(MojangApiHelper.getMinecraftNameByUuid(player.getUniqueId()));
return verifiedUuids.contains(player.getUniqueId()) || !player.getName().equals(MojangApiHelper.getMinecraftNameByUuid(player.getUniqueId())) || AntiLeakAccount.getInstance().getWhiteListStorage().isPlayerWhiteListed(player.getUniqueId());
}

public boolean isUuidVerified(UUID uuid) {
Expand All @@ -48,14 +40,6 @@ public void addVerifiedUuid(UUID uuid) {
}

private void save() {
try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File(AntiLeakAccount.getInstance().getDataFolder(), "verifiedPlayers.dat")))) {
dos.writeInt(verifiedUuids.size());
for (UUID uid : verifiedUuids) {
dos.writeLong(uid.getMostSignificantBits());
dos.writeLong(uid.getLeastSignificantBits());
}
} catch (Exception e) {
e.printStackTrace();
}
UuidHelper.writeUuidListToFile(new File(AntiLeakAccount.getInstance().getDataFolder(), "verifiedPlayers.dat"), verifiedUuids);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cn.mcres.luckyfish.antileakaccount.command;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;

public class CommandHelper {
static List<String> listPlayers(String[] args) {
if (args.length > 1) {
return new ArrayList<>();
}

List<String> playerNames = new ArrayList<>();

for (Player p : Bukkit.getOnlinePlayers()) {
playerNames.add(p.getName());
}

if (args.length == 1) {
playerNames.removeIf((name) -> !name.startsWith(args[0]));
}

return playerNames;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cn.mcres.luckyfish.antileakaccount.command;

import cn.mcres.luckyfish.antileakaccount.AntiLeakAccount;
import cn.mcres.luckyfish.plugincommons.commands.SubCommandBase;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

import java.util.List;

public class WhiteListAddCommand extends SubCommandBase {
@Override
public String getName() {
return "add";
}

@Override
public String getDescription() {
return "添加白名单玩家";
}

@Override
public String getUsage() {
return "add <玩家名>";
}

@Override
public String getPermissionRequired() {
return "antileakaccount.whitelist.add";
}

@Override
public boolean execute(CommandSender sender, Command command, String[] args) {
if (args.length != 1) {
return false;
}

if (AntiLeakAccount.getInstance().getWhiteListStorage().addWhitelistPlayer(args[0])) {
sender.sendMessage(ChatColor.GREEN + "已将" + args[0] + "加入白名单,该玩家不再需要接受黑卡验证");
} else {
sender.sendMessage(ChatColor.RED + "你说的" + args[0] + ",她长什么样?");
}

return true;
}

@Override
public List<String> tabComplete(CommandSender sender, String[] args) {
return CommandHelper.listPlayers(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cn.mcres.luckyfish.antileakaccount.command;

import cn.mcres.luckyfish.antileakaccount.AntiLeakAccount;
import cn.mcres.luckyfish.antileakaccount.mojang.MojangApiHelper;
import cn.mcres.luckyfish.plugincommons.commands.SubCommandBase;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

import java.util.List;
import java.util.UUID;

public class WhiteListListCommand extends SubCommandBase {
@Override
public String getName() {
return "list";
}

@Override
public String getDescription() {
return "列出所有白名单玩家";
}

@Override
public String getUsage() {
return "list";
}

@Override
public boolean execute(CommandSender sender, Command command, String[] args) {
if (args.length != 0) {
return false;
}

List<UUID> whiteList = AntiLeakAccount.getInstance().getWhiteListStorage().getWhiteList();;
for (UUID uid : whiteList) {
sender.sendMessage(ChatColor.YELLOW + "玩家 " + ChatColor.GREEN + MojangApiHelper.getMinecraftNameByUuid(uid) + " - " + uid);
}

return true;
}
}
Loading

0 comments on commit 6e99b42

Please sign in to comment.