Store McMMOPlayer objects as metadata

This commit is contained in:
t00thpick1 2014-02-07 12:50:04 -05:00 committed by TfT_02
parent b3a6c32ef6
commit 04a02cfdc8
7 changed files with 89 additions and 69 deletions

View File

@ -104,7 +104,7 @@ public class BlockListener implements Listener {
public void onBlockPlace(BlockPlaceEvent event) {
Player player = event.getPlayer();
if (Misc.isNPCEntity(player)) {
if (!UserManager.hasPlayerDataKey(player)) {
return;
}
@ -144,7 +144,7 @@ public class BlockListener implements Listener {
Player player = event.getPlayer();
if (Misc.isNPCEntity(player) || player.getGameMode() == GameMode.CREATIVE) {
if (!UserManager.hasPlayerDataKey(player) || player.getGameMode() == GameMode.CREATIVE) {
return;
}
@ -214,7 +214,7 @@ public class BlockListener implements Listener {
Player player = event.getPlayer();
if (Misc.isNPCEntity(player) || player.getGameMode() == GameMode.CREATIVE) {
if (!UserManager.hasPlayerDataKey(player) || player.getGameMode() == GameMode.CREATIVE) {
return;
}
@ -261,7 +261,7 @@ public class BlockListener implements Listener {
Player player = event.getPlayer();
if (Misc.isNPCEntity(player)) {
if (!UserManager.hasPlayerDataKey(player)) {
return;
}
@ -327,7 +327,7 @@ public class BlockListener implements Listener {
Player player = event.getPlayer();
if (Misc.isNPCEntity(player)) {
if (!UserManager.hasPlayerDataKey(player)) {
return;
}

View File

@ -143,7 +143,7 @@ public class EntityListener implements Listener {
return;
}
if (Misc.isNPCEntity(defender) || !defender.isValid() || !(defender instanceof LivingEntity)) {
if (!UserManager.hasPlayerDataKey(defender) || !defender.isValid() || !(defender instanceof LivingEntity)) {
return;
}
@ -424,7 +424,7 @@ public class EntityListener implements Listener {
// We can make this assumption because we (should) be the only ones using this exact metadata
Player player = plugin.getServer().getPlayerExact(entity.getMetadata(mcMMO.tntMetadataKey).get(0).asString());
if (Misc.isNPCEntity(player)) {
if (!UserManager.hasPlayerDataKey(player)) {
return;
}
@ -451,7 +451,7 @@ public class EntityListener implements Listener {
// We can make this assumption because we (should) be the only ones using this exact metadata
Player player = plugin.getServer().getPlayerExact(entity.getMetadata(mcMMO.tntMetadataKey).get(0).asString());
if (Misc.isNPCEntity(player)) {
if (!UserManager.hasPlayerDataKey(player)) {
return;
}
@ -494,7 +494,7 @@ public class EntityListener implements Listener {
Player player = (Player) entity;
if (Misc.isNPCEntity(player)) {
if (!UserManager.hasPlayerDataKey(player)) {
return;
}
@ -560,7 +560,7 @@ public class EntityListener implements Listener {
Player player = (Player) event.getOwner();
LivingEntity entity = event.getEntity();
if (Misc.isNPCEntity(player) || Misc.isNPCEntity(entity) || entity.hasMetadata(mcMMO.entityMetadataKey)) {
if (!UserManager.hasPlayerDataKey(player) || Misc.isNPCEntity(entity) || entity.hasMetadata(mcMMO.entityMetadataKey)) {
return;
}

View File

@ -38,7 +38,6 @@ import com.gmail.nossr50.runnables.PlayerUpdateInventoryTask;
import com.gmail.nossr50.skills.alchemy.Alchemy;
import com.gmail.nossr50.skills.alchemy.AlchemyPotionBrewer;
import com.gmail.nossr50.util.ItemUtils;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.player.UserManager;
import com.gmail.nossr50.util.skills.SkillUtils;
@ -60,7 +59,7 @@ public class InventoryListener implements Listener {
HumanEntity player = event.getPlayer();
if (Misc.isNPCEntity(player)) {
if (!UserManager.hasPlayerDataKey(player)) {
return;
}
@ -77,7 +76,7 @@ public class InventoryListener implements Listener {
HumanEntity player = event.getPlayer();
if (Misc.isNPCEntity(player)) {
if (!UserManager.hasPlayerDataKey(player)) {
return;
}
@ -96,7 +95,7 @@ public class InventoryListener implements Listener {
Player player = getPlayerFromFurnace(furnaceBlock);
if (Misc.isNPCEntity(player) || !Permissions.secondaryAbilityEnabled(player, SecondaryAbility.FUEL_EFFICIENCY)) {
if (!UserManager.hasPlayerDataKey(player) || !Permissions.secondaryAbilityEnabled(player, SecondaryAbility.FUEL_EFFICIENCY)) {
return;
}
@ -114,7 +113,7 @@ public class InventoryListener implements Listener {
Player player = getPlayerFromFurnace(furnaceBlock);
if (Misc.isNPCEntity(player) || !SkillType.SMELTING.getPermissions(player)) {
if (!UserManager.hasPlayerDataKey(player) || !SkillType.SMELTING.getPermissions(player)) {
return;
}
@ -133,7 +132,7 @@ public class InventoryListener implements Listener {
Player player = getPlayerFromFurnace(furnaceBlock);
if (Misc.isNPCEntity(player) || !Permissions.vanillaXpBoost(player, SkillType.SMELTING)) {
if (!UserManager.hasPlayerDataKey(player) || !Permissions.vanillaXpBoost(player, SkillType.SMELTING)) {
return;
}
@ -156,7 +155,7 @@ public class InventoryListener implements Listener {
HumanEntity whoClicked = event.getWhoClicked();
if (Misc.isNPCEntity(whoClicked) || !(whoClicked instanceof Player) || !Permissions.secondaryAbilityEnabled(whoClicked, SecondaryAbility.CONCOCTIONS)) {
if (!UserManager.hasPlayerDataKey(event.getWhoClicked()) || !Permissions.secondaryAbilityEnabled(whoClicked, SecondaryAbility.CONCOCTIONS)) {
return;
}
@ -252,7 +251,7 @@ public class InventoryListener implements Listener {
HumanEntity whoClicked = event.getWhoClicked();
if (Misc.isNPCEntity(whoClicked) || !(whoClicked instanceof Player) || !Permissions.secondaryAbilityEnabled(whoClicked, SecondaryAbility.CONCOCTIONS)) {
if (!UserManager.hasPlayerDataKey(event.getWhoClicked()) || !Permissions.secondaryAbilityEnabled(whoClicked, SecondaryAbility.CONCOCTIONS)) {
return;
}
@ -312,7 +311,7 @@ public class InventoryListener implements Listener {
public void onCraftItem(CraftItemEvent event) {
final HumanEntity whoClicked = event.getWhoClicked();
if (Misc.isNPCEntity(whoClicked) || !(whoClicked instanceof Player)) {
if (!whoClicked.hasMetadata(mcMMO.playerDataKey)) {
return;
}

View File

@ -1,6 +1,5 @@
package com.gmail.nossr50.listeners;
import com.gmail.nossr50.util.scoreboards.ScoreboardManager;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.Sound;
@ -59,6 +58,7 @@ import com.gmail.nossr50.util.MobHealthbarUtils;
import com.gmail.nossr50.util.Motd;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.player.UserManager;
import com.gmail.nossr50.util.scoreboards.ScoreboardManager;
import com.gmail.nossr50.util.skills.SkillUtils;
public class PlayerListener implements Listener {
@ -81,7 +81,7 @@ public class PlayerListener implements Listener {
public void onPlayerTeleport(PlayerTeleportEvent event) {
Player player = event.getPlayer();
if (Misc.isNPCEntity(player) || !Config.getInstance().getPreventXPAfterTeleport() || event.getFrom().equals(event.getTo())) {
if (!UserManager.hasPlayerDataKey(player) || !Config.getInstance().getPreventXPAfterTeleport() || event.getFrom().equals(event.getTo())) {
return;
}
@ -130,7 +130,7 @@ public class PlayerListener implements Listener {
Player killedPlayer = event.getEntity();
if (Misc.isNPCEntity(killedPlayer) || Permissions.hardcoreBypass(killedPlayer)) {
if (!killedPlayer.hasMetadata(mcMMO.playerDataKey) || Permissions.hardcoreBypass(killedPlayer)) {
return;
}
@ -165,7 +165,7 @@ public class PlayerListener implements Listener {
public void onPlayerWorldChange(PlayerChangedWorldEvent event) {
Player player = event.getPlayer();
if (Misc.isNPCEntity(player)) {
if (!UserManager.hasPlayerDataKey(player)) {
return;
}
@ -208,7 +208,7 @@ public class PlayerListener implements Listener {
public void onPlayerFishHighest(PlayerFishEvent event) {
Player player = event.getPlayer();
if (Misc.isNPCEntity(player) || !SkillType.FISHING.getPermissions(player)) {
if (!UserManager.hasPlayerDataKey(player) || !SkillType.FISHING.getPermissions(player)) {
return;
}
@ -253,7 +253,7 @@ public class PlayerListener implements Listener {
public void onPlayerFishMonitor(PlayerFishEvent event) {
Player player = event.getPlayer();
if (Misc.isNPCEntity(player) || !SkillType.FISHING.getPermissions(player)) {
if (!UserManager.hasPlayerDataKey(player) || !SkillType.FISHING.getPermissions(player)) {
return;
}
@ -294,7 +294,7 @@ public class PlayerListener implements Listener {
public void onPlayerPickupItem(PlayerPickupItemEvent event) {
Player player = event.getPlayer();
if (Misc.isNPCEntity(player)) {
if (!UserManager.hasPlayerDataKey(player)) {
return;
}
@ -344,7 +344,7 @@ public class PlayerListener implements Listener {
public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
if (Misc.isNPCEntity(player)) {
if (!UserManager.hasPlayerDataKey(player)) {
return;
}
@ -353,7 +353,7 @@ public class PlayerListener implements Listener {
mcMMOPlayer.resetAbilityMode();
BleedTimerTask.bleedOut(player);
mcMMOPlayer.getProfile().save();
UserManager.remove(player.getName());
UserManager.remove(player);
ScoreboardManager.teardownPlayer(player);
}
@ -374,7 +374,8 @@ public class PlayerListener implements Listener {
return;
}
UserManager.addUser(player).actualizeRespawnATS();
McMMOPlayer mcMMOPlayer = UserManager.addUser(player);
mcMMOPlayer.actualizeRespawnATS();
ScoreboardManager.setupPlayer(player);
if (Config.getInstance().getMOTDEnabled() && Permissions.motd(player)) {
@ -404,7 +405,7 @@ public class PlayerListener implements Listener {
public void onPlayerRespawn(PlayerRespawnEvent event) {
Player player = event.getPlayer();
if (Misc.isNPCEntity(player)) {
if (!UserManager.hasPlayerDataKey(player)) {
return;
}
@ -420,7 +421,7 @@ public class PlayerListener implements Listener {
public void onPlayerInteractLowest(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (Misc.isNPCEntity(player) || player.getGameMode() == GameMode.CREATIVE) {
if (!UserManager.hasPlayerDataKey(player) || player.getGameMode() == GameMode.CREATIVE) {
return;
}
@ -511,7 +512,7 @@ public class PlayerListener implements Listener {
public void onPlayerInteractMonitor(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (Misc.isNPCEntity(player) || player.getGameMode() == GameMode.CREATIVE) {
if (!UserManager.hasPlayerDataKey(player) || player.getGameMode() == GameMode.CREATIVE) {
return;
}

View File

@ -111,6 +111,7 @@ public class mcMMO extends JavaPlugin {
public final static String arrowDistanceKey = "mcMMO: Arrow Distance";
public final static String customDamageKey = "mcMMO: Custom Damage";
public final static String disarmedItemKey = "mcMMO: Disarmed Item";
public final static String playerDataKey = "mcMMO: Player Data";
public static FixedMetadataValue metadataValue;

View File

@ -149,11 +149,10 @@ public final class Misc {
}
public static void profileCleanup(String playerName) {
UserManager.remove(playerName);
Player player = mcMMO.p.getServer().getPlayerExact(playerName);
if (player != null) {
UserManager.remove(player);
UserManager.addUser(player);
}
}

View File

@ -1,18 +1,19 @@
package com.gmail.nossr50.util.player;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.metadata.FixedMetadataValue;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
public final class UserManager {
private final static Map<String, McMMOPlayer> players = new HashMap<String, McMMOPlayer>();
private UserManager() {}
@ -23,16 +24,8 @@ public final class UserManager {
* @return the player's {@link McMMOPlayer} object
*/
public static McMMOPlayer addUser(Player player) {
String playerName = player.getName();
McMMOPlayer mcMMOPlayer = players.get(playerName);
if (mcMMOPlayer != null) {
mcMMOPlayer.setPlayer(player); // The player object is different on each reconnection and must be updated
}
else {
mcMMOPlayer = new McMMOPlayer(player);
players.put(playerName, mcMMOPlayer);
}
McMMOPlayer mcMMOPlayer = new McMMOPlayer(player);
player.setMetadata(mcMMO.playerDataKey, new FixedMetadataValue(mcMMO.p, mcMMOPlayer));
return mcMMOPlayer;
}
@ -40,35 +33,51 @@ public final class UserManager {
/**
* Remove a user.
*
* @param playerName The name of the player to remove
* @param player The Player object
*/
public static void remove(String playerName) {
players.remove(playerName);
public static void remove(Player player) {
player.removeMetadata(mcMMO.playerDataKey, mcMMO.p);
}
/**
* Clear all users.
*/
public static void clearAll() {
players.clear();
for (Player player : mcMMO.p.getServer().getOnlinePlayers()) {
remove(player);
}
}
/**
* Save all users.
*/
public static void saveAll() {
mcMMO.p.debug("Saving mcMMOPlayers... (" + players.size() + ")");
for (McMMOPlayer mcMMOPlayer : players.values()) {
mcMMOPlayer.getProfile().save();
Player[] onlinePlayers = mcMMO.p.getServer().getOnlinePlayers();
mcMMO.p.debug("Saving mcMMOPlayers... (" + onlinePlayers.length + ")");
for (Player player : onlinePlayers) {
getPlayer(player).getProfile().save();
}
}
public static Set<String> getPlayerNames() {
return players.keySet();
Set<String> playerNames = new HashSet<String>();
for (Player player : mcMMO.p.getServer().getOnlinePlayers()) {
playerNames.add(player.getName());
}
return playerNames;
}
public static Collection<McMMOPlayer> getPlayers() {
return players.values();
Collection<McMMOPlayer> playerCollection = new ArrayList<McMMOPlayer>();
for (Player player : mcMMO.p.getServer().getOnlinePlayers()) {
playerCollection.add(getPlayer(player));
}
return playerCollection;
}
/**
@ -88,10 +97,16 @@ public final class UserManager {
* @return the player's McMMOPlayer object
*/
public static McMMOPlayer getPlayer(OfflinePlayer player) {
if (player instanceof Player) {
return getPlayer((Player) player);
}
return retrieveMcMMOPlayer(player.getName(), false);
}
public static McMMOPlayer getPlayer(OfflinePlayer player, boolean offlineValid) {
if (player instanceof Player) {
return getPlayer((Player) player);
}
return retrieveMcMMOPlayer(player.getName(), offlineValid);
}
@ -99,24 +114,29 @@ public final class UserManager {
return retrieveMcMMOPlayer(playerName, offlineValid);
}
public static McMMOPlayer getPlayer(Player player) {
return (McMMOPlayer) player.getMetadata(mcMMO.playerDataKey).get(0).value();
}
private static McMMOPlayer retrieveMcMMOPlayer(String playerName, boolean offlineValid) {
McMMOPlayer mcMMOPlayer = players.get(playerName);
Player player = mcMMO.p.getServer().getPlayerExact(playerName);
if (mcMMOPlayer == null) {
Player player = mcMMO.p.getServer().getPlayerExact(playerName);
if (player == null) {
if (!offlineValid) {
mcMMO.p.getLogger().warning("A valid mcMMOPlayer object could not be found for " + playerName + ".");
}
return null;
if (player == null) {
if (!offlineValid) {
mcMMO.p.getLogger().warning("A valid mcMMOPlayer object could not be found for " + playerName + ".");
}
mcMMOPlayer = new McMMOPlayer(player);
players.put(playerName, mcMMOPlayer);
return null;
}
return mcMMOPlayer;
return getPlayer(player);
}
public static boolean hasPlayerDataKey(Entity entity) {
if (entity == null) {
return false;
}
return entity.hasMetadata(mcMMO.playerDataKey);
}
}