Skip to content

Commit

Permalink
[BREAKING] Add UUID to PlayerData creation. Outlook on data.
Browse files Browse the repository at this point in the history
Breaks: DataManager.getPlayerData(String, boolean) has been removed, new
methods added to do the same without boolean or with UUID passed extra.

Following changes may repeatedly/randomly break PlayerData and check
data access (unless you use CheckType.getDataFactory), this may not
follow directly, but more or less soon. Even Later, CheckType will get
broken too :), in favor of class instances with dynamic registration
ability.

Basic direction is to concentrate stuff in PlayerData, getting rid of
all the static data stores, but also making access to shared data
more efficient (e.g. store last world id + name and permission cache in
PlayerData). Access will be more thread safe (only for PlayerData,
permissions cache, likely for fetching check data too, however returned
objects may have their own contracts).
  • Loading branch information
asofold committed Apr 8, 2017
1 parent bf6c3ff commit 0cd0d50
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 18 deletions.
Expand Up @@ -84,7 +84,7 @@ public void onItemConsume(final PlayerItemConsumeEvent event){
final long time = System.currentTimeMillis();
if (check(player, event.getItem(), time, data)){
event.setCancelled(true);
DataManager.getPlayerData(player.getName(), true).task.updateInventory();
DataManager.getPlayerData(player).task.updateInventory();
}
}

Expand Down
Expand Up @@ -42,7 +42,7 @@ public boolean onCommand(CommandSender sender, Command command, String alias, St
sender.sendMessage(TAG + "Toggling notifications is only available for online players.");
return true;
}
DataManager.getPlayerData(sender.getName(), true).setNotifyOff(true);
DataManager.getPlayerData((Player) sender).setNotifyOff(true);
sender.sendMessage(TAG + "Notifications are now turned " + ChatColor.RED + "off" + ChatColor.WHITE + ".");
return true;
}
Expand Down
Expand Up @@ -42,7 +42,7 @@ public boolean onCommand(CommandSender sender, Command command, String alias, St
sender.sendMessage(TAG + "Toggling notifications is only available for online players.");
return true;
}
DataManager.getPlayerData(sender.getName(), true).setNotifyOff(false);
DataManager.getPlayerData((Player) sender).setNotifyOff(false);
sender.sendMessage(TAG + "Notifications are now turned " + ChatColor.YELLOW + "on" + ChatColor.WHITE + ".");
return true;
}
Expand Down
Expand Up @@ -651,23 +651,26 @@ public void checkConsistency(final Player[] onlinePlayers) {
}

/**
* Convenience method, also hiding how player data is stored for a Player
* instance - always creates a PlayerData instance, if not already present.
* Get a PlayerData instance in any case - always creates a PlayerData
* instance, if none is present. This method should be preferred, as it
* hides details.
*
* @param player
* @return
*/
public static PlayerData getPlayerData(final Player player) {
return getPlayerData(player.getName(), true);
return getPlayerData(player.getUniqueId(), player.getName(), true);
}

/**
* Get PlayerData instances, controlling if data is created, in case it is
* not present.
*
* @param playerName
* @param create
* @return
*/
public static PlayerData getPlayerData(final String playerName, final boolean create) {
public static PlayerData getPlayerData(final UUID playerId, final String playerName, final boolean create) {
final String lcName = playerName.toLowerCase(); // TODO: Store by both lower case and exact case (also store the Player reference).
final PlayerData data = instance.playerData.get(lcName);
if (data != null) {
Expand All @@ -677,12 +680,21 @@ else if (!create) {
return null;
}
else {
final PlayerData newData = new PlayerData(lcName);
final PlayerData newData = new PlayerData(playerId, playerName);
instance.playerData.put(lcName, newData);
return newData;
}
}

/**
* Get the player data, if present.
* @param playerName
* @return The PlayerData instance if present, null otherwise.
*/
public static PlayerData getPlayerData(final String playerName) {
return instance.playerData.get(playerName.toLowerCase());
}

/**
* Check if player instances are stored for efficiency (legacy).
*
Expand Down
Expand Up @@ -16,6 +16,7 @@

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import fr.neatmonster.nocheatplus.components.data.IData;

Expand Down Expand Up @@ -43,23 +44,32 @@
*/
public class PlayerData implements IData {

// TODO: Prefer this data over an extra structure (PlayerMap)?
// TODO: Functionality of PlayerTask in here.

public static final String TAG_NOTIFY_OFF = "notify_off";

public final PlayerTask task;

/** Not sure this is the future of extra properties. */
protected Set<String> tags = null;

/** Unique id of the player. */
final UUID playerId;
/** Exact case name of the player. */
final String playerName;
/** Lower case name of the player. */
final String lcName;

/**
*
* @param playerName Accurate case not (yet) demanded.
*/
public PlayerData(final String playerName) {
public PlayerData(final UUID playerId, final String playerName) {
this.playerId = playerId;
this.playerName = playerName;
this.lcName = playerName.toLowerCase();
this.task = new PlayerTask(this.lcName);
this.task = new PlayerTask(playerId, playerName);
}

/**
Expand Down
Expand Up @@ -14,6 +14,8 @@
*/
package fr.neatmonster.nocheatplus.players;

import java.util.UUID;

import org.bukkit.entity.Player;

import fr.neatmonster.nocheatplus.utilities.OnDemandTickListener;
Expand All @@ -24,30 +26,35 @@
*
*/
public class PlayerTask extends OnDemandTickListener {

// TODO: Transform/remove (TickTask instead).

// TODO: Merge with player-specific TickTask logic - (e.g. store playerId-> PlayerTask there).
// TODO: Also store the UUID here, prefer.
// TODO: Exact case name / rather !?
// TODO: Consider overriding some logic, because it is used in the main thread only (context: isRegisterd + register).

public final String lcName;
public final String name;
public final UUID id;

protected boolean updateInventory = false;

// protected boolean correctDirection = false;

/**
*
* @param name Not demanded to be case sensitive.
* @param id The unique id of the player.
* @param name Preferable the original exact case name.
*/
public PlayerTask(final String name) {
this.lcName = name.toLowerCase();
public PlayerTask(final UUID id, final String name) {
this.name = name;
this.id = id;
}


@SuppressWarnings("deprecation")
@Override
public boolean delegateTick(final int tick, final long timeLast) {
final Player player = DataManager.getPlayer(lcName);
final Player player = DataManager.getPlayer(id);
if (player != null) {
if (player.isOnline()) {
// if (correctDirection) {
Expand Down
Expand Up @@ -354,7 +354,7 @@ public int sendAdminNotifyMessage(final String message) {
}

private boolean hasTurnedOffNotifications(final String playerName) {
final PlayerData data = DataManager.getPlayerData(playerName, false);
final PlayerData data = DataManager.getPlayerData(playerName);
return data != null && data.getNotifyOff();
}

Expand Down Expand Up @@ -424,6 +424,7 @@ public int sendAdminNotifyMessageSubscriptions(final String message) {
*/
@Override
public void sendMessageOnTick(final String playerName, final String message) {
// TODO: Move to PlayerData ?
playerMessageSender.sendMessageThreadSafe(playerName, message);
}

Expand Down Expand Up @@ -1356,7 +1357,7 @@ protected void onJoinLow(final Player player) {
final String playerName = player.getName();
if (nameSetPerms.hasPermission(playerName, Permissions.NOTIFY)) {
// Login notifications...
final PlayerData data = DataManager.getPlayerData(playerName, true);
final PlayerData data = DataManager.getPlayerData(player.getUniqueId(), playerName, true);
// // Update available.
// if (updateAvailable) player.sendMessage(ChatColor.RED + "NCP: " + ChatColor.WHITE + "A new update of NoCheatPlus is available.\n" + "Download it at http://nocheatplus.org/update");

Expand Down

0 comments on commit 0cd0d50

Please sign in to comment.