mirror of
https://github.com/Xiao-MoMi/Custom-Fishing.git
synced 2026-01-04 15:41:35 +00:00
fix bags & custom expression
This commit is contained in:
@@ -146,7 +146,6 @@ public class IntegrationManagerImpl implements IntegrationManager {
|
||||
if (plugin.isHookedPluginEnabled("ClueScrolls")) {
|
||||
ClueScrollsHook clueScrollsHook = new ClueScrollsHook();
|
||||
clueScrollsHook.register();
|
||||
Bukkit.getPluginManager().registerEvents(clueScrollsHook, plugin);
|
||||
hookMessage("ClueScrolls");
|
||||
}
|
||||
if (plugin.isHookedPluginEnabled("BetonQuest")) {
|
||||
|
||||
@@ -36,7 +36,7 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PlaceholderManagerImpl implements PlaceholderManager, Listener {
|
||||
public class PlaceholderManagerImpl implements PlaceholderManager {
|
||||
|
||||
private static PlaceholderManagerImpl instance;
|
||||
private final CustomFishingPlugin plugin;
|
||||
@@ -46,7 +46,6 @@ public class PlaceholderManagerImpl implements PlaceholderManager, Listener {
|
||||
private CompetitionPapi competitionPapi;
|
||||
private StatisticsPapi statisticsPapi;
|
||||
private CFPapi cfPapi;
|
||||
private final ConcurrentHashMap<UUID, CachedPlaceholder> cachedPlaceholders;
|
||||
|
||||
public PlaceholderManagerImpl(CustomFishingPlugin plugin) {
|
||||
instance = this;
|
||||
@@ -54,7 +53,6 @@ public class PlaceholderManagerImpl implements PlaceholderManager, Listener {
|
||||
this.hasPapi = Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI");
|
||||
this.pattern = Pattern.compile("\\{[^{}]+}");
|
||||
this.customPlaceholderMap = new HashMap<>();
|
||||
this.cachedPlaceholders = new ConcurrentHashMap<>();
|
||||
if (this.hasPapi) {
|
||||
competitionPapi = new CompetitionPapi(plugin);
|
||||
statisticsPapi = new StatisticsPapi(plugin);
|
||||
@@ -66,7 +64,6 @@ public class PlaceholderManagerImpl implements PlaceholderManager, Listener {
|
||||
if (competitionPapi != null) competitionPapi.load();
|
||||
if (statisticsPapi != null) statisticsPapi.load();
|
||||
if (cfPapi != null) cfPapi.load();
|
||||
Bukkit.getPluginManager().registerEvents(this, plugin);
|
||||
loadCustomPlaceholders();
|
||||
}
|
||||
|
||||
@@ -74,18 +71,12 @@ public class PlaceholderManagerImpl implements PlaceholderManager, Listener {
|
||||
if (competitionPapi != null) competitionPapi.unload();
|
||||
if (statisticsPapi != null) statisticsPapi.unload();
|
||||
if (cfPapi != null) cfPapi.unload();
|
||||
HandlerList.unregisterAll(this);
|
||||
}
|
||||
|
||||
public void disable() {
|
||||
this.customPlaceholderMap.clear();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onQuit(PlayerQuitEvent event) {
|
||||
cachedPlaceholders.remove(event.getPlayer().getUniqueId());
|
||||
}
|
||||
|
||||
public void loadCustomPlaceholders() {
|
||||
YamlConfiguration config = plugin.getConfig("config.yml");
|
||||
ConfigurationSection section = config.getConfigurationSection("other-settings.placeholder-register");
|
||||
@@ -199,25 +190,6 @@ public class PlaceholderManagerImpl implements PlaceholderManager, Listener {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a text string by replacing placeholders with their corresponding values, using caching for the player's placeholders.
|
||||
*
|
||||
* @param player The player for whom the placeholders are being resolved.
|
||||
* @param text The text string containing placeholders.
|
||||
* @return The text string with placeholders replaced by their values.
|
||||
*/
|
||||
@Override
|
||||
public String parseCacheablePlaceholders(Player player, String text) {
|
||||
var list = detectPlaceholders(text);
|
||||
CachedPlaceholder cachedPlaceholder = cachedPlaceholders.computeIfAbsent(player.getUniqueId(), k -> new CachedPlaceholder());
|
||||
for (String papi : list) {
|
||||
String custom = customPlaceholderMap.get(papi);
|
||||
if (custom != null) {
|
||||
text = text.replace(papi, cachedPlaceholder.get(player, custom));
|
||||
}
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
public static PlaceholderManagerImpl getInstance() {
|
||||
return instance;
|
||||
@@ -226,32 +198,4 @@ public class PlaceholderManagerImpl implements PlaceholderManager, Listener {
|
||||
public boolean hasPapi() {
|
||||
return hasPapi;
|
||||
}
|
||||
|
||||
public static class CachedPlaceholder {
|
||||
|
||||
private long lastUpdated;
|
||||
private final HashMap<String, String> cached;
|
||||
|
||||
public CachedPlaceholder() {
|
||||
this.lastUpdated = 0;
|
||||
this.cached = new HashMap<>();
|
||||
}
|
||||
|
||||
public synchronized String get(Player player, String custom) {
|
||||
long current = System.currentTimeMillis();
|
||||
if (current - lastUpdated > 3000) {
|
||||
lastUpdated = current;
|
||||
String parsed = ParseUtils.setPlaceholders(player, custom);
|
||||
cached.put(custom, parsed);
|
||||
return parsed;
|
||||
} else {
|
||||
String cachedStr = cached.get(custom);
|
||||
if (cachedStr != null)
|
||||
return cachedStr;
|
||||
String parsed = ParseUtils.setPlaceholders(player, custom);
|
||||
cached.put(custom, parsed);
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,14 +51,17 @@ public class ClueScrollsHook implements Listener {
|
||||
event.getAmount(),
|
||||
new ClueDataPair("id", "any")
|
||||
);
|
||||
|
||||
Loot loot = event.getLoot();
|
||||
if (loot != null)
|
||||
if (loot != null) {
|
||||
idClue.handle(
|
||||
player,
|
||||
event.getAmount(),
|
||||
new ClueDataPair("id", loot.getID())
|
||||
);
|
||||
if (loot != null && loot.getLootGroup() != null)
|
||||
}
|
||||
|
||||
if (loot != null && loot.getLootGroup() != null) {
|
||||
for (String group : event.getLoot().getLootGroup()) {
|
||||
groupClue.handle(
|
||||
player,
|
||||
@@ -66,5 +69,6 @@ public class ClueScrollsHook implements Listener {
|
||||
new ClueDataPair("group", group)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,11 +18,14 @@
|
||||
package net.momirealms.customfishing.mechanic.bag;
|
||||
|
||||
import net.momirealms.customfishing.CustomFishingPluginImpl;
|
||||
import net.momirealms.customfishing.adventure.AdventureManagerImpl;
|
||||
import net.momirealms.customfishing.api.CustomFishingPlugin;
|
||||
import net.momirealms.customfishing.api.data.user.OfflineUser;
|
||||
import net.momirealms.customfishing.api.manager.BagManager;
|
||||
import net.momirealms.customfishing.api.manager.EffectManager;
|
||||
import net.momirealms.customfishing.api.mechanic.bag.FishingBagHolder;
|
||||
import net.momirealms.customfishing.api.util.InventoryUtils;
|
||||
import net.momirealms.customfishing.compatibility.papi.PlaceholderManagerImpl;
|
||||
import net.momirealms.customfishing.setting.CFConfig;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
@@ -38,6 +41,8 @@ import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public class BagManagerImpl implements BagManager, Listener {
|
||||
@@ -81,9 +86,40 @@ public class BagManagerImpl implements BagManager, Listener {
|
||||
if (onlinePlayer == null) {
|
||||
return null;
|
||||
}
|
||||
Player player = onlinePlayer.getPlayer();
|
||||
int rows = getBagInventoryRows(player);
|
||||
Inventory bag = onlinePlayer.getHolder().getInventory();
|
||||
if (bag.getSize() != rows * 9) {
|
||||
Inventory newBag = InventoryUtils.createInventory(onlinePlayer.getHolder(), rows * 9,
|
||||
AdventureManagerImpl.getInstance().getComponentFromMiniMessage(
|
||||
PlaceholderManagerImpl.getInstance().parse(
|
||||
player, CFConfig.bagTitle, Map.of("{player}", player.getName())
|
||||
)
|
||||
));
|
||||
onlinePlayer.getHolder().setInventory(newBag);
|
||||
assert newBag != null;
|
||||
ItemStack[] newContents = new ItemStack[rows * 9];
|
||||
ItemStack[] oldContents = bag.getContents();
|
||||
for (int i = 0; i < rows * 9 && i < oldContents.length; i++) {
|
||||
newContents[i] = oldContents[i];
|
||||
}
|
||||
newBag.setContents(newContents);
|
||||
}
|
||||
return onlinePlayer.getHolder().getInventory();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBagInventoryRows(Player player) {
|
||||
int size = 1;
|
||||
for (int i = 6; i > 1; i--) {
|
||||
if (player.hasPermission("fishingbag.rows." + i)) {
|
||||
size = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates the process of editing the bag inventory of an offline player by an admin.
|
||||
*
|
||||
|
||||
@@ -261,6 +261,7 @@ public class GameManagerImpl implements GameManager {
|
||||
|
||||
@Override
|
||||
public boolean isSuccessful() {
|
||||
if (isTimeOut) return false;
|
||||
int last = progress / widthPerSection;
|
||||
return (Math.random() < successRate[last]);
|
||||
}
|
||||
|
||||
@@ -285,25 +285,15 @@ public class ConfigUtils {
|
||||
}
|
||||
case '=' -> {
|
||||
String formula = text.substring(1);
|
||||
return (player, weight) -> getExpressionValue(player, formula, Map.of("0", weight));
|
||||
return (player, weight) -> getExpressionValue(player, formula, Map.of("{0}", String.valueOf(weight)));
|
||||
}
|
||||
default -> throw new IllegalArgumentException("Invalid weight: " + text);
|
||||
}
|
||||
}
|
||||
|
||||
public static double getExpressionValue(Player player, String formula, Map<String, Double> vars) {
|
||||
boolean hasPapi = PlaceholderManagerImpl.getInstance().hasPapi();
|
||||
if (hasPapi)
|
||||
formula = PlaceholderManagerImpl.getInstance().parseCacheablePlaceholders(player, formula);
|
||||
ExpressionBuilder expressionBuilder = new ExpressionBuilder(formula);
|
||||
for (Map.Entry<String, Double> entry : vars.entrySet()) {
|
||||
expressionBuilder.variables(entry.getKey());
|
||||
}
|
||||
Expression expression = expressionBuilder.build();
|
||||
for (Map.Entry<String, Double> entry : vars.entrySet()) {
|
||||
expression.setVariable(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return expression.evaluate();
|
||||
public static double getExpressionValue(Player player, String formula, Map<String, String> vars) {
|
||||
formula = PlaceholderManagerImpl.getInstance().parse(player, formula, vars);
|
||||
return new ExpressionBuilder(formula).build().evaluate();
|
||||
}
|
||||
|
||||
public static ArrayList<String> getReadableSection(Map<String, Object> map) {
|
||||
|
||||
@@ -193,4 +193,4 @@ other-settings:
|
||||
'-16':
|
||||
'-32':
|
||||
'-64':
|
||||
'-128':
|
||||
'-128':
|
||||
@@ -9,32 +9,22 @@ depend:
|
||||
softdepend:
|
||||
- Vault
|
||||
- PlaceholderAPI
|
||||
|
||||
- RealisticSeasons
|
||||
|
||||
- ItemsAdder
|
||||
- MythicMobs
|
||||
- Oraxen
|
||||
- eco
|
||||
- MMOItems
|
||||
|
||||
- mcMMO
|
||||
- AureliumSkills
|
||||
- CustomCrops
|
||||
- MMOCore
|
||||
- EcoSkills
|
||||
|
||||
- BattlePass
|
||||
- ClueScrolls
|
||||
- BetonQuest
|
||||
|
||||
- AdvancedEnchantments
|
||||
- EcoEnchants
|
||||
|
||||
- Jobs
|
||||
- EcoJobs
|
||||
- Zaphkiel
|
||||
|
||||
permissions:
|
||||
fishingbag.user:
|
||||
default: true
|
||||
Reference in New Issue
Block a user