mirror of
https://github.com/Xiao-MoMi/Custom-Fishing.git
synced 2025-12-31 04:46:36 +00:00
2.0-backup-6
This commit is contained in:
@@ -75,7 +75,8 @@ dependencies {
|
||||
|
||||
tasks {
|
||||
shadowJar {
|
||||
relocate ("de.tr7zw", "net.momirealms.customfishing.libraries")
|
||||
relocate ("de.tr7zw.changeme", "net.momirealms.customfishing.libraries")
|
||||
relocate ("de.tr7zw.annotations", "net.momirealms.customfishing.libraries.annotations")
|
||||
relocate ("net.kyori", "net.momirealms.customfishing.libraries")
|
||||
relocate ("net.wesjd", "net.momirealms.customfishing.libraries")
|
||||
relocate ("org.bstats", "net.momirealms.customfishing.libraries.bstats")
|
||||
|
||||
@@ -148,6 +148,8 @@ public class CustomFishingPluginImpl extends CustomFishingPlugin {
|
||||
((CompetitionManagerImpl) this.competitionManager).unload();
|
||||
((CompetitionManagerImpl) this.competitionManager).load();
|
||||
((StorageManagerImpl) this.storageManager).reload();
|
||||
((PlaceholderManagerImpl) this.placeholderManager).unload();
|
||||
((PlaceholderManagerImpl) this.placeholderManager).load();
|
||||
this.commandManager.loadCommands();
|
||||
}
|
||||
|
||||
|
||||
@@ -18,12 +18,27 @@
|
||||
package net.momirealms.customfishing.compatibility.papi;
|
||||
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import net.momirealms.customfishing.api.CustomFishingPlugin;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class PlaceholderAPIHook extends PlaceholderExpansion {
|
||||
|
||||
private CustomFishingPlugin plugin;
|
||||
|
||||
public PlaceholderAPIHook(CustomFishingPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
super.register();
|
||||
}
|
||||
|
||||
public void unload() {
|
||||
super.unregister();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getIdentifier() {
|
||||
return "customfishing";
|
||||
@@ -46,6 +61,9 @@ public class PlaceholderAPIHook extends PlaceholderExpansion {
|
||||
|
||||
@Override
|
||||
public @Nullable String onRequest(OfflinePlayer player, @NotNull String params) {
|
||||
|
||||
|
||||
|
||||
return super.onRequest(player, params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,8 @@ import net.momirealms.customfishing.api.CustomFishingPlugin;
|
||||
import net.momirealms.customfishing.api.manager.PlaceholderManager;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -39,6 +41,7 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
|
||||
private final boolean hasPapi;
|
||||
private final Pattern pattern;
|
||||
private final HashMap<String, String> customPlaceholderMap;
|
||||
private PlaceholderAPIHook placeholderAPIHook;
|
||||
|
||||
public PlaceholderManagerImpl(CustomFishingPlugin plugin) {
|
||||
instance = this;
|
||||
@@ -46,12 +49,34 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
|
||||
this.hasPapi = Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI");
|
||||
this.pattern = Pattern.compile("\\{[^{}]+}");
|
||||
this.customPlaceholderMap = new HashMap<>();
|
||||
if (this.hasPapi) {
|
||||
placeholderAPIHook = new PlaceholderAPIHook(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
public void load() {
|
||||
if (placeholderAPIHook != null) placeholderAPIHook.load();
|
||||
loadCustomPlaceholders();
|
||||
}
|
||||
|
||||
public void unload() {
|
||||
if (placeholderAPIHook != null) placeholderAPIHook.unload();
|
||||
}
|
||||
|
||||
public void disable() {
|
||||
this.customPlaceholderMap.clear();
|
||||
}
|
||||
|
||||
public void loadCustomPlaceholders() {
|
||||
YamlConfiguration config = plugin.getConfig("config.yml");
|
||||
ConfigurationSection section = config.getConfigurationSection("other-settings.placeholder-register");
|
||||
if (section != null) {
|
||||
for (Map.Entry<String, Object> entry : section.getValues(false).entrySet()) {
|
||||
this.customPlaceholderMap.put(entry.getKey(), (String) entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String setPlaceholders(Player player, String text) {
|
||||
return hasPapi ? ParseUtils.setPlaceholders(player, text) : text;
|
||||
@@ -72,11 +97,14 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
|
||||
|
||||
@Override
|
||||
public String getSingleValue(@Nullable Player player, String placeholder, Map<String, String> placeholders) {
|
||||
String result;
|
||||
result = placeholders.get(placeholder);
|
||||
if (result != null) return result;
|
||||
String result = null;
|
||||
if (placeholders != null)
|
||||
result = placeholders.get(placeholder);
|
||||
if (result != null)
|
||||
return result;
|
||||
String custom = customPlaceholderMap.get(placeholder);
|
||||
if (custom == null) return placeholder;
|
||||
if (custom == null)
|
||||
return placeholder;
|
||||
return setPlaceholders(player, custom);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package net.momirealms.customfishing.mechanic.requirement;
|
||||
|
||||
import net.momirealms.customfishing.api.mechanic.condition.Condition;
|
||||
import net.momirealms.customfishing.api.mechanic.requirement.Requirement;
|
||||
|
||||
public class EmptyRequirement implements Requirement {
|
||||
|
||||
public static EmptyRequirement instance = new EmptyRequirement();
|
||||
|
||||
@Override
|
||||
public boolean isConditionMet(Condition condition) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -28,19 +28,16 @@ import net.momirealms.customfishing.api.mechanic.condition.Condition;
|
||||
import net.momirealms.customfishing.api.mechanic.requirement.Requirement;
|
||||
import net.momirealms.customfishing.api.mechanic.requirement.RequirementBuilder;
|
||||
import net.momirealms.customfishing.api.util.LogUtils;
|
||||
import net.momirealms.customfishing.mechanic.requirement.inbuilt.LogicRequirement;
|
||||
import net.momirealms.customfishing.compatibility.papi.ParseUtils;
|
||||
import net.momirealms.customfishing.util.ConfigUtils;
|
||||
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;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class RequirementManagerImpl implements RequirementManager {
|
||||
|
||||
@@ -96,8 +93,9 @@ public class RequirementManagerImpl implements RequirementManager {
|
||||
private void registerInbuiltRequirements() {
|
||||
this.registerTimeRequirement();
|
||||
this.registerYRequirement();
|
||||
this.registerLogicRequirement();
|
||||
this.registerCompare();
|
||||
this.registerContainRequirement();
|
||||
this.registerStartWithRequirement();
|
||||
this.registerEqualsRequirement();
|
||||
this.registerBiomeRequirement();
|
||||
this.registerDateRequirement();
|
||||
this.registerPluginLevelRequirement();
|
||||
@@ -108,6 +106,9 @@ public class RequirementManagerImpl implements RequirementManager {
|
||||
this.registerInLavaRequirement();
|
||||
this.registerRodRequirement();
|
||||
this.registerBaitRequirement();
|
||||
this.registerCompareRequirement();
|
||||
this.registerAndRequirement();
|
||||
this.registerOrRequirement();
|
||||
}
|
||||
|
||||
public ConditionalLoots getConditionalLoots(ConfigurationSection section) {
|
||||
@@ -178,8 +179,8 @@ public class RequirementManagerImpl implements RequirementManager {
|
||||
List<Action> actionList = null;
|
||||
if (advanced) {
|
||||
actionList = new ArrayList<>();
|
||||
if (section.contains("actions")) {
|
||||
for (Map.Entry<String, Object> entry : Objects.requireNonNull(section.getConfigurationSection("actions")).getValues(false).entrySet()) {
|
||||
if (section.contains("not-met-actions")) {
|
||||
for (Map.Entry<String, Object> entry : Objects.requireNonNull(section.getConfigurationSection("not-met-actions")).getValues(false).entrySet()) {
|
||||
if (entry.getValue() instanceof MemorySection inner) {
|
||||
actionList.add(plugin.getActionManager().getAction(inner));
|
||||
}
|
||||
@@ -190,11 +191,12 @@ public class RequirementManagerImpl implements RequirementManager {
|
||||
}
|
||||
String type = section.getString("type");
|
||||
if (type == null) {
|
||||
throw new NullPointerException(section.getCurrentPath() + ".type" + " doesn't exist");
|
||||
LogUtils.warn("No requirement type found at " + section.getCurrentPath());
|
||||
return EmptyRequirement.instance;
|
||||
}
|
||||
var builder = getRequirementBuilder(type);
|
||||
if (builder == null) {
|
||||
throw new NullPointerException("Requirement type: " + type + " doesn't exist");
|
||||
return EmptyRequirement.instance;
|
||||
}
|
||||
return builder.build(section.get("value"), actionList, advanced);
|
||||
}
|
||||
@@ -214,12 +216,6 @@ public class RequirementManagerImpl implements RequirementManager {
|
||||
return requirementBuilderMap.get(type);
|
||||
}
|
||||
|
||||
private void registerLogicRequirement() {
|
||||
registerRequirement("logic", (args, actions, advanced) ->
|
||||
new LogicRequirement(this, args, actions, advanced)
|
||||
);
|
||||
}
|
||||
|
||||
private void registerTimeRequirement() {
|
||||
registerRequirement("time", (args, actions, advanced) -> {
|
||||
List<Pair<Integer, Integer>> timePairs = ConfigUtils.stringListArgs(args).stream().map(this::getIntegerPair).toList();
|
||||
@@ -248,6 +244,51 @@ public class RequirementManagerImpl implements RequirementManager {
|
||||
});
|
||||
}
|
||||
|
||||
private void registerOrRequirement() {
|
||||
registerRequirement("||", (args, actions, advanced) -> {
|
||||
if (args instanceof ConfigurationSection section) {
|
||||
Requirement[] requirements = getRequirements(section, advanced);
|
||||
return condition -> {
|
||||
if (requirements == null) return true;
|
||||
for (Requirement requirement : requirements) {
|
||||
if (requirement.isConditionMet(condition)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
LogUtils.warn("Wrong value format found at || requirement.");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void registerAndRequirement() {
|
||||
registerRequirement("&&", (args, actions, advanced) -> {
|
||||
if (args instanceof ConfigurationSection section) {
|
||||
Requirement[] requirements = getRequirements(section, advanced);
|
||||
return condition -> {
|
||||
if (requirements == null) return true;
|
||||
outer: {
|
||||
for (Requirement requirement : requirements) {
|
||||
if (!requirement.isConditionMet(condition)) {
|
||||
break outer;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
LogUtils.warn("Wrong value format found at && requirement.");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void registerInLavaRequirement() {
|
||||
registerRequirement("in-lava", (args, actions, advanced) -> {
|
||||
boolean inLava = (boolean) args;
|
||||
@@ -375,6 +416,243 @@ public class RequirementManagerImpl implements RequirementManager {
|
||||
});
|
||||
}
|
||||
|
||||
private void registerCompareRequirement() {
|
||||
registerRequirement(">=", (args, actions, advanced) -> {
|
||||
if (args instanceof ConfigurationSection section) {
|
||||
String v1 = section.getString("value1", "");
|
||||
String v2 = section.getString("value2", "");
|
||||
return condition -> {
|
||||
String p1 = v1.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v1) : v1;
|
||||
String p2 = v2.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v2) : v2;
|
||||
if (Double.parseDouble(p1) >= Double.parseDouble(p2)) return true;
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
LogUtils.warn("Wrong value format found at >= requirement.");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
registerRequirement(">", (args, actions, advanced) -> {
|
||||
if (args instanceof ConfigurationSection section) {
|
||||
String v1 = section.getString("value1", "");
|
||||
String v2 = section.getString("value2", "");
|
||||
return condition -> {
|
||||
String p1 = v1.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v1) : v1;
|
||||
String p2 = v2.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v2) : v2;
|
||||
if (Double.parseDouble(p1) > Double.parseDouble(p2)) return true;
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
LogUtils.warn("Wrong value format found at > requirement.");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
registerRequirement("<", (args, actions, advanced) -> {
|
||||
if (args instanceof ConfigurationSection section) {
|
||||
String v1 = section.getString("value1", "");
|
||||
String v2 = section.getString("value2", "");
|
||||
return condition -> {
|
||||
String p1 = v1.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v1) : v1;
|
||||
String p2 = v2.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v2) : v2;
|
||||
if (Double.parseDouble(p1) < Double.parseDouble(p2)) return true;
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
LogUtils.warn("Wrong value format found at < requirement.");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
registerRequirement("<=", (args, actions, advanced) -> {
|
||||
if (args instanceof ConfigurationSection section) {
|
||||
String v1 = section.getString("value1", "");
|
||||
String v2 = section.getString("value2", "");
|
||||
return condition -> {
|
||||
String p1 = v1.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v1) : v1;
|
||||
String p2 = v2.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v2) : v2;
|
||||
if (Double.parseDouble(p1) <= Double.parseDouble(p2)) return true;
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
LogUtils.warn("Wrong value format found at <= requirement.");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
registerRequirement("==", (args, actions, advanced) -> {
|
||||
if (args instanceof ConfigurationSection section) {
|
||||
String v1 = section.getString("value1", "");
|
||||
String v2 = section.getString("value2", "");
|
||||
return condition -> {
|
||||
String p1 = v1.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v1) : v1;
|
||||
String p2 = v2.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v2) : v2;
|
||||
if (Double.parseDouble(p1) == Double.parseDouble(p2)) return true;
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
LogUtils.warn("Wrong value format found at !startsWith requirement.");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
registerRequirement("!=", (args, actions, advanced) -> {
|
||||
if (args instanceof ConfigurationSection section) {
|
||||
String v1 = section.getString("value1", "");
|
||||
String v2 = section.getString("value2", "");
|
||||
return condition -> {
|
||||
String p1 = v1.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v1) : v1;
|
||||
String p2 = v2.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v2) : v2;
|
||||
if (Double.parseDouble(p1) != Double.parseDouble(p2)) return true;
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
LogUtils.warn("Wrong value format found at !startsWith requirement.");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void registerStartWithRequirement() {
|
||||
registerRequirement("startsWith", (args, actions, advanced) -> {
|
||||
if (args instanceof ConfigurationSection section) {
|
||||
String v1 = section.getString("value1", "");
|
||||
String v2 = section.getString("value2", "");
|
||||
return condition -> {
|
||||
String p1 = v1.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v1) : v1;
|
||||
String p2 = v2.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v2) : v2;
|
||||
if (p1.startsWith(p2)) return true;
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
LogUtils.warn("Wrong value format found at startsWith requirement.");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
registerRequirement("!startsWith", (args, actions, advanced) -> {
|
||||
if (args instanceof ConfigurationSection section) {
|
||||
String v1 = section.getString("value1", "");
|
||||
String v2 = section.getString("value2", "");
|
||||
return condition -> {
|
||||
String p1 = v1.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v1) : v1;
|
||||
String p2 = v2.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v2) : v2;
|
||||
if (!p1.startsWith(p2)) return true;
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
LogUtils.warn("Wrong value format found at !startsWith requirement.");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
registerRequirement("endsWith", (args, actions, advanced) -> {
|
||||
if (args instanceof ConfigurationSection section) {
|
||||
String v1 = section.getString("value1", "");
|
||||
String v2 = section.getString("value2", "");
|
||||
return condition -> {
|
||||
String p1 = v1.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v1) : v1;
|
||||
String p2 = v2.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v2) : v2;
|
||||
if (p1.endsWith(p2)) return true;
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
LogUtils.warn("Wrong value format found at endsWith requirement.");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
registerRequirement("!endsWith", (args, actions, advanced) -> {
|
||||
if (args instanceof ConfigurationSection section) {
|
||||
String v1 = section.getString("value1", "");
|
||||
String v2 = section.getString("value2", "");
|
||||
return condition -> {
|
||||
String p1 = v1.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v1) : v1;
|
||||
String p2 = v2.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v2) : v2;
|
||||
if (!p1.endsWith(p2)) return true;
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
LogUtils.warn("Wrong value format found at !endsWith requirement.");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void registerContainRequirement() {
|
||||
registerRequirement("contains", (args, actions, advanced) -> {
|
||||
if (args instanceof ConfigurationSection section) {
|
||||
String v1 = section.getString("value1", "");
|
||||
String v2 = section.getString("value2", "");
|
||||
return condition -> {
|
||||
String p1 = v1.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v1) : v1;
|
||||
String p2 = v2.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v2) : v2;
|
||||
if (p1.contains(p2)) return true;
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
LogUtils.warn("Wrong value format found at contains requirement.");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
registerRequirement("!contains", (args, actions, advanced) -> {
|
||||
if (args instanceof ConfigurationSection section) {
|
||||
String v1 = section.getString("value1", "");
|
||||
String v2 = section.getString("value2", "");
|
||||
return condition -> {
|
||||
String p1 = v1.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v1) : v1;
|
||||
String p2 = v2.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v2) : v2;
|
||||
if (!p1.contains(p2)) return true;
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
LogUtils.warn("Wrong value format found at !contains requirement.");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void registerEqualsRequirement() {
|
||||
registerRequirement("equals", (args, actions, advanced) -> {
|
||||
if (args instanceof ConfigurationSection section) {
|
||||
String v1 = section.getString("value1", "");
|
||||
String v2 = section.getString("value2", "");
|
||||
return condition -> {
|
||||
String p1 = v1.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v1) : v1;
|
||||
String p2 = v2.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v2) : v2;
|
||||
if (p1.equals(p2)) return true;
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
LogUtils.warn("Wrong value format found at equals requirement.");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
registerRequirement("!equals", (args, actions, advanced) -> {
|
||||
if (args instanceof ConfigurationSection section) {
|
||||
String v1 = section.getString("value1", "");
|
||||
String v2 = section.getString("value2", "");
|
||||
return condition -> {
|
||||
String p1 = v1.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v1) : v1;
|
||||
String p2 = v2.startsWith("%") ? ParseUtils.setPlaceholders(condition.getPlayer(), v2) : v2;
|
||||
if (!p1.equals(p2)) return true;
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
LogUtils.warn("Wrong value format found at !equals requirement.");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void registerRodRequirement() {
|
||||
registerRequirement("rod", (args, actions, advanced) -> {
|
||||
List<String> rods = ConfigUtils.stringListArgs(args);
|
||||
@@ -434,17 +712,10 @@ public class RequirementManagerImpl implements RequirementManager {
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
LogUtils.warn("Wrong value format found at plugin-level requirement.");
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private void registerCompare() {
|
||||
registerRequirement("compare", (args, actions, advanced) -> condition -> {
|
||||
if (evaluateExpression((String) args, condition.getPlayer()))
|
||||
return true;
|
||||
if (advanced) triggerActions(actions, condition);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -453,40 +724,4 @@ public class RequirementManagerImpl implements RequirementManager {
|
||||
for (Action action : actions)
|
||||
action.trigger(condition);
|
||||
}
|
||||
|
||||
private double doubleArg(String s, Player player) {
|
||||
double arg = 0;
|
||||
try {
|
||||
arg = Double.parseDouble(s);
|
||||
} catch (NumberFormatException e1) {
|
||||
try {
|
||||
arg = Double.parseDouble(plugin.getPlaceholderManager().setPlaceholders(player, s));
|
||||
} catch (NumberFormatException e2) {
|
||||
LogUtils.severe(String.format("Invalid placeholder %s", s), e2);
|
||||
}
|
||||
}
|
||||
return arg;
|
||||
}
|
||||
|
||||
public boolean evaluateExpression(String input, Player player) {
|
||||
input = input.replace("\\s", "");
|
||||
Pattern pattern = Pattern.compile("(-?\\d+\\.?\\d*)(==|!=|<=?|>=?)(-?\\d+\\.?\\d*)");
|
||||
Matcher matcher = pattern.matcher(input);
|
||||
if (matcher.matches()) {
|
||||
double num1 = doubleArg(matcher.group(1), player);
|
||||
String operator = matcher.group(2);
|
||||
double num2 = doubleArg(matcher.group(3), player);
|
||||
return switch (operator) {
|
||||
case ">" -> num1 > num2;
|
||||
case "<" -> num1 < num2;
|
||||
case ">=" -> num1 >= num2;
|
||||
case "<=" -> num1 <= num2;
|
||||
case "==" -> num1 == num2;
|
||||
case "!=" -> num1 != num2;
|
||||
default -> throw new IllegalArgumentException("Unsupported operator: " + operator);
|
||||
};
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid input format: " + input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,134 +0,0 @@
|
||||
/*
|
||||
* 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.mechanic.requirement.inbuilt;
|
||||
|
||||
import net.momirealms.customfishing.api.manager.RequirementManager;
|
||||
import net.momirealms.customfishing.api.mechanic.action.Action;
|
||||
import net.momirealms.customfishing.api.mechanic.condition.Condition;
|
||||
import net.momirealms.customfishing.api.mechanic.requirement.Requirement;
|
||||
import net.momirealms.customfishing.mechanic.requirement.AbstractRequirement;
|
||||
import org.bukkit.configuration.MemorySection;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class LogicRequirement extends AbstractRequirement {
|
||||
|
||||
private List<Requirement> requirementList;
|
||||
private boolean checkAction;
|
||||
|
||||
public LogicRequirement(RequirementManager requirementManager, Object args, List<Action> actions, boolean checkAction) {
|
||||
super(actions);
|
||||
if (!(args instanceof MemorySection section1))
|
||||
return;
|
||||
this.requirementList = new ArrayList<>();
|
||||
this.checkAction = checkAction;
|
||||
Deque<StackElement> stack = new ArrayDeque<>();
|
||||
stack.push(new StackElement(section1.getValues(false), requirementList));
|
||||
while (!stack.isEmpty()) {
|
||||
StackElement stackElement = stack.pop();
|
||||
Map<String, Object> currentMap = stackElement.getMap();
|
||||
List<Requirement> currentResult = stackElement.getRequirements();
|
||||
for (Map.Entry<String, Object> entry : currentMap.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
Object value = entry.getValue();
|
||||
if (value instanceof MemorySection section2) {
|
||||
Map<String, Object> sectionMap = section2.getValues(false);
|
||||
if (key.startsWith("&&")) {
|
||||
List<Requirement> andReqList = new ArrayList<>();
|
||||
currentResult.add(new AndRequirement(andReqList));
|
||||
stack.push(new StackElement(sectionMap, andReqList));
|
||||
} else if (key.startsWith("||")) {
|
||||
List<Requirement> orReqList = new ArrayList<>();
|
||||
currentResult.add(new OrRequirement(orReqList));
|
||||
stack.push(new StackElement(sectionMap, orReqList));
|
||||
} else {
|
||||
currentResult.add(requirementManager.getRequirement(section2, checkAction));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class StackElement {
|
||||
|
||||
private final Map<String, Object> map;
|
||||
private final List<Requirement> requirements;
|
||||
|
||||
public StackElement(Map<String, Object> map, List<Requirement> requirements) {
|
||||
this.map = map;
|
||||
this.requirements = requirements;
|
||||
}
|
||||
|
||||
public Map<String, Object> getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public List<Requirement> getRequirements() {
|
||||
return requirements;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConditionMet(Condition condition) {
|
||||
for (Requirement requirement : requirementList) {
|
||||
if (!requirement.isConditionMet(condition)) {
|
||||
if (checkAction) super.triggerActions(condition);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static class AndRequirement implements Requirement {
|
||||
|
||||
private final List<Requirement> requirementList;
|
||||
|
||||
public AndRequirement(List<Requirement> requirementList) {
|
||||
this.requirementList = requirementList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConditionMet(Condition condition) {
|
||||
for (Requirement requirement : requirementList) {
|
||||
if (!requirement.isConditionMet(condition)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static class OrRequirement implements Requirement {
|
||||
|
||||
private final List<Requirement> requirementList;
|
||||
|
||||
public OrRequirement(List<Requirement> requirementList) {
|
||||
this.requirementList = requirementList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConditionMet(Condition condition) {
|
||||
for (Requirement requirement : requirementList) {
|
||||
if (requirement.isConditionMet(condition)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -131,7 +131,7 @@ other-settings:
|
||||
|
||||
# Requires PlaceholderAPI to work
|
||||
placeholder-register:
|
||||
'{date}': '%server_date%'
|
||||
'{date}': '%server_time_yyyy-MM-dd-HH:mm:ss%'
|
||||
|
||||
# CustomFishing supports using items from other plugins
|
||||
# If items share the same id, they would inherit the effects
|
||||
|
||||
Reference in New Issue
Block a user