Merge branch 'Ricardo' into 'master'

!Merge with Ricardo's changes.

See merge request mythiccraft/mmocore!1
This commit is contained in:
Ricardo 2021-02-09 14:22:37 -07:00
commit ba1ceb957e
6 changed files with 72 additions and 54 deletions

View File

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

View File

@ -92,20 +92,6 @@ public class PlayerData extends OfflinePlayerData {
this.questData = new PlayerQuests(this); this.questData = new PlayerQuests(this);
} }
/*
* easily solves some issues where other plugins use PlayerData.get
*/
public static final PlayerData NOT_LOADED = new PlayerData();
@Deprecated
private PlayerData() {
super(UUID.randomUUID());
mmoData = new MMOPlayerData(null);
playerStats = new PlayerStats(this);
questData = new PlayerQuests(this, null);
}
/* /*
* update all references after /mmocore reload so there can be garbage * update all references after /mmocore reload so there can be garbage
* collection with old plugin objects like class or skill instances. * collection with old plugin objects like class or skill instances.
@ -130,7 +116,7 @@ public class PlayerData extends OfflinePlayerData {
+ getProfess().getId() + " while refreshing player data."); + getProfess().getId() + " while refreshing player data.");
} catch (NullPointerException npe2) { } catch (NullPointerException npe2) {
MMOCore.log(Level.SEVERE, MMOCore.log(Level.SEVERE,
"[Userdata] Could not find unidentified skill in the class " + getProfess().getId() + " while refreshing player data."); "[Userdata] Could not find unidentified skill in class " + getProfess().getId() + " while refreshing player data.");
} }
} }
} }
@ -789,6 +775,11 @@ public class PlayerData extends OfflinePlayerData {
return cast; return cast;
} }
@Override
public int hashCode() {
return mmoData.hashCode();
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
return obj instanceof PlayerData && ((PlayerData) obj).getUniqueId().equals(getUniqueId()); return obj instanceof PlayerData && ((PlayerData) obj).getUniqueId().equals(getUniqueId());

View File

@ -13,8 +13,6 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.attribute.Attribute; import org.bukkit.attribute.Attribute;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class RPGPlaceholders extends PlaceholderExpansion { public class RPGPlaceholders extends PlaceholderExpansion {
@ -45,7 +43,7 @@ public class RPGPlaceholders extends PlaceholderExpansion {
@SuppressWarnings("DuplicateExpressions") @SuppressWarnings("DuplicateExpressions")
@Override @Override
public String onRequest(@Nullable OfflinePlayer player, @NotNull String identifier) { public String onRequest(OfflinePlayer player, String identifier) {
PlayerData playerData = PlayerData.get(player); PlayerData playerData = PlayerData.get(player);
if (identifier.equals("mana_icon")) if (identifier.equals("mana_icon"))

View File

@ -1,5 +1,7 @@
package net.Indyuce.mmocore.gui; package net.Indyuce.mmocore.gui;
import net.Indyuce.mmocore.api.event.PlayerAttributeUseEvent;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
@ -127,6 +129,10 @@ public class AttributeView extends EditableInventory {
playerData.giveAttributePoints(-1); playerData.giveAttributePoints(-1);
MMOCore.plugin.configManager.getSimpleMessage("attribute-level-up", "attribute", attribute.getName(), "level", "" + ins.getBase()).send(player); MMOCore.plugin.configManager.getSimpleMessage("attribute-level-up", "attribute", attribute.getName(), "level", "" + ins.getBase()).send(player);
MMOCore.plugin.soundManager.play(getPlayer(), SoundManager.SoundEvent.LEVEL_ATTRIBUTE); MMOCore.plugin.soundManager.play(getPlayer(), SoundManager.SoundEvent.LEVEL_ATTRIBUTE);
PlayerAttributeUseEvent playerAttributeUseEvent = new PlayerAttributeUseEvent(playerData);
Bukkit.getServer().getPluginManager().callEvent(playerAttributeUseEvent);
open(); open();
} }
} }

View File

@ -12,6 +12,7 @@ import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason; import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
@ -20,15 +21,21 @@ import net.Indyuce.mmocore.api.event.PlayerRegenResourceEvent;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource; import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource;
import net.Indyuce.mmocore.gui.api.PluginInventory; import net.Indyuce.mmocore.gui.api.PluginInventory;
import org.bukkit.scheduler.BukkitRunnable;
public class PlayerListener implements Listener { public class PlayerListener implements Listener {
/* /*
* initialize player data We load our player data.
*/ */
@EventHandler(priority = EventPriority.LOW) @EventHandler(priority = EventPriority.NORMAL)
public void a(PlayerJoinEvent event) { public void playerLoadingEvent(PlayerJoinEvent e) {
MMOCore.plugin.dataProvider.getDataManager().setup(event.getPlayer().getUniqueId()); new BukkitRunnable() {
@Override
public void run() {
MMOCore.plugin.dataProvider.getDataManager().setup(e.getPlayer().getUniqueId());
}
}.runTaskAsynchronously(MMOCore.plugin);
} }
/* /*

View File

@ -1,9 +1,6 @@
package net.Indyuce.mmocore.manager.data; package net.Indyuce.mmocore.manager.data;
import java.util.Collection; import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@ -14,9 +11,11 @@ import net.Indyuce.mmocore.api.event.PlayerDataLoadEvent;
import net.Indyuce.mmocore.api.player.OfflinePlayerData; import net.Indyuce.mmocore.api.player.OfflinePlayerData;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import io.lumine.mythic.lib.api.player.MMOPlayerData; import io.lumine.mythic.lib.api.player.MMOPlayerData;
import org.bukkit.scheduler.BukkitRunnable;
public abstract class PlayerDataManager { public abstract class PlayerDataManager {
private final static Map<UUID, PlayerData> data = 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) { public PlayerData get(OfflinePlayer player) {
@ -33,34 +32,29 @@ public abstract class PlayerDataManager {
public abstract OfflinePlayerData getOffline(UUID uuid); public abstract OfflinePlayerData getOffline(UUID uuid);
public PlayerData setup(UUID uuid) { public PlayerData setup(UUID uniqueId) {
/* return data.compute(uniqueId, (uuid, searchData) -> {
* Setup playerData based on loadData method to support both MySQL and if (searchData == null) {
* YAML data storage PlayerData playerData = new PlayerData(MMOPlayerData.get(uniqueId));
*/
PlayerData playerData = data.get(uuid);
if (playerData == null) {
playerData = data.put(uuid, new PlayerData(MMOPlayerData.get(uuid)));
/* loadData(playerData);
* Loads player data and ONLY THEN refresh the player statistics and playerData.getStats().updateStats();
* calls the load event on the MAIN thread
*/ // We call the player data load event. TODO: Convert this event to async.
Bukkit.getScheduler().runTaskAsynchronously(MMOCore.plugin, () -> { new BukkitRunnable() {
PlayerData loaded = PlayerData.get(uuid); @Override
if (!loaded.isOnline()) public void run() {
return; Bukkit.getPluginManager().callEvent(new PlayerDataLoadEvent(playerData));
loadData(loaded); }
Bukkit.getScheduler().runTask(MMOCore.plugin, () -> { }.runTask(MMOCore.plugin);
if (loaded.isOnline())
Bukkit.getPluginManager().callEvent(new PlayerDataLoadEvent(loaded)); return playerData;
}); } else return searchData;
loaded.getStats().updateStats();
}); });
}
return playerData;
} }
public DefaultPlayerData getDefaultData() { public DefaultPlayerData getDefaultData() {
return defaultData; return defaultData;
} }