Transfered unlockedItems from MythicLib to MMOCore.

This commit is contained in:
Ka0rX 2023-03-29 23:35:47 +01:00
parent fc5b34a352
commit 41aad7b6a1
18 changed files with 206 additions and 62 deletions

View File

@ -57,8 +57,6 @@ import java.util.logging.Level;
public class MMOCore extends JavaPlugin {
public static MMOCore plugin;
public final static String MMOCORE_ITEM_ID = "mmocore:";
public final WaypointManager waypointManager = new WaypointManager();
public final SoundManager soundManager = new SoundManager();
public final RequestManager requestManager = new RequestManager();

View File

@ -0,0 +1,36 @@
package net.Indyuce.mmocore.api.event.unlocking;
import net.Indyuce.mmocore.api.event.PlayerDataEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
public abstract class ItemChangeEvent extends PlayerDataEvent {
private final String itemKey;
public ItemChangeEvent(PlayerData playerData, String itemKey) {
super(playerData);
this.itemKey = itemKey;
}
/**
* @return The full item key in the format <plugin-id>:<item-type-id>:<item-id>.
*/
public String getItemKey() {
return itemKey;
}
/**
* @return The item-type-id which is the first parameter in the key format <item-type-id>:<item-id>.
*/
public String getItemTypeId() {
return itemKey.split(":")[0];
}
/**
* @return The item--id which is the last parameter in the key format <item-type-id>:<item-id>.
*/
public String getItemId() {
return itemKey.split(":")[1];
}
}

View File

@ -0,0 +1,24 @@
package net.Indyuce.mmocore.api.event.unlocking;
import net.Indyuce.mmocore.api.player.PlayerData;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
public class ItemLockedEvent extends ItemChangeEvent {
private static final HandlerList handlers = new HandlerList();
public ItemLockedEvent(PlayerData playerData, String itemKey) {
super(playerData, itemKey);
}
@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -0,0 +1,22 @@
package net.Indyuce.mmocore.api.event.unlocking;
import net.Indyuce.mmocore.api.player.PlayerData;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
public class ItemUnlockedEvent extends ItemChangeEvent {
private static final HandlerList handlers = new HandlerList();
public ItemUnlockedEvent(PlayerData playerData, String itemKey) {
super(playerData, itemKey);
}
@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -12,6 +12,8 @@ import net.Indyuce.mmocore.api.SoundEvent;
import net.Indyuce.mmocore.api.event.PlayerExperienceGainEvent;
import net.Indyuce.mmocore.api.event.PlayerLevelUpEvent;
import net.Indyuce.mmocore.api.event.PlayerResourceUpdateEvent;
import net.Indyuce.mmocore.api.event.unlocking.ItemLockedEvent;
import net.Indyuce.mmocore.api.event.unlocking.ItemUnlockedEvent;
import net.Indyuce.mmocore.api.player.attribute.PlayerAttribute;
import net.Indyuce.mmocore.api.player.attribute.PlayerAttributes;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
@ -65,9 +67,6 @@ import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;
import static net.Indyuce.mmocore.MMOCore.MMOCORE_ITEM_ID;
public class PlayerData extends OfflinePlayerData implements Closable, ExperienceTableClaimer, ClassDataContainer {
/**
@ -113,7 +112,14 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
private final Map<SkillTreeNode, Integer> nodeLevels = new HashMap<>();
private final Map<String, Integer> skillTreePoints = new HashMap<>();
/**
* Saves the namespacedkey of the items that have been unlocked in the form item-type:item-key.
* This is used for:
* -Waypoints
* -Skills
* -Skill Books
*/
private final Set<String> unlockedItems= new HashSet<>();
/**
* Saves the amount of times the player has claimed some
* exp item in exp tables, for both exp tables used
@ -366,27 +372,59 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
return result;
}
/**
* @return All the unlocked items that are handled by MMOCore (the ones that MMOCore saves & load by itself.
* @return If the item is unlocked by the player
* This is used for skills that can be locked & unlocked.
* <p>
* Looks at the real value and thus remove the plugin identifier
*/
@Override
public Set<String> getMMOUnlockedItems() {
return mmoData.getUnlockedItems().stream().filter(key -> key.startsWith(MMOCORE_ITEM_ID)).collect(Collectors.toSet());
public boolean hasUnlocked(Unlockable unlockable) {
String unlockableKey = unlockable.getUnlockNamespacedKey().substring(unlockable.getUnlockNamespacedKey().indexOf(":"));
return unlockedItems
.stream()
.filter(key -> key.substring(key.indexOf(":")).equals(unlockableKey))
.collect(Collectors.toList()).size() != 0;
}
/**
* Used to change the value of the unlockedItems handled by mmocore.
* Unlocks an item for the player. This is mainly used to unlock skills.
*
* @param unlockedItems
* @return If the item was locked when calling this method.
*/
public boolean unlock(Unlockable unlockable) {
boolean wasLocked = unlockedItems.add(unlockable.getUnlockNamespacedKey());
if (wasLocked)
//Calls the event synchronously
Bukkit.getScheduler().runTask(MythicLib.plugin,
() -> Bukkit.getPluginManager().callEvent(new ItemUnlockedEvent(this, unlockable.getUnlockNamespacedKey())));
return wasLocked;
}
/**
* Locks an item for the player by removing it from the unlocked items map if it is present.
* This is mainly used to remove unlocked items when changing class or reallocating a skill tree.
*
* @return If the item was unlocked when calling this method.
*/
public boolean lock(Unlockable unlockable) {
boolean wasUnlocked = unlockedItems.remove(unlockable.getUnlockNamespacedKey());
if (wasUnlocked)
//Calls the event synchronously
Bukkit.getScheduler().runTask(MythicLib.plugin, () -> Bukkit.getPluginManager().callEvent(new ItemLockedEvent(this, unlockable.getUnlockNamespacedKey())));
return wasUnlocked;
}
public Set<String> getUnlockedItems() {
return new HashSet<>(unlockedItems);
}
public void setUnlockedItems(Set<String> unlockedItems) {
Set<String> mythicUnlockedItems = mmoData.getUnlockedItems()
.stream()
.filter((key) -> !key.startsWith(MMOCORE_ITEM_ID))
.collect(Collectors.toSet());
mythicUnlockedItems.addAll(unlockedItems);
mmoData.setUnlockedItems(mythicUnlockedItems);
unlockedItems.clear();
unlockedItems.addAll(unlockedItems);
}
@ -1134,7 +1172,7 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
profess.getSkills()
.stream()
.filter(ClassSkill::isUnlockedByDefault)
.forEach(skill -> mmoData.unlock(skill.getSkill()));
.forEach(skill ->unlock(skill.getSkill()));
}
public boolean hasSkillBound(int slot) {

View File

@ -0,0 +1,24 @@
package net.Indyuce.mmocore.api.player;
/**
* Some item that can be unlocked. All unlockables are saved in the
* same list in the player data. This useful list can be used for:
* - waypoints
* - skill tree nodes
* - skills using skill books?
* - external plugins that implement other unlockable items
*
* @see {@link PlayerData#unlock(Unlockable)} and {@link PlayerData#hasUnlocked(Unlockable)}
*/
public interface Unlockable {
/**
* Format being used is the minecraft's default namespaced
* key format, e.g "skill_tree:strength_1_5" for readability
*/
String getUnlockNamespacedKey();
void whenLocked(PlayerData playerData);
void whenUnlocked(PlayerData playerData);
}

View File

@ -124,7 +124,7 @@ public class SavedClassInformation {
data.getNodeLevels().forEach((node, level) -> nodeLevels.put(node.getFullId(), level));
data.getNodeTimesClaimed().forEach((key, val) -> nodeTimesClaimed.put(key, val));
data.mapBoundSkills().forEach((slot, skill) -> boundSkills.put(slot, skill));
data.getMMOUnlockedItems().forEach(item->unlockedItems.add(item));
data.getUnlockedItems().forEach(item->unlockedItems.add(item));
}
public int getLevel() {

View File

@ -19,11 +19,11 @@ public class UnlockSkillTrigger extends Trigger implements Removable {
@Override
public void apply(PlayerData player) {
player.getMMOPlayerData().unlock(skill);
player.unlock(skill);
}
@Override
public void remove(PlayerData playerData) {
playerData.getMMOPlayerData().lock(skill);
playerData.lock(skill);
}
}

View File

@ -111,18 +111,18 @@ public class SkillCommandTreeNode extends CommandTreeNode {
return CommandResult.FAILURE;
}
if (lock) {
if (!playerData.getMMOPlayerData().hasUnlocked(skill.getSkill())) {
if (!playerData.hasUnlocked(skill.getSkill())) {
CommandVerbose.verbose(sender, CommandVerbose.CommandType.SKILL, ChatColor.RED + "The skill " + skill.getSkill().getName() + " is already locked" + " for " + player.getName());
return CommandResult.SUCCESS;
}
playerData.getMMOPlayerData().lock(skill.getSkill());
playerData.lock(skill.getSkill());
} else {
if (playerData.getMMOPlayerData().hasUnlocked(skill.getSkill())) {
if (playerData.hasUnlocked(skill.getSkill())) {
CommandVerbose.verbose(sender, CommandVerbose.CommandType.SKILL, ChatColor.RED + "The skill " + skill.getSkill().getName() + " is already unlocked" + " for " + player.getName());
return CommandResult.SUCCESS;
}
playerData.getMMOPlayerData().unlock(skill.getSkill());
playerData.unlock(skill.getSkill());
}
CommandVerbose.verbose(sender, CommandVerbose.CommandType.SKILL, ChatColor.GOLD + "The skill " + skill.getSkill().getName() + " is now " + (lock ? "locked" : "unlocked" + " for " + player.getName()));
return CommandResult.SUCCESS;

View File

@ -324,7 +324,7 @@ public class SkillList extends EditableInventory {
super(playerData, editable);
skills = playerData.getProfess().getSkills()
.stream()
.filter((classSkill)->playerData.getMMOPlayerData().hasUnlocked(classSkill.getSkill()))
.filter((classSkill)->playerData.hasUnlocked(classSkill.getSkill()))
.collect(Collectors.toList());
skillSlots = getEditable().getByFunction("skill").getSlots();
Validate.notNull(getEditable().getByFunction("slot"), "Your skill GUI config file is out-of-date, please regenerate it.");

View File

@ -63,7 +63,7 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
updater.addData("professions", data.getCollectionSkills().toJsonString());
updater.addData("quests", data.getQuestData().toJsonString());
updater.addData("class_info", createClassInfoData(data).toString());
updater.addJSONArray("unlocked_items", data.getMMOUnlockedItems());
updater.addJSONArray("unlocked_items", data.getUnlockedItems());
if (logout)
updater.addData("is_saved", 1);
@ -90,7 +90,7 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
classinfo.addProperty("stamina", info.getStamina());
classinfo.addProperty("stellium", info.getStellium());
JsonArray array = new JsonArray();
for (String unlockedItem : playerData.getMMOUnlockedItems()) {
for (String unlockedItem : playerData.getUnlockedItems()) {
array.add(unlockedItem);
}
classinfo.add("unlocked-items", array);

View File

@ -159,7 +159,7 @@ public class YAMLPlayerDataManager extends PlayerDataManager {
data.getItemClaims().forEach((key, times) -> config.set("times-claimed." + key, times));
data.mapBoundSkills().forEach((slot, skill) -> config.set("bound-skills." + slot, skill));
config.set("unlocked-items", data.getMMOUnlockedItems().stream().collect(Collectors.toList()));
config.set("unlocked-items", data.getUnlockedItems().stream().collect(Collectors.toList()));
config.set("attribute", null);
config.createSection("attribute");
data.getAttributes().save(config.getConfigurationSection("attribute"));

View File

@ -47,5 +47,5 @@ public interface ClassDataContainer {
Map<String, Integer> getNodeTimesClaimed();
Set<String> getMMOUnlockedItems();
Set<String> getUnlockedItems();
}

View File

@ -120,7 +120,7 @@ public class DefaultPlayerData implements ClassDataContainer {
}
@Override
public Set<String> getMMOUnlockedItems() {
public Set<String> getUnlockedItems() {
return new HashSet<>();
}

View File

@ -1,10 +1,11 @@
package net.Indyuce.mmocore.skill;
import io.lumine.mythic.lib.UtilityMethods;
import io.lumine.mythic.lib.player.Unlockable;
import io.lumine.mythic.lib.skill.handler.SkillHandler;
import io.lumine.mythic.lib.skill.trigger.TriggerType;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.Unlockable;
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
import net.Indyuce.mmocore.api.util.math.formula.IntegerLinearValue;
import net.Indyuce.mmocore.api.util.math.formula.LinearValue;
@ -12,6 +13,7 @@ import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
public class RegisteredSkill implements Unlockable {
@ -33,7 +35,7 @@ public class RegisteredSkill implements Unlockable {
categories = config.getStringList("categories");
// Trigger type
triggerType = getHandler().isTriggerable() ? (config.contains("passive-type") ? TriggerType.valueOf(UtilityMethods.enumName(config.getString("passive-type"))) : TriggerType.CAST) : TriggerType.API;
if(triggerType.isPassive())
if (triggerType.isPassive())
categories.add("passive");
else
categories.add("active");
@ -60,7 +62,22 @@ public class RegisteredSkill implements Unlockable {
@Override
public String getUnlockNamespacedKey() {
return MMOCore.MMOCORE_ITEM_ID+"skill:" + handler.getId().toLowerCase();
return "skill:" + handler.getId().toLowerCase();
}
@Override
public void whenLocked(PlayerData playerData) {
playerData.mapBoundSkills()
.forEach((slot, skill) ->
{
if (skill.equals(getUnlockNamespacedKey().split(":")[1]))
playerData.unbindSkill(slot);
});
}
@Override
public void whenUnlocked(PlayerData playerData) {
}
public SkillHandler<?> getHandler() {

View File

@ -2,8 +2,9 @@ package net.Indyuce.mmocore.waypoint;
import io.lumine.mythic.lib.api.MMOLineConfig;
import io.lumine.mythic.lib.api.util.PostLoadObject;
import io.lumine.mythic.lib.player.Unlockable;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.Unlockable;
import net.Indyuce.mmocore.loot.chest.condition.Condition;
import net.Indyuce.mmocore.loot.chest.condition.ConditionInstance;
import org.apache.commons.lang.Validate;
@ -212,6 +213,16 @@ public class Waypoint extends PostLoadObject implements Unlockable {
return "waypoint:" + getId();
}
@Override
public void whenLocked(PlayerData playerData) {
}
@Override
public void whenUnlocked(PlayerData playerData) {
}
@Override
public int hashCode() {
return Objects.hash(id);

View File

@ -47,7 +47,6 @@ public class MMOCoreBukkit {
Bukkit.getPluginManager().registerEvents(new FishingListener(), plugin);
Bukkit.getPluginManager().registerEvents(new PlayerCollectStats(), plugin);
Bukkit.getPluginManager().registerEvents(new PlayerPressKeyListener(), plugin);
Bukkit.getPluginManager().registerEvents(new SkillLockingListener(),plugin);
// Bukkit.getPluginManager().registerEvents(new ClassTriggers(), plugin);
}
}

View File

@ -1,25 +0,0 @@
package net.Indyuce.mmocore.listener;
import io.lumine.mythic.lib.api.event.unlocking.ItemLockedEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
public class SkillLockingListener implements Listener {
/**
* Unbind the skill from boundSkills when it becomes locked.
*/
@EventHandler
public void onSkillLock(ItemLockedEvent event) {
if (event.getItemTypeId().equals("skill")) {
PlayerData playerData = PlayerData.get(event.getData().getUniqueId());
playerData.mapBoundSkills()
.forEach((slot, skillId) -> {
if (skillId.equalsIgnoreCase(event.getItemId()))
playerData.unbindSkill(slot);
});
}
}
}