mirror of
https://github.com/Xiao-MoMi/Custom-Fishing.git
synced 2026-01-06 15:51:50 +00:00
1.3.0-beta-2
This commit is contained in:
@@ -42,6 +42,7 @@ import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -55,22 +56,25 @@ public class BagDataManager extends Function {
|
||||
private final JoinQuitListener joinQuitListener;
|
||||
private final BukkitTask timerSave;
|
||||
private final CustomFishing plugin;
|
||||
private final HashMap<UUID, Integer> triedTimes;
|
||||
|
||||
public BagDataManager(CustomFishing plugin) {
|
||||
this.plugin = plugin;
|
||||
this.dataMap = new ConcurrentHashMap<>();
|
||||
this.tempData = new HashSet<>();
|
||||
this.triedTimes = new HashMap<>();
|
||||
|
||||
this.inventoryListener = new InventoryListener(this);
|
||||
this.windowPacketListener = new WindowPacketListener(this);
|
||||
this.joinQuitListener = new JoinQuitListener(this);
|
||||
|
||||
this.timerSave = Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, () -> {
|
||||
AdventureUtil.consoleMessage("[CustomFishing] Saving Fishing bag data...");
|
||||
DataManager dataManager = plugin.getDataManager();
|
||||
for (PlayerBagData playerBagData : dataMap.values()) {
|
||||
dataManager.getDataStorageInterface().saveBagData(playerBagData);
|
||||
dataManager.getDataStorageInterface().saveBagData(playerBagData, false);
|
||||
}
|
||||
AdventureUtil.consoleMessage("[CustomFishing] Fishing bag data saving for " + dataMap.size() + " online players...");
|
||||
AdventureUtil.consoleMessage("[CustomFishing] Fishing bag data saved for " + dataMap.size() + " online players.");
|
||||
}, 12000, 12000);
|
||||
}
|
||||
|
||||
@@ -93,7 +97,7 @@ public class BagDataManager extends Function {
|
||||
unload();
|
||||
for (PlayerBagData playerBagData : dataMap.values()) {
|
||||
DataManager dataManager = CustomFishing.getInstance().getDataManager();
|
||||
dataManager.getDataStorageInterface().saveBagData(playerBagData);
|
||||
dataManager.getDataStorageInterface().saveBagData(playerBagData, true);
|
||||
}
|
||||
dataMap.clear();
|
||||
tempData.clear();
|
||||
@@ -104,21 +108,29 @@ public class BagDataManager extends Function {
|
||||
return dataMap.get(uuid);
|
||||
}
|
||||
|
||||
public void openFishingBag(Player viewer, OfflinePlayer ownerOffline) {
|
||||
public void openFishingBag(Player viewer, OfflinePlayer ownerOffline, boolean force) {
|
||||
Player owner = ownerOffline.getPlayer();
|
||||
//not online
|
||||
if (owner == null) {
|
||||
Inventory inventory = plugin.getDataManager().getDataStorageInterface().loadBagData(ownerOffline);
|
||||
Inventory inventory = plugin.getDataManager().getDataStorageInterface().loadBagData(ownerOffline, force);
|
||||
if (inventory == null) {
|
||||
AdventureUtil.playerMessage(viewer, "<red>[CustomFishing] Failed to load bag data for player " + ownerOffline.getName());
|
||||
AdventureUtil.playerMessage(viewer, "<red>This might be caused when the target player is online but on another server");
|
||||
AdventureUtil.playerMessage(viewer, "<red>Use /fishingbag open [Player] --force to ignore this warning");
|
||||
return;
|
||||
}
|
||||
PlayerBagData playerBagData = new PlayerBagData(ownerOffline, inventory);
|
||||
tempData.add(playerBagData);
|
||||
viewer.openInventory(inventory);
|
||||
}
|
||||
//online
|
||||
else {
|
||||
PlayerBagData playerBagData = dataMap.get(owner.getUniqueId());
|
||||
if (playerBagData == null) {
|
||||
AdventureUtil.consoleMessage("<red>[CustomFishing] Bag data is not loaded for player " + owner.getName());
|
||||
}
|
||||
else {
|
||||
tryOpen(owner, viewer, playerBagData);
|
||||
openGui(owner, viewer, playerBagData);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -126,9 +138,10 @@ public class BagDataManager extends Function {
|
||||
@Override
|
||||
public void onQuit(Player player) {
|
||||
PlayerBagData playerBagData = dataMap.remove(player.getUniqueId());
|
||||
triedTimes.remove(player.getUniqueId());
|
||||
if (playerBagData != null) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
plugin.getDataManager().getDataStorageInterface().saveBagData(playerBagData);
|
||||
plugin.getDataManager().getDataStorageInterface().saveBagData(playerBagData, true);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -136,17 +149,32 @@ public class BagDataManager extends Function {
|
||||
@Override
|
||||
public void onJoin(Player player) {
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, () -> {
|
||||
readData(player);
|
||||
}, 20);
|
||||
joinReadData(player, false);
|
||||
}, 15);
|
||||
}
|
||||
|
||||
public void readData(Player player) {
|
||||
public void joinReadData(Player player, boolean force) {
|
||||
if (player == null || !player.isOnline()) return;
|
||||
Inventory inventory = plugin.getDataManager().getDataStorageInterface().loadBagData(player);
|
||||
Inventory inventory = plugin.getDataManager().getDataStorageInterface().loadBagData(player, force);
|
||||
if (inventory != null) {
|
||||
PlayerBagData playerBagData = new PlayerBagData(player, inventory);
|
||||
dataMap.put(player.getUniqueId(), playerBagData);
|
||||
}
|
||||
// If sql exception or data is locked
|
||||
else if (!force) {
|
||||
// can still try to load
|
||||
if (!checkTriedTimes(player.getUniqueId())) {
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, () -> {
|
||||
joinReadData(player, false);
|
||||
}, 20);
|
||||
}
|
||||
// tried 3 times
|
||||
else {
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, () -> {
|
||||
joinReadData(player, true);
|
||||
}, 20);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -214,14 +242,14 @@ public class BagDataManager extends Function {
|
||||
if (temp.getInventory() == inventory) {
|
||||
tempData.remove(temp);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
plugin.getDataManager().getDataStorageInterface().saveBagData(temp);
|
||||
plugin.getDataManager().getDataStorageInterface().saveBagData(temp, true);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void tryOpen(Player owner, Player viewer, PlayerBagData playerBagData) {
|
||||
public void openGui(Player owner, Player viewer, PlayerBagData playerBagData) {
|
||||
Inventory inventory = playerBagData.getInventory();
|
||||
int size = 1;
|
||||
for (int i = 6; i > 1; i--) {
|
||||
@@ -241,4 +269,20 @@ public class BagDataManager extends Function {
|
||||
viewer.openInventory(inventory);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean checkTriedTimes(UUID uuid) {
|
||||
Integer previous = triedTimes.get(uuid);
|
||||
if (previous == null) {
|
||||
triedTimes.put(uuid, 1);
|
||||
return false;
|
||||
}
|
||||
else if (previous > 2) {
|
||||
triedTimes.remove(uuid);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
triedTimes.put(uuid, previous + 1);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,11 +49,13 @@ public class ConfigManager {
|
||||
public static boolean enableFishingBag;
|
||||
public static boolean alwaysFishingBar;
|
||||
public static boolean allRodsFishInLava;
|
||||
public static boolean enableSuccessTitle;
|
||||
public static String[] successTitle;
|
||||
public static String[] successSubTitle;
|
||||
public static int successFadeIn;
|
||||
public static int successFadeStay;
|
||||
public static int successFadeOut;
|
||||
public static boolean enableFailureTitle;
|
||||
public static String[] failureTitle;
|
||||
public static String[] failureSubTitle;
|
||||
public static int failureFadeIn;
|
||||
@@ -127,6 +129,8 @@ public class ConfigManager {
|
||||
}
|
||||
|
||||
private static void loadTitle(YamlConfiguration config) {
|
||||
enableSuccessTitle = config.getBoolean("titles.success.enable", true);
|
||||
enableFailureTitle = config.getBoolean("titles.failure.enable", true);
|
||||
successTitle = config.getStringList("titles.success.title").toArray(new String[0]);
|
||||
successSubTitle = config.getStringList("titles.success.subtitle").toArray(new String[0]);
|
||||
successFadeIn = config.getInt("titles.success.fade.in", 10) * 50;
|
||||
|
||||
@@ -589,7 +589,7 @@ public class FishingManager extends Function {
|
||||
if (itemStack.getType() == Material.AIR) return;
|
||||
Entity item = location.getWorld().dropItem(location, itemStack);
|
||||
Vector vector = player.getLocation().subtract(location).toVector().multiply(0.1);
|
||||
vector = vector.setY((vector.getY()+0.18) * 1.15);
|
||||
vector = vector.setY((vector.getY()+0.2) * 1.15);
|
||||
item.setVelocity(vector);
|
||||
if (isDouble) {
|
||||
Entity item2 = location.getWorld().dropItem(location, itemStack);
|
||||
@@ -674,21 +674,25 @@ public class FishingManager extends Function {
|
||||
}
|
||||
|
||||
private void sendSuccessTitle(Player player, String loot) {
|
||||
AdventureUtil.playerTitle(
|
||||
player,
|
||||
ConfigManager.successTitle[new Random().nextInt(ConfigManager.successTitle.length)]
|
||||
.replace("{loot}", loot)
|
||||
.replace("{player}", player.getName()),
|
||||
ConfigManager.successSubTitle[new Random().nextInt(ConfigManager.successSubTitle.length)]
|
||||
.replace("{loot}", loot)
|
||||
.replace("{player}", player.getName()),
|
||||
ConfigManager.successFadeIn,
|
||||
ConfigManager.successFadeStay,
|
||||
ConfigManager.successFadeOut
|
||||
);
|
||||
if (!ConfigManager.enableSuccessTitle) return;
|
||||
Bukkit.getScheduler().runTaskLater(plugin, () -> {
|
||||
AdventureUtil.playerTitle(
|
||||
player,
|
||||
ConfigManager.successTitle[new Random().nextInt(ConfigManager.successTitle.length)]
|
||||
.replace("{loot}", loot)
|
||||
.replace("{player}", player.getName()),
|
||||
ConfigManager.successSubTitle[new Random().nextInt(ConfigManager.successSubTitle.length)]
|
||||
.replace("{loot}", loot)
|
||||
.replace("{player}", player.getName()),
|
||||
ConfigManager.successFadeIn,
|
||||
ConfigManager.successFadeStay,
|
||||
ConfigManager.successFadeOut
|
||||
);
|
||||
}, 8);
|
||||
}
|
||||
|
||||
private void sendSuccessTitle(Player player, ItemStack itemStack) {
|
||||
if (!ConfigManager.enableSuccessTitle) return;
|
||||
String title = ConfigManager.successTitle[new Random().nextInt(ConfigManager.successTitle.length)];
|
||||
Component titleComponent = getTitleComponent(itemStack, title);
|
||||
String subTitle = ConfigManager.successSubTitle[new Random().nextInt(ConfigManager.successSubTitle.length)];
|
||||
@@ -734,6 +738,7 @@ public class FishingManager extends Function {
|
||||
action.doOn(player, null);
|
||||
}
|
||||
|
||||
if (!ConfigManager.enableFailureTitle) return;
|
||||
Bukkit.getScheduler().runTaskLater(plugin, () -> {
|
||||
AdventureUtil.playerTitle(
|
||||
player,
|
||||
|
||||
@@ -85,16 +85,18 @@ public class SellManager extends Function {
|
||||
public static HashMap<Material, Float> vanillaPrices = new HashMap<>();
|
||||
public static boolean sellLimitation;
|
||||
public static int upperLimit;
|
||||
private final HashMap<Player, Inventory> inventoryCache;
|
||||
private final HashMap<UUID, PlayerSellData> playerCache;
|
||||
private final HashMap<Player, Inventory> inventoryMap;
|
||||
private final HashMap<UUID, PlayerSellData> sellDataMap;
|
||||
private final HashMap<UUID, Integer> triedTimes;
|
||||
|
||||
public SellManager(CustomFishing plugin) {
|
||||
this.plugin = plugin;
|
||||
this.windowPacketListener = new WindowPacketListener(this);
|
||||
this.inventoryListener = new InventoryListener(this);
|
||||
this.inventoryCache = new HashMap<>();
|
||||
this.inventoryMap = new HashMap<>();
|
||||
this.joinQuitListener = new JoinQuitListener(this);
|
||||
this.playerCache = new HashMap<>();
|
||||
this.sellDataMap = new HashMap<>();
|
||||
this.triedTimes = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -110,10 +112,10 @@ public class SellManager extends Function {
|
||||
|
||||
@Override
|
||||
public void unload() {
|
||||
for (Player player : this.inventoryCache.keySet()) {
|
||||
for (Player player : this.inventoryMap.keySet()) {
|
||||
player.closeInventory();
|
||||
}
|
||||
this.inventoryCache.clear();
|
||||
this.inventoryMap.clear();
|
||||
CustomFishing.getProtocolManager().removePacketListener(windowPacketListener);
|
||||
HandlerList.unregisterAll(inventoryListener);
|
||||
HandlerList.unregisterAll(joinQuitListener);
|
||||
@@ -122,37 +124,52 @@ public class SellManager extends Function {
|
||||
public void disable() {
|
||||
unload();
|
||||
DataStorageInterface dataStorage = plugin.getDataManager().getDataStorageInterface();
|
||||
for (Map.Entry<UUID, PlayerSellData> entry : playerCache.entrySet()) {
|
||||
dataStorage.saveSellCache(entry.getKey(), entry.getValue());
|
||||
for (Map.Entry<UUID, PlayerSellData> entry : sellDataMap.entrySet()) {
|
||||
dataStorage.saveSellData(entry.getKey(), entry.getValue(), true);
|
||||
}
|
||||
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;
|
||||
plugin.getDataManager().getDataStorageInterface().saveSellCache(uuid, sellData);
|
||||
sellDataMap.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onQuit(Player player) {
|
||||
UUID uuid = player.getUniqueId();
|
||||
PlayerSellData sellData = sellDataMap.remove(uuid);
|
||||
if (sellData == null) return;
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
savePlayerToFile(player.getUniqueId());
|
||||
plugin.getDataManager().getDataStorageInterface().saveSellData(uuid, sellData, true);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onJoin(Player player) {
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, () -> {
|
||||
if (player == null || !player.isOnline()) return;
|
||||
plugin.getDataManager().getDataStorageInterface().loadSellCache(player);
|
||||
joinReadData(player, false);
|
||||
}, 20);
|
||||
}
|
||||
|
||||
public void joinReadData(Player player, boolean force) {
|
||||
if (player == null || !player.isOnline()) return;
|
||||
PlayerSellData sellData = plugin.getDataManager().getDataStorageInterface().loadSellData(player, force);
|
||||
if (sellData != null) {
|
||||
sellDataMap.put(player.getUniqueId(), sellData);
|
||||
}
|
||||
// If sql exception or data is locked
|
||||
else if (!force) {
|
||||
// can still try to load
|
||||
if (!checkTriedTimes(player.getUniqueId())) {
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, () -> {
|
||||
joinReadData(player, false);
|
||||
}, 20);
|
||||
}
|
||||
// tried 3 times
|
||||
else {
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, () -> {
|
||||
joinReadData(player, true);
|
||||
}, 20);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadConfig() {
|
||||
YamlConfiguration config = ConfigUtil.getConfig("sell-fish-gui.yml");
|
||||
formula = config.getString("price-formula", "{base} + {bonus} * {size}");
|
||||
@@ -223,7 +240,7 @@ public class SellManager extends Function {
|
||||
|
||||
public void openGuiForPlayer(Player player) {
|
||||
player.closeInventory();
|
||||
if (!playerCache.containsKey(player.getUniqueId())) {
|
||||
if (!sellDataMap.containsKey(player.getUniqueId())) {
|
||||
AdventureUtil.consoleMessage("<red>Sell cache is not loaded for player " + player.getName());
|
||||
return;
|
||||
}
|
||||
@@ -231,7 +248,7 @@ public class SellManager extends Function {
|
||||
for (Map.Entry<Integer, ItemStack> entry : guiItems.entrySet()) {
|
||||
inventory.setItem(entry.getKey(), entry.getValue());
|
||||
}
|
||||
inventoryCache.put(player, inventory);
|
||||
inventoryMap.put(player, inventory);
|
||||
player.openInventory(inventory);
|
||||
if (openKey != null) AdventureUtil.playerSound(player, soundSource, openKey, 1, 1);
|
||||
}
|
||||
@@ -239,7 +256,7 @@ public class SellManager extends Function {
|
||||
@Override
|
||||
public void onOpenInventory(InventoryOpenEvent event) {
|
||||
final Player player = (Player) event.getPlayer();
|
||||
Inventory inventory = inventoryCache.get(player);
|
||||
Inventory inventory = inventoryMap.get(player);
|
||||
if (inventory == null) return;
|
||||
if (inventory == event.getInventory()) {
|
||||
for (int slot : functionIconSlots) {
|
||||
@@ -251,7 +268,7 @@ public class SellManager extends Function {
|
||||
@Override
|
||||
public void onClickInventory(InventoryClickEvent event) {
|
||||
final Player player = (Player) event.getView().getPlayer();
|
||||
Inventory inventory = inventoryCache.get(player);
|
||||
Inventory inventory = inventoryMap.get(player);
|
||||
if (inventory == null) return;
|
||||
boolean update = true;
|
||||
if (inventory == event.getClickedInventory()) {
|
||||
@@ -265,7 +282,7 @@ public class SellManager extends Function {
|
||||
|
||||
if (totalPrice > 0) {
|
||||
|
||||
PlayerSellData sellData = playerCache.get(player.getUniqueId());
|
||||
PlayerSellData sellData = sellDataMap.get(player.getUniqueId());
|
||||
|
||||
if (sellData == null) {
|
||||
inventory.close();
|
||||
@@ -329,7 +346,7 @@ public class SellManager extends Function {
|
||||
@Override
|
||||
public void onCloseInventory(InventoryCloseEvent event) {
|
||||
final Player player = (Player) event.getPlayer();
|
||||
Inventory inventory = inventoryCache.remove(player);
|
||||
Inventory inventory = inventoryMap.remove(player);
|
||||
if (inventory == null) return;
|
||||
if (event.getInventory() == inventory) {
|
||||
returnItems(getPlayerItems(event.getInventory()), player);
|
||||
@@ -428,4 +445,20 @@ public class SellManager extends Function {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean checkTriedTimes(UUID uuid) {
|
||||
Integer previous = triedTimes.get(uuid);
|
||||
if (previous == null) {
|
||||
triedTimes.put(uuid, 1);
|
||||
return false;
|
||||
}
|
||||
else if (previous > 2) {
|
||||
triedTimes.remove(uuid);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
triedTimes.put(uuid, previous + 1);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user