mirror of
https://github.com/Xiao-MoMi/Custom-Fishing.git
synced 2025-12-29 03:49:07 +00:00
1.2.13
This commit is contained in:
@@ -44,6 +44,7 @@ public final class CustomFishing extends JavaPlugin {
|
||||
private LayoutManager layoutManager;
|
||||
private BagDataManager bagDataManager;
|
||||
private TotemManager totemManager;
|
||||
private DataManager dataManager;
|
||||
private SellManager sellManager;
|
||||
|
||||
// _ooOoo_
|
||||
@@ -82,6 +83,7 @@ public final class CustomFishing extends JavaPlugin {
|
||||
adventure = BukkitAudiences.create(this);
|
||||
protocolManager = ProtocolLibrary.getProtocolManager();
|
||||
this.fishingManager = new FishingManager();
|
||||
this.dataManager = new DataManager();
|
||||
this.integrationManager = new IntegrationManager();
|
||||
this.competitionManager = new CompetitionManager();
|
||||
this.bonusManager = new BonusManager();
|
||||
@@ -90,6 +92,7 @@ public final class CustomFishing extends JavaPlugin {
|
||||
this.totemManager = new TotemManager();
|
||||
this.sellManager = new SellManager();
|
||||
this.bagDataManager = new BagDataManager();
|
||||
|
||||
ConfigUtil.reload();
|
||||
registerCommands();
|
||||
|
||||
@@ -109,6 +112,8 @@ public final class CustomFishing extends JavaPlugin {
|
||||
this.bagDataManager.disable();
|
||||
this.totemManager.unload();
|
||||
this.sellManager.unload();
|
||||
this.sellManager.disable();
|
||||
this.dataManager.unload();
|
||||
if (adventure != null) {
|
||||
adventure.close();
|
||||
adventure = null;
|
||||
@@ -151,10 +156,6 @@ public final class CustomFishing extends JavaPlugin {
|
||||
return layoutManager;
|
||||
}
|
||||
|
||||
public BagDataManager getDataManager() {
|
||||
return bagDataManager;
|
||||
}
|
||||
|
||||
public TotemManager getTotemManager() {
|
||||
return totemManager;
|
||||
}
|
||||
@@ -166,4 +167,8 @@ public final class CustomFishing extends JavaPlugin {
|
||||
public BagDataManager getBagDataManager() {
|
||||
return bagDataManager;
|
||||
}
|
||||
|
||||
public DataManager getDataManager() {
|
||||
return dataManager;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ public class PluginCommand implements TabExecutor {
|
||||
regSubCommand(CompetitionCommand.INSTANCE);
|
||||
regSubCommand(ImportCommand.INSTANCE);
|
||||
regSubCommand(SellShopCommand.INSTANCE);
|
||||
regSubCommand(OpenBagCommand.INSTANCE);
|
||||
}
|
||||
|
||||
public void regSubCommand(SubCommand executor) {
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package net.momirealms.customfishing.commands.subcmd;
|
||||
|
||||
import net.momirealms.customfishing.CustomFishing;
|
||||
import net.momirealms.customfishing.commands.AbstractSubCommand;
|
||||
import net.momirealms.customfishing.commands.SubCommand;
|
||||
import net.momirealms.customfishing.manager.ConfigManager;
|
||||
import net.momirealms.customfishing.manager.MessageManager;
|
||||
import net.momirealms.customfishing.util.AdventureUtil;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class OpenBagCommand extends AbstractSubCommand {
|
||||
|
||||
public static final SubCommand INSTANCE = new OpenBagCommand();
|
||||
|
||||
private OpenBagCommand() {
|
||||
super("forceopenbag", null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, List<String> args) {
|
||||
if (!ConfigManager.enableFishingBag) return true;
|
||||
if (args.size() < 1){
|
||||
AdventureUtil.sendMessage(sender, MessageManager.prefix + MessageManager.lackArgs);
|
||||
return true;
|
||||
}
|
||||
if (args.size() == 1) {
|
||||
if (!sender.hasPermission("customfishing.admin")) {
|
||||
AdventureUtil.sendMessage(sender, MessageManager.prefix + MessageManager.noPerm);
|
||||
return true;
|
||||
}
|
||||
Player viewer = Bukkit.getPlayer(args.get(0));
|
||||
if (viewer == null) {
|
||||
AdventureUtil.sendMessage(sender, MessageManager.prefix + MessageManager.playerNotExist);
|
||||
return true;
|
||||
}
|
||||
viewer.closeInventory();
|
||||
CustomFishing.plugin.getBagDataManager().openFishingBag(viewer, viewer);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, List<String> args) {
|
||||
if (!ConfigManager.enableFishingBag) return null;
|
||||
if (!sender.hasPermission("customfishing.admin")) return null;
|
||||
if (args.size() == 1) {
|
||||
List<String> arrayList = new ArrayList<>();
|
||||
for (String cmd : online_players()) {
|
||||
if (cmd.startsWith(args.get(0)))
|
||||
arrayList.add(cmd);
|
||||
}
|
||||
return arrayList;
|
||||
}
|
||||
return super.onTabComplete(sender, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package net.momirealms.customfishing.data;
|
||||
|
||||
public class PlayerSellData {
|
||||
|
||||
private double money;
|
||||
private int date;
|
||||
|
||||
public PlayerSellData(double money, int date) {
|
||||
this.money = money;
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public double getMoney() {
|
||||
return money;
|
||||
}
|
||||
|
||||
public void setMoney(double money) {
|
||||
this.money = money;
|
||||
}
|
||||
|
||||
public int getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(int date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,13 +18,19 @@
|
||||
package net.momirealms.customfishing.data.storage;
|
||||
|
||||
import net.momirealms.customfishing.data.PlayerBagData;
|
||||
import net.momirealms.customfishing.data.PlayerSellData;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public interface DataStorageInterface {
|
||||
|
||||
void initialize();
|
||||
void disable();
|
||||
Inventory loadBagData(OfflinePlayer player);
|
||||
void saveBagData(PlayerBagData playerBagData);
|
||||
void loadSellCache(Player player);
|
||||
void saveSellCache(UUID uuid, PlayerSellData playerSellData);
|
||||
}
|
||||
|
||||
@@ -19,27 +19,37 @@ package net.momirealms.customfishing.data.storage;
|
||||
|
||||
import net.momirealms.customfishing.CustomFishing;
|
||||
import net.momirealms.customfishing.data.PlayerBagData;
|
||||
import net.momirealms.customfishing.data.PlayerSellData;
|
||||
import net.momirealms.customfishing.util.ConfigUtil;
|
||||
import net.momirealms.customfishing.util.InventoryUtil;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class FileStorageImpl implements DataStorageInterface {
|
||||
|
||||
private YamlConfiguration data;
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
|
||||
data = ConfigUtil.readData(new File(CustomFishing.plugin.getDataFolder(), "sell-cache.yml"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disable() {
|
||||
|
||||
try {
|
||||
data.save(new File(CustomFishing.plugin.getDataFolder(), "sell-cache.yml"));
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -67,4 +77,18 @@ public class FileStorageImpl implements DataStorageInterface {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadSellCache(Player player) {
|
||||
UUID uuid = player.getUniqueId();
|
||||
int date = data.getInt(uuid + ".date");
|
||||
double money = data.getDouble(uuid + ".sell");
|
||||
CustomFishing.plugin.getSellManager().loadPlayerToCache(player.getUniqueId(), date, money);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveSellCache(UUID uuid, PlayerSellData playerSellData) {
|
||||
data.set(uuid + ".date", playerSellData.getDate());
|
||||
data.set(uuid + ".sell", playerSellData.getMoney());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,11 +17,14 @@
|
||||
|
||||
package net.momirealms.customfishing.data.storage;
|
||||
|
||||
import net.momirealms.customfishing.CustomFishing;
|
||||
import net.momirealms.customfishing.data.PlayerBagData;
|
||||
import net.momirealms.customfishing.data.PlayerSellData;
|
||||
import net.momirealms.customfishing.util.AdventureUtil;
|
||||
import net.momirealms.customfishing.util.InventoryUtil;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
@@ -29,6 +32,7 @@ import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Calendar;
|
||||
import java.util.UUID;
|
||||
|
||||
public class MySQLStorageImpl implements DataStorageInterface {
|
||||
@@ -38,7 +42,8 @@ public class MySQLStorageImpl implements DataStorageInterface {
|
||||
@Override
|
||||
public void initialize() {
|
||||
sqlConnection.createNewHikariConfiguration();
|
||||
createTableIfNotExist(sqlConnection.getTablePrefix() + "_fishingbag");
|
||||
createTableIfNotExist(sqlConnection.getTablePrefix() + "_fishingbag", SqlConstants.SQL_CREATE_BAG_TABLE);
|
||||
createTableIfNotExist(sqlConnection.getTablePrefix() + "_sellcache", SqlConstants.SQL_CREATE_SELL_TABLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -78,7 +83,7 @@ public class MySQLStorageImpl implements DataStorageInterface {
|
||||
Inventory inventory = playerBagData.getInventory();
|
||||
String contents = InventoryUtil.toBase64(inventory.getContents());
|
||||
if (contents == null) contents = "";
|
||||
if (exists(uuid)) {
|
||||
if (exists(uuid, SqlConstants.SQL_SELECT_BAG_BY_UUID, "fishingbag")) {
|
||||
updateBagData(uuid, inventory.getSize(), contents);
|
||||
}
|
||||
else {
|
||||
@@ -86,8 +91,40 @@ public class MySQLStorageImpl implements DataStorageInterface {
|
||||
}
|
||||
}
|
||||
|
||||
private void createTableIfNotExist(String table) {
|
||||
String sql = String.format(SqlConstants.SQL_CREATE_BAG_TABLE, table);
|
||||
@Override
|
||||
public void loadSellCache(Player player) {
|
||||
try {
|
||||
Connection connection = sqlConnection.getConnectionAndCheck();
|
||||
String sql = String.format(SqlConstants.SQL_SELECT_SELL_BY_UUID, sqlConnection.getTablePrefix() + "_sellcache");
|
||||
PreparedStatement statement = connection.prepareStatement(sql);
|
||||
statement.setString(1, player.getUniqueId().toString());
|
||||
ResultSet rs = statement.executeQuery();
|
||||
if (rs.next()) {
|
||||
int date = rs.getInt(2);
|
||||
int money = rs.getInt(3);
|
||||
CustomFishing.plugin.getSellManager().loadPlayerToCache(player.getUniqueId(), date, money);
|
||||
}
|
||||
else {
|
||||
CustomFishing.plugin.getSellManager().loadPlayerToCache(player.getUniqueId(), Calendar.getInstance().get(Calendar.DATE), 0);
|
||||
}
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveSellCache(UUID uuid, PlayerSellData playerSellData) {
|
||||
if (exists(uuid, SqlConstants.SQL_SELECT_SELL_BY_UUID, "sellcache")) {
|
||||
updateSellData(uuid, playerSellData.getDate(), (int) playerSellData.getMoney());
|
||||
}
|
||||
else {
|
||||
insertSellData(uuid, playerSellData.getDate(), (int) playerSellData.getMoney());
|
||||
}
|
||||
}
|
||||
|
||||
private void createTableIfNotExist(String table, String sqlStat) {
|
||||
String sql = String.format(sqlStat, table);
|
||||
try {
|
||||
Connection connection = sqlConnection.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql);
|
||||
@@ -112,6 +149,20 @@ public class MySQLStorageImpl implements DataStorageInterface {
|
||||
}
|
||||
}
|
||||
|
||||
private void insertSellData(UUID uuid, int date, int money) {
|
||||
String sql = String.format(SqlConstants.SQL_INSERT_SELL, sqlConnection.getTablePrefix() + "_sellcache");
|
||||
try {
|
||||
Connection connection = sqlConnection.getConnection(); PreparedStatement statement = connection.prepareStatement(sql);
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setInt(2, date);
|
||||
statement.setInt(3, money);
|
||||
statement.executeUpdate();
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
AdventureUtil.consoleMessage("[CustomFishing] Failed to insert data for " + uuid);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateBagData(UUID uuid, int size, String contents) {
|
||||
String sql = String.format(SqlConstants.SQL_UPDATE_BAG_BY_UUID, sqlConnection.getTablePrefix() + "_fishingbag");
|
||||
try {
|
||||
@@ -126,8 +177,22 @@ public class MySQLStorageImpl implements DataStorageInterface {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean exists(UUID uuid) {
|
||||
String sql = String.format(SqlConstants.SQL_SELECT_BAG_BY_UUID, sqlConnection.getTablePrefix() + "_fishingbag");
|
||||
private void updateSellData(UUID uuid, int date, int money) {
|
||||
String sql = String.format(SqlConstants.SQL_UPDATE_SELL_BY_UUID, sqlConnection.getTablePrefix() + "_sellcache");
|
||||
try {
|
||||
Connection connection = sqlConnection.getConnection(); PreparedStatement statement = connection.prepareStatement(sql);
|
||||
statement.setInt(1, date);
|
||||
statement.setInt(2, money);
|
||||
statement.setString(3, uuid.toString());
|
||||
statement.executeUpdate();
|
||||
connection.close();
|
||||
} catch (SQLException ex) {
|
||||
AdventureUtil.consoleMessage("[CustomFishing] Failed to update data for " + uuid);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean exists(UUID uuid, String sqlStat, String suffix) {
|
||||
String sql = String.format(sqlStat, sqlConnection.getTablePrefix() + "_" + suffix);
|
||||
boolean exist;
|
||||
try {
|
||||
Connection connection = sqlConnection.getConnection(); PreparedStatement statement = connection.prepareStatement(sql);
|
||||
|
||||
@@ -3,7 +3,11 @@ package net.momirealms.customfishing.data.storage;
|
||||
public class SqlConstants {
|
||||
|
||||
public static final String SQL_CREATE_BAG_TABLE = "CREATE TABLE IF NOT EXISTS `%s` ( `uuid` VARCHAR(36) NOT NULL , `size` INT(8) NOT NULL , `contents` LONGTEXT NOT NULL , PRIMARY KEY (`uuid`) )";
|
||||
public static final String SQL_CREATE_SELL_TABLE = "CREATE TABLE IF NOT EXISTS `%s` ( `uuid` VARCHAR(36) NOT NULL , `date` INT(4) NOT NULL , `money` INT(12) NOT NULL , PRIMARY KEY (`uuid`) )";
|
||||
public static final String SQL_INSERT_BAG = "INSERT INTO `%s`(`uuid`, `size`, `contents`) VALUES (?, ?, ?)";
|
||||
public static final String SQL_INSERT_SELL = "INSERT INTO `%s`(`uuid`, `date`, `money`) VALUES (?, ?, ?)";
|
||||
public static final String SQL_UPDATE_BAG_BY_UUID = "UPDATE `%s` SET `size` = ?, `contents` = ? WHERE `uuid` = ?";
|
||||
public static final String SQL_UPDATE_SELL_BY_UUID = "UPDATE `%s` SET `date` = ?, `money` = ? WHERE `uuid` = ?";
|
||||
public static final String SQL_SELECT_BAG_BY_UUID = "SELECT * FROM `%s` WHERE `uuid` = ?";
|
||||
public static final String SQL_SELECT_SELL_BY_UUID = "SELECT * FROM `%s` WHERE `uuid` = ?";
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ public class EcoSkillsImpl implements SkillInterface {
|
||||
|
||||
@Override
|
||||
public void addXp(Player player, double amount) {
|
||||
ecoSkillsAPI.giveSkillExperience(player, Skills.FARMING, amount);
|
||||
ecoSkillsAPI.giveSkillExperience(player, Skills.FISHING, amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -55,7 +55,6 @@ public class BagDataManager extends Function {
|
||||
|
||||
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;
|
||||
@@ -64,17 +63,15 @@ public class BagDataManager extends Function {
|
||||
public BagDataManager() {
|
||||
dataCache = new ConcurrentHashMap<>();
|
||||
tempCache = new HashSet<>();
|
||||
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, () -> {
|
||||
DataManager dataManager = CustomFishing.plugin.getDataManager();
|
||||
for (PlayerBagData playerBagData : dataCache.values()) {
|
||||
dataStorageInterface.saveBagData(playerBagData);
|
||||
dataManager.getDataStorageInterface().saveBagData(playerBagData);
|
||||
}
|
||||
AdventureUtil.consoleMessage("[CustomFishing] Fishing bag data saving for " + dataCache.size() + " online players...");
|
||||
}, 12000, 12000);
|
||||
@@ -93,13 +90,13 @@ public class BagDataManager extends Function {
|
||||
HandlerList.unregisterAll(inventoryListener);
|
||||
HandlerList.unregisterAll(simpleListener);
|
||||
CustomFishing.protocolManager.removePacketListener(windowPacketListener);
|
||||
for (PlayerBagData playerBagData : dataCache.values()) {
|
||||
dataStorageInterface.saveBagData(playerBagData);
|
||||
}
|
||||
}
|
||||
|
||||
public void disable() {
|
||||
this.dataStorageInterface.disable();
|
||||
for (PlayerBagData playerBagData : dataCache.values()) {
|
||||
DataManager dataManager = CustomFishing.plugin.getDataManager();
|
||||
dataManager.getDataStorageInterface().saveBagData(playerBagData);
|
||||
}
|
||||
dataCache.clear();
|
||||
tempCache.clear();
|
||||
timerSave.cancel();
|
||||
@@ -108,7 +105,7 @@ public class BagDataManager extends Function {
|
||||
public void openFishingBag(Player viewer, OfflinePlayer ownerOffline) {
|
||||
Player owner = ownerOffline.getPlayer();
|
||||
if (owner == null) {
|
||||
Inventory inventory = dataStorageInterface.loadBagData(ownerOffline);
|
||||
Inventory inventory = CustomFishing.plugin.getDataManager().getDataStorageInterface().loadBagData(ownerOffline);
|
||||
PlayerBagData playerBagData = new PlayerBagData(ownerOffline, inventory);
|
||||
tempCache.add(playerBagData);
|
||||
viewer.openInventory(inventory);
|
||||
@@ -130,7 +127,7 @@ public class BagDataManager extends Function {
|
||||
PlayerBagData playerBagData = dataCache.remove(player.getUniqueId());
|
||||
if (playerBagData != null) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(CustomFishing.plugin, () -> {
|
||||
dataStorageInterface.saveBagData(playerBagData);
|
||||
CustomFishing.plugin.getDataManager().getDataStorageInterface().saveBagData(playerBagData);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -143,7 +140,7 @@ public class BagDataManager extends Function {
|
||||
}
|
||||
|
||||
public PlayerBagData readData(Player player) {
|
||||
Inventory inventory = dataStorageInterface.loadBagData(player);
|
||||
Inventory inventory = CustomFishing.plugin.getDataManager().getDataStorageInterface().loadBagData(player);
|
||||
PlayerBagData playerBagData = new PlayerBagData(player, inventory);
|
||||
dataCache.put(player.getUniqueId(), playerBagData);
|
||||
return playerBagData;
|
||||
@@ -204,7 +201,7 @@ public class BagDataManager extends Function {
|
||||
if (temp.getInventory() == inventory) {
|
||||
tempCache.remove(temp);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(CustomFishing.plugin, () -> {
|
||||
dataStorageInterface.saveBagData(temp);
|
||||
CustomFishing.plugin.getDataManager().getDataStorageInterface().saveBagData(temp);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,6 +136,7 @@ public class BonusManager extends Function {
|
||||
File[] files = bait_file.listFiles();
|
||||
if (files == null) return;
|
||||
for (File file : files) {
|
||||
if (!file.getName().endsWith(".yml")) continue;
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
|
||||
Set<String> keys = config.getKeys(false);
|
||||
for (String key : keys) {
|
||||
@@ -189,6 +190,7 @@ public class BonusManager extends Function {
|
||||
File[] files = rod_file.listFiles();
|
||||
if (files == null) return;
|
||||
for (File file : files) {
|
||||
if (!file.getName().endsWith(".yml")) continue;
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
|
||||
Set<String> keys = config.getKeys(false);
|
||||
for (String key : keys) {
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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 net.momirealms.customfishing.data.storage.DataStorageInterface;
|
||||
import net.momirealms.customfishing.data.storage.FileStorageImpl;
|
||||
import net.momirealms.customfishing.data.storage.MySQLStorageImpl;
|
||||
import net.momirealms.customfishing.object.Function;
|
||||
import net.momirealms.customfishing.util.ConfigUtil;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
public class DataManager extends Function {
|
||||
|
||||
private final DataStorageInterface dataStorageInterface;
|
||||
|
||||
public DataManager() {
|
||||
YamlConfiguration config = ConfigUtil.getConfig("database.yml");
|
||||
if (config.getString("data-storage-method","YAML").equalsIgnoreCase("YAML")) {
|
||||
this.dataStorageInterface = new FileStorageImpl();
|
||||
} else {
|
||||
this.dataStorageInterface = new MySQLStorageImpl();
|
||||
}
|
||||
load();
|
||||
}
|
||||
|
||||
public DataStorageInterface getDataStorageInterface() {
|
||||
return dataStorageInterface;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
this.dataStorageInterface.initialize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unload() {
|
||||
this.dataStorageInterface.disable();
|
||||
}
|
||||
}
|
||||
@@ -81,6 +81,7 @@ public class LootManager extends Function {
|
||||
File[] files = mob_file.listFiles();
|
||||
if (files == null) return;
|
||||
for (File file : files) {
|
||||
if (!file.getName().endsWith(".yml")) continue;
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
|
||||
Set<String> keys = config.getKeys(false);
|
||||
for (String key : keys) {
|
||||
@@ -123,6 +124,7 @@ public class LootManager extends Function {
|
||||
File[] files = loot_file.listFiles();
|
||||
if (files == null) return;
|
||||
for (File file : files) {
|
||||
if (!file.getName().endsWith(".yml")) continue;
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
|
||||
Set<String> keys = config.getKeys(false);
|
||||
for (String key : keys) {
|
||||
|
||||
@@ -28,8 +28,11 @@ import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import net.momirealms.customfishing.CustomFishing;
|
||||
import net.momirealms.customfishing.api.event.SellFishEvent;
|
||||
import net.momirealms.customfishing.data.PlayerBagData;
|
||||
import net.momirealms.customfishing.data.PlayerSellData;
|
||||
import net.momirealms.customfishing.integration.papi.PlaceholderManager;
|
||||
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.object.loot.Item;
|
||||
@@ -50,14 +53,13 @@ import org.bukkit.event.inventory.InventoryOpenEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
public class SellManager extends Function {
|
||||
|
||||
private final WindowPacketListener windowPacketListener;
|
||||
private final InventoryListener inventoryListener;
|
||||
private SimpleListener simpleListener;
|
||||
public static String formula;
|
||||
public static String title;
|
||||
public static int guiSize;
|
||||
@@ -82,54 +84,25 @@ public class SellManager extends Function {
|
||||
public static boolean sellLimitation;
|
||||
public static int upperLimit;
|
||||
private final HashMap<Player, Inventory> inventoryCache;
|
||||
private HashMap<String, Double> todayEarning;
|
||||
private int date;
|
||||
private final HashMap<UUID, PlayerSellData> playerCache;
|
||||
|
||||
public SellManager() {
|
||||
this.windowPacketListener = new WindowPacketListener(this);
|
||||
this.inventoryListener = new InventoryListener(this);
|
||||
this.inventoryCache = new HashMap<>();
|
||||
|
||||
this.simpleListener = new SimpleListener(this);
|
||||
this.playerCache = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
functionIconSlots = new HashSet<>();
|
||||
guiItems = new HashMap<>();
|
||||
vanillaPrices = new HashMap<>();
|
||||
loadConfig();
|
||||
CustomFishing.protocolManager.addPacketListener(windowPacketListener);
|
||||
Bukkit.getPluginManager().registerEvents(inventoryListener, CustomFishing.plugin);
|
||||
readLimitationCache();
|
||||
}
|
||||
|
||||
private void readLimitationCache() {
|
||||
this.todayEarning = new HashMap<>();
|
||||
YamlConfiguration data = ConfigUtil.readData(new File(CustomFishing.plugin.getDataFolder(), "sell-cache.yml"));
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
date = calendar.get(Calendar.DATE);
|
||||
int lastDate = data.getInt("date");
|
||||
if (lastDate == date) {
|
||||
ConfigurationSection configurationSection = data.getConfigurationSection("player_data");
|
||||
if (configurationSection != null) {
|
||||
for (String player : configurationSection.getKeys(false)) {
|
||||
todayEarning.put(player, configurationSection.getDouble(player));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void unloadLimitationCache() {
|
||||
YamlConfiguration data = new YamlConfiguration();
|
||||
data.set("date", date);
|
||||
for (Map.Entry<String, Double> entry : todayEarning.entrySet()) {
|
||||
data.set("player_data." + entry.getKey(), entry.getValue());
|
||||
}
|
||||
try {
|
||||
data.save(new File(CustomFishing.plugin.getDataFolder(), "sell-cache.yml"));
|
||||
}
|
||||
catch (IOException e) {
|
||||
AdventureUtil.consoleMessage("<red>[CustomFishing] Failed to unload earnings data!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.todayEarning.clear();
|
||||
Bukkit.getPluginManager().registerEvents(simpleListener, CustomFishing.plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -140,13 +113,41 @@ public class SellManager extends Function {
|
||||
this.inventoryCache.clear();
|
||||
CustomFishing.protocolManager.removePacketListener(windowPacketListener);
|
||||
HandlerList.unregisterAll(inventoryListener);
|
||||
if (sellLimitation) unloadLimitationCache();
|
||||
HandlerList.unregisterAll(simpleListener);
|
||||
}
|
||||
|
||||
public void disable() {
|
||||
for (Map.Entry<UUID, PlayerSellData> entry : playerCache.entrySet()) {;
|
||||
CustomFishing.plugin.getDataManager().getDataStorageInterface().saveSellCache(entry.getKey(), entry.getValue());
|
||||
}
|
||||
playerCache.clear();
|
||||
}
|
||||
|
||||
public void loadPlayerToCache(UUID uuid, int date, double money) {
|
||||
playerCache.put(uuid, new PlayerSellData(money, date));
|
||||
}
|
||||
|
||||
public void savePlayerToFile(UUID uuid) {
|
||||
PlayerSellData sellData = playerCache.remove(uuid);
|
||||
if (sellData == null) return;
|
||||
CustomFishing.plugin.getDataManager().getDataStorageInterface().saveSellCache(uuid, sellData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onQuit(Player player) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(CustomFishing.plugin, () -> {
|
||||
savePlayerToFile(player.getUniqueId());
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onJoin(Player player) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(CustomFishing.plugin, () -> {
|
||||
CustomFishing.plugin.getDataManager().getDataStorageInterface().loadSellCache(player);
|
||||
});
|
||||
}
|
||||
|
||||
private void loadConfig() {
|
||||
functionIconSlots = new HashSet<>();
|
||||
guiItems = new HashMap<>();
|
||||
vanillaPrices = new HashMap<>();
|
||||
YamlConfiguration config = ConfigUtil.getConfig("sell-fish.yml");
|
||||
formula = config.getString("price-formula", "{base} + {bonus} * {size}");
|
||||
sellLimitation = config.getBoolean("sell-limitation.enable", false);
|
||||
@@ -247,19 +248,29 @@ public class SellManager extends Function {
|
||||
if (functionIconSlots.contains(clickedSlot)) {
|
||||
List<ItemStack> playerItems = getPlayerItems(inventory);
|
||||
float totalPrice = getTotalPrice(playerItems);
|
||||
|
||||
if (totalPrice > 0) {
|
||||
|
||||
if (sellLimitation) {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
int currentDate = calendar.get(Calendar.DATE);
|
||||
if (currentDate != date) {
|
||||
date = currentDate;
|
||||
todayEarning.clear();
|
||||
}
|
||||
PlayerSellData sellData = playerCache.get(player.getUniqueId());
|
||||
|
||||
if (sellData == null) {
|
||||
inventory.close();
|
||||
AdventureUtil.playerMessage(player, MessageManager.prefix + "Internal error, please contact the server owner");
|
||||
AdventureUtil.consoleMessage("<red>[CustomFishing] Unexpected issue, " + player.getName() + "'s sell-cache is not loaded!");
|
||||
if (denyKey != null) AdventureUtil.playerSound(player, soundSource, denyKey, 1, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
double earnings = Optional.ofNullable(todayEarning.get(player.getName())).orElse(0d);
|
||||
if (sellLimitation && earnings + totalPrice > upperLimit) {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
int currentDate = calendar.get(Calendar.DATE);
|
||||
if (currentDate != sellData.getDate()) {
|
||||
sellData.setDate(currentDate);
|
||||
sellData.setMoney(0);
|
||||
}
|
||||
|
||||
double sell = sellData.getMoney();
|
||||
|
||||
if (sellLimitation && sell + totalPrice > upperLimit) {
|
||||
inventory.close();
|
||||
AdventureUtil.playerMessage(player, MessageManager.prefix + MessageManager.reachSellLimit);
|
||||
if (denyKey != null) AdventureUtil.playerSound(player, soundSource, denyKey, 1, 1);
|
||||
@@ -278,8 +289,8 @@ public class SellManager extends Function {
|
||||
playerItem.setAmount(0);
|
||||
}
|
||||
|
||||
todayEarning.put(player.getName(), earnings + totalPrice);
|
||||
doActions(player, sellFishEvent.getMoney(), upperLimit - earnings - totalPrice);
|
||||
sellData.setMoney(totalPrice + sell);
|
||||
doActions(player, sellFishEvent.getMoney(), upperLimit - sell - totalPrice);
|
||||
inventory.close();
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user