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

View File

@ -143,7 +143,7 @@ public class EntityListener implements Listener {
return; return;
} }
if (Misc.isNPCEntity(defender) || !defender.isValid() || !(defender instanceof LivingEntity)) { if (!UserManager.hasPlayerDataKey(defender) || !defender.isValid() || !(defender instanceof LivingEntity)) {
return; 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 // 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()); Player player = plugin.getServer().getPlayerExact(entity.getMetadata(mcMMO.tntMetadataKey).get(0).asString());
if (Misc.isNPCEntity(player)) { if (!UserManager.hasPlayerDataKey(player)) {
return; 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 // 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()); Player player = plugin.getServer().getPlayerExact(entity.getMetadata(mcMMO.tntMetadataKey).get(0).asString());
if (Misc.isNPCEntity(player)) { if (!UserManager.hasPlayerDataKey(player)) {
return; return;
} }
@ -494,7 +494,7 @@ public class EntityListener implements Listener {
Player player = (Player) entity; Player player = (Player) entity;
if (Misc.isNPCEntity(player)) { if (!UserManager.hasPlayerDataKey(player)) {
return; return;
} }
@ -560,7 +560,7 @@ public class EntityListener implements Listener {
Player player = (Player) event.getOwner(); Player player = (Player) event.getOwner();
LivingEntity entity = event.getEntity(); 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; 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.Alchemy;
import com.gmail.nossr50.skills.alchemy.AlchemyPotionBrewer; import com.gmail.nossr50.skills.alchemy.AlchemyPotionBrewer;
import com.gmail.nossr50.util.ItemUtils; import com.gmail.nossr50.util.ItemUtils;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
import com.gmail.nossr50.util.skills.SkillUtils; import com.gmail.nossr50.util.skills.SkillUtils;
@ -60,7 +59,7 @@ public class InventoryListener implements Listener {
HumanEntity player = event.getPlayer(); HumanEntity player = event.getPlayer();
if (Misc.isNPCEntity(player)) { if (!UserManager.hasPlayerDataKey(player)) {
return; return;
} }
@ -77,7 +76,7 @@ public class InventoryListener implements Listener {
HumanEntity player = event.getPlayer(); HumanEntity player = event.getPlayer();
if (Misc.isNPCEntity(player)) { if (!UserManager.hasPlayerDataKey(player)) {
return; return;
} }
@ -96,7 +95,7 @@ public class InventoryListener implements Listener {
Player player = getPlayerFromFurnace(furnaceBlock); 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; return;
} }
@ -114,7 +113,7 @@ public class InventoryListener implements Listener {
Player player = getPlayerFromFurnace(furnaceBlock); Player player = getPlayerFromFurnace(furnaceBlock);
if (Misc.isNPCEntity(player) || !SkillType.SMELTING.getPermissions(player)) { if (!UserManager.hasPlayerDataKey(player) || !SkillType.SMELTING.getPermissions(player)) {
return; return;
} }
@ -133,7 +132,7 @@ public class InventoryListener implements Listener {
Player player = getPlayerFromFurnace(furnaceBlock); Player player = getPlayerFromFurnace(furnaceBlock);
if (Misc.isNPCEntity(player) || !Permissions.vanillaXpBoost(player, SkillType.SMELTING)) { if (!UserManager.hasPlayerDataKey(player) || !Permissions.vanillaXpBoost(player, SkillType.SMELTING)) {
return; return;
} }
@ -156,7 +155,7 @@ public class InventoryListener implements Listener {
HumanEntity whoClicked = event.getWhoClicked(); 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; return;
} }
@ -252,7 +251,7 @@ public class InventoryListener implements Listener {
HumanEntity whoClicked = event.getWhoClicked(); 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; return;
} }
@ -312,7 +311,7 @@ public class InventoryListener implements Listener {
public void onCraftItem(CraftItemEvent event) { public void onCraftItem(CraftItemEvent event) {
final HumanEntity whoClicked = event.getWhoClicked(); final HumanEntity whoClicked = event.getWhoClicked();
if (Misc.isNPCEntity(whoClicked) || !(whoClicked instanceof Player)) { if (!whoClicked.hasMetadata(mcMMO.playerDataKey)) {
return; return;
} }

View File

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

View File

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

View File

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

View File

@ -1,18 +1,19 @@
package com.gmail.nossr50.util.player; package com.gmail.nossr50.util.player;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.metadata.FixedMetadataValue;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.McMMOPlayer;
public final class UserManager { public final class UserManager {
private final static Map<String, McMMOPlayer> players = new HashMap<String, McMMOPlayer>();
private UserManager() {} private UserManager() {}
@ -23,16 +24,8 @@ public final class UserManager {
* @return the player's {@link McMMOPlayer} object * @return the player's {@link McMMOPlayer} object
*/ */
public static McMMOPlayer addUser(Player player) { public static McMMOPlayer addUser(Player player) {
String playerName = player.getName(); McMMOPlayer mcMMOPlayer = new McMMOPlayer(player);
McMMOPlayer mcMMOPlayer = players.get(playerName); player.setMetadata(mcMMO.playerDataKey, new FixedMetadataValue(mcMMO.p, mcMMOPlayer));
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);
}
return mcMMOPlayer; return mcMMOPlayer;
} }
@ -40,35 +33,51 @@ public final class UserManager {
/** /**
* Remove a user. * Remove a user.
* *
* @param playerName The name of the player to remove * @param player The Player object
*/ */
public static void remove(String playerName) { public static void remove(Player player) {
players.remove(playerName); player.removeMetadata(mcMMO.playerDataKey, mcMMO.p);
} }
/** /**
* Clear all users. * Clear all users.
*/ */
public static void clearAll() { public static void clearAll() {
players.clear(); for (Player player : mcMMO.p.getServer().getOnlinePlayers()) {
remove(player);
}
} }
/** /**
* Save all users. * Save all users.
*/ */
public static void saveAll() { public static void saveAll() {
mcMMO.p.debug("Saving mcMMOPlayers... (" + players.size() + ")"); Player[] onlinePlayers = mcMMO.p.getServer().getOnlinePlayers();
for (McMMOPlayer mcMMOPlayer : players.values()) { mcMMO.p.debug("Saving mcMMOPlayers... (" + onlinePlayers.length + ")");
mcMMOPlayer.getProfile().save();
for (Player player : onlinePlayers) {
getPlayer(player).getProfile().save();
} }
} }
public static Set<String> getPlayerNames() { 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() { 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 * @return the player's McMMOPlayer object
*/ */
public static McMMOPlayer getPlayer(OfflinePlayer player) { public static McMMOPlayer getPlayer(OfflinePlayer player) {
if (player instanceof Player) {
return getPlayer((Player) player);
}
return retrieveMcMMOPlayer(player.getName(), false); return retrieveMcMMOPlayer(player.getName(), false);
} }
public static McMMOPlayer getPlayer(OfflinePlayer player, boolean offlineValid) { public static McMMOPlayer getPlayer(OfflinePlayer player, boolean offlineValid) {
if (player instanceof Player) {
return getPlayer((Player) player);
}
return retrieveMcMMOPlayer(player.getName(), offlineValid); return retrieveMcMMOPlayer(player.getName(), offlineValid);
} }
@ -99,10 +114,11 @@ public final class UserManager {
return retrieveMcMMOPlayer(playerName, offlineValid); return retrieveMcMMOPlayer(playerName, offlineValid);
} }
private static McMMOPlayer retrieveMcMMOPlayer(String playerName, boolean offlineValid) { public static McMMOPlayer getPlayer(Player player) {
McMMOPlayer mcMMOPlayer = players.get(playerName); return (McMMOPlayer) player.getMetadata(mcMMO.playerDataKey).get(0).value();
}
if (mcMMOPlayer == null) { private static McMMOPlayer retrieveMcMMOPlayer(String playerName, boolean offlineValid) {
Player player = mcMMO.p.getServer().getPlayerExact(playerName); Player player = mcMMO.p.getServer().getPlayerExact(playerName);
if (player == null) { if (player == null) {
@ -113,10 +129,14 @@ public final class UserManager {
return null; return null;
} }
mcMMOPlayer = new McMMOPlayer(player); return getPlayer(player);
players.put(playerName, mcMMOPlayer);
} }
return mcMMOPlayer; public static boolean hasPlayerDataKey(Entity entity) {
if (entity == null) {
return false;
}
return entity.hasMetadata(mcMMO.playerDataKey);
} }
} }