Skip to content

Commit

Permalink
Add requirement.override_global option to main config
Browse files Browse the repository at this point in the history
- If true, global requirements will be ignored if item-specific requirements are defined
- Fix global requirements and world options not being updated on skills reload
  • Loading branch information
Archy-X committed Jun 24, 2024
1 parent 13ce134 commit 1dfc860
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public void onEnable() {
migrationManager.attemptUserMigration();
// Load blocked/disabled worlds lists
worldManager = new BukkitWorldManager(this);
worldManager.loadWorlds(getConfig());
worldManager.loadWorlds(); // Requires generateConfigs before
regionManager = new BukkitRegionManager(this);
backupProvider = new BackupProvider(this);
xpRequirements = new XpRequirements(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public void reload(CommandSender sender) {
// Load config.yml file
plugin.config().loadOptions();
plugin.getMessageProvider().loadDefaultLanguageOption();
plugin.getRequirementManager().load();
// Load blocked/disabled worlds lists
plugin.getWorldManager().loadWorlds(plugin.getConfig());
plugin.getWorldManager().loadWorlds();
// Load skills
plugin.loadSkills();
plugin.getLevelManager().loadXpRequirements();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,15 @@ public boolean meetsRequirements(ModifierType type, Player player) {
if (!plugin.configBoolean(Option.REQUIREMENT_ENABLED)) return true;
if (player.hasMetadata("NPC")) return true;
User user = plugin.getUser(player);
// Check global requirements
for (Map.Entry<Skill, Integer> entry : getGlobalRequirements(type).entrySet()) {
if (user.getSkillLevel(entry.getKey()) < entry.getValue()) {
return false;
Map<Skill, Integer> itemRequirements = getRequirements(type);

// If override_global is true, only check global if the item has no defined NBT requirements
if (!plugin.configBoolean(Option.REQUIREMENT_OVERRIDE_GLOBAL) || itemRequirements.isEmpty()) {
// Check global requirements
for (Map.Entry<Skill, Integer> entry : getGlobalRequirements(type).entrySet()) {
if (user.getSkillLevel(entry.getKey()) < entry.getValue()) {
return false;
}
}
}
// Check requirements on item
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package dev.aurelium.auraskills.bukkit.region;

import dev.aurelium.auraskills.bukkit.AuraSkills;
import dev.aurelium.auraskills.common.config.ConfigurateLoader;
import dev.aurelium.auraskills.common.region.WorldManager;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.TypeSerializerCollection;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -20,24 +24,31 @@ public BukkitWorldManager(AuraSkills plugin) {
this.plugin = plugin;
}

public void loadWorlds(FileConfiguration config) {
int blockedWorldsLoaded = 0;
blockedWorlds = new HashSet<>();
disabledWorlds = new HashSet<>();
blockedCheckBlockReplaceWorlds = new HashSet<>();
for (String blockedWorld : config.getStringList("blocked_worlds")) {
blockedWorlds.add(blockedWorld);
blockedWorldsLoaded++;
}
for (String blockedWorld : config.getStringList("disabled_worlds")) {
disabledWorlds.add(blockedWorld);
blockedWorldsLoaded++;
}
for (String blockedWorld : config.getStringList("check_block_replace.blocked_worlds")) {
blockedCheckBlockReplaceWorlds.add(blockedWorld);
blockedWorldsLoaded++;
public void loadWorlds() {
ConfigurateLoader loader = new ConfigurateLoader(plugin, TypeSerializerCollection.builder().build());
try {
ConfigurationNode config = loader.loadUserFile("config.yml");

int blockedWorldsLoaded = 0;
blockedWorlds = new HashSet<>();
disabledWorlds = new HashSet<>();
blockedCheckBlockReplaceWorlds = new HashSet<>();
for (String blockedWorld : config.node("blocked_worlds").getList(String.class, new ArrayList<>())) {
blockedWorlds.add(blockedWorld);
blockedWorldsLoaded++;
}
for (String blockedWorld : config.node("disabled_worlds").getList(String.class, new ArrayList<>())) {
disabledWorlds.add(blockedWorld);
blockedWorldsLoaded++;
}
for (String blockedWorld : config.node("check_block_replace", "blocked_worlds").getList(String.class, new ArrayList<>())) {
blockedCheckBlockReplaceWorlds.add(blockedWorld);
blockedWorldsLoaded++;
}
plugin.logger().info("Loaded " + blockedWorldsLoaded + " blocked/disabled world" + (blockedWorldsLoaded != 1 ? "s" : ""));
} catch (IOException e) {
e.printStackTrace();
}
plugin.logger().info("Loaded " + blockedWorldsLoaded + " blocked worlds.");
}

public boolean isInBlockedWorld(Location location) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ private void sendMessage(MessageKey baseMessage, MessageKey entryMessage, Modifi
requirementsString.append(TextUtil.replace(plugin.getMsg(entryMessage, locale),
"{skill}", entry.getKey().getDisplayName(locale), "{level}", RomanNumber.toRoman(entry.getValue(), plugin)));
}
Map<Skill, Integer> globalRequirementMap = skillsItem.getGlobalRequirements(modifierType);
for (Map.Entry<Skill, Integer> entry : globalRequirementMap.entrySet()) {
requirementsString.append(TextUtil.replace(plugin.getMsg(entryMessage, locale),
"{skill}", entry.getKey().getDisplayName(locale), "{level}", RomanNumber.toRoman(entry.getValue(), plugin)));
if (!plugin.configBoolean(Option.REQUIREMENT_OVERRIDE_GLOBAL) || requirementMap.isEmpty()) {
Map<Skill, Integer> globalRequirementMap = skillsItem.getGlobalRequirements(modifierType);
for (Map.Entry<Skill, Integer> entry : globalRequirementMap.entrySet()) {
requirementsString.append(TextUtil.replace(plugin.getMsg(entryMessage, locale),
"{skill}", entry.getKey().getDisplayName(locale), "{level}", RomanNumber.toRoman(entry.getValue(), plugin)));
}
}
if (requirementsString.length() >= 2) {
requirementsString.delete(requirementsString.length() - 2, requirementsString.length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import dev.aurelium.auraskills.api.registry.NamespacedId;
import dev.aurelium.auraskills.api.skill.Skill;
import dev.aurelium.auraskills.bukkit.AuraSkills;
import dev.aurelium.auraskills.common.config.ConfigurateLoader;
import dev.aurelium.auraskills.common.scheduler.TaskRunnable;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.TypeSerializerCollection;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;

Expand All @@ -28,30 +31,41 @@ public RequirementManager(AuraSkills plugin) {
}

public void load() {
FileConfiguration config = plugin.getConfig();
this.globalRequirements = new HashSet<>();
for (ModifierType type : ModifierType.values()) {
List<String> list = config.getStringList("requirement." + type.name().toLowerCase(Locale.ROOT) + ".global");
for (String text : list) {
String[] splitText = text.split(" ");
Material material = Material.valueOf(splitText[0].toUpperCase(Locale.ROOT));
Map<Skill, Integer> requirements = new HashMap<>();
for (int i = 1; i < splitText.length; i++) {
String requirementText = splitText[i];
try {
Skill skill = plugin.getSkillRegistry().getOrNull(NamespacedId.fromDefault(requirementText.split(":")[0].toLowerCase(Locale.ROOT)));
if (skill != null) {
int level = Integer.parseInt(requirementText.split(":")[1]);
requirements.put(skill, level);
ConfigurateLoader loader = new ConfigurateLoader(plugin, TypeSerializerCollection.builder().build());
try {
ConfigurationNode config = loader.loadUserFile("config.yml");

this.globalRequirements = new HashSet<>();
int loaded = 0;
for (ModifierType type : ModifierType.values()) {
List<String> list = config.node("requirement", type.name().toLowerCase(Locale.ROOT), "global").getList(String.class, new ArrayList<>());
for (String text : list) {
String[] splitText = text.split(" ");
Material material = Material.valueOf(splitText[0].toUpperCase(Locale.ROOT));
Map<Skill, Integer> requirements = new HashMap<>();
for (int i = 1; i < splitText.length; i++) {
String requirementText = splitText[i];
try {
Skill skill = plugin.getSkillRegistry().getOrNull(NamespacedId.fromDefault(requirementText.split(":")[0].toLowerCase(Locale.ROOT)));
if (skill != null) {
int level = Integer.parseInt(requirementText.split(":")[1]);
requirements.put(skill, level);
}
} catch (Exception e) {
plugin.logger().warn("Error parsing global skill " + type.name().toLowerCase(Locale.ROOT) + " requirement skill level pair with text " + requirementText);
}
} catch (Exception e) {
plugin.logger().warn("Error parsing global skill " + type.name().toLowerCase(Locale.ROOT) + " requirement skill level pair with text " + requirementText);
}
GlobalRequirement globalRequirement = new GlobalRequirement(type, material, requirements);
globalRequirements.add(globalRequirement);
loaded++;
}
GlobalRequirement globalRequirement = new GlobalRequirement(type, material, requirements);
globalRequirements.add(globalRequirement);

}
if (loaded > 0) {
plugin.logger().info("Loaded " + loaded + " global requirement" + (loaded != 1 ? "s" : ""));
}
} catch (IOException e) {
plugin.logger().warn("Error loading global requirements: " + e.getMessage());
e.printStackTrace();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public enum Option {
MODIFIER_AUTO_CONVERT_FROM_LEGACY("modifier.auto_convert_from_legacy", OptionType.BOOLEAN),
// Requirement options
REQUIREMENT_ENABLED("requirement.enabled", OptionType.BOOLEAN),
REQUIREMENT_OVERRIDE_GLOBAL("requirement.override_global", OptionType.BOOLEAN),
REQUIREMENT_ITEM_PREVENT_TOOL_USE("requirement.item.prevent_tool_use", OptionType.BOOLEAN),
REQUIREMENT_ITEM_PREVENT_WEAPON_USE("requirement.item.prevent_weapon_use", OptionType.BOOLEAN),
REQUIREMENT_ITEM_PREVENT_BLOCK_PLACE("requirement.item.prevent_block_place", OptionType.BOOLEAN),
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ modifier:
auto_convert_from_legacy: false
requirement:
enabled: true
override_global: false
item:
prevent_tool_use: true
prevent_weapon_use: true
Expand Down

0 comments on commit 1dfc860

Please sign in to comment.