mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-23 00:42:05 +01:00
Implemented (offline)player date methods
By: Nathan Adams <dinnerbone@dinnerbone.com>
This commit is contained in:
parent
a02181ac29
commit
586d6ad790
2 changed files with 113 additions and 0 deletions
|
@ -1,8 +1,11 @@
|
|||
package org.bukkit.craftbukkit;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import net.minecraft.server.EntityPlayer;
|
||||
import net.minecraft.server.NBTTagCompound;
|
||||
import net.minecraft.server.WorldNBTStorage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
|
@ -14,10 +17,12 @@ import org.bukkit.entity.Player;
|
|||
public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializable {
|
||||
private final String name;
|
||||
private final CraftServer server;
|
||||
private final WorldNBTStorage storage;
|
||||
|
||||
protected CraftOfflinePlayer(CraftServer server, String name) {
|
||||
this.server = server;
|
||||
this.name = name;
|
||||
this.storage = (WorldNBTStorage)(server.console.worlds.get(0).getDataManager());
|
||||
}
|
||||
|
||||
public boolean isOnline() {
|
||||
|
@ -119,4 +124,65 @@ public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializa
|
|||
hash = 97 * hash + (this.getName() != null ? this.getName().hashCode() : 0);
|
||||
return hash;
|
||||
}
|
||||
|
||||
private NBTTagCompound getData() {
|
||||
return storage.getPlayerData(getName());
|
||||
}
|
||||
|
||||
private NBTTagCompound getBukkitData() {
|
||||
NBTTagCompound result = getData();
|
||||
|
||||
if (result != null) {
|
||||
if (!result.hasKey("bukkit")) {
|
||||
result.setCompound("bukkit", new NBTTagCompound());
|
||||
}
|
||||
result = result.getCompound("bukkit");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private File getDataFile() {
|
||||
return new File(storage.getPlayerDir(), name + ".dat");
|
||||
}
|
||||
|
||||
public long getFirstPlayed() {
|
||||
Player player = getPlayer();
|
||||
if (player != null) return player.getFirstPlayed();
|
||||
|
||||
NBTTagCompound data = getBukkitData();
|
||||
|
||||
if (data != null) {
|
||||
if (data.hasKey("firstPlayed")) {
|
||||
return data.getLong("firstPlayed");
|
||||
} else {
|
||||
File file = getDataFile();
|
||||
return file.lastModified();
|
||||
}
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public long getLastPlayed() {
|
||||
Player player = getPlayer();
|
||||
if (player != null) return player.getFirstPlayed();
|
||||
|
||||
NBTTagCompound data = getBukkitData();
|
||||
|
||||
if (data != null) {
|
||||
if (data.hasKey("lastPlayed")) {
|
||||
return data.getLong("lastPlayed");
|
||||
} else {
|
||||
File file = getDataFile();
|
||||
return file.lastModified();
|
||||
}
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasPlayedBefore() {
|
||||
return getData() != null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,9 +2,12 @@ package org.bukkit.craftbukkit.entity;
|
|||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import net.minecraft.server.EntityPlayer;
|
||||
import net.minecraft.server.NBTTagCompound;
|
||||
import net.minecraft.server.Packet131ItemData;
|
||||
import net.minecraft.server.Packet200Statistic;
|
||||
import net.minecraft.server.Packet201PlayerInfo;
|
||||
|
@ -39,8 +42,14 @@ import org.bukkit.map.MapView;
|
|||
|
||||
@DelegateDeserialization(CraftOfflinePlayer.class)
|
||||
public class CraftPlayer extends CraftHumanEntity implements Player {
|
||||
private long firstPlayed = 0;
|
||||
private long lastPlayed = 0;
|
||||
private boolean hasPlayedBefore = false;
|
||||
|
||||
public CraftPlayer(CraftServer server, EntityPlayer entity) {
|
||||
super(server, entity);
|
||||
|
||||
firstPlayed = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -567,4 +576,42 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
|||
hash = 97 * hash + (this.getName() != null ? this.getName().hashCode() : 0);
|
||||
return hash;
|
||||
}
|
||||
|
||||
public long getFirstPlayed() {
|
||||
return firstPlayed;
|
||||
}
|
||||
|
||||
public long getLastPlayed() {
|
||||
return lastPlayed;
|
||||
}
|
||||
|
||||
public boolean hasPlayedBefore() {
|
||||
return hasPlayedBefore;
|
||||
}
|
||||
|
||||
public void setFirstPlayed(long firstPlayed) {
|
||||
this.firstPlayed = firstPlayed;
|
||||
}
|
||||
|
||||
public void readExtraData(NBTTagCompound nbttagcompound) {
|
||||
hasPlayedBefore = true;
|
||||
if (nbttagcompound.hasKey("bukkit")) {
|
||||
NBTTagCompound data = nbttagcompound.getCompound("bukkit");
|
||||
|
||||
if (data.hasKey("firstPlayed")) {
|
||||
firstPlayed = data.getLong("firstPlayed");
|
||||
lastPlayed = data.getLong("lastPlayed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setExtraData(NBTTagCompound nbttagcompound) {
|
||||
if (!nbttagcompound.hasKey("bukkit")) {
|
||||
nbttagcompound.setCompound("bukkit", new NBTTagCompound());
|
||||
}
|
||||
|
||||
NBTTagCompound data = nbttagcompound.getCompound("bukkit");
|
||||
data.setLong("firstPlayed", getFirstPlayed());
|
||||
data.setLong("lastPlayed", System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue