Supports profiles

This commit is contained in:
Jules 2023-05-13 00:38:45 +02:00
parent b14f0e590b
commit eeef8db5ad
6 changed files with 119 additions and 92 deletions

View File

@ -174,6 +174,14 @@
<version>5.1.2-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- Profile API -->
<dependency>
<groupId>fr.phoenixdevt</groupId>
<artifactId>Profile-API</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<!-- WordGuard -->
<dependency>
<groupId>com.sk89q.worldguard</groupId>

View File

@ -37,6 +37,7 @@ import net.Indyuce.mmoitems.comp.rpg.RPGHandler;
import net.Indyuce.mmoitems.gui.PluginInventory;
import net.Indyuce.mmoitems.gui.edition.recipe.RecipeBrowserGUI;
import net.Indyuce.mmoitems.manager.*;
import net.Indyuce.mmoitems.manager.data.PlayerDataManager;
import net.Indyuce.mmoitems.util.PluginUtils;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
@ -44,6 +45,7 @@ import org.bukkit.ChatColor;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
@ -76,6 +78,7 @@ public class MMOItems extends JavaPlugin {
private final List<EnchantPlugin<? extends Enchantment>> enchantPlugins = new ArrayList<>();
private final StatManager statManager = new StatManager();
private PlayerDataManager playerDataManager;
private DropTableManager dropTableManager;
private WorldGenManager worldGenManager;
private UpgradeManager upgradeManager;
@ -239,7 +242,8 @@ public class MMOItems extends JavaPlugin {
}*/
// Compatibility with /reload
Bukkit.getScheduler().runTask(this, () -> Bukkit.getOnlinePlayers().forEach(PlayerData::load));
playerDataManager = new PlayerDataManager();
playerDataManager.initialize(true, EventPriority.NORMAL, EventPriority.HIGHEST);
// Amount and bukkit recipes
getLogger().log(Level.INFO, "Loading recipes, please wait...");
@ -261,7 +265,7 @@ public class MMOItems extends JavaPlugin {
return;
// Save player data
PlayerData.getLoaded().forEach(data -> data.save(false));
playerDataManager.saveAll(false);
// Drop abandoned items
DeathItemsHandler.getActive().forEach(info -> info.giveItems(true));
@ -435,6 +439,10 @@ public class MMOItems extends JavaPlugin {
enchantPlugins.add(enchantPlugin);
}
public PlayerDataManager getPlayerDataManager() {
return playerDataManager;
}
public StatManager getStats() {
return statManager;
}

View File

@ -5,13 +5,14 @@ import io.lumine.mythic.lib.api.item.NBTItem;
import io.lumine.mythic.lib.api.player.EquipmentSlot;
import io.lumine.mythic.lib.api.player.MMOPlayerData;
import io.lumine.mythic.lib.damage.AttackMetadata;
import io.lumine.mythic.lib.data.SynchronizedDataHolder;
import io.lumine.mythic.lib.player.PlayerMetadata;
import io.lumine.mythic.lib.player.modifier.ModifierSource;
import io.lumine.mythic.lib.player.skill.PassiveSkill;
import io.lumine.mythic.lib.skill.trigger.TriggerMetadata;
import io.lumine.mythic.lib.util.Closeable;
import net.Indyuce.mmoitems.ItemStats;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ConfigFile;
import net.Indyuce.mmoitems.api.ItemSet;
import net.Indyuce.mmoitems.api.ItemSet.SetBonuses;
import net.Indyuce.mmoitems.api.crafting.CraftingStatus;
@ -28,7 +29,6 @@ import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
@ -39,7 +39,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.*;
public class PlayerData {
public class PlayerData extends SynchronizedDataHolder implements Closeable {
@NotNull
private final MMOPlayerData mmoData;
@ -59,44 +59,17 @@ public class PlayerData {
private final PlayerStats stats;
private final Set<String> permissions = new HashSet<>();
private static final Map<UUID, PlayerData> data = new HashMap<>();
public PlayerData(@NotNull MMOPlayerData mmoData) {
super(mmoData);
private PlayerData(@NotNull MMOPlayerData mmoData) {
this.mmoData = mmoData;
rpgPlayer = MMOItems.plugin.getMainRPG().getInfo(this);
stats = new PlayerStats(this);
load(new ConfigFile("/userdata", getUniqueId().toString()).getConfig());
}
private void load(FileConfiguration config) {
if (config.contains("crafting-queue"))
craftingStatus.load(this, config.getConfigurationSection("crafting-queue"));
if (MMOItems.plugin.hasPermissions() && config.contains("permissions-from-items")) {
final Permission perms = MMOItems.plugin.getVault().getPermissions();
config.getStringList("permissions-from-items").forEach(perm -> {
if (perms.has(getPlayer(), perm))
perms.playerRemove(getPlayer(), perm);
});
}
}
public void save(boolean clearForMap) {
// Empty map if required
if (clearForMap)
data.remove(getUniqueId());
// Cancel runnables
@Override
public void close() {
cancelRunnables();
// Save data in config
final ConfigFile config = new ConfigFile("/userdata", getUniqueId().toString());
config.getConfig().createSection("crafting-queue");
config.getConfig().set("permissions-from-items", new ArrayList<>(permissions));
craftingStatus.save(config.getConfig().getConfigurationSection("crafting-queue"));
config.save();
}
public MMOPlayerData getMMOPlayerData() {
@ -346,6 +319,10 @@ public class PlayerData {
return permanentEffects.containsKey(type) ? permanentEffects.get(type).getAmplifier() : -1;
}
public Set<String> getPermissions() {
return permissions;
}
public Collection<PotionEffect> getPermanentPotionEffects() {
return permanentEffects.values();
}
@ -424,49 +401,18 @@ public class PlayerData {
* @return If player data is loaded for a player UUID
*/
public static boolean has(UUID uuid) {
return data.containsKey(uuid);
return MMOItems.plugin.getPlayerDataManager().isLoaded(uuid);
}
@NotNull
public static PlayerData get(UUID uuid) {
return Objects.requireNonNull(data.get(uuid), "Player data not loaded");
}
/**
* Called when the corresponding MMOPlayerData has already been initialized.
*/
public static @NotNull PlayerData load(@NotNull Player player) {
return load(player.getUniqueId());
}
/**
* Called when the corresponding MMOPlayerData has already been initialized.
*/
public static PlayerData load(@NotNull UUID player) {
/*
* Double check they are online, for some reason even if this is fired
* from the join event the player can be offline if they left in the
* same tick or something.
*/
if (!data.containsKey(player)) {
PlayerData playerData = new PlayerData(MMOPlayerData.get(player));
data.put(player, playerData);
playerData.getInventory().scheduleUpdate();
return playerData;
}
/*
* Update the cached RPGPlayer in case of any major change in the player
* data of other rpg plugins
*/
PlayerData playerData = data.get(player);
playerData.rpgPlayer = MMOItems.plugin.getMainRPG().getInfo(playerData);
return playerData;
return MMOItems.plugin.getPlayerDataManager().get(uuid);
}
@Deprecated
@NotNull
public static Collection<PlayerData> getLoaded() {
return data.values();
return MMOItems.plugin.getPlayerDataManager().getLoaded();
}
public enum CooldownType {

View File

@ -0,0 +1,25 @@
package net.Indyuce.mmoitems.manager.data;
import io.lumine.mythic.lib.api.player.MMOPlayerData;
import io.lumine.mythic.lib.comp.profile.DefaultProfileDataModule;
import io.lumine.mythic.lib.data.DefaultOfflineDataHolder;
import io.lumine.mythic.lib.data.SynchronizedDataManager;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.player.PlayerData;
import org.jetbrains.annotations.NotNull;
public class PlayerDataManager extends SynchronizedDataManager<PlayerData, DefaultOfflineDataHolder> {
public PlayerDataManager() {
super(MMOItems.plugin, new YAMLDataHandler());
}
@Override
public PlayerData newPlayerData(@NotNull MMOPlayerData mmoPlayerData) {
return new PlayerData(mmoPlayerData);
}
@Override
public Object newProfileDataModule() {
return new DefaultProfileDataModule(MMOItems.plugin);
}
}

View File

@ -0,0 +1,54 @@
package net.Indyuce.mmoitems.manager.data;
import io.lumine.mythic.lib.data.DefaultOfflineDataHolder;
import io.lumine.mythic.lib.data.yaml.YAMLSynchronizedDataHandler;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.player.PlayerData;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.UUID;
public class YAMLDataHandler extends YAMLSynchronizedDataHandler<PlayerData, DefaultOfflineDataHolder> {
public YAMLDataHandler() {
super(MMOItems.plugin);
}
@Override
public void saveInSection(PlayerData playerData, ConfigurationSection config) {
config.createSection("crafting-queue");
config.set("permissions-from-items", new ArrayList<>(playerData.getPermissions()));
playerData.getCrafting().save(config.getConfigurationSection("crafting-queue"));
}
@Override
public void loadFromSection(PlayerData playerData, ConfigurationSection config) {
if (config.contains("crafting-queue"))
playerData.getCrafting().load(playerData, config.getConfigurationSection("crafting-queue"));
if (MMOItems.plugin.hasPermissions() && config.contains("permissions-from-items")) {
final Permission perms = MMOItems.plugin.getVault().getPermissions();
config.getStringList("permissions-from-items").forEach(perm -> {
if (perms.has(playerData.getPlayer(), perm)) perms.playerRemove(playerData.getPlayer(), perm);
});
}
}
@Override
public void setup() {
// Nothing
}
@Override
public DefaultOfflineDataHolder getOffline(@NotNull UUID uuid) {
return new DefaultOfflineDataHolder(uuid);
}
@Override
public void close() {
// Nothing
}
}

View File

@ -11,7 +11,6 @@ import net.Indyuce.mmoitems.api.interaction.util.InteractItem;
import net.Indyuce.mmoitems.api.interaction.weapon.Weapon;
import net.Indyuce.mmoitems.api.player.PlayerData;
import net.Indyuce.mmoitems.api.util.DeathDowngrading;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.entity.Trident;
@ -20,30 +19,17 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.event.player.*;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.Iterator;
public class PlayerListener implements Listener {
/**
* MythicLib runs of LOWEST
* MMOCore and MMOInventory runs on LOW
*/
@EventHandler(priority = EventPriority.NORMAL)
public void loadPlayerData(PlayerJoinEvent event) {
MMOItems.plugin.getRecipes().refreshRecipeBook(event.getPlayer());
PlayerData.load(event.getPlayer());
}
@EventHandler(priority = EventPriority.HIGHEST)
public void savePlayerData(PlayerQuitEvent event) {
PlayerData.get(event.getPlayer()).save(true);
}
/**
* If the player dies, its time to roll the death-downgrade stat!
*/