9
0
mirror of https://github.com/Xiao-MoMi/Custom-Fishing.git synced 2025-12-27 02:49:17 +00:00

improve weight modifiers

This commit is contained in:
XiaoMoMi
2023-09-08 02:56:44 +08:00
parent 0e7bda8d4e
commit f56169c56e
11 changed files with 125 additions and 25 deletions

View File

@@ -1,3 +1,20 @@
/*
* Copyright (C) <2022> <XiaoMoMi>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.momirealms.customfishing.compatibility;
import net.momirealms.customfishing.adventure.AdventureManagerImpl;
@@ -7,10 +24,12 @@ import net.momirealms.customfishing.api.integration.LevelInterface;
import net.momirealms.customfishing.api.integration.SeasonInterface;
import net.momirealms.customfishing.api.manager.IntegrationManager;
import net.momirealms.customfishing.api.util.LogUtils;
import net.momirealms.customfishing.compatibility.block.ItemsAdderBlockImpl;
import net.momirealms.customfishing.compatibility.enchant.AdvancedEnchantmentsImpl;
import net.momirealms.customfishing.compatibility.enchant.VanillaEnchantmentsImpl;
import net.momirealms.customfishing.compatibility.item.*;
import net.momirealms.customfishing.compatibility.level.*;
import net.momirealms.customfishing.compatibility.mob.MythicMobsLibraryImpl;
import net.momirealms.customfishing.compatibility.season.CustomCropsSeasonImpl;
import net.momirealms.customfishing.compatibility.season.RealisticSeasonsImpl;
import org.bukkit.inventory.ItemStack;
@@ -42,6 +61,7 @@ public class IntegrationManagerImpl implements IntegrationManager {
public void init() {
if (plugin.isHookedPluginEnabled("ItemsAdder")) {
plugin.getItemManager().registerItemLibrary(new ItemsAdderItemImpl());
plugin.getBlockManager().registerBlockLibrary(new ItemsAdderBlockImpl());
hookMessage("ItemsAdder");
}
if (plugin.isHookedPluginEnabled("MMOItems")) {
@@ -58,6 +78,7 @@ public class IntegrationManagerImpl implements IntegrationManager {
}
if (plugin.isHookedPluginEnabled("MythicMobs")) {
plugin.getItemManager().registerItemLibrary(new MythicMobsItemImpl());
plugin.getMobManager().registerMobLibrary(new MythicMobsLibraryImpl());
hookMessage("MythicMobs");
}
if (plugin.isHookedPluginEnabled("EcoJobs")) {

View File

@@ -26,10 +26,8 @@ import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -43,6 +41,7 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
private final HashMap<String, String> customPlaceholderMap;
private CompetitionPapi competitionPapi;
private StatisticsPapi statisticsPapi;
private final ConcurrentHashMap<UUID, CachedPlaceholder> cachedPlaceholders;
public PlaceholderManagerImpl(CustomFishingPlugin plugin) {
instance = this;
@@ -50,6 +49,7 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
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);
@@ -116,7 +116,10 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
public String parse(@Nullable OfflinePlayer player, String text, Map<String, String> placeholders) {
var list = detectPlaceholders(text);
for (String papi : list) {
String replacer = placeholders.get(papi);
String replacer = null;
if (placeholders != null) {
replacer = placeholders.get(papi);
}
if (replacer == null) {
String custom = customPlaceholderMap.get(papi);
if (custom != null) {
@@ -130,6 +133,19 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
return text;
}
@Override
public String parseCacheable(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;
}
@Override
public List<String> parse(@Nullable OfflinePlayer player, List<String> list, Map<String, String> replacements) {
return list.stream()
@@ -140,4 +156,36 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
public static PlaceholderManagerImpl getInstance() {
return instance;
}
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;
}
}
}
}

View File

@@ -603,9 +603,10 @@ public class FishingManagerImpl implements Listener, FishingManager {
return null;
}
Player player = fishingPreparation.getPlayer();
for (Pair<String, Modifier> pair : initialEffect.getLootWeightModifier()) {
double previous = lootWithWeight.getOrDefault(pair.left(), 0d);
lootWithWeight.put(pair.left(), pair.right().modify(previous));
lootWithWeight.put(pair.left(), pair.right().modify(player, previous));
}
String key = WeightUtils.getRandom(lootWithWeight);

View File

@@ -25,6 +25,7 @@ import net.momirealms.customfishing.api.mechanic.loot.CFLoot;
import net.momirealms.customfishing.api.mechanic.loot.Loot;
import net.momirealms.customfishing.api.mechanic.loot.LootType;
import net.momirealms.customfishing.api.util.LogUtils;
import net.momirealms.customfishing.util.ConfigUtils;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
@@ -46,7 +47,7 @@ public class LootManagerImpl implements LootManager {
public static boolean instantGame;
public static boolean showInFinder;
public static String gameGroup;
public static String lootGroup;
public static String[] lootGroup;
}
public LootManagerImpl(CustomFishingPlugin plugin) {
@@ -140,10 +141,12 @@ public class LootManagerImpl implements LootManager {
} else {
lootMap.put(entry.getKey(), loot);
}
String group = loot.getLootGroup();
String[] group = loot.getLootGroup();
if (group != null) {
List<String> groupMembers = lootGroupMap.computeIfAbsent(group, k -> new ArrayList<>());
groupMembers.add(loot.getID());
for (String g : group) {
List<String> groupMembers = lootGroupMap.computeIfAbsent(g, k -> new ArrayList<>());
groupMembers.add(loot.getID());
}
}
}
}
@@ -156,13 +159,14 @@ public class LootManagerImpl implements LootManager {
.instantGame(section.getBoolean("instant-game", GlobalSetting.instantGame))
.showInFinder(section.getBoolean("show-in-fishfinder", GlobalSetting.showInFinder))
.gameConfig(section.getString("game-group", GlobalSetting.gameGroup))
.lootGroup(section.getString("loot-group", GlobalSetting.lootGroup))
.lootGroup(ConfigUtils.stringListArgs(Optional.ofNullable(section.get("loot-group")).orElse(GlobalSetting.lootGroup)).toArray(new String[0]))
.nick(section.getString("nick", section.getString("display.name", key)))
.addActions(getActionMap(section.getConfigurationSection("events")))
.addTimesActions(getTimesActionMap(section.getConfigurationSection("events.success-times")))
.build();
}
private HashMap<ActionTrigger, Action[]> getActionMap(ConfigurationSection section) {
HashMap<ActionTrigger, Action[]> actionMap = new HashMap<>();
if (section == null) return actionMap;

View File

@@ -21,6 +21,7 @@ import net.momirealms.customfishing.api.common.Pair;
import net.momirealms.customfishing.api.mechanic.condition.Condition;
import net.momirealms.customfishing.api.mechanic.loot.Modifier;
import net.momirealms.customfishing.api.mechanic.requirement.Requirement;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.List;
@@ -41,10 +42,10 @@ public class ConditionalLoots {
this.subLoots = subLoots;
}
synchronized public void combine(HashMap<String, Double> weightMap) {
synchronized public void combine(Player player, HashMap<String, Double> weightMap) {
for (Pair<String, Modifier> modifierPair : this.modifierList) {
double previous = weightMap.getOrDefault(modifierPair.left(), 0d);
weightMap.put(modifierPair.left(), modifierPair.right().modify(previous));
weightMap.put(modifierPair.left(), modifierPair.right().modify(player, previous));
}
}

View File

@@ -36,6 +36,7 @@ import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemorySection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -146,11 +147,12 @@ public class RequirementManagerImpl implements RequirementManager {
HashMap<String, Double> lootWeightMap = new HashMap<>();
Queue<HashMap<String, ConditionalLoots>> lootQueue = new LinkedList<>();
lootQueue.add(conditionalLootsMap);
Player player = condition.getPlayer();
while (!lootQueue.isEmpty()) {
HashMap<String, ConditionalLoots> currentLootMap = lootQueue.poll();
for (ConditionalLoots loots : currentLootMap.values()) {
if (loots.isConditionsMet(condition)) {
loots.combine(lootWeightMap);
loots.combine(player, lootWeightMap);
if (loots.getSubLoots() != null) {
lootQueue.add(loots.getSubLoots());
}

View File

@@ -20,6 +20,9 @@ package net.momirealms.customfishing.util;
import net.momirealms.customfishing.api.common.Pair;
import net.momirealms.customfishing.api.mechanic.loot.Modifier;
import net.momirealms.customfishing.api.util.LogUtils;
import net.momirealms.customfishing.compatibility.papi.PlaceholderManagerImpl;
import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
@@ -37,6 +40,8 @@ public class ConfigUtils {
list.add(member);
} else if (object instanceof List<?> members) {
list.addAll((Collection<? extends String>) members);
} else if (object instanceof String[] strings) {
list.addAll(List.of(strings));
}
return list;
}
@@ -101,23 +106,37 @@ public class ConfigUtils {
switch (text.charAt(0)) {
case '/' -> {
double arg = Double.parseDouble(text.substring(1));
return weight -> weight / arg;
return (player, weight) -> weight / arg;
}
case '*' -> {
double arg = Double.parseDouble(text.substring(1));
return weight -> weight * arg;
return (player, weight) -> weight * arg;
}
case '-' -> {
double arg = Double.parseDouble(text.substring(1));
return weight -> weight - arg;
return (player, weight) -> weight - arg;
}
case '%' -> {
double arg = Double.parseDouble(text.substring(1));
return weight -> weight % arg;
return (player, weight) -> weight % arg;
}
case '+' -> {
double arg = Double.parseDouble(text.substring(1));
return weight -> weight + arg;
return (player, weight) -> weight + arg;
}
case '=' -> {
String formula = text.substring(1);
boolean hasPapi = PlaceholderManagerImpl.getInstance().hasPapi();
return (player, weight) -> {
String temp = formula;
if (hasPapi)
temp = PlaceholderManagerImpl.getInstance().parseCacheable(player, formula);
Expression expression = new ExpressionBuilder(temp)
.variables("0")
.build()
.setVariable("0", weight);
return expression.evaluate();
};
}
default -> throw new IllegalArgumentException("Invalid weight: " + text);
}