Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
fix: なぜかログイン時のMCBansチェックがコミットされてなかった (#904)
Browse files Browse the repository at this point in the history
  • Loading branch information
book000 authored Aug 9, 2022
1 parent 6f74ab4 commit 5943e0d
Showing 1 changed file with 59 additions and 158 deletions.
217 changes: 59 additions & 158 deletions src/main/java/com/jaoafa/mymaid4/event/Event_MCBansLoginCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,21 @@

import com.jaoafa.mymaid4.Main;
import com.jaoafa.mymaid4.lib.EventPremise;
import com.jaoafa.mymaid4.lib.MyMaidData;
import com.jaoafa.mymaid4.lib.MCBansCache;
import com.jaoafa.mymaid4.lib.MyMaidLibrary;
import com.jaoafa.mymaid4.lib.MySQLDBManager;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent.Result;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.json.JSONObject;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.UUID;

public class Event_MCBansLoginCheck extends MyMaidLibrary implements Listener, EventPremise {
Expand All @@ -51,170 +40,82 @@ public String description() {
public void OnLoginCheck(AsyncPlayerPreLoginEvent event) {
// MCBansが落ちている場合を考慮してjaoafaデータベースからチェック

if (Main.getMyMaidConfig().isDevelopmentServer()) {
Main.getMyMaidLogger().warning("開発サーバのため、MCBansチェックは動作しません。");
return;
}

// Reputationチェック
String name = event.getName();
UUID uuid = event.getUniqueId();
OfflinePlayer player = Bukkit.getOfflinePlayer(uuid);

MySQLDBManager MySQLDBManager = MyMaidData.getMainMySQLDBManager();
if (MySQLDBManager == null) {
MCBansCache mcbans;
try {
mcbans = MCBansCache.get(player);
} catch (IOException e) {
Main.getMyMaidLogger().warning("MCBansキャッシュサーバへの接続に失敗しました: %s".formatted(e.getMessage()));
return;
}

try {
Connection conn = MySQLDBManager.getConnection();
try (PreparedStatement statement = conn.prepareStatement(
"SELECT * FROM mcbans WHERE uuid = ?")) {
statement.setString(1, uuid.toString());
try (ResultSet res = statement.executeQuery()) {
if (res.next()) {
float reputation = res.getFloat("reputation");
if (reputation < 3) {
// 3未満は規制
Component component = Component.text().append(
Component.text("----- MCBans Checker -----", NamedTextColor.RED),
Component.newline(),
Component.text("Access denied.", NamedTextColor.WHITE),
Component.newline(),
Component.text("Your reputation is below this server's threshold.", NamedTextColor.WHITE)
).build();
event.disallow(Result.KICK_BANNED, component);
return;
}
if (reputation != 10) {
sendAMR(Component.text().append(
Component.text("[MCBansChecker]", NamedTextColor.RED),
Component.space(),
Component.text(name, NamedTextColor.GREEN),
Component.space(),
Component.text("reputation: ", NamedTextColor.GREEN),
Component.space(),
Component.text(reputation, NamedTextColor.GREEN)
).build());
}
}
}
}
} catch (SQLException e) {
MyMaidLibrary.reportError(getClass(), e);
if (mcbans == null) {
return;
}

// jaoでBan済みかどうか
try {
Connection conn = MySQLDBManager.getConnection();
try (PreparedStatement statement = conn.prepareStatement(
"SELECT * FROM mcbans_jaoafa WHERE uuid = ?")) {
statement.setString(1, uuid.toString());
try (ResultSet res = statement.executeQuery()) {
if (res.next()) {
int banid = res.getInt("banid");
String type = res.getString("type");
String reason = res.getString("reason");
Component component = Component.text().append(
Component.text("----- MCBans Checker -----", NamedTextColor.RED),
Component.newline(),
Component.text("Access denied.", NamedTextColor.WHITE),
Component.newline(),
Component.text("Reason: " + reason, NamedTextColor.WHITE),
Component.newline(),
Component.text("Ban type: " + type, NamedTextColor.WHITE),
Component.newline(),
Component.text("https://mcbans.com/ban/" + banid, NamedTextColor.WHITE)
.clickEvent(ClickEvent.openUrl("https://mcbans.com/ban/" + banid))
).build();
event.disallow(Result.KICK_BANNED, component);
}
}
}
} catch (SQLException e) {
MyMaidLibrary.reportError(getClass(), e);
if (mcbans.getReputation() < 3) {
// 3未満は規制
Component component = Component.text().append(
Component.text("----- MCBans Checker -----", NamedTextColor.RED),
Component.newline(),
Component.text("Access denied.", NamedTextColor.WHITE),
Component.newline(),
Component.text("Your reputation is below this server's threshold.", NamedTextColor.WHITE)
).build();
event.disallow(Result.KICK_BANNED, component);
return;
}
if (mcbans.getReputation() != 10) {
sendAMR(Component.text().append(
Component.text("[MCBansChecker]", NamedTextColor.RED),
Component.space(),
Component.text(name, NamedTextColor.GREEN),
Component.space(),
Component.text("reputation: ", NamedTextColor.GREEN),
Component.space(),
Component.text(mcbans.getReputation(), NamedTextColor.GREEN)
).build());
}
}

@EventHandler
public void OnLoginAfterCheck(PlayerJoinEvent event) {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();

if (Main.getMyMaidConfig().isDevelopmentServer()) {
Main.getMyMaidLogger().warning("開発サーバのため、MCBansチェックは動作しません。");
return;
}

new BukkitRunnable() {
public void run() {
MCBansCache mcbans;
try {
String url = String.format("https://api.jaoafa.com/users/mcbans/%s", uuid);

Main.getMyMaidLogger().info(MessageFormat.format("OnLoginAfterCheck: APIサーバへの接続を開始: {0} -> {1}", player.getName(), url));
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).get().build();
JSONObject json;
try (Response response = client.newCall(request).execute()) {
if (response.code() != 200) {
Main.getMyMaidLogger().info("OnLoginAfterCheck: APIサーバへの接続に失敗: %s -> %d%n".formatted(url, response.code()));
response.close();
return;
}
try (ResponseBody body = response.body()) {
if (body == null) {
Main.getMyMaidLogger().info(MessageFormat.format("OnLoginAfterCheck: APIサーバへの接続に失敗: {0} -> response.body() is null.", url));
response.close();
return;
}
mcbans = MCBansCache.get(player);
} catch (IOException e) {
Main.getMyMaidLogger().warning("MCBansキャッシュサーバへの接続に失敗しました: %s".formatted(e.getMessage()));
return;
}
if (mcbans == null) {
return;
}

json = new JSONObject(body.string());
sendAMR(Component.text().append(
Component.text("[MCBans]"),
Component.space(),
Component.text(String.format("%s -> %s (G:%d | L:%d)", player.getName(), mcbans.getReputation(), mcbans.getGlobalCount(), mcbans.getLocalCount()), NamedTextColor.GRAY)
).build());
if (mcbans.getReputation() < 3) {
// 3未満はキック
Component component = Component.text().append(
Component.text("----- MCBans Checker -----", NamedTextColor.RED),
Component.newline(),
Component.text("Access denied.", NamedTextColor.WHITE),
Component.newline(),
Component.text("Your reputation is below this server's threshold.", NamedTextColor.WHITE)
).build();
new BukkitRunnable() {
public void run() {
player.kick(component);
}
}

if (!json.has("status")) {
Main.getMyMaidLogger().info("OnLoginAfterCheck: レスポンスの解析に失敗: status not found.");
return;
}
if (!json.getBoolean("status")) {
Main.getMyMaidLogger().info("OnLoginAfterCheck: レスポンスの解析に失敗: status not boolean.");
return;
}
if (!json.has("data")) {
Main.getMyMaidLogger().info("OnLoginAfterCheck: レスポンスの解析に失敗: data not found.");
return;
}

JSONObject data = json.getJSONObject("data");
double reputation = data.getDouble("reputation");
int globalCount = data.getInt("global");
int localCount = data.getInt("local");

sendAMR(Component.text().append(
Component.text("[MCBans]"),
Component.space(),
Component.text(String.format("%s -> %s (G:%d | L:%d)", player.getName(), reputation, globalCount, localCount), NamedTextColor.GRAY)
).build());
if (reputation < 3) {
// 3未満はキック
Component component = Component.text().append(
Component.text("----- MCBans Checker -----", NamedTextColor.RED),
Component.newline(),
Component.text("Access denied.", NamedTextColor.WHITE),
Component.newline(),
Component.text("Your reputation is below this server's threshold.", NamedTextColor.WHITE)
).build();
new BukkitRunnable() {
public void run() {
player.kick(component);
}
}.runTask(Main.getJavaPlugin());
}
} catch (SocketTimeoutException e) {
Main.getMyMaidLogger().info("OnLoginAfterCheck: Timeout");
} catch (IOException e) {
Main.getMyMaidLogger().info("OnLoginAfterCheck: IOException Error...");
MyMaidLibrary.reportError(getClass(), e);
}.runTask(Main.getJavaPlugin());
}
}
}.runTaskAsynchronously(Main.getJavaPlugin());
Expand Down

0 comments on commit 5943e0d

Please sign in to comment.