Work on mixins and PS.

This commit is contained in:
Olof Larsson 2013-01-29 18:58:36 +01:00
parent 33ab2f0e88
commit d9c6abe12b
14 changed files with 438 additions and 289 deletions

View File

@ -12,6 +12,7 @@ import com.massivecraft.mcore5.adapter.ItemStackAdapter;
import com.massivecraft.mcore5.adapter.MongoURIAdapter; import com.massivecraft.mcore5.adapter.MongoURIAdapter;
import com.massivecraft.mcore5.adapter.PSAdapter; import com.massivecraft.mcore5.adapter.PSAdapter;
import com.massivecraft.mcore5.cmd.CmdMcore; import com.massivecraft.mcore5.cmd.CmdMcore;
import com.massivecraft.mcore5.mixin.DefaultSenderIdMixin;
import com.massivecraft.mcore5.store.Coll; import com.massivecraft.mcore5.store.Coll;
import com.massivecraft.mcore5.store.Db; import com.massivecraft.mcore5.store.Db;
import com.massivecraft.mcore5.store.MStore; import com.massivecraft.mcore5.store.MStore;
@ -100,6 +101,8 @@ public class MCore extends MPlugin
// Setup PlayerUtil and it's events // Setup PlayerUtil and it's events
new PlayerUtil(this); new PlayerUtil(this);
DefaultSenderIdMixin.get().register();
DefaultSenderIdMixin.get().setup();
// Register events // Register events
this.internalListener = new InternalListener(this); this.internalListener = new InternalListener(this);

View File

@ -21,6 +21,7 @@ import com.massivecraft.mcore5.mixin.Mixin;
import com.massivecraft.mcore5.mixin.PsTeleporterException; import com.massivecraft.mcore5.mixin.PsTeleporterException;
import com.massivecraft.mcore5.usys.Aspect; import com.massivecraft.mcore5.usys.Aspect;
import com.massivecraft.mcore5.usys.Multiverse; import com.massivecraft.mcore5.usys.Multiverse;
import com.massivecraft.mcore5.util.MUtil;
import com.massivecraft.mcore5.util.Txt; import com.massivecraft.mcore5.util.Txt;
import com.massivecraft.mcore5.xlib.gson.annotations.SerializedName; import com.massivecraft.mcore5.xlib.gson.annotations.SerializedName;
@ -119,7 +120,7 @@ public class PS implements Cloneable, Serializable
// Field: locationZ // Field: locationZ
@SerializedName("lz") @SerializedName("lz")
@Getter @Setter protected Double locationZ = null; @Getter @Setter protected Double locationZ = null;
public Double calclocationZ() public Double calcLocationZ()
{ {
return calcLocation(this.locationZ, this.blockZ, this.chunkZ); return calcLocation(this.locationZ, this.blockZ, this.chunkZ);
} }
@ -224,7 +225,7 @@ public class PS implements Cloneable, Serializable
} }
public synchronized Location calcLocation() public synchronized Location calcLocation()
{ {
return this.innerLocation(this.calcLocationX(), this.calcLocationY(), this.calclocationZ()); return this.innerLocation(this.calcLocationX(), this.calcLocationY(), this.calcLocationZ());
} }
protected synchronized Location innerLocation(Double x, Double y, Double z) protected synchronized Location innerLocation(Double x, Double y, Double z)
{ {
@ -487,7 +488,7 @@ public class PS implements Cloneable, Serializable
public synchronized void write(Entity entity) throws PsTeleporterException public synchronized void write(Entity entity) throws PsTeleporterException
{ {
Mixin.getPsTeleporterMixin().teleport(entity, this); Mixin.teleport(entity, this);
} }
//----------------------------------------------// //----------------------------------------------//
@ -623,6 +624,44 @@ public class PS implements Cloneable, Serializable
// STATIC COMPARISON TOOLS // STATIC COMPARISON TOOLS
//----------------------------------------------// //----------------------------------------------//
public static Double locationDistanceSquared(PS one, PS two)
{
if (one == null) return null;
if (two == null) return null;
String w1 = one.getWorldName();
String w2 = two.getWorldName();
if (!MUtil.equals(w1, w2)) return null;
Double x1 = one.calcLocationX();
if (x1 == null) return null;
Double y1 = one.calcLocationY();
if (y1 == null) return null;
Double z1 = one.calcLocationZ();
if (z1 == null) return null;
Double x2 = two.calcLocationX();
if (x2 == null) return null;
Double y2 = two.calcLocationY();
if (y2 == null) return null;
Double z2 = two.calcLocationZ();
if (z2 == null) return null;
return Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2) + Math.pow(z1 - z2, 2);
}
public static Double locationDistance(PS one, PS two)
{
Double ret = locationDistanceSquared(one, two);
if (ret == null) return null;
return Math.sqrt(ret);
}
public static boolean inSameWorld(PS one, PS two) public static boolean inSameWorld(PS one, PS two)
{ {
if (one == null) return false; if (one == null) return false;

View File

@ -39,7 +39,7 @@ public class DefaultDisplayNameMixin implements DisplayNameMixin
Player player = Bukkit.getPlayerExact(senderId); Player player = Bukkit.getPlayerExact(senderId);
if (player != null) return player.getDisplayName(); if (player != null) return player.getDisplayName();
return Mixin.getSenderIdFixerMixin().tryFix(senderId); return Mixin.tryFix(senderId);
} }
@Override @Override

View File

@ -39,7 +39,7 @@ public class DefaultListNameMixin implements ListNameMixin
Player player = Bukkit.getPlayerExact(senderId); Player player = Bukkit.getPlayerExact(senderId);
if (player != null) return player.getDisplayName(); if (player != null) return player.getDisplayName();
return Mixin.getSenderIdFixerMixin().tryFix(senderId); return Mixin.tryFix(senderId);
} }
@Override @Override

View File

@ -1,128 +0,0 @@
package com.massivecraft.mcore5.mixin;
import java.io.File;
import java.util.Map;
import java.util.TreeMap;
import net.minecraft.server.v1_4_R1.DedicatedServer;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_4_R1.CraftServer;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;
import com.massivecraft.mcore5.MCore;
import com.massivecraft.mcore5.util.MUtil;
public class DefaultSenderIdFixerMixin implements SenderIdFixerMixin, Listener
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static DefaultSenderIdFixerMixin i = new DefaultSenderIdFixerMixin();
public static DefaultSenderIdFixerMixin get() { return i; }
// -------------------------------------------- //
// REGISTER & UNREGISTER
// -------------------------------------------- //
public void register()
{
Bukkit.getServer().getPluginManager().registerEvents(this, MCore.p);
}
public void unregister()
{
HandlerList.unregisterAll(this);
}
// -------------------------------------------- //
// SETUP
// -------------------------------------------- //
public void setup()
{
this.populateIdToCorrectId();
}
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
protected File playerfolder = this.getPlayerFolder();
protected Map<String, String> idToCorrectId = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public boolean canFix(String name)
{
return this.reqFix(name) != null;
}
@Override
public String tryFix(String name)
{
String ret = this.reqFix(name);
if (ret != null) return ret;
return name;
}
@Override
public String reqFix(String name)
{
if (!MUtil.isValidPlayerName(name)) return name;
return this.idToCorrectId.get(name);
}
// -------------------------------------------- //
// LISTENER
// -------------------------------------------- //
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerLogin(PlayerLoginEvent event)
{
String id = event.getPlayer().getName();
idToCorrectId.put(id, id);
}
// -------------------------------------------- //
// INTERNAL METHODS
// -------------------------------------------- //
/**
* You might ask yourself why we do this in such a low-level way.
* The reason is this info is not yet "compiled" for plugins that init early.
*/
protected File getPlayerFolder()
{
CraftServer cserver = (CraftServer)Bukkit.getServer();
DedicatedServer dserver = (DedicatedServer)cserver.getServer();
String levelName = dserver.propertyManager.getString("level-name", "world");
return new File(Bukkit.getWorldContainer(), new File(levelName, "players").getPath());
}
protected void populateIdToCorrectId()
{
// List the files in the player folder
File[] playerfiles = this.playerfolder.listFiles();
// The player file may not exist yet
if (playerfiles == null) return;
// Populate by removing .dat
for (File playerfile : playerfiles)
{
String filename = playerfile.getName();
String playername = filename.substring(0, filename.length()-4);
this.idToCorrectId.put(playername, playername);
}
}
}

View File

@ -0,0 +1,180 @@
package com.massivecraft.mcore5.mixin;
import java.io.File;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentSkipListMap;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;
import com.massivecraft.mcore5.MCore;
import com.massivecraft.mcore5.util.MUtil;
import com.massivecraft.mcore5.util.SenderUtil;
public class DefaultSenderIdMixin implements SenderIdMixin, Listener
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static DefaultSenderIdMixin i = new DefaultSenderIdMixin();
public static DefaultSenderIdMixin get() { return i; }
// -------------------------------------------- //
// REGISTER & UNREGISTER
// -------------------------------------------- //
public void register()
{
Bukkit.getServer().getPluginManager().registerEvents(this, MCore.p);
}
public void unregister()
{
HandlerList.unregisterAll(this);
}
// -------------------------------------------- //
// SETUP
// -------------------------------------------- //
public void setup()
{
for (String playerName : MUtil.getPlayerDirectoryNames())
{
this.idToCorrectId.put(playerName, playerName);
}
}
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
// This map will contain all players that ever were on the server.
// It will however not contain any senders registered through SenderUtil.
protected Map<String, String> idToCorrectId = new ConcurrentSkipListMap<String, String>(String.CASE_INSENSITIVE_ORDER);
// -------------------------------------------- //
// LISTENER
// -------------------------------------------- //
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerLogin(PlayerLoginEvent event)
{
String id = event.getPlayer().getName();
idToCorrectId.put(id, id);
}
// -------------------------------------------- //
// OVERRIDE: FIX
// -------------------------------------------- //
@Override
public String reqFix(String senderId)
{
if (!SenderUtil.isPlayerId(senderId)) return senderId;
return this.idToCorrectId.get(senderId);
}
@Override
public String tryFix(String senderId)
{
String ret = this.reqFix(senderId);
if (ret != null) return ret;
return senderId;
}
@Override
public boolean canFix(String senderId)
{
return this.reqFix(senderId) != null;
}
// -------------------------------------------- //
// OVERRIDE: SIMPLES
// -------------------------------------------- //
@Override
public boolean isOnline(String senderId)
{
if (senderId == null) return false;
if (SenderUtil.isPlayerId(senderId))
{
Player player = Bukkit.getPlayerExact(senderId);
return player != null;
}
else
{
// Non-players must be registered for us to consider them online.
CommandSender sender = SenderUtil.getSender(senderId);
return sender != null;
}
}
@Override
public boolean isOffline(String senderId)
{
return !this.isOnline(senderId);
}
@Override
public boolean hasBeenOnline(String senderId)
{
CommandSender sender = SenderUtil.getSender(senderId);
if (sender != null) return true;
return this.idToCorrectId.containsKey(senderId);
}
@Override
public Long getLastOnline(String senderId)
{
if (this.isOnline(senderId)) return System.currentTimeMillis();
String playerNameCC = this.reqFix(senderId);
if (playerNameCC == null) return null;
File playerFile = new File(MUtil.getPlayerDirectory(), playerNameCC+".dat");
return playerFile.lastModified();
}
// -------------------------------------------- //
// OVERRIDE: SETS
// -------------------------------------------- //
@Override
public Set<String> getAllSenderIds()
{
Set<String> ret = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
ret.addAll(SenderUtil.getIdToSender().keySet());
ret.addAll(this.idToCorrectId.keySet());
return ret;
}
@Override
public Set<String> getOnlineSenderIds()
{
Set<String> ret = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
ret.addAll(SenderUtil.getIdToSender().keySet());
for (Player player : Bukkit.getOnlinePlayers())
{
ret.add(player.getName());
}
return ret;
}
@Override
public Set<String> getOfflineSenderIds()
{
Set<String> ret = this.getAllSenderIds();
ret.removeAll(this.getOnlineSenderIds());
return ret;
}
}

View File

@ -1,7 +1,18 @@
package com.massivecraft.mcore5.mixin; package com.massivecraft.mcore5.mixin;
import java.util.Set;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import com.massivecraft.mcore5.PS;
public class Mixin public class Mixin
{ {
// -------------------------------------------- //
// GET/SET MIXINS
// -------------------------------------------- //
private static PsTeleporterMixin psTeleporterMixin = DefaultPsTeleporterMixin.get(); private static PsTeleporterMixin psTeleporterMixin = DefaultPsTeleporterMixin.get();
public static PsTeleporterMixin getPsTeleporterMixin() { return psTeleporterMixin; } public static PsTeleporterMixin getPsTeleporterMixin() { return psTeleporterMixin; }
public static void setPsTeleporterMixin(PsTeleporterMixin val) { psTeleporterMixin = val; } public static void setPsTeleporterMixin(PsTeleporterMixin val) { psTeleporterMixin = val; }
@ -14,13 +25,112 @@ public class Mixin
public static ListNameMixin getListNameMixin() { return listNameMixin; } public static ListNameMixin getListNameMixin() { return listNameMixin; }
public static void setListNameMixin(ListNameMixin val) { listNameMixin = val; } public static void setListNameMixin(ListNameMixin val) { listNameMixin = val; }
private static SenderIdFixerMixin senderIdFixerMixin = DefaultSenderIdFixerMixin.get(); private static SenderIdMixin senderIdMixin = DefaultSenderIdMixin.get();
public static SenderIdFixerMixin getSenderIdFixerMixin() { return senderIdFixerMixin; } public static SenderIdMixin getSenderIdMixin() { return senderIdMixin; }
public static void setSenderIdFixerMixin(SenderIdFixerMixin val) { senderIdFixerMixin = val; } public static void setSenderIdMixin(SenderIdMixin val) { senderIdMixin = val; }
// Add the command sender id resolver system as a mixin? // -------------------------------------------- //
// Nah that could possibly stay a util. // STATIC EXPOSE: PS TELEPORTER
// -------------------------------------------- //
// Player last online should be added public static void teleport(Entity entity, PS ps) throws PsTeleporterException
{
getPsTeleporterMixin().teleport(entity, ps);
}
// -------------------------------------------- //
// STATIC EXPOSE: DISPLAY NAME
// -------------------------------------------- //
public static String getDisplayName(String senderId)
{
return getDisplayNameMixin().get(senderId);
}
public static void setDisplayName(String senderId, String displayName)
{
getDisplayNameMixin().set(senderId, displayName);
}
public static String getDisplayName(CommandSender sender)
{
return getDisplayNameMixin().get(sender);
}
public static void setDisplayName(CommandSender sender, String displayName)
{
getDisplayNameMixin().set(sender, displayName);
}
// -------------------------------------------- //
// STATIC EXPOSE: LIST NAME
// -------------------------------------------- //
public static String getListName(String senderId)
{
return getListNameMixin().get(senderId);
}
public static void setListName(String senderId, String listName)
{
getListNameMixin().set(senderId, listName);
}
public static String getListName(CommandSender sender)
{
return getListNameMixin().get(sender);
}
public static void setListName(CommandSender sender, String listName)
{
getListNameMixin().set(sender, listName);
}
// -------------------------------------------- //
// STATIC EXPOSE: SENDER ID
// -------------------------------------------- //
public static String reqFix(String senderId)
{
return getSenderIdMixin().reqFix(senderId);
}
public static String tryFix(String senderId)
{
return getSenderIdMixin().tryFix(senderId);
}
public static boolean canFix(String senderId)
{
return getSenderIdMixin().canFix(senderId);
}
public static boolean isOnline(String senderId)
{
return getSenderIdMixin().isOnline(senderId);
}
public static boolean isOffline(String senderId)
{
return getSenderIdMixin().isOffline(senderId);
}
public static boolean hasBeenOnline(String senderId)
{
return getSenderIdMixin().hasBeenOnline(senderId);
}
public static Long getLastOnline(String senderId)
{
return getSenderIdMixin().getLastOnline(senderId);
}
public static Set<String> getAllSenderIds()
{
return getSenderIdMixin().getAllSenderIds();
}
public static Set<String> getOnlineSenderIds()
{
return getSenderIdMixin().getOnlineSenderIds();
}
public static Set<String> getOfflineSenderIds()
{
return getSenderIdMixin().getOfflineSenderIds();
}
} }

View File

@ -1,8 +0,0 @@
package com.massivecraft.mcore5.mixin;
public interface SenderIdFixerMixin
{
public boolean canFix(String name);
public String tryFix(String name);
public String reqFix(String name);
}

View File

@ -0,0 +1,23 @@
package com.massivecraft.mcore5.mixin;
import java.util.Set;
public interface SenderIdMixin
{
// Q: What do you mean "fix" a senderId?
// A: A senderId is case insensitive. However a certain caseing may be the best looking one. Player do for example have an optimal caseing.
public String reqFix(String senderId);
public String tryFix(String senderId);
public boolean canFix(String senderId);
public boolean isOnline(String senderId);
public boolean isOffline(String senderId);
public boolean hasBeenOnline(String senderId);
public Long getLastOnline(String senderId);
// All of these are static snapshots that are alterable and won't change over time.
public Set<String> getAllSenderIds();
public Set<String> getOnlineSenderIds();
public Set<String> getOfflineSenderIds();
}

View File

@ -3,6 +3,7 @@ package com.massivecraft.mcore5.store;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.TreeSet;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -12,6 +13,7 @@ import com.massivecraft.mcore5.cmd.arg.ARStringEntity;
import com.massivecraft.mcore5.cmd.arg.ARStringMatchFullCI; import com.massivecraft.mcore5.cmd.arg.ARStringMatchFullCI;
import com.massivecraft.mcore5.cmd.arg.ARStringMatchStartCI; import com.massivecraft.mcore5.cmd.arg.ARStringMatchStartCI;
import com.massivecraft.mcore5.cmd.arg.ArgReader; import com.massivecraft.mcore5.cmd.arg.ArgReader;
import com.massivecraft.mcore5.mixin.Mixin;
import com.massivecraft.mcore5.util.MUtil; import com.massivecraft.mcore5.util.MUtil;
import com.massivecraft.mcore5.util.PlayerUtil; import com.massivecraft.mcore5.util.PlayerUtil;
import com.massivecraft.mcore5.util.SenderUtil; import com.massivecraft.mcore5.util.SenderUtil;
@ -65,6 +67,16 @@ public class SenderColl<E extends SenderEntity<E>> extends Coll<E, String>
// EXTRAS // EXTRAS
// -------------------------------------------- // // -------------------------------------------- //
public TreeSet<String> getFixedIds()
{
TreeSet<String> ret = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
for (String senderId : this.getIds())
{
ret.add(Mixin.tryFix(senderId));
}
return ret;
}
@Override @Override
public String fixId(Object oid) public String fixId(Object oid)
{ {

View File

@ -9,6 +9,7 @@ import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.RemoteConsoleCommandSender; import org.bukkit.command.RemoteConsoleCommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.massivecraft.mcore5.mixin.Mixin;
import com.massivecraft.mcore5.util.SenderUtil; import com.massivecraft.mcore5.util.SenderUtil;
public abstract class SenderEntity<E extends SenderEntity<E>> extends Entity<E, String> public abstract class SenderEntity<E extends SenderEntity<E>> extends Entity<E, String>
@ -127,24 +128,24 @@ public abstract class SenderEntity<E extends SenderEntity<E>> extends Entity<E,
public String getDisplayName() public String getDisplayName()
{ {
return SenderUtil.getDisplayName(this.getId()); return Mixin.getDisplayName(this.getId());
} }
public void setDisplayName(String displayName) public void setDisplayName(String displayName)
{ {
SenderUtil.setDisplayName(this.getId(), displayName); Mixin.setDisplayName(this.getId(), displayName);
} }
// LIST NAME // LIST NAME
public String getListName() public String getListName()
{ {
return SenderUtil.getListName(this.getId()); return Mixin.getListName(this.getId());
} }
public void setListName(String listName) public void setListName(String listName)
{ {
SenderUtil.setListName(this.getId(), listName); Mixin.setListName(this.getId(), listName);
} }
// CONVENIENCE SEND MESSAGE // CONVENIENCE SEND MESSAGE

View File

@ -1,5 +1,6 @@
package com.massivecraft.mcore5.util; package com.massivecraft.mcore5.util;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -17,9 +18,13 @@ import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import net.minecraft.server.v1_4_R1.DedicatedServer;
import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.v1_4_R1.CraftServer;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile; import org.bukkit.entity.Projectile;
@ -59,6 +64,43 @@ public class MUtil
return playerNamePattern.matcher((String) o).matches(); return playerNamePattern.matcher((String) o).matches();
} }
// -------------------------------------------- //
// PLAYER DIRECTORY
// -------------------------------------------- //
// A way of getting the player directory even when the server just started
public static File getPlayerDirectory()
{
CraftServer cserver = (CraftServer)Bukkit.getServer();
DedicatedServer dserver = (DedicatedServer)cserver.getServer();
String levelName = dserver.propertyManager.getString("level-name", "world");
return new File(Bukkit.getWorldContainer(), new File(levelName, "players").getPath());
}
public static List<String> getPlayerDirectoryNames()
{
List<String> ret = new ArrayList<String>();
// Get the player directory
File playerDirectory = getPlayerDirectory();
// List the files in the player folder
File[] playerfiles = playerDirectory.listFiles();
// The player directory may not exist yet
if (playerfiles == null) return ret;
// Populate by removing .dat
for (File playerfile : playerfiles)
{
String filename = playerfile.getName();
String playername = filename.substring(0, filename.length()-4);
ret.add(playername);
}
return ret;
}
// -------------------------------------------- // // -------------------------------------------- //
// MATERIAL FACTS // MATERIAL FACTS
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -7,12 +7,10 @@ import java.util.TreeSet;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.ConcurrentSkipListMap;
import net.minecraft.server.v1_4_R1.DedicatedServer;
import net.minecraft.server.v1_4_R1.EntityPlayer; import net.minecraft.server.v1_4_R1.EntityPlayer;
import net.minecraft.server.v1_4_R1.Packet8UpdateHealth; import net.minecraft.server.v1_4_R1.Packet8UpdateHealth;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_4_R1.CraftServer;
import org.bukkit.craftbukkit.v1_4_R1.entity.CraftPlayer; import org.bukkit.craftbukkit.v1_4_R1.entity.CraftPlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -30,7 +28,7 @@ public class PlayerUtil implements Listener
/** /**
* We will use this folder later. * We will use this folder later.
*/ */
public static File playerfolder = getPlayerFolder(); public static File playerDirectory = MUtil.getPlayerDirectory();
/** /**
* This map is populated using the player.dat files on disk. * This map is populated using the player.dat files on disk.
@ -52,7 +50,11 @@ public class PlayerUtil implements Listener
public PlayerUtil(Plugin plugin) public PlayerUtil(Plugin plugin)
{ {
Bukkit.getPluginManager().registerEvents(this, plugin); Bukkit.getPluginManager().registerEvents(this, plugin);
populateCaseInsensitiveNameToCaseCorrectName();
for (String playername : MUtil.getPlayerDirectoryNames())
{
nameToCorrectName.put(playername, playername);
}
} }
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
@ -78,15 +80,6 @@ public class PlayerUtil implements Listener
// PUBLIC METHODS // PUBLIC METHODS
// -------------------------------------------- // // -------------------------------------------- //
/**
* This method simply checks if the playerName is a valid one.
* Mojangs rules for Minecraft character registration is used.
*/
public static boolean isValidPlayerName(final String playerName)
{
return MUtil.isValidPlayerName(playerName);
}
public static Set<String> getAllVisitorNames() public static Set<String> getAllVisitorNames()
{ {
return nameToCorrectName.keySet(); return nameToCorrectName.keySet();
@ -183,7 +176,7 @@ public class PlayerUtil implements Listener
Player player = Bukkit.getPlayerExact(playerNameCC); Player player = Bukkit.getPlayerExact(playerNameCC);
if (player != null && player.isOnline()) return System.currentTimeMillis(); if (player != null && player.isOnline()) return System.currentTimeMillis();
File playerFile = new File(playerfolder, playerNameCC+".dat"); File playerFile = new File(playerDirectory, playerNameCC+".dat");
return playerFile.lastModified(); return playerFile.lastModified();
} }
@ -197,37 +190,4 @@ public class PlayerUtil implements Listener
eplayer.playerConnection.sendPacket(new Packet8UpdateHealth(eplayer.getHealth(), eplayer.getFoodData().a(), eplayer.getFoodData().e())); eplayer.playerConnection.sendPacket(new Packet8UpdateHealth(eplayer.getHealth(), eplayer.getFoodData().a(), eplayer.getFoodData().e()));
} }
// -------------------------------------------- //
// INTERNAL METHODS
// -------------------------------------------- //
protected static void populateCaseInsensitiveNameToCaseCorrectName()
{
// The player file may not exist yet
if (playerfolder == null) return;
File[] playerfiles = playerfolder.listFiles();
if (playerfiles == null) return;
// Populate by removing .dat
for (File playerfile : playerfiles)
{
String filename = playerfile.getName();
String playername = filename.substring(0, filename.length()-4);
nameToCorrectName.put(playername, playername);
}
}
/**
* You might ask yourself why we do this in such a low-level way.
* The reason is this info is not yet "compiled" for plugins that init early.
*/
protected static File getPlayerFolder()
{
CraftServer cserver = (CraftServer)Bukkit.getServer();
DedicatedServer dserver = (DedicatedServer)cserver.getServer();
String levelName = dserver.propertyManager.getString("level-name", "world");
return new File(Bukkit.getWorldContainer(), new File(levelName, "players").getPath());
}
} }

View File

@ -105,14 +105,14 @@ public class SenderUtil
register(getBlock()); register(getBlock());
// Display Name // Display Name
setDisplayName(ID_CONSOLE, ChatColor.RED.toString()+ID_CONSOLE.toUpperCase()); Mixin.setDisplayName(ID_CONSOLE, ChatColor.RED.toString()+ID_CONSOLE.toUpperCase());
setDisplayName(ID_RCON, ChatColor.RED.toString()+ID_RCON.toUpperCase()); Mixin.setDisplayName(ID_RCON, ChatColor.RED.toString()+ID_RCON.toUpperCase());
setDisplayName(ID_BLOCK, ChatColor.RED.toString()+ID_BLOCK.toUpperCase()); Mixin.setDisplayName(ID_BLOCK, ChatColor.RED.toString()+ID_BLOCK.toUpperCase());
// List Name // List Name
setListName(ID_CONSOLE, ChatColor.RED.toString()+ID_CONSOLE.toUpperCase()); Mixin.setListName(ID_CONSOLE, ChatColor.RED.toString()+ID_CONSOLE.toUpperCase());
setListName(ID_RCON, ChatColor.RED.toString()+ID_RCON.toUpperCase()); Mixin.setListName(ID_RCON, ChatColor.RED.toString()+ID_RCON.toUpperCase());
setListName(ID_BLOCK, ChatColor.RED.toString()+ID_BLOCK.toUpperCase()); Mixin.setListName(ID_BLOCK, ChatColor.RED.toString()+ID_BLOCK.toUpperCase());
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -332,43 +332,6 @@ public class SenderUtil
return FakeBlockCommandSender.get(); return FakeBlockCommandSender.get();
} }
// -------------------------------------------- //
// ONLINE/OFFLINE
// -------------------------------------------- //
// What about visibility? And the hide player API?
public static boolean isOnline(String senderId)
{
if (senderId == null) return false;
if (isPlayerId(senderId))
{
Player player = Bukkit.getPlayer(senderId);
if (player == null) return false;
return player.isOnline();
}
else
{
// Non-players must be registered for us to consider them online.
CommandSender sender = getSender(senderId);
return sender != null;
}
}
public static boolean isOffline(String senderId)
{
return ! isOnline(senderId);
}
public static boolean isOnline(CommandSender sender)
{
return isOnline(getSenderId(sender));
}
public static boolean isOffline(CommandSender sender)
{
return isOffline(getSenderId(sender));
}
// -------------------------------------------- // // -------------------------------------------- //
// GET ALL ONLINE // GET ALL ONLINE
// -------------------------------------------- // // -------------------------------------------- //
@ -386,54 +349,6 @@ public class SenderUtil
return ret; return ret;
} }
// -------------------------------------------- //
// DISPLAY NAME
// -------------------------------------------- //
public static String getDisplayName(String senderId)
{
return Mixin.getDisplayNameMixin().get(senderId);
}
public static void setDisplayName(String senderId, String displayName)
{
Mixin.getDisplayNameMixin().set(senderId, displayName);
}
public static String getDisplayName(CommandSender sender)
{
return Mixin.getDisplayNameMixin().get(sender);
}
public static void setDisplayName(CommandSender sender, String displayName)
{
Mixin.getDisplayNameMixin().set(sender, displayName);
}
// -------------------------------------------- //
// LIST NAME
// -------------------------------------------- //
public static String getListName(String senderId)
{
return Mixin.getListNameMixin().get(senderId);
}
public static void setListName(String senderId, String displayName)
{
Mixin.getListNameMixin().set(senderId, displayName);
}
public static String getListName(CommandSender sender)
{
return Mixin.getListNameMixin().get(sender);
}
public static void setListName(CommandSender sender, String displayName)
{
Mixin.getListNameMixin().set(sender, displayName);
}
// -------------------------------------------- // // -------------------------------------------- //
// CONVENIENCE CMD // CONVENIENCE CMD
// -------------------------------------------- // // -------------------------------------------- //