Skip to content

Commit

Permalink
Implement modifiers, multipliers, and requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
Archy-X committed Aug 14, 2023
1 parent e0a257f commit b364c3d
Show file tree
Hide file tree
Showing 37 changed files with 1,958 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static NamespacedId fromString(String string) {
return new NamespacedId(split[0], split[1]);
}

public static NamespacedId fromStringOrDefault(String string) {
public static NamespacedId fromDefault(String string) {
String[] split = string.split("/");
if (split.length == 1) {
return new NamespacedId(NamespacedId.AURASKILLS, split[0]); // Use default namespace if not specified
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import co.aikar.commands.PaperCommandManager;
import com.archyx.slate.Slate;
import com.archyx.slate.menu.MenuManager;
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
import dev.aurelium.auraskills.api.AuraSkillsApi;
import dev.aurelium.auraskills.bukkit.ability.BukkitAbilityManager;
import dev.aurelium.auraskills.bukkit.commands.CommandRegistrar;
Expand All @@ -18,9 +19,13 @@
import dev.aurelium.auraskills.bukkit.mana.BukkitManaAbilityManager;
import dev.aurelium.auraskills.bukkit.menus.MenuFileManager;
import dev.aurelium.auraskills.bukkit.menus.MenuRegistrar;
import dev.aurelium.auraskills.bukkit.modifier.ArmorModifierListener;
import dev.aurelium.auraskills.bukkit.modifier.ItemListener;
import dev.aurelium.auraskills.bukkit.modifier.ModifierManager;
import dev.aurelium.auraskills.bukkit.region.BukkitWorldManager;
import dev.aurelium.auraskills.bukkit.region.RegionManager;
import dev.aurelium.auraskills.bukkit.requirement.RequirementListener;
import dev.aurelium.auraskills.bukkit.requirement.RequirementManager;
import dev.aurelium.auraskills.bukkit.reward.BukkitRewardManager;
import dev.aurelium.auraskills.bukkit.scheduler.BukkitScheduler;
import dev.aurelium.auraskills.bukkit.skills.agility.AgilityAbilities;
Expand All @@ -29,6 +34,7 @@
import dev.aurelium.auraskills.bukkit.ui.BukkitUiProvider;
import dev.aurelium.auraskills.bukkit.user.BukkitUser;
import dev.aurelium.auraskills.bukkit.user.BukkitUserManager;
import dev.aurelium.auraskills.bukkit.util.armor.ArmorListener;
import dev.aurelium.auraskills.common.AuraSkillsPlugin;
import dev.aurelium.auraskills.common.ability.AbilityRegistry;
import dev.aurelium.auraskills.common.api.ApiAuraSkills;
Expand Down Expand Up @@ -110,6 +116,8 @@ public class AuraSkills extends JavaPlugin implements AuraSkillsPlugin {
private BukkitWorldManager worldManager;
private LootTableManager lootTableManager;
private ModifierManager modifierManager;
private RequirementManager requirementManager;
private boolean nbtApiEnabled;

@Override
public void onEnable() {
Expand All @@ -135,6 +143,7 @@ public void onEnable() {
traitManager = new BukkitTraitManager(this);
regionManager = new RegionManager(this);
modifierManager = new ModifierManager(this);
requirementManager = new RequirementManager(this);

// Init registries
skillRegistry = new SkillRegistry(this);
Expand Down Expand Up @@ -173,6 +182,14 @@ public void onEnable() {

// Update leaderboards
leaderboardManager.updateLeaderboards();

// Check if NBT API is supported for the version
if (MinecraftVersion.getVersion() == MinecraftVersion.UNKNOWN) {
getLogger().warning("NBT API is not yet supported for your Minecraft version, item modifier, requirement, and some other functionality is disabled!");
nbtApiEnabled = false;
} else {
nbtApiEnabled = true;
}
}

@Override
Expand Down Expand Up @@ -243,6 +260,10 @@ private void registerEvents() {
pm.registerEvents(new DamageListener(this), this);
pm.registerEvents(new BlockLootHandler(this), this);
pm.registerEvents(new FishingLootHandler(this), this);
pm.registerEvents(new RequirementListener(this), this);
pm.registerEvents(new ItemListener(this), this);
pm.registerEvents(new ArmorListener(configStringList(Option.MODIFIER_ARMOR_EQUIP_BLOCKED_MATERIALS)), this);
pm.registerEvents(new ArmorModifierListener(this), this);
}

public BukkitAudiences getAudiences() {
Expand Down Expand Up @@ -277,6 +298,10 @@ public ModifierManager getModifierManager() {
return modifierManager;
}

public RequirementManager getRequirementManager() {
return requirementManager;
}

@Override
public AuraSkillsApi getApi() {
return api;
Expand Down Expand Up @@ -455,4 +480,7 @@ public Locale getLocale(CommandSender sender) {
}
}

public boolean isNbtApiEnabled() {
return nbtApiEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private void registerContexts(PaperCommandManager manager) {
});
contexts.registerContext(Skill.class, c -> {
String arg = c.popFirstArg();
Skill skill = plugin.getSkillRegistry().get(NamespacedId.fromStringOrDefault(arg));
Skill skill = plugin.getSkillRegistry().get(NamespacedId.fromDefault(arg));
if (!skill.isEnabled()) {
Locale locale = plugin.getLocale(c.getSender());
c.getIssuer().sendMessage(plugin.getPrefix(locale) + plugin.getMsg(CommandMessage.UNKNOWN_SKILL, locale));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public ItemRegistryMenuProvider(BukkitItemRegistry itemRegistry) {

@Override
public @Nullable ItemStack getItem(String key) {
return itemRegistry.getItem(NamespacedId.fromStringOrDefault(key));
return itemRegistry.getItem(NamespacedId.fromDefault(key));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public boolean shouldUseParser(Map<?, ?> map) {

@Override
public ItemStack parseCustomItem(Map<?, ?> map) {
NamespacedId itemKey = NamespacedId.fromStringOrDefault(DataUtil.getString(map, "key"));
NamespacedId itemKey = NamespacedId.fromDefault(DataUtil.getString(map, "key"));
ItemStack item = plugin.getItemRegistry().getItem(itemKey);
if (item != null) {
return item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void loadLootTables() {
}
// Parse skill from file name
String skillName = lootTableFile.getName().replace(".yml", "");
Skill skill = plugin.getSkillRegistry().getOrNull(NamespacedId.fromStringOrDefault(skillName));
Skill skill = plugin.getSkillRegistry().getOrNull(NamespacedId.fromDefault(skillName));
if (skill == null) return;

FileConfiguration config = YamlConfiguration.loadConfiguration(lootTableFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Set<LootContext> parseContext(Map<?, ?> parentMap) {
if (parentMap.containsKey("sources")) {
List<String> sourcesList = DataUtil.getStringList(parentMap, "sources");
for (String name : sourcesList) {
NamespacedId sourceId = NamespacedId.fromStringOrDefault(name);
NamespacedId sourceId = NamespacedId.fromDefault(name);
XpSource source = plugin.getSkillManager().getSourceById(sourceId);
if (source != null) {
contexts.add(new SourceContextWrapper(source));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public AbilityContext(AuraSkills plugin) {
@Override
@Nullable
public Ability parse(String menuName, String input) {
Ability ability = plugin.getAbilityRegistry().getOrNull(NamespacedId.fromStringOrDefault(input));
Ability ability = plugin.getAbilityRegistry().getOrNull(NamespacedId.fromDefault(input));
if (ability != null && ability.isEnabled()) {
return ability;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ManaAbilityContext(AuraSkills plugin) {
@Override
@Nullable
public ManaAbility parse(String menuName, String input) {
ManaAbility manaAbility = plugin.getManaAbilityRegistry().getOrNull(NamespacedId.fromStringOrDefault(input));
ManaAbility manaAbility = plugin.getManaAbilityRegistry().getOrNull(NamespacedId.fromDefault(input));
if (manaAbility != null && manaAbility.isEnabled()) {
return manaAbility;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public SkillContext(AuraSkills plugin) {
@Nullable
@Override
public Skill parse(String menuName, String input) {
Skill skill = plugin.getSkillRegistry().getOrNull(NamespacedId.fromStringOrDefault(input));
Skill skill = plugin.getSkillRegistry().getOrNull(NamespacedId.fromDefault(input));
if (skill != null && skill.isEnabled()) {
return skill;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public SourceContext(AuraSkills plugin) {
public XpSource parse(String menuName, String input) {
String[] split = input.split(" ");
// Parse Skill from first part of input
Skill skill = plugin.getSkillRegistry().getOrNull(NamespacedId.fromStringOrDefault(split[0]));
Skill skill = plugin.getSkillRegistry().getOrNull(NamespacedId.fromDefault(split[0]));
if (skill == null || !skill.isEnabled()) {
return null;
}
// Parse Source from second part of input
NamespacedId sourceId = NamespacedId.fromStringOrDefault(split[1]);
NamespacedId sourceId = NamespacedId.fromDefault(split[1]);
// Find source from skill that matches id
return skill.getSources().stream().filter(source -> source.getId().equals(sourceId)).findFirst().orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public StatContext(AuraSkills plugin) {
@Nullable
@Override
public Stat parse(String menuName, String input) {
Stat stat = plugin.getStatRegistry().getOrNull(NamespacedId.fromStringOrDefault(input));
Stat stat = plugin.getStatRegistry().getOrNull(NamespacedId.fromDefault(input));
if (stat != null && stat.isEnabled()) {
return stat;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public String onPlaceholderReplace(String placeholder, Player player, ActiveMenu
}
User user = plugin.getUser(player);
// Handle each stat entry
Stat stat = plugin.getStatRegistry().getOrNull(NamespacedId.fromStringOrDefault(placeholder));
Stat stat = plugin.getStatRegistry().getOrNull(NamespacedId.fromDefault(placeholder));
if (stat != null) {
return TextUtil.replace(plugin.getMsg(MenuMessage.PLAYER_STAT_ENTRY, locale),
"{color}", stat.getColor(locale),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package dev.aurelium.auraskills.bukkit.modifier;

import dev.aurelium.auraskills.api.event.AuraSkillsEventHandler;
import dev.aurelium.auraskills.api.event.user.UserLoadEvent;
import dev.aurelium.auraskills.api.stat.Stat;
import dev.aurelium.auraskills.api.stat.StatModifier;
import dev.aurelium.auraskills.bukkit.AuraSkills;
import dev.aurelium.auraskills.bukkit.requirement.Requirements;
import dev.aurelium.auraskills.bukkit.user.BukkitUser;
import dev.aurelium.auraskills.bukkit.util.armor.ArmorEquipEvent;
import dev.aurelium.auraskills.bukkit.util.armor.ArmorType;
import dev.aurelium.auraskills.common.config.Option;
import dev.aurelium.auraskills.common.modifier.Multiplier;
import dev.aurelium.auraskills.common.scheduler.TaskRunnable;
import dev.aurelium.auraskills.common.stat.StatManager;
import dev.aurelium.auraskills.common.user.User;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;

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

public class ArmorModifierListener implements Listener {

private final AuraSkills plugin;
private final Modifiers modifiers;
private final Requirements requirements;
private final Multipliers multipliers;
private final StatManager statManager;
private final Map<UUID, Map<ArmorType, ItemStack>> storedArmor;

public ArmorModifierListener(AuraSkills plugin) {
this.plugin = plugin;
this.modifiers = new Modifiers(plugin);
this.requirements = new Requirements(plugin);
this.multipliers = new Multipliers(plugin);
this.statManager = plugin.getStatManager();
this.storedArmor = new HashMap<>();
if (plugin.configBoolean(Option.MODIFIER_ARMOR_TIMER_ENABLED)) {
startTimer();
}
}

@AuraSkillsEventHandler
public void onJoin(UserLoadEvent event) {
Player player = BukkitUser.getPlayer(event.getUser());
User user = BukkitUser.getUser(event.getUser());
for (ItemStack armor : player.getInventory().getArmorContents()) {
if (armor == null) {
continue;
}
if (plugin.configBoolean(Option.MODIFIER_ARMOR_TIMER_ENABLED)) {
storedArmor.computeIfAbsent(player.getUniqueId(), k -> new HashMap<>()).put(ArmorType.matchType(armor), armor.clone());
}
if (armor.getType().equals(Material.AIR)) {
continue;
}
if (requirements.meetsRequirements(ModifierType.ARMOR, armor, player)) {
for (StatModifier modifier : modifiers.getModifiers(ModifierType.ARMOR, armor)) {
user.addStatModifier(modifier, false);
}
for (Multiplier multiplier : multipliers.getMultipliers(ModifierType.ARMOR, armor)) {
user.addMultiplier(multiplier);
}
}
}
}

@EventHandler
public void onEquip(ArmorEquipEvent event) {
if (plugin.configBoolean(Option.MODIFIER_ARMOR_TIMER_ENABLED)) return; // Don't use if timer is enabled
Player player = event.getPlayer();
User user = plugin.getUser(player);
// Equip
if (event.getNewArmorPiece() != null && event.getNewArmorPiece().getType() != Material.AIR) {
ItemStack item = event.getNewArmorPiece();
if (requirements.meetsRequirements(ModifierType.ARMOR, item, player)) {
for (StatModifier modifier : modifiers.getModifiers(ModifierType.ARMOR, item)) {
user.addStatModifier(modifier);
}
for (Multiplier multiplier : multipliers.getMultipliers(ModifierType.ARMOR, item)) {
user.addMultiplier(multiplier);
}
}
}
// Un-equip
if (event.getOldArmorPiece() != null && event.getOldArmorPiece().getType() != Material.AIR) {
ItemStack item = event.getOldArmorPiece();
for (StatModifier modifier : modifiers.getModifiers(ModifierType.ARMOR, item)) {
user.removeStatModifier(modifier.name());
}
for (Multiplier multiplier : multipliers.getMultipliers(ModifierType.ARMOR, item)) {
user.removeMultiplier(multiplier.name());
}
}
}

// Timer based detection
private void startTimer() {
var task = new TaskRunnable() {
@Override
public void run() {
for (Player player : Bukkit.getOnlinePlayers()) {
UUID uuid = player.getUniqueId();
// Get the stored armor player has or create if it doesn't exist
Map<ArmorType, ItemStack> playerStoredArmor = storedArmor.computeIfAbsent(uuid, k -> new HashMap<>());
for (ArmorType armorType : ArmorType.values()) { // Go through each armor slot
ItemStack stored = playerStoredArmor.get(armorType); // Get the stored item in the slot
ItemStack wearing = player.getInventory().getItem(armorType.getEquipmentSlot()); // Get the armor player is currently wearing

boolean remove = true;
if (stored == null) {
remove = false;
} else if (stored.equals(wearing)) { // Don't check if stored and wearing are the same item
continue;
}

Set<Stat> statsToReload = new HashSet<>();
// Remove modifiers and multipliers that are on stored item from player
if (remove && stored.getType() != Material.AIR) {
User user = plugin.getUser(player);

for (StatModifier modifier : modifiers.getModifiers(ModifierType.ARMOR, stored)) {
user.removeStatModifier(modifier.name(), false);
statsToReload.add(modifier.stat());
}
for (Multiplier multiplier : multipliers.getMultipliers(ModifierType.ARMOR, stored)) {
user.removeMultiplier(multiplier.name());
}
}
// Add modifiers and multipliers that are on worn item to the player
if (wearing != null && wearing.getType() != Material.AIR) {
User user = plugin.getUser(player);

if (requirements.meetsRequirements(ModifierType.ARMOR, wearing, player)) {
for (StatModifier modifier : modifiers.getModifiers(ModifierType.ARMOR, wearing)) {
user.addStatModifier(modifier, false);
statsToReload.add(modifier.stat());
}
for (Multiplier multiplier : multipliers.getMultipliers(ModifierType.ARMOR, wearing)) {
user.addMultiplier(multiplier);
}
}
}
for (Stat stat : statsToReload) {
statManager.reloadStat(plugin.getUser(player), stat);
}
// Set stored item to worn item
if (wearing != null) {
playerStoredArmor.put(armorType, wearing.clone());
} else {
playerStoredArmor.put(armorType, new ItemStack(Material.AIR));
}
}
}
}
};
plugin.getScheduler().timerSync(task, 0L, plugin.configInt(Option.MODIFIER_ARMOR_TIMER_CHECK_PERIOD) * 50L, TimeUnit.MILLISECONDS);
}

@EventHandler
public void onLeave(PlayerQuitEvent event) {
storedArmor.remove(event.getPlayer().getUniqueId());
}

}
Loading

0 comments on commit b364c3d

Please sign in to comment.