fix: 修复Web注册不写注册IP、玩家登录IP的bug; 新增库存高频轮询; 更新README下载地址
This commit is contained in:
@@ -399,6 +399,8 @@ public class GUIManager implements Listener {
|
||||
|
||||
public void openSubMenu(
|
||||
Player p, String fileName) {
|
||||
// ★ 剥离MC颜色代码(§f、§7等),防止污染文件名
|
||||
fileName = fileName.replaceAll("\u00A7[0-9a-fk-or]", "").trim();
|
||||
String menuTitle =
|
||||
"§6§l" + fileName
|
||||
.replace(".txt", "");
|
||||
@@ -406,21 +408,153 @@ public class GUIManager implements Listener {
|
||||
null, 54, menuTitle);
|
||||
fillBg(g);
|
||||
|
||||
java.io.File f = new java.io.File(
|
||||
plugin.getDataFolder(), fileName);
|
||||
if (!f.exists()) {
|
||||
p.sendMessage("§c文件不存在: "
|
||||
java.io.File dir = plugin.getDataFolder();
|
||||
java.io.File f = null;
|
||||
String cleanName = fileName.trim();
|
||||
|
||||
plugin.getLogger().info(
|
||||
"[Menu] openSubMenu: searching '"
|
||||
+ cleanName + "' in '"
|
||||
+ dir.getAbsolutePath() + "'");
|
||||
|
||||
// ★ 第1步:listFiles遍历匹配
|
||||
java.io.File[] allTxt = dir.listFiles(
|
||||
(d, n) -> n.toLowerCase()
|
||||
.endsWith(".txt"));
|
||||
if (allTxt != null) {
|
||||
plugin.getLogger().info(
|
||||
"[Menu] 目录.txt文件数: "
|
||||
+ allTxt.length);
|
||||
for (java.io.File ff : allTxt) {
|
||||
plugin.getLogger().info(
|
||||
"[Menu] > " + ff.getName()
|
||||
+ " size=" + ff.length());
|
||||
if (f == null && ff.getName()
|
||||
.equalsIgnoreCase(cleanName)) {
|
||||
f = ff;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
plugin.getLogger().warning(
|
||||
"[Menu] listFiles返回null!");
|
||||
}
|
||||
|
||||
// ★ 第2步:直接构造路径
|
||||
if (f == null) {
|
||||
f = new java.io.File(dir, cleanName);
|
||||
plugin.getLogger().info(
|
||||
"[Menu] 回退构造路径: "
|
||||
+ f.getAbsolutePath()
|
||||
+ " exists=" + f.exists()
|
||||
+ " size=" + f.length());
|
||||
}
|
||||
|
||||
// ★ 第3步:逐个尝试读取方法(不检查exists,直接试)
|
||||
List<String> lines = null;
|
||||
if (f != null) {
|
||||
// 方法1:NIO UTF-8
|
||||
try {
|
||||
lines = java.nio.file.Files
|
||||
.readAllLines(f.toPath(),
|
||||
java.nio.charset
|
||||
.StandardCharsets
|
||||
.UTF_8);
|
||||
plugin.getLogger().info(
|
||||
"[Menu] UTF-8读取: "
|
||||
+ lines.size() + "行");
|
||||
} catch (Throwable t) {
|
||||
plugin.getLogger().warning(
|
||||
"[Menu] UTF-8失败: "
|
||||
+ t.getClass().getSimpleName()
|
||||
+ ": " + t.getMessage());
|
||||
}
|
||||
|
||||
// 方法2:NIO GBK
|
||||
if (lines == null || lines.isEmpty()) {
|
||||
try {
|
||||
lines = java.nio.file.Files
|
||||
.readAllLines(f.toPath(),
|
||||
java.nio.charset
|
||||
.Charset
|
||||
.forName("GBK"));
|
||||
plugin.getLogger().info(
|
||||
"[Menu] GBK读取: "
|
||||
+ lines.size() + "行");
|
||||
} catch (Throwable t) {
|
||||
plugin.getLogger().warning(
|
||||
"[Menu] GBK失败: "
|
||||
+ t.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 方法3:FileInputStream原始字节
|
||||
if (lines == null || lines.isEmpty()) {
|
||||
try {
|
||||
java.io.FileInputStream fis =
|
||||
new java.io.FileInputStream(f);
|
||||
byte[] raw = fis.readAllBytes();
|
||||
fis.close();
|
||||
if (raw.length > 0) {
|
||||
plugin.getLogger().info(
|
||||
"[Menu] 原始字节: "
|
||||
+ raw.length + "B");
|
||||
// 尝试UTF-8解码
|
||||
String content = new String(raw,
|
||||
java.nio.charset
|
||||
.StandardCharsets
|
||||
.UTF_8);
|
||||
java.util.List<String> rawLines =
|
||||
java.util.Arrays.asList(
|
||||
content.split("\\r?\\n"));
|
||||
lines = rawLines;
|
||||
plugin.getLogger().info(
|
||||
"[Menu] 原始读取: "
|
||||
+ lines.size() + "行");
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
plugin.getLogger().warning(
|
||||
"[Menu] 原始读取失败: "
|
||||
+ t.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ★ 打印前5行调试
|
||||
if (lines != null && !lines.isEmpty()) {
|
||||
plugin.getLogger().info(
|
||||
"[Menu] 最终行数: "
|
||||
+ lines.size());
|
||||
for (int i = 0;
|
||||
i < Math.min(5, lines.size());
|
||||
i++) {
|
||||
plugin.getLogger().info(
|
||||
"[Menu] L" + (i + 1)
|
||||
+ ": [" + lines.get(i)
|
||||
+ "]");
|
||||
}
|
||||
}
|
||||
|
||||
if (lines == null || lines.isEmpty()) {
|
||||
p.sendMessage("§c§l子菜单读取失败§r §7"
|
||||
+ fileName);
|
||||
p.sendMessage("§7文件: "
|
||||
+ (f != null
|
||||
? f.getAbsolutePath()
|
||||
+ " (" + f.length() + "B)"
|
||||
: "null"));
|
||||
p.sendMessage("§7存在: "
|
||||
+ (f != null ? f.exists() : "N/A"));
|
||||
if (allTxt != null) {
|
||||
p.sendMessage("§7目录.txt文件:");
|
||||
for (java.io.File ff : allTxt) {
|
||||
p.sendMessage("§7 "
|
||||
+ ff.getName()
|
||||
+ " (" + ff.length()
|
||||
+ "B)");
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
try {
|
||||
List<String> lines =
|
||||
java.nio.file.Files
|
||||
.readAllLines(
|
||||
f.toPath(),
|
||||
java.nio.charset
|
||||
.StandardCharsets
|
||||
.UTF_8);
|
||||
List<String[]> items =
|
||||
new ArrayList<>();
|
||||
StringBuilder buf =
|
||||
@@ -484,11 +618,6 @@ public class GUIManager implements Listener {
|
||||
it.setItemMeta(im);
|
||||
g.setItem(i, it);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning(
|
||||
"[Menu] 子菜单读取失败: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
|
||||
g.setItem(49, mkItem(
|
||||
Material.ARROW,
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.bukkit.event.block.BlockPlaceEvent;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.event.entity.EntityDeathEvent;
|
||||
import org.bukkit.event.entity.ProjectileLaunchEvent;
|
||||
import org.bukkit.event.inventory.CraftItemEvent;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryDragEvent;
|
||||
@@ -2328,6 +2329,63 @@ public class Main extends JavaPlugin
|
||||
// ★ Sdf1_game宝箱物品不干预,由Sdf1_game自行处理
|
||||
}
|
||||
|
||||
// ★ 防止雪球菜单被投掷出去(弱网兜底 + 防止快乐恶魂投喂)
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onProjectileLaunch(
|
||||
org.bukkit.event.entity.ProjectileLaunchEvent e) {
|
||||
if (e.isCancelled()) return;
|
||||
if (!(e.getEntity()
|
||||
instanceof org.bukkit.entity.Snowball))
|
||||
return;
|
||||
org.bukkit.entity.Snowball sb =
|
||||
(org.bukkit.entity.Snowball) e.getEntity();
|
||||
// 检查发射者(可能是玩家或生物)
|
||||
Object shooter = sb.getShooter();
|
||||
// 如果是玩家发射菜单雪球 → 取消并归还
|
||||
if (shooter instanceof Player) {
|
||||
Player p = (Player) shooter;
|
||||
ItemStack main =
|
||||
p.getInventory().getItemInMainHand();
|
||||
ItemStack off =
|
||||
p.getInventory().getItemInOffHand();
|
||||
if (isMenuSnowball(main)
|
||||
|| isMenuSnowball(off)) {
|
||||
e.setCancelled(true);
|
||||
giveMenuSnowball(p);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// 检查雪球的item meta是否携带菜单标记(兜底:恶魂投喂时雪球已被分离)
|
||||
if (sb.hasMetadata("menu_snowball")) {
|
||||
e.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// ★ 防止任何生物拾取菜单雪球掉落物(防恶魂投喂)
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onEntityPickupItem(
|
||||
org.bukkit.event.entity.EntityPickupItemEvent e) {
|
||||
if (e.isCancelled()) return;
|
||||
ItemStack item = e.getItem().getItemStack();
|
||||
if (isMenuSnowball(item)) {
|
||||
e.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
// ★ 防止玩家右键实体时交出菜单雪球(防恶魂投喂)
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onPlayerInteractEntity(
|
||||
org.bukkit.event.player.PlayerInteractEntityEvent e) {
|
||||
if (e.isCancelled()) return;
|
||||
Player p = e.getPlayer();
|
||||
ItemStack main = p.getInventory().getItemInMainHand();
|
||||
ItemStack off = p.getInventory().getItemInOffHand();
|
||||
if (isMenuSnowball(main) || isMenuSnowball(off)) {
|
||||
e.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@EventHandler
|
||||
public void onPickup(PlayerPickupItemEvent e) {
|
||||
@@ -3097,62 +3155,6 @@ public class Main extends JavaPlugin
|
||||
}
|
||||
|
||||
|
||||
// ===== 二级菜单(.txt)—— 移到前面 =====
|
||||
if (title.startsWith("§6§l")
|
||||
&& !title.equals(GUIManager.T_MAIN)
|
||||
&& !title.equals(GUIManager.T_ADMIN)
|
||||
&& !title.equals(GUIManager.T_MY_INFO)
|
||||
&& !title.equals(GUIManager.T_INVITE)
|
||||
&& !title.equals(
|
||||
GUIManager.T_TASK_CENTER)
|
||||
&& !title.equals(
|
||||
GUIManager.T_GIFT_STAGES)
|
||||
&& !title.equals("§6§l任务面板")
|
||||
&& !title.equals(
|
||||
"§6§l垃圾回收站")) {
|
||||
e.setCancelled(true);
|
||||
if (slot == 49) {
|
||||
gui.openMain(p);
|
||||
return;
|
||||
}
|
||||
// ★ bounds检查 ★
|
||||
if (slot < 45
|
||||
&& slot < e.getInventory()
|
||||
.getSize()
|
||||
&& e.getCurrentItem() != null) {
|
||||
ItemMeta subLm =
|
||||
e.getCurrentItem().getItemMeta();
|
||||
if (subLm != null
|
||||
&& subLm.getLore() != null) {
|
||||
for (String subLn
|
||||
: subLm.getLore()) {
|
||||
if (subLn.contains(
|
||||
"§7指令: §f")) {
|
||||
String cmd =
|
||||
subLn.replace(
|
||||
"§7指令: §f",
|
||||
"").trim();
|
||||
if (cmd.isEmpty()
|
||||
|| cmd.equals("/")
|
||||
|| cmd.equals(
|
||||
"null")) {
|
||||
p.sendMessage(
|
||||
"§c指令为空");
|
||||
return;
|
||||
}
|
||||
p.closeInventory();
|
||||
if (cmd.startsWith("/"))
|
||||
cmd = cmd.substring(1);
|
||||
Bukkit.dispatchCommand(
|
||||
p, cmd);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// ===== [DEBUG] 标题追踪 =====
|
||||
{
|
||||
String dt = title;
|
||||
@@ -3203,7 +3205,8 @@ public class Main extends JavaPlugin
|
||||
"§7指令: §f")) {
|
||||
String cmd = subLn
|
||||
.replace("§7指令: §f", "")
|
||||
.trim();
|
||||
.trim()
|
||||
.replaceAll("\u00A7[0-9a-fk-or]", ""); // 剥离残留MC颜色代码
|
||||
if (cmd.isEmpty()
|
||||
|| cmd.equals("/")
|
||||
|| cmd.equals("null")) {
|
||||
@@ -3214,7 +3217,12 @@ public class Main extends JavaPlugin
|
||||
if (cmd.startsWith("/")) {
|
||||
cmd = cmd.substring(1);
|
||||
}
|
||||
Bukkit.dispatchCommand(p, cmd);
|
||||
// ★ 子菜单项如果是.txt文件,打开子菜单而非执行命令
|
||||
if (cmd.endsWith(".txt")) {
|
||||
gui.openSubMenu(p, cmd);
|
||||
} else {
|
||||
Bukkit.dispatchCommand(p, cmd);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -3464,7 +3472,8 @@ public class Main extends JavaPlugin
|
||||
for (String ln : meta.getLore()) {
|
||||
if (ln.contains("§7指令: §f")) {
|
||||
String cmd = ln.replace(
|
||||
"§7指令: §f", "").trim();
|
||||
"§7指令: §f", "").trim()
|
||||
.replaceAll("\u00A7[0-9a-fk-or]", ""); // 剥离残留MC颜色代码
|
||||
if (cmd.isEmpty()
|
||||
|| cmd.equals("/")
|
||||
|| cmd.equals("null")) {
|
||||
@@ -4281,6 +4290,25 @@ public class Main extends JavaPlugin
|
||||
}
|
||||
return shopManager.handleCommand(sender, args);
|
||||
}
|
||||
// ★ menu 子命令 - 重载菜单配置
|
||||
if (cmd.getName().equalsIgnoreCase("sdf1_login")
|
||||
&& args.length >= 2
|
||||
&& args[0].equalsIgnoreCase("menu")
|
||||
&& args[1].equalsIgnoreCase("reload")) {
|
||||
if (!isAdmin(sender)) {
|
||||
sender.sendMessage("§c权限不足");
|
||||
return true;
|
||||
}
|
||||
if (menu == null) {
|
||||
sender.sendMessage("§c菜单系统未加载");
|
||||
return true;
|
||||
}
|
||||
menu.loadMenu();
|
||||
sender.sendMessage("§a菜单已重载,共 "
|
||||
+ menu.getItems().size()
|
||||
+ " 个菜单项");
|
||||
return true;
|
||||
}
|
||||
// ★ weblogin 子命令 - 生成Web登录Token
|
||||
if (sub.equals("weblogin")) {
|
||||
if (!(sender instanceof Player)) {
|
||||
@@ -5948,7 +5976,7 @@ public class Main extends JavaPlugin
|
||||
"take", "add", "kick",
|
||||
"ticket", "oa", "stop",
|
||||
"shopadd", "shopdel", "back", "radio", "back", "shop",
|
||||
"update", "updateall"));
|
||||
"update", "updateall", "menu"));
|
||||
} else {
|
||||
list.addAll(Arrays.asList(
|
||||
"pw", "email", "sign",
|
||||
@@ -5956,6 +5984,14 @@ public class Main extends JavaPlugin
|
||||
"ticket", "oa", "stop"));
|
||||
}
|
||||
}
|
||||
// ★ menu reload Tab补全
|
||||
if (cmd.getName().equalsIgnoreCase("sdf1_login")
|
||||
&& args.length == 2
|
||||
&& args[0].equalsIgnoreCase("menu")) {
|
||||
if (isAdmin(sender)) {
|
||||
list.add("reload");
|
||||
}
|
||||
}
|
||||
// 独立 /update 命令无子命令
|
||||
if (cmd.getName().equalsIgnoreCase("update")) {
|
||||
return list;
|
||||
|
||||
@@ -83,18 +83,29 @@ public class MenuManager implements Listener {
|
||||
return items;
|
||||
}
|
||||
|
||||
// ===== 读取菜单.txt =====
|
||||
// ===== 读取菜单.txt(主菜单唯一来源) =====
|
||||
public void loadMenu() {
|
||||
items.clear();
|
||||
|
||||
// 优先菜单.txt,备选menu.txt
|
||||
File f = new File(
|
||||
plugin.getDataFolder(),
|
||||
"菜单.txt");
|
||||
if (!f.exists()) createDefault(f);
|
||||
if (!f.exists()) {
|
||||
File alt = new File(
|
||||
plugin.getDataFolder(),
|
||||
"menu.txt");
|
||||
if (alt.exists()) {
|
||||
f = alt;
|
||||
} else {
|
||||
createDefault(f);
|
||||
}
|
||||
}
|
||||
parseFile(f);
|
||||
plugin.getLogger().info(
|
||||
"[Menu] 加载 "
|
||||
+ items.size()
|
||||
+ " 个菜单项");
|
||||
"[Menu] 加载 " + items.size()
|
||||
+ " 个菜单项 (源: "
|
||||
+ f.getName() + ")");
|
||||
}
|
||||
|
||||
private void parseFile(File f) {
|
||||
@@ -372,9 +383,16 @@ public class MenuManager implements Listener {
|
||||
|
||||
// ===== 保存 =====
|
||||
public void saveMenu() {
|
||||
// 保存到加载源文件
|
||||
File f = new File(
|
||||
plugin.getDataFolder(),
|
||||
"菜单.txt");
|
||||
if (!f.exists()) {
|
||||
File alt = new File(
|
||||
plugin.getDataFolder(),
|
||||
"menu.txt");
|
||||
if (alt.exists()) f = alt;
|
||||
}
|
||||
try (PrintWriter pw = new PrintWriter(
|
||||
new OutputStreamWriter(
|
||||
new FileOutputStream(f),
|
||||
|
||||
@@ -237,6 +237,7 @@ public class WebManager {
|
||||
syncBondBalances();
|
||||
syncBondTransactions();
|
||||
syncAllPlayerIps();
|
||||
syncServiceProviders();
|
||||
// 首次同步只打一次,后续由调度器静默执行
|
||||
plugin.getLogger().info("[Web通信] 首次全量同步完成");
|
||||
|
||||
@@ -259,6 +260,9 @@ public class WebManager {
|
||||
// ★ 启动交易高频轮询(每5秒检查PHP端是否有待处理交易)
|
||||
startTransactionPolling();
|
||||
|
||||
// ★ 启动库存高频轮询(每5秒检测Web库存改动,无改动跳过,有改动立即更新)
|
||||
startShopStockFastPolling();
|
||||
|
||||
// ★ 启动CDK验证轮询(每2秒检查PHP端CDK验证请求)
|
||||
startCdkValidationPolling();
|
||||
|
||||
@@ -631,6 +635,7 @@ public class WebManager {
|
||||
pushWebLoginCredentials();
|
||||
syncUserRegistrations();
|
||||
syncAllPlayerIps();
|
||||
syncServiceProviders();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -708,6 +713,7 @@ public class WebManager {
|
||||
syncUserRegistrations();
|
||||
syncBondTransactions();
|
||||
syncAllPlayerIps();
|
||||
syncServiceProviders();
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("[Web通信] 全量同步异常: " + e.getMessage());
|
||||
} finally {
|
||||
@@ -779,6 +785,163 @@ public class WebManager {
|
||||
}.runTaskTimerAsynchronously(plugin, 20L * 5, 20L * 5); // 每5秒检查一次
|
||||
}
|
||||
|
||||
// ==================== 库存高频轮询 ====================
|
||||
|
||||
/** 上次检测到的库存修改时间戳 */
|
||||
private volatile long lastKnownShopStockModified = 0;
|
||||
/** 连续失败计数 */
|
||||
private volatile int shopStockPollFailCount = 0;
|
||||
private static final int SHOP_STOCK_POLL_FAIL_THRESHOLD = 10;
|
||||
/** 防重入锁:pullShopStock执行期间不重复拉取 */
|
||||
private volatile boolean shopStockPulling = false;
|
||||
|
||||
/**
|
||||
* 库存高频轮询:每5秒请求Web端库存变更检测接口
|
||||
* 无改动跳过,有改动立即拉取完整库存并更新游戏
|
||||
* 60~90秒的全量同步定时器作为兜底
|
||||
*/
|
||||
private void startShopStockFastPolling() {
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
// ★ 防重入:正在拉取中则跳过
|
||||
if (shopStockPulling) return;
|
||||
|
||||
// ★ 连续失败过多时跳过(等兜底定时器)
|
||||
if (shopStockPollFailCount >= SHOP_STOCK_POLL_FAIL_THRESHOLD) {
|
||||
if (shopStockPollFailCount % 12 != 0) {
|
||||
shopStockPollFailCount++;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
String urlStr = webBaseUrl + "/api/sync.php?action=check_shop_stock_changed&secret="
|
||||
+ java.net.URLEncoder.encode(secretKey, "UTF-8")
|
||||
+ "&last_modified=" + lastKnownShopStockModified;
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setRequestProperty("User-Agent", "Sdf1-WebManager/1.0");
|
||||
conn.setConnectTimeout(5000);
|
||||
conn.setReadTimeout(5000);
|
||||
|
||||
int code = conn.getResponseCode();
|
||||
if (code != 200) {
|
||||
shopStockPollFailCount++;
|
||||
conn.disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) sb.append(line);
|
||||
reader.close();
|
||||
conn.disconnect();
|
||||
|
||||
String resp = sb.toString();
|
||||
shopStockPollFailCount = 0;
|
||||
|
||||
// 解析响应
|
||||
boolean changed = resp.contains("\"changed\":true") || resp.contains("\"changed\": true");
|
||||
|
||||
if (changed) {
|
||||
// ★ 从响应中提取服务端last_modified(先提取,后面决定是否采纳)
|
||||
long serverLastModified = 0;
|
||||
int lmIdx = resp.indexOf("\"last_modified\":");
|
||||
if (lmIdx >= 0) {
|
||||
lmIdx += 16;
|
||||
int lmEnd = resp.indexOf(",", lmIdx);
|
||||
if (lmEnd < 0) lmEnd = resp.indexOf("}", lmIdx);
|
||||
if (lmEnd > lmIdx) {
|
||||
try {
|
||||
serverLastModified = Long.parseLong(
|
||||
resp.substring(lmIdx, lmEnd).trim());
|
||||
} catch (NumberFormatException e) { /* 忽略 */ }
|
||||
}
|
||||
}
|
||||
|
||||
// ★ 拉取并应用库存,只在成功更新后才推进时间戳
|
||||
shopStockPulling = true;
|
||||
try {
|
||||
boolean applied = pullShopStockSync();
|
||||
if (applied && serverLastModified > 0) {
|
||||
lastKnownShopStockModified = serverLastModified;
|
||||
}
|
||||
// applied=false → 不更新lastKnownShopStockModified → 下次重试
|
||||
} finally {
|
||||
shopStockPulling = false;
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
shopStockPollFailCount++;
|
||||
}
|
||||
}
|
||||
}.runTaskTimerAsynchronously(plugin, 20L * 5, 20L * 5);
|
||||
plugin.getLogger().info("[Web通信] 库存高频轮询已启动(每5秒)");
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步拉取库存(阻塞调用,用于高频轮询器内避免嵌套异步)
|
||||
* 逻辑与pullShopStock相同,但不在BukkitRunnable内执行
|
||||
* @return 是否成功应用了库存更新
|
||||
*/
|
||||
private boolean pullShopStockSync() {
|
||||
try {
|
||||
String urlStr = webBaseUrl + "/api/sync.php?action=pull_shop_stock&secret="
|
||||
+ java.net.URLEncoder.encode(secretKey, "UTF-8");
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setRequestProperty("User-Agent", "Sdf1-WebManager/1.0");
|
||||
conn.setConnectTimeout(5000);
|
||||
conn.setReadTimeout(5000);
|
||||
|
||||
int code = conn.getResponseCode();
|
||||
if (code != 200) {
|
||||
plugin.getLogger().warning("[库存高频] pull_shop_stock HTTP " + code);
|
||||
return false;
|
||||
}
|
||||
|
||||
BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) sb.append(line);
|
||||
reader.close();
|
||||
|
||||
String json = sb.toString();
|
||||
if (!json.contains("\"success\":true")) {
|
||||
plugin.getLogger().warning("[库存高频] pull_shop_stock 返回失败");
|
||||
return false;
|
||||
}
|
||||
|
||||
int dataStart = json.indexOf("\"items\":");
|
||||
if (dataStart < 0) {
|
||||
plugin.getLogger().warning("[库存高频] pull_shop_stock 无items字段");
|
||||
return false;
|
||||
}
|
||||
String dataStr = json.substring(dataStart + 8);
|
||||
int arrEnd = findMatchingBracket(dataStr, 0);
|
||||
if (arrEnd < 0) {
|
||||
plugin.getLogger().warning("[库存高频] pull_shop_stock JSON解析失败");
|
||||
return false;
|
||||
}
|
||||
String arrStr = dataStr.substring(0, arrEnd + 1);
|
||||
|
||||
boolean applied = updateLocalShopStock(arrStr);
|
||||
// ★ PHP端pullShopStock已自动清除admin_stock,无需再调clearAdminStock()
|
||||
return applied;
|
||||
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("[库存高频] 拉取库存异常: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== CDK验证轮询 ====================
|
||||
|
||||
/**
|
||||
@@ -1557,6 +1720,46 @@ public class WebManager {
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 服务商列表同步到Web ====================
|
||||
public void syncServiceProviders() {
|
||||
try {
|
||||
String token = generateAndSyncToken("system", "sync");
|
||||
List<Map<String, Object>> providers = new ArrayList<>();
|
||||
for (Map<String, Object> sp : plugin.getDb().getAllServiceProviders()) {
|
||||
Map<String, Object> p = new LinkedHashMap<>();
|
||||
p.put("player_name", sp.get("player_name"));
|
||||
p.put("role", sp.get("role"));
|
||||
p.put("active", sp.get("active"));
|
||||
p.put("join_time", sp.get("join_time"));
|
||||
providers.add(p);
|
||||
}
|
||||
|
||||
if (providers.isEmpty()) {
|
||||
plugin.getLogger().info("[Web通信] 无服务商,跳过同步");
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("data", providers);
|
||||
|
||||
String response = httpPostWithToken("api/sync.php?action=sync_service_providers", token, body);
|
||||
|
||||
if (response != null) {
|
||||
Map<String, Object> result = parseJson(response);
|
||||
Boolean success = (Boolean) result.get("success");
|
||||
if (Boolean.TRUE.equals(success)) {
|
||||
plugin.getLogger().info("[Web通信] 服务商同步成功: " + providers.size() + "人");
|
||||
} else {
|
||||
plugin.getLogger().warning("[Web通信] 服务商同步失败: " + result.get("message"));
|
||||
}
|
||||
} else {
|
||||
plugin.getLogger().warning("[Web通信] 服务商同步请求失败");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("[Web通信] 服务商同步异常: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析md商品文件
|
||||
* 格式: | ID | 品名 | 材质 | 购入价 | 售出价 | 库存 | 本小时销量 | 总销量 |
|
||||
@@ -2466,13 +2669,20 @@ public class WebManager {
|
||||
boolean isRegistered = plugin.getDb().userExists(playerName);
|
||||
boolean syncSuccess = false;
|
||||
try {
|
||||
// ★ 获取玩家真实IP地址
|
||||
String playerIp = "";
|
||||
java.net.InetSocketAddress addr = sender.getAddress();
|
||||
if (addr != null && addr.getAddress() != null) {
|
||||
playerIp = addr.getAddress().getHostAddress();
|
||||
}
|
||||
String urlStr = webBaseUrl + "/api/sync.php?action=push_player_login_status"
|
||||
+ "&secret=" + java.net.URLEncoder.encode(secretKey, "UTF-8")
|
||||
+ "&player=" + java.net.URLEncoder.encode(playerName, "UTF-8")
|
||||
+ "&web_token=" + java.net.URLEncoder.encode(token, "UTF-8")
|
||||
+ "&expire_seconds=" + tokenExpireSeconds
|
||||
+ "&online=1"
|
||||
+ "®istered=" + (isRegistered ? "1" : "0");
|
||||
+ "®istered=" + (isRegistered ? "1" : "0")
|
||||
+ "&ip=" + java.net.URLEncoder.encode(playerIp, "UTF-8");
|
||||
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
@@ -2545,13 +2755,20 @@ public class WebManager {
|
||||
boolean syncSuccess = false;
|
||||
try {
|
||||
// 用GET请求,所有参数通过URL传递
|
||||
// ★ 获取玩家真实IP地址
|
||||
String playerIp = "";
|
||||
java.net.InetSocketAddress addr = player.getAddress();
|
||||
if (addr != null && addr.getAddress() != null) {
|
||||
playerIp = addr.getAddress().getHostAddress();
|
||||
}
|
||||
String urlStr = webBaseUrl + "/api/sync.php?action=push_player_login_status"
|
||||
+ "&secret=" + java.net.URLEncoder.encode(secretKey, "UTF-8")
|
||||
+ "&player=" + java.net.URLEncoder.encode(playerName, "UTF-8")
|
||||
+ "&web_token=" + java.net.URLEncoder.encode(token, "UTF-8")
|
||||
+ "&expire_seconds=" + tokenExpireSeconds
|
||||
+ "&online=1"
|
||||
+ "®istered=" + (isRegistered ? "1" : "0");
|
||||
+ "®istered=" + (isRegistered ? "1" : "0")
|
||||
+ "&ip=" + java.net.URLEncoder.encode(playerIp, "UTF-8");
|
||||
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
@@ -2639,6 +2856,7 @@ public class WebManager {
|
||||
syncBondBalances();
|
||||
syncBondTransactions();
|
||||
syncUserRegistrations();
|
||||
syncServiceProviders();
|
||||
}
|
||||
}.runTaskAsynchronously(plugin);
|
||||
sender.sendMessage("§a[Web] 同步任务已启动...");
|
||||
@@ -3592,8 +3810,13 @@ public class WebManager {
|
||||
if (arrEnd < 0) return;
|
||||
String arrStr = dataStr.substring(0, arrEnd + 1);
|
||||
|
||||
// 更新本地商品库存
|
||||
updateLocalShopStock(arrStr);
|
||||
// ★ 更新本地商品库存,返回是否有改动
|
||||
boolean changed = updateLocalShopStock(arrStr);
|
||||
|
||||
// ★ 如果有改动,清除PHP端的admin_stock标记(防止重复同步)
|
||||
if (changed) {
|
||||
clearAdminStock();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
// 静默处理
|
||||
@@ -3603,20 +3826,42 @@ public class WebManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新本地商品库存
|
||||
* 清除PHP端的管理员库存标记
|
||||
*/
|
||||
private void updateLocalShopStock(String itemsJson) {
|
||||
private void clearAdminStock() {
|
||||
try {
|
||||
String urlStr = webBaseUrl + "/api/sync.php?action=clear_admin_stock&secret="
|
||||
+ java.net.URLEncoder.encode(secretKey, "UTF-8");
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setRequestProperty("User-Agent", "Sdf1-WebManager/1.0");
|
||||
conn.setConnectTimeout(5000);
|
||||
conn.setReadTimeout(5000);
|
||||
conn.getResponseCode(); // 执行即可
|
||||
conn.disconnect();
|
||||
} catch (Exception e) {
|
||||
// 静默处理
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新本地商品库存
|
||||
* @return 是否有任何库存被更新
|
||||
*/
|
||||
private boolean updateLocalShopStock(String itemsJson) {
|
||||
try {
|
||||
File shopDir = new File(plugin.getDataFolder(), "shop");
|
||||
if (!shopDir.exists()) return;
|
||||
if (!shopDir.exists()) return false;
|
||||
|
||||
File[] mdFiles = shopDir.listFiles((d, n) -> n.endsWith(".md"));
|
||||
if (mdFiles == null) return;
|
||||
if (mdFiles == null) return false;
|
||||
|
||||
// 解析Web端返回的库存数据
|
||||
// 格式: [{"id":"ITEM_ID","stock":100,"last_sync":1234567890}, ...]
|
||||
// 解析JSON提取id和stock
|
||||
// 格式: [{"id":"ITEM_ID","stock":100,"last_sync":...,"admin_stock_override":true}, ...]
|
||||
Map<String, Integer> webStock = new HashMap<>();
|
||||
// ★ 跟踪哪些商品是管理员手动修改的(admin_stock_override=true)
|
||||
Set<String> adminOverride = new HashSet<>();
|
||||
int idx = 0;
|
||||
while (true) {
|
||||
int idStart = itemsJson.indexOf("\"id\":\"", idx);
|
||||
@@ -3633,7 +3878,6 @@ public class WebManager {
|
||||
if (stockEnd < 0) stockEnd = itemsJson.indexOf("}", stockStart);
|
||||
if (stockEnd < 0) break;
|
||||
String stockStr = itemsJson.substring(stockStart, stockEnd).trim();
|
||||
// 去除可能的引号
|
||||
stockStr = stockStr.replace("\"", "");
|
||||
|
||||
try {
|
||||
@@ -3643,10 +3887,16 @@ public class WebManager {
|
||||
// 忽略无效值
|
||||
}
|
||||
|
||||
// ★ 检查admin_stock_override标记
|
||||
int overrideCheck = itemsJson.indexOf("\"admin_stock_override\":", idEnd);
|
||||
if (overrideCheck > idEnd && overrideCheck < stockEnd + 20) {
|
||||
adminOverride.add(itemId);
|
||||
}
|
||||
|
||||
idx = stockEnd + 1;
|
||||
}
|
||||
|
||||
if (webStock.isEmpty()) return;
|
||||
if (webStock.isEmpty()) return false;
|
||||
|
||||
// 更新每个md文件中的库存
|
||||
int updatedCount = 0;
|
||||
@@ -3671,17 +3921,21 @@ public class WebManager {
|
||||
|
||||
int newStock = webStock.get(itemId);
|
||||
String oldStock = cols[6].trim();
|
||||
// 始终用数字写入md文件,不写"无限"字符串(防止parseTableRow的Integer.parseInt失败)
|
||||
String newStockStr = String.valueOf(newStock);
|
||||
// 兼容旧文件中的"无限"文本:转为-1用于比较
|
||||
int oldStockNum;
|
||||
try {
|
||||
oldStockNum = Integer.parseInt(oldStock);
|
||||
} catch (NumberFormatException e) {
|
||||
oldStockNum = ("无限".equals(oldStock) || "∞".equals(oldStock)) ? -1 : 0;
|
||||
}
|
||||
// 不要把Web端的0库存覆盖到游戏的无限库存(-1)
|
||||
if (newStock == 0 && oldStockNum == -1) continue;
|
||||
|
||||
// ★ 管理员手动修改的库存:跳过保护逻辑,直接应用
|
||||
// 管理员设stock=0表示"售罄",需要同步到游戏
|
||||
if (!adminOverride.contains(itemId)) {
|
||||
// stock<-1 为未定义值(-2以下),不生效,跳过同步
|
||||
// -1=无限库存, 0=售罄, >=1=有库存,都正常同步
|
||||
if (newStock < -1) continue;
|
||||
}
|
||||
|
||||
if (oldStockNum != newStock) {
|
||||
cols[6] = " " + newStockStr + " ";
|
||||
@@ -3711,9 +3965,12 @@ public class WebManager {
|
||||
plugin.getLogger().info("[Web通信] 已同步Web端商品库存,更新了 " + updatedCount + " 个商品");
|
||||
lastPullShopStockLog = now;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("[Web通信] 更新商品库存失败: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3724,20 +3981,38 @@ public class WebManager {
|
||||
try {
|
||||
File notifyFile = new File(plugin.getDataFolder(), "../sync_notify.txt");
|
||||
if (notifyFile.exists()) {
|
||||
long notifyTime = Long.parseLong(new String(java.nio.file.Files.readAllBytes(notifyFile.toPath())).trim());
|
||||
String content = new String(java.nio.file.Files.readAllBytes(notifyFile.toPath())).trim();
|
||||
// ★ 兼容两种格式:纯数字时间戳 或 JSON {"time":xxx,"tx_id":xxx}
|
||||
long notifyTime = 0;
|
||||
if (content.startsWith("{")) {
|
||||
// JSON格式
|
||||
int timeIdx = content.indexOf("\"time\":");
|
||||
if (timeIdx > 0) {
|
||||
int start = timeIdx + 8;
|
||||
int end = content.indexOf(",", start);
|
||||
if (end < 0) end = content.indexOf("}", start);
|
||||
if (end > start) {
|
||||
notifyTime = Long.parseLong(content.substring(start, end).trim());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
notifyTime = Long.parseLong(content);
|
||||
}
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
// 如果通知时间在5分钟内,执行同步
|
||||
if (now - notifyTime < 300000) {
|
||||
if (notifyTime > 0 && now - notifyTime < 300000) {
|
||||
// 有人在线才响应通知
|
||||
if (!Bukkit.getOnlinePlayers().isEmpty()) {
|
||||
syncShopData();
|
||||
syncBondBalances();
|
||||
syncBondTransactions();
|
||||
pushWebLoginCredentials();
|
||||
// ★ 先拉取(PHP→Java),再推送(Java→PHP),确保不覆盖管理员修改
|
||||
pullPendingTransactions();
|
||||
pullShopStock();
|
||||
pullBondChanges();
|
||||
syncOnlinePlayers();
|
||||
pushWebLoginCredentials();
|
||||
syncUserRegistrations();
|
||||
syncServiceProviders();
|
||||
syncShopData();
|
||||
// 删除通知文件
|
||||
notifyFile.delete();
|
||||
}
|
||||
@@ -4029,6 +4304,12 @@ public class WebManager {
|
||||
plugin.getLogger().info("[Web注册] 游戏账号创建成功: " + fName);
|
||||
}
|
||||
|
||||
// ★ 写入注册IP到login.db
|
||||
if (!fIp.isEmpty()) {
|
||||
dbMgr.setField(fName, "ip_address", fIp);
|
||||
dbMgr.setField(fName, "register_ip", fIp);
|
||||
}
|
||||
|
||||
// ★ 如果玩家当前在线,自动登录
|
||||
Player onlinePlayer = Bukkit.getPlayer(fName);
|
||||
if (onlinePlayer != null && onlinePlayer.isOnline()) {
|
||||
@@ -4155,6 +4436,12 @@ public class WebManager {
|
||||
dbMgr.createUser(fName, fHash, fSalt);
|
||||
plugin.getLogger().info("[Web注册] 补全成功: " + fName);
|
||||
|
||||
// ★ 写入注册IP到login.db
|
||||
if (!fIp.isEmpty()) {
|
||||
dbMgr.setField(fName, "ip_address", fIp);
|
||||
dbMgr.setField(fName, "register_ip", fIp);
|
||||
}
|
||||
|
||||
Player onlinePlayer = Bukkit.getPlayer(fName);
|
||||
if (onlinePlayer != null && onlinePlayer.isOnline()) {
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
@@ -4303,6 +4590,7 @@ public class WebManager {
|
||||
int points = Integer.parseInt(extractJsonNumber(dataJson, "points") != null ? extractJsonNumber(dataJson, "points") : "0");
|
||||
int giftStage = Integer.parseInt(extractJsonNumber(dataJson, "gift_stage") != null ? extractJsonNumber(dataJson, "gift_stage") : "0");
|
||||
int totalOnlineTime = Integer.parseInt(extractJsonNumber(dataJson, "total_online_time") != null ? extractJsonNumber(dataJson, "total_online_time") : "0");
|
||||
String ipAddress = extractJsonString(dataJson, "ip_address");
|
||||
|
||||
plugin.getLogger().info("[Web注册] 玩家在PHP后端已注册: " + playerName + ", 有密码凭证:" + !extractJsonString(dataJson, "password_hash").isEmpty());
|
||||
|
||||
@@ -4310,6 +4598,7 @@ public class WebManager {
|
||||
final String fEmail = email != null ? email : "";
|
||||
final String fHash = passwordHash != null ? passwordHash : "";
|
||||
final String fSalt = salt != null ? salt : "";
|
||||
final String fIp = ipAddress != null ? ipAddress : "";
|
||||
final long fRegisterTime = registerTime;
|
||||
final long fLastLoginTime = lastLoginTime;
|
||||
final int fPoints = points;
|
||||
@@ -4333,6 +4622,11 @@ public class WebManager {
|
||||
if (!fHash.isEmpty() && !fSalt.isEmpty()) {
|
||||
dbMgr.createUser(fName, fHash, fSalt);
|
||||
plugin.getLogger().info("[Web注册] 用户 " + fName + " 已从PHP后端同步注册数据到Java本地(含密码凭证)");
|
||||
// ★ 写入注册IP到login.db
|
||||
if (!fIp.isEmpty()) {
|
||||
dbMgr.setField(fName, "ip_address", fIp);
|
||||
dbMgr.setField(fName, "register_ip", fIp);
|
||||
}
|
||||
} else {
|
||||
// PHP后端没有密码凭证,可能是通过register.php的webRegister()直接注册的
|
||||
// 需要等待Java插件通过pollWebRegisterRequests()轮询后创建用户
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
name: Sdf1_login
|
||||
version: 2.5
|
||||
version: 2.6
|
||||
main: Sdf1_login.Main
|
||||
api-version: '1.21'
|
||||
commands:
|
||||
|
||||
Reference in New Issue
Block a user