mirror of
https://github.com/Xiao-MoMi/Custom-Fishing.git
synced 2025-12-29 11:59:11 +00:00
完善
This commit is contained in:
@@ -1,3 +1,20 @@
|
||||
/*
|
||||
* Copyright (C) <2022> <XiaoMoMi>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.momirealms.customfishing.manager;
|
||||
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
@@ -10,41 +27,57 @@ import net.momirealms.customfishing.CustomFishing;
|
||||
import net.momirealms.customfishing.data.PlayerBagData;
|
||||
import net.momirealms.customfishing.data.storage.DataStorageInterface;
|
||||
import net.momirealms.customfishing.data.storage.FileStorageImpl;
|
||||
import net.momirealms.customfishing.data.storage.MySQLStorageImpl;
|
||||
import net.momirealms.customfishing.listener.InventoryListener;
|
||||
import net.momirealms.customfishing.listener.SimpleListener;
|
||||
import net.momirealms.customfishing.listener.WindowPacketListener;
|
||||
import net.momirealms.customfishing.object.Function;
|
||||
import net.momirealms.customfishing.util.AdventureUtil;
|
||||
import net.momirealms.customfishing.util.ConfigUtil;
|
||||
import net.momirealms.customfishing.util.ItemStackUtil;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||
import org.bukkit.event.inventory.InventoryOpenEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class BagDataManager extends Function {
|
||||
|
||||
public static HashMap<UUID, PlayerBagData> dataCache;
|
||||
public static ConcurrentHashMap<UUID, PlayerBagData> dataCache;
|
||||
public static HashSet<PlayerBagData> tempCache;
|
||||
private final DataStorageInterface dataStorageInterface;
|
||||
private final InventoryListener inventoryListener;
|
||||
private final WindowPacketListener windowPacketListener;
|
||||
private final SimpleListener simpleListener;
|
||||
private final BukkitTask timerSave;
|
||||
|
||||
public BagDataManager() {
|
||||
dataCache = new HashMap<>();
|
||||
dataCache = new ConcurrentHashMap<>();
|
||||
tempCache = new HashSet<>();
|
||||
this.dataStorageInterface = new FileStorageImpl();
|
||||
YamlConfiguration config = ConfigUtil.getConfig("database.yml");
|
||||
if (config.getString("data-storage-method","YAML").equalsIgnoreCase("YAML")) {
|
||||
this.dataStorageInterface = new FileStorageImpl();
|
||||
} else this.dataStorageInterface = new MySQLStorageImpl();
|
||||
this.dataStorageInterface.initialize();
|
||||
this.inventoryListener = new InventoryListener(this);
|
||||
this.windowPacketListener = new WindowPacketListener(this);
|
||||
this.simpleListener = new SimpleListener(this);
|
||||
this.timerSave = Bukkit.getScheduler().runTaskTimerAsynchronously(CustomFishing.plugin, () -> {
|
||||
for (PlayerBagData playerBagData : dataCache.values()) {
|
||||
dataStorageInterface.saveBagData(playerBagData);
|
||||
}
|
||||
AdventureUtil.consoleMessage("[CustomFishing] Fishing bag data saving for " + dataCache.size() + " online players...");
|
||||
}, 6000, 6000);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -61,14 +94,21 @@ public class BagDataManager extends Function {
|
||||
HandlerList.unregisterAll(simpleListener);
|
||||
CustomFishing.protocolManager.removePacketListener(windowPacketListener);
|
||||
for (PlayerBagData playerBagData : dataCache.values()) {
|
||||
dataStorageInterface.save(playerBagData);
|
||||
dataStorageInterface.saveBagData(playerBagData);
|
||||
}
|
||||
}
|
||||
|
||||
public void disable() {
|
||||
this.dataStorageInterface.disable();
|
||||
dataCache.clear();
|
||||
tempCache.clear();
|
||||
timerSave.cancel();
|
||||
}
|
||||
|
||||
public void openFishingBag(Player viewer, OfflinePlayer ownerOffline) {
|
||||
Player owner = ownerOffline.getPlayer();
|
||||
if (owner == null) {
|
||||
Inventory inventory = dataStorageInterface.load(ownerOffline);
|
||||
Inventory inventory = dataStorageInterface.loadBagData(ownerOffline);
|
||||
PlayerBagData playerBagData = new PlayerBagData(ownerOffline, inventory);
|
||||
tempCache.add(playerBagData);
|
||||
viewer.openInventory(inventory);
|
||||
@@ -89,17 +129,21 @@ public class BagDataManager extends Function {
|
||||
public void onQuit(Player player) {
|
||||
PlayerBagData playerBagData = dataCache.remove(player.getUniqueId());
|
||||
if (playerBagData != null) {
|
||||
dataStorageInterface.save(playerBagData);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(CustomFishing.plugin, () -> {
|
||||
dataStorageInterface.saveBagData(playerBagData);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onJoin(Player player) {
|
||||
readData(player);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(CustomFishing.plugin, () -> {
|
||||
readData(player);
|
||||
});
|
||||
}
|
||||
|
||||
public PlayerBagData readData(Player player) {
|
||||
Inventory inventory = dataStorageInterface.load(player);
|
||||
Inventory inventory = dataStorageInterface.loadBagData(player);
|
||||
PlayerBagData playerBagData = new PlayerBagData(player, inventory);
|
||||
dataCache.put(player.getUniqueId(), playerBagData);
|
||||
return playerBagData;
|
||||
@@ -159,7 +203,9 @@ public class BagDataManager extends Function {
|
||||
for (PlayerBagData temp : tempCache) {
|
||||
if (temp.getInventory() == inventory) {
|
||||
tempCache.remove(temp);
|
||||
dataStorageInterface.save(temp);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(CustomFishing.plugin, () -> {
|
||||
dataStorageInterface.saveBagData(temp);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user