diff --git a/build.gradle b/build.gradle index c1412c43..0f14e99c 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group = 'net.momirealms' -version = '1.2.10' +version = '1.2.13' repositories { mavenCentral() diff --git a/src/main/java/net/momirealms/customfishing/CustomFishing.java b/src/main/java/net/momirealms/customfishing/CustomFishing.java index c788710f..b89b9875 100644 --- a/src/main/java/net/momirealms/customfishing/CustomFishing.java +++ b/src/main/java/net/momirealms/customfishing/CustomFishing.java @@ -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; + } } diff --git a/src/main/java/net/momirealms/customfishing/commands/PluginCommand.java b/src/main/java/net/momirealms/customfishing/commands/PluginCommand.java index e4d331b1..e7c0efeb 100644 --- a/src/main/java/net/momirealms/customfishing/commands/PluginCommand.java +++ b/src/main/java/net/momirealms/customfishing/commands/PluginCommand.java @@ -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) { diff --git a/src/main/java/net/momirealms/customfishing/commands/subcmd/OpenBagCommand.java b/src/main/java/net/momirealms/customfishing/commands/subcmd/OpenBagCommand.java new file mode 100644 index 00000000..f922d514 --- /dev/null +++ b/src/main/java/net/momirealms/customfishing/commands/subcmd/OpenBagCommand.java @@ -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 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 onTabComplete(CommandSender sender, List args) { + if (!ConfigManager.enableFishingBag) return null; + if (!sender.hasPermission("customfishing.admin")) return null; + if (args.size() == 1) { + List arrayList = new ArrayList<>(); + for (String cmd : online_players()) { + if (cmd.startsWith(args.get(0))) + arrayList.add(cmd); + } + return arrayList; + } + return super.onTabComplete(sender, args); + } +} diff --git a/src/main/java/net/momirealms/customfishing/data/PlayerSellData.java b/src/main/java/net/momirealms/customfishing/data/PlayerSellData.java new file mode 100644 index 00000000..e3d2eaa7 --- /dev/null +++ b/src/main/java/net/momirealms/customfishing/data/PlayerSellData.java @@ -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; + } + +} diff --git a/src/main/java/net/momirealms/customfishing/data/storage/DataStorageInterface.java b/src/main/java/net/momirealms/customfishing/data/storage/DataStorageInterface.java index 307be88b..bf8b9cfe 100644 --- a/src/main/java/net/momirealms/customfishing/data/storage/DataStorageInterface.java +++ b/src/main/java/net/momirealms/customfishing/data/storage/DataStorageInterface.java @@ -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); } diff --git a/src/main/java/net/momirealms/customfishing/data/storage/FileStorageImpl.java b/src/main/java/net/momirealms/customfishing/data/storage/FileStorageImpl.java index 9e29e1ce..5583a248 100644 --- a/src/main/java/net/momirealms/customfishing/data/storage/FileStorageImpl.java +++ b/src/main/java/net/momirealms/customfishing/data/storage/FileStorageImpl.java @@ -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()); + } } diff --git a/src/main/java/net/momirealms/customfishing/data/storage/MySQLStorageImpl.java b/src/main/java/net/momirealms/customfishing/data/storage/MySQLStorageImpl.java index 7349db21..90d2c867 100644 --- a/src/main/java/net/momirealms/customfishing/data/storage/MySQLStorageImpl.java +++ b/src/main/java/net/momirealms/customfishing/data/storage/MySQLStorageImpl.java @@ -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); diff --git a/src/main/java/net/momirealms/customfishing/data/storage/SqlConstants.java b/src/main/java/net/momirealms/customfishing/data/storage/SqlConstants.java index 8846a356..c330910f 100644 --- a/src/main/java/net/momirealms/customfishing/data/storage/SqlConstants.java +++ b/src/main/java/net/momirealms/customfishing/data/storage/SqlConstants.java @@ -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` = ?"; } diff --git a/src/main/java/net/momirealms/customfishing/integration/skill/EcoSkillsImpl.java b/src/main/java/net/momirealms/customfishing/integration/skill/EcoSkillsImpl.java index c04b6024..1df3888c 100644 --- a/src/main/java/net/momirealms/customfishing/integration/skill/EcoSkillsImpl.java +++ b/src/main/java/net/momirealms/customfishing/integration/skill/EcoSkillsImpl.java @@ -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 diff --git a/src/main/java/net/momirealms/customfishing/manager/BagDataManager.java b/src/main/java/net/momirealms/customfishing/manager/BagDataManager.java index 22d83739..0ffd3cc4 100644 --- a/src/main/java/net/momirealms/customfishing/manager/BagDataManager.java +++ b/src/main/java/net/momirealms/customfishing/manager/BagDataManager.java @@ -55,7 +55,6 @@ public class BagDataManager extends Function { public static ConcurrentHashMap dataCache; public static HashSet 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); }); } } diff --git a/src/main/java/net/momirealms/customfishing/manager/BonusManager.java b/src/main/java/net/momirealms/customfishing/manager/BonusManager.java index 67ac2c05..de6d3666 100644 --- a/src/main/java/net/momirealms/customfishing/manager/BonusManager.java +++ b/src/main/java/net/momirealms/customfishing/manager/BonusManager.java @@ -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 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 keys = config.getKeys(false); for (String key : keys) { diff --git a/src/main/java/net/momirealms/customfishing/manager/DataManager.java b/src/main/java/net/momirealms/customfishing/manager/DataManager.java new file mode 100644 index 00000000..491c3164 --- /dev/null +++ b/src/main/java/net/momirealms/customfishing/manager/DataManager.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) <2022> + * + * 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 . + */ + +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(); + } +} diff --git a/src/main/java/net/momirealms/customfishing/manager/LootManager.java b/src/main/java/net/momirealms/customfishing/manager/LootManager.java index 86b16eea..ca2c4be0 100644 --- a/src/main/java/net/momirealms/customfishing/manager/LootManager.java +++ b/src/main/java/net/momirealms/customfishing/manager/LootManager.java @@ -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 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 keys = config.getKeys(false); for (String key : keys) { diff --git a/src/main/java/net/momirealms/customfishing/manager/SellManager.java b/src/main/java/net/momirealms/customfishing/manager/SellManager.java index eb1e7f06..1de10ca9 100644 --- a/src/main/java/net/momirealms/customfishing/manager/SellManager.java +++ b/src/main/java/net/momirealms/customfishing/manager/SellManager.java @@ -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 inventoryCache; - private HashMap todayEarning; - private int date; + private final HashMap 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 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("[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 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 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("[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 { diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 6de98fff..2709257b 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -43,7 +43,7 @@ commands: description: fishing bag command sellfish: usage: /sellfish - description: fishing bag command + description: sell fish command permission: customfishing.sellfish permissions: