PLayer data now loaded async on player join

This commit is contained in:
Jules 2021-07-25 15:06:07 +02:00
parent 4da75c0864
commit 9cbc951fad
13 changed files with 1226 additions and 1115 deletions

12
pom.xml
View File

@ -172,7 +172,7 @@
<!-- Local repo -->
<dependency>
<groupId>net.Indyuce.mmocore.lib</groupId>
<groupId>com.bekvon.bukkit.residence</groupId>
<artifactId>Residence</artifactId>
<version>4.8.7.2</version>
<scope>system</scope>
@ -180,7 +180,7 @@
</dependency>
<dependency>
<groupId>net.Indyuce.mmocore.lib</groupId>
<groupId>com.Zrips.CMI</groupId>
<artifactId>CMI</artifactId>
<version>8.6.5.0</version>
<scope>system</scope>
@ -188,7 +188,7 @@
</dependency>
<dependency>
<groupId>net.Indyuce.mmocore.lib</groupId>
<groupId>com.sainttx.holograms</groupId>
<artifactId>Holograms</artifactId>
<version>2.9.1</version>
<scope>system</scope>
@ -196,7 +196,7 @@
</dependency>
<dependency>
<groupId>net.Indyuce.mmocore.lib</groupId>
<groupId>com.gmail.filoghost</groupId>
<artifactId>HolographicDisplays</artifactId>
<version>2.4.6</version>
<scope>system</scope>
@ -204,7 +204,7 @@
</dependency>
<dependency>
<groupId>net.Indyuce.mmocore.lib</groupId>
<groupId>net.citizensnpcs</groupId>
<artifactId>Citizens</artifactId>
<version>2.0.25</version>
<scope>system</scope>
@ -212,7 +212,7 @@
</dependency>
<dependency>
<groupId>net.Indyuce.mmocore.lib</groupId>
<groupId>me.vagdedes.spartan</groupId>
<artifactId>SpartanAPI</artifactId>
<version>1.0</version>
<scope>system</scope>

View File

@ -225,7 +225,7 @@ public class MMOCore extends LuminePlugin {
}
/*
* resource regeneration. must check if entity is dead otherwise regen will make
* Resource regeneration. Must check if entity is dead otherwise regen will make
* the 'respawn' button glitched plus HURT entity effect bug
*/
new BukkitRunnable() {
@ -240,7 +240,7 @@ public class MMOCore extends LuminePlugin {
}.runTaskTimer(MMOCore.plugin, 100, 20);
/*
* clean active loot chests every 5 minutes. cannot register this runnable in
* Clean active loot chests every 5 minutes. Cannot register this runnable in
* the loot chest manager because it is instanced when the plugin loads
*/
new BukkitRunnable() {
@ -255,7 +255,7 @@ public class MMOCore extends LuminePlugin {
* Stamina Addon...This should prevent a couple error reports produced by people
* not reading the installation guide...
*/
if (Bukkit.getPluginManager().getPlugin("MMOItemsMana") != null) {
if (Bukkit.getPluginManager().getPlugin("MMOMana") != null) {
getLogger().log(Level.SEVERE, ChatColor.DARK_RED + "MMOCore is not meant to be used with MMOItems ManaAndStamina");
getLogger().log(Level.SEVERE, ChatColor.DARK_RED + "Please read the installation guide!");
Bukkit.broadcastMessage(ChatColor.DARK_RED + "[MMOCore] MMOCore is not meant to be used with MMOItems ManaAndStamina");
@ -297,20 +297,16 @@ public class MMOCore extends LuminePlugin {
Bukkit.getPluginManager().registerEvents(new PlayerCollectStats(), this);
/*
* initialize player data from all online players. this is very important to do
* Initialize player data from all online players. This is very important to do
* that after registering all the professses otherwise the player datas can't
* recognize what profess the player has and professes will be lost
*/
Bukkit.getOnlinePlayers().forEach(player -> dataProvider.getDataManager().setup(player.getUniqueId()));
/*
* load guild data after loading player data
*/
// load guild data after loading player data
dataProvider.getGuildManager().load();
/*
* commands
*/
// Command
try {
final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");

View File

@ -0,0 +1,27 @@
package net.Indyuce.mmocore.api.event;
import net.Indyuce.mmocore.api.player.PlayerData;
import org.bukkit.event.HandlerList;
public class AsyncPlayerDataLoadEvent extends PlayerDataEvent {
private static final HandlerList handlers = new HandlerList();
/**
* Called when a player data is being loaded into the game.
* This event is called async.
*
* @param playerData Player data being loaded
*/
public AsyncPlayerDataLoadEvent(PlayerData playerData) {
super(playerData);
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -1,15 +1,29 @@
package net.Indyuce.mmocore.api.event;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.attribute.PlayerAttribute;
import org.bukkit.event.HandlerList;
public class PlayerAttributeUseEvent extends PlayerDataEvent{
public class PlayerAttributeUseEvent extends PlayerDataEvent {
private static final HandlerList handlers = new HandlerList();
public PlayerAttributeUseEvent(PlayerData playerData) {
private final PlayerAttribute attribute;
/**
* Called when a player increases an attribute using the attribute viewer GUI
*
* @param playerData PLayer increasing his attribute
* @param attribute Attribute being increased
*/
public PlayerAttributeUseEvent(PlayerData playerData, PlayerAttribute attribute) {
super(playerData);
this.attribute = attribute;
}
public PlayerAttribute getAttribute() {
return attribute;
}
@Override
public HandlerList getHandlers() {

View File

@ -1,30 +1,36 @@
package net.Indyuce.mmocore.api.event;
import net.Indyuce.mmocore.api.player.PlayerData;
import org.bukkit.event.HandlerList;
import net.Indyuce.mmocore.api.player.PlayerData;
public class PlayerCombatEvent extends PlayerDataEvent {
private static final HandlerList handlers = new HandlerList();
private static final HandlerList handlers = new HandlerList();
private final boolean enter;
private final boolean enter;
public PlayerCombatEvent(PlayerData playerData, boolean enter) {
super(playerData);
/**
* Called when a player either enters or leaves combat
* by dealing damage to, or being hit by another entity
*
* @param playerData Player interacting
* @param enter If the player is entering combt
*/
public PlayerCombatEvent(PlayerData playerData, boolean enter) {
super(playerData);
this.enter = enter;
}
this.enter = enter;
}
public boolean entersCombat() {
return enter;
}
public boolean entersCombat() {
return enter;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -1,22 +1,30 @@
package net.Indyuce.mmocore.api.event;
import net.Indyuce.mmocore.api.player.PlayerData;
import org.bukkit.event.HandlerList;
import net.Indyuce.mmocore.api.player.PlayerData;
/**
* @deprecated Use {@link AsyncPlayerDataLoadEvent} instead
*/
@Deprecated
public class PlayerDataLoadEvent extends PlayerDataEvent {
private static final HandlerList handlers = new HandlerList();
private static final HandlerList handlers = new HandlerList();
public PlayerDataLoadEvent(PlayerData playerData) {
super(playerData);
}
/**
* Called when a player data is being loaded into the game.
*
* @param playerData Player data being loaded
*/
public PlayerDataLoadEvent(PlayerData playerData) {
super(playerData);
}
@Override
public HandlerList getHandlers() {
return handlers;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -1,44 +1,48 @@
package net.Indyuce.mmocore.api.event;
import org.bukkit.event.HandlerList;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.skill.Skill.SkillInfo;
import net.Indyuce.mmocore.api.skill.SkillResult;
import org.bukkit.event.HandlerList;
public class PlayerPostCastSkillEvent extends PlayerDataEvent {
private static final HandlerList handlers = new HandlerList();
private static final HandlerList handlers = new HandlerList();
private final SkillInfo skill;
private final SkillResult result;
private final boolean successful;
private final SkillInfo skill;
private final SkillResult result;
public PlayerPostCastSkillEvent(PlayerData playerData, SkillInfo skill, SkillResult result, boolean successful) {
super(playerData);
this.skill = skill;
this.result = result;
this.successful = successful;
}
/**
* Called right after a player casts a skill.
*
* @param playerData Player casting the skill
* @param skill Skill being cast
* @param result SKill casting result
*/
public PlayerPostCastSkillEvent(PlayerData playerData, SkillInfo skill, SkillResult result) {
super(playerData);
public SkillInfo getCast() {
return skill;
}
this.skill = skill;
this.result = result;
}
public SkillResult getResult() {
return result;
}
public boolean wasSuccessful() {
return successful;
}
public SkillInfo getCast() {
return skill;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public SkillResult getResult() {
return result;
}
public static HandlerList getHandlerList() {
return handlers;
}
public boolean wasSuccessful() {
return result.isSuccessful();
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -1,44 +1,50 @@
package net.Indyuce.mmocore.api.event;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.skill.Skill.SkillInfo;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
public class PlayerPreCastSkillEvent extends PlayerDataEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private static final HandlerList handlers = new HandlerList();
private final SkillInfo skill;
private final SkillInfo skill;
private boolean cancelled;
private boolean cancelled;
public PlayerPreCastSkillEvent(PlayerData playerData, SkillInfo skill) {
super(playerData);
this.skill = skill;
}
/**
* Called right before a player casts a skill. This occurs before
* checking for mana, stamina costs and ability cooldown.
*
* @param playerData Player casting the skill
* @param skill Skill being cast
*/
public PlayerPreCastSkillEvent(PlayerData playerData, SkillInfo skill) {
super(playerData);
public SkillInfo getCast() {
return skill;
}
this.skill = skill;
}
@Override
public boolean isCancelled() {
return cancelled;
}
public SkillInfo getCast() {
return skill;
}
@Override
public void setCancelled(boolean value) {
cancelled = value;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
@Override
public void setCancelled(boolean value) {
cancelled = value;
}
public static HandlerList getHandlerList() {
return handlers;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -1,5 +1,7 @@
package net.Indyuce.mmocore.api.loot;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.manager.SoundManager;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
@ -9,97 +11,98 @@ import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.manager.SoundManager;
public class LootChest {
private final ChestTier tier;
private final LootChestRegion region;
private final ReplacedBlock block;
private final BukkitRunnable effectRunnable;
private final long date = System.currentTimeMillis();
private final ChestTier tier;
private final LootChestRegion region;
private final ReplacedBlock block;
private final BukkitRunnable effectRunnable;
private final long date = System.currentTimeMillis();
/*
* instance generated when a loot chest is placed (as a bukkit block), and
* used to save the data of the block which has been replaced (can replace
* non-solid blocks)
*/
public LootChest(ChestTier tier, LootChestRegion region, Block block) {
this.tier = tier;
this.region = region;
this.block = new ReplacedBlock(block);
this.effectRunnable = tier.hasEffect() ? tier.getEffect().startNewRunnable(block.getLocation().add(.5, .5, .5)) : null;
}
/**
* Called when a loot chest is placed as a Bukkit block, and used
* to save the data of the block which has been replaced.
* <p>
* A placed drop chest may only replace non solid blocks like grass
* or levels..
*/
public LootChest(ChestTier tier, LootChestRegion region, Block block) {
this.tier = tier;
this.region = region;
this.block = new ReplacedBlock(block);
this.effectRunnable = tier.hasEffect() ? tier.getEffect().startNewRunnable(block.getLocation().add(.5, .5, .5)) : null;
}
public ChestTier getTier() {
return tier;
}
public ChestTier getTier() {
return tier;
}
public ReplacedBlock getBlock() {
return block;
}
public ReplacedBlock getBlock() {
return block;
}
public LootChestRegion getRegion() {
return region;
}
public LootChestRegion getRegion() {
return region;
}
public boolean hasPlayerNearby() {
for (Player player : block.loc.getWorld().getPlayers())
if (player.getLocation().distanceSquared(block.loc) < 625)
return true;
return false;
}
public boolean hasPlayerNearby() {
for (Player player : block.loc.getWorld().getPlayers())
if (player.getLocation().distanceSquared(block.loc) < 625)
return true;
return false;
}
public boolean shouldExpire() {
return System.currentTimeMillis() - date > MMOCore.plugin.configManager.lootChestExpireTime;
}
public boolean shouldExpire() {
return System.currentTimeMillis() - date > MMOCore.plugin.configManager.lootChestExpireTime;
}
public void unregister(boolean player) {
/**
* @param player If a player just the chest. It's set to false
* when a loot chest expires or when MMOCore disables.
*/
public void unregister(boolean player) {
/*
* if a player is responsible of closing the chest, close it with sound
*/
if (player) {
MMOCore.plugin.soundManager.play(block.loc.getBlock(), SoundManager.SoundEvent.CLOSE_LOOT_CHEST);
block.loc.getWorld().spawnParticle(Particle.CRIT, block.loc.clone().add(.5, .5, .5), 16, 0, 0, 0, .5);
MMOCore.plugin.lootChests.unregister(this);
}
// If a player is responsible of closing the chest, close it with sound
if (player) {
MMOCore.plugin.soundManager.play(block.loc.getBlock(), SoundManager.SoundEvent.CLOSE_LOOT_CHEST);
block.loc.getWorld().spawnParticle(Particle.CRIT, block.loc.clone().add(.5, .5, .5), 16, 0, 0, 0, .5);
MMOCore.plugin.lootChests.unregister(this);
}
/*
* must clean block inventory before replacing block otherwise loots fly
* off and accumulate on the ground (+during dev phase)
*/
else
((Chest) block.loc.getBlock().getState()).getBlockInventory().clear();
/*
* Must clean block inventory before replacing block otherwise loots fly
* off and accumulate on the ground (+during dev phase)
*/
else
((Chest) block.loc.getBlock().getState()).getBlockInventory().clear();
block.restore();
if (effectRunnable != null)
effectRunnable.cancel();
}
block.restore();
if (effectRunnable != null)
effectRunnable.cancel();
}
public static class ReplacedBlock {
private final Material material;
private final BlockData data;
private final Location loc;
public static class ReplacedBlock {
private final Material material;
private final BlockData data;
private final Location loc;
public ReplacedBlock(Block block) {
this.material = block.getType();
this.data = block.getBlockData();
this.loc = block.getLocation();
}
public ReplacedBlock(Block block) {
this.material = block.getType();
this.data = block.getBlockData();
this.loc = block.getLocation();
}
public Location getLocoation() {
return loc;
}
public Location getLocoation() {
return loc;
}
public boolean matches(Location loc) {
return this.loc.getWorld().equals(loc.getWorld()) && this.loc.getBlockX() == loc.getBlockX() && this.loc.getBlockY() == loc.getBlockY()
&& this.loc.getBlockZ() == loc.getBlockZ();
}
public boolean matches(Location loc) {
return this.loc.getWorld().equals(loc.getWorld()) && this.loc.getBlockX() == loc.getBlockX() && this.loc.getBlockY() == loc.getBlockY()
&& this.loc.getBlockZ() == loc.getBlockZ();
}
public void restore() {
loc.getBlock().setType(material);
loc.getBlock().setBlockData(data);
}
}
public void restore() {
loc.getBlock().setType(material);
loc.getBlock().setBlockData(data);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -42,7 +42,7 @@ public class AttributeView extends EditableInventory {
}
public GeneratedInventory newInventory(PlayerData data) {
return new SkillViewerInventory(data, this);
return new AttributeViewerInventory(data, this);
}
public static class AttributeItem extends InventoryPlaceholderItem {
@ -74,8 +74,8 @@ public class AttributeView extends EditableInventory {
}
}
public class SkillViewerInventory extends GeneratedInventory {
public SkillViewerInventory(PlayerData playerData, EditableInventory editable) {
public class AttributeViewerInventory extends GeneratedInventory {
public AttributeViewerInventory(PlayerData playerData, EditableInventory editable) {
super(playerData, editable);
}
@ -130,7 +130,7 @@ public class AttributeView extends EditableInventory {
MMOCore.plugin.configManager.getSimpleMessage("attribute-level-up", "attribute", attribute.getName(), "level", "" + ins.getBase()).send(player);
MMOCore.plugin.soundManager.play(getPlayer(), SoundManager.SoundEvent.LEVEL_ATTRIBUTE);
PlayerAttributeUseEvent playerAttributeUseEvent = new PlayerAttributeUseEvent(playerData);
PlayerAttributeUseEvent playerAttributeUseEvent = new PlayerAttributeUseEvent(playerData, attribute);
Bukkit.getServer().getPluginManager().callEvent(playerAttributeUseEvent);
open();

View File

@ -1,6 +1,11 @@
package net.Indyuce.mmocore.listener;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.event.PlayerRegenResourceEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource;
import net.Indyuce.mmocore.gui.api.PluginInventory;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
@ -12,97 +17,71 @@ import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.event.PlayerRegenResourceEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource;
import net.Indyuce.mmocore.gui.api.PluginInventory;
import org.bukkit.scheduler.BukkitRunnable;
public class PlayerListener implements Listener {
/*
We load our player data.
// Player data loading
@EventHandler(priority = EventPriority.NORMAL)
public void playerLoadingEvent(PlayerJoinEvent event) {
MMOCore.plugin.dataProvider.getDataManager().setup(event.getPlayer().getUniqueId());
}
// Register custom inventory clicks
@EventHandler
public void b(InventoryClickEvent event) {
if (event.getInventory().getHolder() instanceof PluginInventory)
((PluginInventory) event.getInventory().getHolder()).whenClicked(event);
}
// Register custom inventory close effect
@EventHandler
public void c(InventoryCloseEvent event) {
if (event.getInventory().getHolder() instanceof PluginInventory)
((PluginInventory) event.getInventory().getHolder()).whenClosed(event);
}
/**
* Updates the player's combat log data every time he hits an entity, or
* gets hit by an entity or a projectile sent by another entity
*/
@EventHandler(priority = EventPriority.NORMAL)
public void playerLoadingEvent(PlayerJoinEvent e) {
new BukkitRunnable() {
@Override
public void run() {
MMOCore.plugin.dataProvider.getDataManager().setup(e.getPlayer().getUniqueId());
}
}.runTaskAsynchronously(MMOCore.plugin);
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void d(EntityDamageByEntityEvent event) {
if (event.getEntity() instanceof Player && !event.getEntity().hasMetadata("NPC"))
PlayerData.get((Player) event.getEntity()).updateCombat();
/*
* custom inventories register
*/
@EventHandler
public void b(InventoryClickEvent event) {
if (event.getInventory().getHolder() instanceof PluginInventory)
((PluginInventory) event.getInventory().getHolder()).whenClicked(event);
}
if (event.getDamager() instanceof Player && !event.getDamager().hasMetadata("NPC"))
PlayerData.get((Player) event.getDamager()).updateCombat();
@EventHandler
public void c(InventoryCloseEvent event) {
if (event.getInventory().getHolder() instanceof PluginInventory)
((PluginInventory) event.getInventory().getHolder()).whenClosed(event);
}
if (event.getDamager() instanceof Projectile && ((Projectile) event.getDamager()).getShooter() instanceof Player)
if (!((Player) ((Projectile) event.getDamager()).getShooter()).hasMetadata("NPC"))
PlayerData.get((Player) ((Projectile) event.getDamager()).getShooter()).updateCombat();
}
/*
* updates the player's combat log data every time he hits an entity, or
* gets hit by an entity or a projectile sent by another entity. updates
* this stuff on LOW level so other plugins can check if the player just
* entered combat
*/
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void d(EntityDamageByEntityEvent event) {
if (event.getEntity() instanceof Player && !event.getEntity().hasMetadata("NPC"))
PlayerData.get((Player) event.getEntity()).updateCombat();
@EventHandler
public void e(PlayerQuitEvent event) {
PlayerData playerData = PlayerData.get(event.getPlayer());
if (playerData.hasParty())
playerData.getParty().removeMember(playerData);
if (event.getDamager() instanceof Player && !event.getDamager().hasMetadata("NPC"))
PlayerData.get((Player) event.getDamager()).updateCombat();
MMOCore.plugin.dataProvider.getDataManager().remove(playerData);
}
if (event.getDamager() instanceof Projectile && ((Projectile) event.getDamager()).getShooter() instanceof Player)
if (!((Player) ((Projectile) event.getDamager()).getShooter()).hasMetadata("NPC"))
PlayerData.get((Player) ((Projectile) event.getDamager()).getShooter()).updateCombat();
}
@EventHandler
public void e(PlayerQuitEvent event) {
PlayerData playerData = PlayerData.get(event.getPlayer());
if (playerData.hasParty())
playerData.getParty().removeMember(playerData);
MMOCore.plugin.dataProvider.getDataManager().remove(playerData);
}
/*
* reset skill data when leaving combat
*/
// @EventHandler
// public void f(PlayerCombatEvent event) {
// if (!event.entersCombat())
// event.getData().getSkillData().resetData();
// }
/*
* Warning: this really is not the best way to interface with MMOCore
* generation. Use instead PlayerRegenResourceEvent to be able to access
* directly the PlayerData without an extra map lookup.
*/
@Deprecated
@EventHandler(priority = EventPriority.HIGH)
public void g(PlayerRegenResourceEvent event) {
if (event.getResource() == PlayerResource.HEALTH) {
EntityRegainHealthEvent bukkitEvent = new EntityRegainHealthEvent(event.getPlayer(), event.getAmount(), RegainReason.CUSTOM);
Bukkit.getPluginManager().callEvent(bukkitEvent);
event.setCancelled(bukkitEvent.isCancelled());
event.setAmount(bukkitEvent.getAmount());
}
}
/**
* Warning: this really is not the best way to interface with MMOCore
* generation. Use instead PlayerRegenResourceEvent to be able to access
* directly the PlayerData without an extra map lookup.
*/
@Deprecated
@EventHandler(priority = EventPriority.HIGH)
public void g(PlayerRegenResourceEvent event) {
if (event.getResource() == PlayerResource.HEALTH) {
EntityRegainHealthEvent bukkitEvent = new EntityRegainHealthEvent(event.getPlayer(), event.getAmount(), RegainReason.CUSTOM);
Bukkit.getPluginManager().callEvent(bukkitEvent);
event.setCancelled(bukkitEvent.isCancelled());
event.setAmount(bukkitEvent.getAmount());
}
}
}

View File

@ -1,127 +1,168 @@
package net.Indyuce.mmocore.manager.data;
import java.util.*;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection;
import io.lumine.mythic.lib.api.player.MMOPlayerData;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.event.AsyncPlayerDataLoadEvent;
import net.Indyuce.mmocore.api.event.PlayerDataLoadEvent;
import net.Indyuce.mmocore.api.player.OfflinePlayerData;
import net.Indyuce.mmocore.api.player.PlayerData;
import io.lumine.mythic.lib.api.player.MMOPlayerData;
import org.bukkit.scheduler.BukkitRunnable;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import java.util.*;
public abstract class PlayerDataManager {
private final static Map<UUID, PlayerData> data = Collections.synchronizedMap(new HashMap<>());
private final static Map<UUID, PlayerData> data = Collections.synchronizedMap(new HashMap<>());
private DefaultPlayerData defaultData = new DefaultPlayerData(1, 0, 0, 0, 0);
private DefaultPlayerData defaultData = new DefaultPlayerData(1, 0, 0, 0, 0);
public PlayerData get(OfflinePlayer player) {
return get(player.getUniqueId());
}
public PlayerData get(OfflinePlayer player) {
return get(player.getUniqueId());
}
public PlayerData get(UUID uuid) {
return data.getOrDefault(uuid, setup(uuid));
}
/**
* Gets the player data, or throws an exception if not found.
* The player data should be loaded when the player logs in
* so it's really bad practice to setup the player data if it's not loaded.
*
* @param uuid Player UUID
* @return Player data, if it's loaded
*/
public PlayerData get(UUID uuid) {
Validate.isTrue(data.containsKey(uuid), "Player data is not loaded");
return data.get(uuid);
}
public void remove(UUID uuid) {
data.remove(uuid);
}
public void remove(UUID uuid) {
data.remove(uuid);
}
public abstract OfflinePlayerData getOffline(UUID uuid);
/**
* Offline player data is used to handle processes like friend removal
* which can still occur if one of the two players is offline.
* <p>
* Unlike {@link #get(UUID)} this method never returns a null instance
*
* @param uuid Player unique id
* @return Offline player data
*/
@NotNull
public abstract OfflinePlayerData getOffline(UUID uuid);
public PlayerData setup(UUID uniqueId) {
return data.compute(uniqueId, (uuid, searchData) -> {
if (searchData == null) {
PlayerData playerData = new PlayerData(MMOPlayerData.get(uniqueId));
/**
* Called when a player logs in, loading the player data inside the map.
* <p>
* For YAML configs or SQL databases, data is loaded as not to overload
* the main thread with SQL requests. Therefore, the player data returned
* by that method, when the player joined for the first time, is not
* fully loaded YET.
*
* @param uniqueId Player UUID
* @return The loaded player data.
*/
public PlayerData setup(UUID uniqueId) {
loadData(playerData);
playerData.getStats().updateStats();
// Load player data if it does not exist
if (!data.containsKey(uniqueId)) {
PlayerData newData = new PlayerData(MMOPlayerData.get(uniqueId));
// We call the player data load event. TODO: Convert this event to async.
new BukkitRunnable() {
@Override
public void run() {
Bukkit.getPluginManager().callEvent(new PlayerDataLoadEvent(playerData));
}
}.runTask(MMOCore.plugin);
// Schedule async data loading
Bukkit.getScheduler().runTaskAsynchronously(MMOCore.plugin, () -> {
loadData(newData);
newData.getStats().updateStats();
Bukkit.getPluginManager().callEvent(new AsyncPlayerDataLoadEvent(newData));
Bukkit.getScheduler().runTask(MMOCore.plugin, () -> Bukkit.getPluginManager().callEvent(new PlayerDataLoadEvent(newData)));
});
return playerData;
} else return searchData;
// Update data map
data.put(uniqueId, newData);
});
}
return newData;
}
return data.get(uniqueId);
}
public DefaultPlayerData getDefaultData() {
return defaultData;
}
public DefaultPlayerData getDefaultData() {
return defaultData;
}
public void loadDefaultData(ConfigurationSection config) {
defaultData = new DefaultPlayerData(config);
}
public void loadDefaultData(ConfigurationSection config) {
defaultData = new DefaultPlayerData(config);
}
public boolean isLoaded(UUID uuid) {
return data.containsKey(uuid);
}
public boolean isLoaded(UUID uuid) {
return data.containsKey(uuid);
}
public Collection<PlayerData> getLoaded() {
return data.values();
}
public Collection<PlayerData> getLoaded() {
return data.values();
}
public abstract void loadData(PlayerData data);
/**
* Called when player data must be loaded from database or config.
*
* @param data Player data to load
*/
public abstract void loadData(PlayerData data);
public abstract void saveData(PlayerData data);
/**
* Called when player data must be saved in configs or database.
*
* @param data Player data to save
*/
public abstract void saveData(PlayerData data);
public abstract void remove(PlayerData data);
public abstract void remove(PlayerData data);
public static class DefaultPlayerData {
private final int level, classPoints, skillPoints, attributePoints, attrReallocPoints;
public static class DefaultPlayerData {
private final int level, classPoints, skillPoints, attributePoints, attrReallocPoints;
public DefaultPlayerData(ConfigurationSection config) {
level = config.getInt("level", 1);
classPoints = config.getInt("class-points");
skillPoints = config.getInt("skill-points");
attributePoints = config.getInt("attribute-points");
attrReallocPoints = config.getInt("attribute-realloc-points");
}
public DefaultPlayerData(ConfigurationSection config) {
level = config.getInt("level", 1);
classPoints = config.getInt("class-points");
skillPoints = config.getInt("skill-points");
attributePoints = config.getInt("attribute-points");
attrReallocPoints = config.getInt("attribute-realloc-points");
}
public DefaultPlayerData(int level, int classPoints, int skillPoints, int attributePoints, int attrReallocPoints) {
this.level = level;
this.classPoints = classPoints;
this.skillPoints = skillPoints;
this.attributePoints = attributePoints;
this.attrReallocPoints = attrReallocPoints;
}
public DefaultPlayerData(int level, int classPoints, int skillPoints, int attributePoints, int attrReallocPoints) {
this.level = level;
this.classPoints = classPoints;
this.skillPoints = skillPoints;
this.attributePoints = attributePoints;
this.attrReallocPoints = attrReallocPoints;
}
public int getLevel() {
return level;
}
public int getLevel() {
return level;
}
public int getSkillPoints() {
return skillPoints;
}
public int getSkillPoints() {
return skillPoints;
}
public int getClassPoints() {
return classPoints;
}
public int getClassPoints() {
return classPoints;
}
public int getAttrReallocPoints() {
return attrReallocPoints;
}
public int getAttrReallocPoints() {
return attrReallocPoints;
}
public int getAttributePoints() {
return attributePoints;
}
public int getAttributePoints() {
return attributePoints;
}
public void apply(PlayerData player) {
player.setLevel(level);
player.setClassPoints(classPoints);
player.setSkillPoints(skillPoints);
player.setAttributePoints(attributePoints);
player.setAttributeReallocationPoints(attrReallocPoints);
}
}
public void apply(PlayerData player) {
player.setLevel(level);
player.setClassPoints(classPoints);
player.setSkillPoints(skillPoints);
player.setAttributePoints(attributePoints);
player.setAttributeReallocationPoints(attrReallocPoints);
}
}
}