Remove plugin reference from MCommand. Aim to work with CommandSender instead of Player in most situations.

This commit is contained in:
Olof Larsson 2013-01-17 15:44:38 +01:00
parent e4942a2eb8
commit 8675a87aba
41 changed files with 1318 additions and 218 deletions

View File

@ -22,6 +22,7 @@ import com.massivecraft.mcore5.event.MCoreAfterPlayerTeleportEvent;
import com.massivecraft.mcore5.event.MCorePlayerLeaveEvent; import com.massivecraft.mcore5.event.MCorePlayerLeaveEvent;
import com.massivecraft.mcore5.store.Coll; import com.massivecraft.mcore5.store.Coll;
import com.massivecraft.mcore5.store.PlayerColl; import com.massivecraft.mcore5.store.PlayerColl;
import com.massivecraft.mcore5.store.SenderColl;
import com.massivecraft.mcore5.util.SmokeUtil; import com.massivecraft.mcore5.util.SmokeUtil;
public class InternalListener implements Listener public class InternalListener implements Listener
@ -71,26 +72,39 @@ public class InternalListener implements Listener
} }
// -------------------------------------------- // // -------------------------------------------- //
// PLAYER REFERENCES // PLAYER AND SENDER REFERENCES
// -------------------------------------------- // // -------------------------------------------- //
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
public void playerReferencesLoginLowest(PlayerLoginEvent event) public void playerReferencesLoginLowest(PlayerLoginEvent event)
{ {
PlayerColl.setPlayerRefferences(event.getPlayer().getName(), event.getPlayer()); String id = event.getPlayer().getName();
Player player = event.getPlayer();
PlayerColl.setPlayerRefferences(id, player);
SenderColl.setSenderRefferences(id, player);
} }
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void playerReferencesLoginMonitor(PlayerLoginEvent event) public void playerReferencesLoginMonitor(PlayerLoginEvent event)
{ {
if (event.getResult() == Result.ALLOWED) return; if (event.getResult() == Result.ALLOWED) return;
PlayerColl.setPlayerRefferences(event.getPlayer().getName(), null);
String id = event.getPlayer().getName();
Player player = null;
PlayerColl.setPlayerRefferences(id, player);
SenderColl.setSenderRefferences(id, player);
} }
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void playerReferencesQuitMonitor(PlayerQuitEvent event) public void playerReferencesQuitMonitor(PlayerQuitEvent event)
{ {
PlayerColl.setPlayerRefferences(event.getPlayer().getName(), null); String id = event.getPlayer().getName();
Player player = null;
PlayerColl.setPlayerRefferences(id, player);
SenderColl.setSenderRefferences(id, player);
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -113,10 +113,10 @@ public class MCore extends MPlugin
// Register commands // Register commands
this.cmdUsys = new CmdUsys(); this.cmdUsys = new CmdUsys();
this.cmdUsys.register(true); this.cmdUsys.register(this, true);
this.cmdMcore = new CmdMcore(); this.cmdMcore = new CmdMcore();
this.cmdMcore.register(true); this.cmdMcore.register(this, true);
this.postEnable(); this.postEnable();
} }

View File

@ -26,9 +26,7 @@ public class PSTeleporterDefault implements PSTeleporter
// INSTANCE // INSTANCE
// -------------------------------------------- // // -------------------------------------------- //
protected static PSTeleporterDefault instance = new PSTeleporterDefault(); protected static PSTeleporterDefault i = new PSTeleporterDefault();
public static PSTeleporterDefault get() public static PSTeleporterDefault get() { return i; }
{
return instance;
}
} }

View File

@ -3,25 +3,25 @@ package com.massivecraft.mcore5.cmd;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.mcore5.MPlugin;
import com.massivecraft.mcore5.util.Txt; import com.massivecraft.mcore5.util.Txt;
public class BukkitGlueCommand extends Command public class BukkitGlueCommand extends Command
{ {
protected MCommand mcommand; public final MCommand mcommand;
public BukkitGlueCommand(MCommand mcommand) public final MPlugin mplugin;
public BukkitGlueCommand(MCommand mcommand, MPlugin mplugin)
{ {
super(mcommand.getAliases().get(0), mcommand.getDesc(), mcommand.getUseageTemplate(), mcommand.getAliases()); super(mcommand.getAliases().get(0), mcommand.getDesc(), mcommand.getUseageTemplate(), mcommand.getAliases());
this.mcommand = mcommand; this.mcommand = mcommand;
this.mplugin = mplugin;
} }
@Override @Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) public boolean execute(CommandSender sender, String commandLabel, String[] args)
{ {
if ( ! mcommand.p().isEnabled()) if ( ! mplugin.isEnabled()) return false;
{
return false;
}
this.mcommand.execute(sender, Txt.tokenizeArguments(Txt.implode(args, " "))); this.mcommand.execute(sender, Txt.tokenizeArguments(Txt.implode(args, " ")));
return true; return true;
} }

View File

@ -21,10 +21,4 @@ public class CmdMcore extends MCommand
this.msg("<i>You are running %s", MCore.p.getDescription().getFullName()); this.msg("<i>You are running %s", MCore.p.getDescription().getFullName());
this.msg("<i>The id of this server is \"<h>%s<i>\".", Conf.serverid); this.msg("<i>The id of this server is \"<h>%s<i>\".", Conf.serverid);
} }
@Override
public MCore p()
{
return MCore.p;
}
} }

View File

@ -2,7 +2,6 @@ package com.massivecraft.mcore5.cmd;
import java.util.ArrayList; import java.util.ArrayList;
import com.massivecraft.mcore5.MPlugin;
import com.massivecraft.mcore5.cmd.MCommand; import com.massivecraft.mcore5.cmd.MCommand;
import com.massivecraft.mcore5.cmd.arg.ARInteger; import com.massivecraft.mcore5.cmd.arg.ARInteger;
import com.massivecraft.mcore5.util.Txt; import com.massivecraft.mcore5.util.Txt;
@ -43,13 +42,6 @@ public class HelpCommand extends MCommand
sendMessage(Txt.getPage(lines, pagenumber, "Help for command \""+parentCommand.getAliases().get(0)+"\"", sender)); sendMessage(Txt.getPage(lines, pagenumber, "Help for command \""+parentCommand.getAliases().get(0)+"\"", sender));
} }
@Override
public MPlugin p()
{
if (this.commandChain.size() == 0) return null;
return this.commandChain.get(this.commandChain.size()-1).p();
}
// -------------------------------------------- // // -------------------------------------------- //
// INSTANCE // INSTANCE
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -12,6 +12,7 @@ import org.bukkit.command.SimpleCommandMap;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.massivecraft.mcore5.Lang; import com.massivecraft.mcore5.Lang;
import com.massivecraft.mcore5.MCore;
import com.massivecraft.mcore5.MPlugin; import com.massivecraft.mcore5.MPlugin;
import com.massivecraft.mcore5.cmd.arg.ArgReader; import com.massivecraft.mcore5.cmd.arg.ArgReader;
import com.massivecraft.mcore5.cmd.arg.ArgResult; import com.massivecraft.mcore5.cmd.arg.ArgResult;
@ -19,12 +20,11 @@ import com.massivecraft.mcore5.cmd.req.IReq;
import com.massivecraft.mcore5.cmd.req.ReqHasPerm; import com.massivecraft.mcore5.cmd.req.ReqHasPerm;
import com.massivecraft.mcore5.util.BukkitCommandUtil; import com.massivecraft.mcore5.util.BukkitCommandUtil;
import com.massivecraft.mcore5.util.PermUtil; import com.massivecraft.mcore5.util.PermUtil;
import com.massivecraft.mcore5.util.SenderUtil;
import com.massivecraft.mcore5.util.Txt; import com.massivecraft.mcore5.util.Txt;
public abstract class MCommand public abstract class MCommand
{ {
public abstract MPlugin p();
// -------------------------------------------- // // -------------------------------------------- //
// COMMAND BEHAVIOR // COMMAND BEHAVIOR
// -------------------------------------------- // // -------------------------------------------- //
@ -135,12 +135,22 @@ public abstract class MCommand
public boolean register() public boolean register()
{ {
return register(false); return register(MCore.p, false);
}
public boolean register(MPlugin mplugin)
{
return this.register(mplugin, false);
} }
public boolean register(boolean override) public boolean register(boolean override)
{ {
BukkitGlueCommand bgc = new BukkitGlueCommand(this); return this.register(MCore.p, override);
}
public boolean register(MPlugin mplugin, boolean override)
{
BukkitGlueCommand bgc = new BukkitGlueCommand(this, mplugin);
SimpleCommandMap scm = BukkitCommandUtil.getBukkitCommandMap(); SimpleCommandMap scm = BukkitCommandUtil.getBukkitCommandMap();
if (override) if (override)
@ -227,7 +237,7 @@ public abstract class MCommand
// -------------------------------------------- // // -------------------------------------------- //
// Call Validation // CALL VALIDATION
// -------------------------------------------- // // -------------------------------------------- //
/** /**
@ -303,7 +313,7 @@ public abstract class MCommand
} }
// -------------------------------------------- // // -------------------------------------------- //
// Help and Usage information // HELP AND USAGE INFORMATION
// -------------------------------------------- // // -------------------------------------------- //
public String getUseageTemplate(List<MCommand> commandChain, boolean addDesc, boolean onlyFirstAlias) public String getUseageTemplate(List<MCommand> commandChain, boolean addDesc, boolean onlyFirstAlias)
@ -383,42 +393,45 @@ public abstract class MCommand
} }
// -------------------------------------------- // // -------------------------------------------- //
// Message Sending Helpers // MESSAGE SENDING HELPERS
// -------------------------------------------- // // -------------------------------------------- //
public void msg(String str, Object... args) // CONVENIENCE SEND MESSAGE
public boolean sendMessage(String message)
{ {
sender.sendMessage(Txt.parse(str, args)); return SenderUtil.sendMessage(this.sender, message);
} }
public void msg(String str) public boolean sendMessage(String... messages)
{ {
sender.sendMessage(Txt.parse(str)); return SenderUtil.sendMessage(this.sender, messages);
} }
public void msg(Collection<String> msgs) public boolean sendMessage(Collection<String> messages)
{ {
for(String msg : msgs) return SenderUtil.sendMessage(this.sender, messages);
{
this.msg(msg);
}
} }
public void sendMessage(String msg) // CONVENIENCE MSG
public boolean msg(String msg)
{ {
sender.sendMessage(msg); return SenderUtil.msg(this.sender, msg);
} }
public void sendMessage(Collection<String> msgs) public boolean msg(String msg, Object... args)
{ {
for(String msg : msgs) return SenderUtil.msg(this.sender, msg, args);
{
this.sendMessage(msg);
} }
public boolean msg(Collection<String> msgs)
{
return SenderUtil.msg(this.sender, msgs);
} }
// -------------------------------------------- // // -------------------------------------------- //
// Argument Readers // ARGUMENT READERS
// -------------------------------------------- // // -------------------------------------------- //
public String argConcatFrom(int idx) public String argConcatFrom(int idx)
@ -458,63 +471,4 @@ public abstract class MCommand
if (result.hasErrors()) this.msg(result.getErrors()); if (result.hasErrors()) this.msg(result.getErrors());
return result.getResult(); return result.getResult();
} }
// -------------------------------------------- //
// Argument Readers DEPRACATED TODO
// -------------------------------------------- //
/*@Deprecated
public synchronized <T> T argAs(int idx, Class<T> clazz, String style, T defaultNotSet, T defaultNotFound)
{
if ( ! this.argIsSet(idx))
{
return defaultNotSet;
}
IArgHandler<T> handler = p().cmd.getArgHandler(clazz);
if (handler == null)
{
p().log(Level.SEVERE, "There is no ArgHandler for " + clazz.getName());
}
T ret = handler.parse(this.arg(idx), style, this.sender, p());
if (ret == null)
{
this.msg(handler.getErrors());
return defaultNotFound;
}
return ret;
}
@Deprecated
public <T> T argAs(int idx, Class<T> clazz, T defaultNotSet, T defaultNotFound)
{
return this.argAs(idx, clazz, null, defaultNotSet, defaultNotFound);
}
@Deprecated
public <T> T argAs(int idx, Class<T> clazz, String style, T defaultNotSet)
{
return this.argAs(idx, clazz, style, defaultNotSet, null);
}
@Deprecated
public <T> T argAs(int idx, Class<T> clazz, T defaultNotSet)
{
return this.argAs(idx, clazz, null, defaultNotSet, null);
}
@Deprecated
public <T> T argAs(int idx, Class<T> clazz, String style)
{
return this.argAs(idx, clazz, style, null, null);
}
@Deprecated
public <T> T argAs(int idx, Class<T> clazz)
{
return this.argAs(idx, clazz, (T)null, null);
}
*/
} }

View File

@ -0,0 +1,54 @@
package com.massivecraft.mcore5.sender;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.PermissibleBase;
public class BasicCommandSender extends PermissibleBase implements CommandSender
{
private final String name;
public BasicCommandSender(String name, boolean op, boolean opChangeable)
{
super(new BasicServerOperator(name, op, opChangeable));
this.name = name;
}
@Override
public void setOp(boolean value)
{
boolean before = this.isOp();
super.setOp(value);
boolean after = this.isOp();
if (before == after) return;
this.recalculatePermissions();
}
@Override
public String getName()
{
return this.name;
}
@Override
public Server getServer()
{
return Bukkit.getServer();
}
@Override
public void sendMessage(String message)
{
// Per default
}
@Override
public void sendMessage(String[] messages)
{
for (String message : messages)
{
this.sendMessage(message);
}
}
}

View File

@ -0,0 +1,33 @@
package com.massivecraft.mcore5.sender;
import org.bukkit.permissions.ServerOperator;
public class BasicServerOperator implements ServerOperator
{
private String name;
private boolean op;
private boolean changeable;
public BasicServerOperator(String name, boolean op, boolean opChangeable)
{
this.name = name;
this.op = op;
this.changeable = opChangeable;
}
@Override
public boolean isOp()
{
return this.op;
}
@Override
public void setOp(boolean value)
{
if (!this.changeable)
{
throw new UnsupportedOperationException("Cannot change operator status for "+this.name);
}
this.op = value;
}
}

View File

@ -0,0 +1,27 @@
package com.massivecraft.mcore5.sender;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.command.BlockCommandSender;
public class FakeBlockCommandSender extends BasicCommandSender implements BlockCommandSender
{
public FakeBlockCommandSender()
{
super("@", true, false);
}
@Override
public Block getBlock()
{
return Bukkit.getWorlds().get(0).getBlockAt(0, 0, 0);
}
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
private static FakeBlockCommandSender i = new FakeBlockCommandSender();
public static FakeBlockCommandSender get() { return i; }
}

View File

@ -142,10 +142,14 @@ public class Coll<E, L extends Comparable<? super L>> implements CollInterface<E
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({ "rawtypes", "unchecked" })
public void copy(Object ofrom, Object oto) public void copy(Object ofrom, Object oto)
{ {
if (ofrom == null) throw new NullPointerException("ofrom");
if (oto == null) throw new NullPointerException("oto");
if (ofrom instanceof Entity) if (ofrom instanceof Entity)
{ {
Entity efrom = (Entity)ofrom; Entity efrom = (Entity)ofrom;
Entity eto = (Entity)oto; Entity eto = (Entity)oto;
eto.load(efrom); eto.load(efrom);
} }
else else

View File

@ -17,7 +17,7 @@ public class JsonFileFilter implements FileFilter
// INSTANCE // INSTANCE
// -------------------------------------------- // // -------------------------------------------- //
private static JsonFileFilter instance = new JsonFileFilter(); private static JsonFileFilter i = new JsonFileFilter();
public static JsonFileFilter get() { return instance; } public static JsonFileFilter get() { return i; }
} }

View File

@ -0,0 +1,157 @@
package com.massivecraft.mcore5.store;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import org.bukkit.command.CommandSender;
import com.massivecraft.mcore5.MPlugin;
import com.massivecraft.mcore5.Predictate;
import com.massivecraft.mcore5.cmd.arg.ARStringEntity;
import com.massivecraft.mcore5.cmd.arg.ARStringMatchFullCI;
import com.massivecraft.mcore5.cmd.arg.ARStringMatchStartCI;
import com.massivecraft.mcore5.cmd.arg.ArgReader;
import com.massivecraft.mcore5.util.MUtil;
import com.massivecraft.mcore5.util.PlayerUtil;
import com.massivecraft.mcore5.util.SenderUtil;
public class SenderColl<E extends SenderEntity<E>> extends Coll<E, String>
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
public final static boolean DEFAULT_LOWERCASING = true;
public final static boolean DEFAULT_CREATIVE = true;
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
// "Lowercasing" means that the ids are always converted to lower case when fixed.
// This is highly recommended for sender colls.
// The senderIds are case insensitive by nature and some times you simply can't know the correct casing.
protected boolean lowercasing;
public boolean isLowercasing() { return this.lowercasing; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public SenderColl(Db<?> db, MPlugin mplugin, String name, Class<E> entityClass, boolean creative, boolean lowercasing, Comparator<? super String> idComparator, Comparator<? super E> entityComparator)
{
super(db, mplugin, "ai", name, entityClass, String.class, creative, idComparator, entityComparator);
this.lowercasing = lowercasing;
}
public SenderColl(Db<?> db, MPlugin mplugin, String name, Class<E> entityClass, boolean creative, boolean lowercasing)
{
this(db, mplugin, name, entityClass, creative, lowercasing, null, null);
}
public SenderColl(Db<?> db, MPlugin mplugin, String name, Class<E> entityClass, boolean creative)
{
this(db, mplugin, name, entityClass, creative, DEFAULT_LOWERCASING);
}
public SenderColl(Db<?> db, MPlugin mplugin, String name, Class<E> entityClass)
{
this(db, mplugin, name, entityClass, DEFAULT_CREATIVE);
}
// -------------------------------------------- //
// EXTRAS
// -------------------------------------------- //
@Override
public String fixId(Object oid)
{
if (oid == null) return null;
String ret = MUtil.extract(String.class, "senderId", oid);
if (ret == null) return ret;
return this.lowercasing ? ret.toLowerCase() : ret;
}
public Collection<E> getAllOnline()
{
return this.getAll(new Predictate<E>()
{
public boolean apply(E entity)
{
return entity.isOnline();
}
});
}
public Collection<E> getAllOffline()
{
return this.getAll(new Predictate<E>()
{
public boolean apply(E entity)
{
return entity.isOffline();
}
});
}
// -------------------------------------------- //
// SENDER REFFERENCE MANAGEMENT
// -------------------------------------------- //
protected void setSenderRefference(String senderId, CommandSender sender)
{
E senderEntity = this.get(senderId, false);
if (senderEntity == null) return;
senderEntity.sender = sender;
senderEntity.senderInitiated = true;
}
public static void setSenderRefferences(String senderId, CommandSender sender)
{
for (Coll<?, ?> coll : Coll.instances)
{
if (!(coll instanceof SenderColl)) continue;
SenderColl<?> senderColl = (SenderColl<?>)coll;
senderColl.setSenderRefference(senderId, sender);
}
}
// -------------------------------------------- //
// ARGUMENT READERS
// -------------------------------------------- //
protected Collection<Collection<String>> forgeAltColls()
{
Collection<Collection<String>> ret = new ArrayList<Collection<String>>();
ret.add(this.getIds());
if (this.isCreative())
{
ret.add(PlayerUtil.getAllVisitorNames());
ret.add(SenderUtil.getIdToSender().keySet());
}
return ret;
}
public ArgReader<String> argReaderSenderIdFull()
{
return new ARStringMatchFullCI("player", this.forgeAltColls());
}
public ArgReader<String> argReaderSenderIdStart()
{
return new ARStringMatchStartCI("player", this.forgeAltColls());
}
public ArgReader<E> argReaderEntityFull()
{
return new ARStringEntity<E>(this.argReaderSenderIdFull(), this);
}
public ArgReader<E> argReaderEntityStart()
{
return new ARStringEntity<E>(this.argReaderSenderIdStart(), this);
}
}

View File

@ -0,0 +1,196 @@
package com.massivecraft.mcore5.store;
import java.util.Collection;
import org.bukkit.GameMode;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.RemoteConsoleCommandSender;
import org.bukkit.entity.Player;
import com.massivecraft.mcore5.util.SenderUtil;
public abstract class SenderEntity<E extends SenderEntity<E>> extends Entity<E, String>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
// We keep a reference to the sender to provide fast "reverse lookup".
// The sender reference is initiated here and is kept updated using the InternalListener
protected transient CommandSender sender = null;
protected transient boolean senderInitiated = false;
public CommandSender getSender()
{
if ( ! this.senderInitiated)
{
this.sender = SenderUtil.getSender(this.getId());
this.senderInitiated = true;
}
return this.sender;
}
// -------------------------------------------- //
// OVERRIDES
// -------------------------------------------- //
@Override
public SenderColl<E> getColl()
{
return (SenderColl<E>) super.getColl();
}
// -------------------------------------------- //
// SENDER UTIL METHOD MIRRORING
// -------------------------------------------- //
// IS
public boolean isSender()
{
return SenderUtil.isSenderId(this.getId());
}
public boolean isPlayer()
{
return SenderUtil.isPlayerId(this.getId());
}
public boolean isConsole()
{
return SenderUtil.isConsoleId(this.getId());
}
public boolean isRcon()
{
return SenderUtil.isRconId(this.getId());
}
public boolean isBlock()
{
return SenderUtil.isBlockId(this.getId());
}
public boolean isNonplayer()
{
return SenderUtil.isNonplayerId(this.getId());
}
public boolean isStandardNonplayer()
{
return SenderUtil.isStandardNonplayerId(this.getId());
}
public boolean isNonstandardNonplayer()
{
return SenderUtil.isNonstandardNonplayerId(this.getId());
}
// GET
// TODO: Usage of sender instead of id here is cheating but is good performance-wise so it can be ok.
public Player getPlayer()
{
return SenderUtil.getAsPlayer(this.getSender());
}
public ConsoleCommandSender getConsole()
{
return SenderUtil.getAsConsole(this.getSender());
}
public RemoteConsoleCommandSender getRcon()
{
return SenderUtil.getAsRcon(this.getSender());
}
public BlockCommandSender getBlock()
{
return SenderUtil.getAsBlock(this.getSender());
}
// ONLINE / OFFLINE
public boolean isOnline()
{
return this.getSender() != null;
}
public boolean isOffline()
{
return ! isOnline();
}
// DISPLAY NAME
public String getDisplayName()
{
return SenderUtil.getDisplayName(this.getId());
}
public void setDisplayName(String displayName)
{
SenderUtil.setDisplayName(this.getId(), displayName);
}
// LIST NAME
public String getListName()
{
return SenderUtil.getListName(this.getId());
}
public void setListName(String listName)
{
SenderUtil.setListName(this.getId(), listName);
}
// CONVENIENCE SEND MESSAGE
public boolean sendMessage(String message)
{
return SenderUtil.sendMessage(this.getId(), message);
}
public boolean sendMessage(String... messages)
{
return SenderUtil.sendMessage(this.getId(), messages);
}
public boolean sendMessage(Collection<String> messages)
{
return SenderUtil.sendMessage(this.getId(), messages);
}
// CONVENIENCE MSG
public boolean msg(String msg)
{
return SenderUtil.msg(this.getId(), msg);
}
public boolean msg(String msg, Object... args)
{
return SenderUtil.msg(this.getId(), msg, args);
}
public boolean msg(Collection<String> msgs)
{
return SenderUtil.msg(this.getId(), msgs);
}
// CONVENIENCE GAME-MODE
public GameMode getGameMode(GameMode def)
{
return SenderUtil.getGameMode(this.getId(), def);
}
public boolean isGameMode(GameMode gm, boolean def)
{
return SenderUtil.isGameMode(this.getId(), gm, def);
}
}

View File

@ -19,6 +19,8 @@ public class StoreAdapterGson extends StoreAdapterAbstract
@Override @Override
public void write(Coll<?, ?> coll, Object raw, Object entity) public void write(Coll<?, ?> coll, Object raw, Object entity)
{ {
if (raw == null) throw new NullPointerException("raw");
if (entity == null) throw new NullPointerException("entity");
Object temp = coll.getMplugin().gson.fromJson((JsonElement)raw, coll.getEntityClass()); Object temp = coll.getMplugin().gson.fromJson((JsonElement)raw, coll.getEntityClass());
coll.copy(temp, entity); coll.copy(temp, entity);
} }

View File

@ -3,9 +3,10 @@ package com.massivecraft.mcore5.usys.cmd;
import com.massivecraft.mcore5.Conf; import com.massivecraft.mcore5.Conf;
import com.massivecraft.mcore5.Permission; import com.massivecraft.mcore5.Permission;
import com.massivecraft.mcore5.cmd.HelpCommand; import com.massivecraft.mcore5.cmd.HelpCommand;
import com.massivecraft.mcore5.cmd.MCommand;
import com.massivecraft.mcore5.cmd.req.ReqHasPerm; import com.massivecraft.mcore5.cmd.req.ReqHasPerm;
public class CmdUsys extends UsysCommand public class CmdUsys extends MCommand
{ {
public final static String USYS = "usys"; public final static String USYS = "usys";

View File

@ -2,9 +2,10 @@ package com.massivecraft.mcore5.usys.cmd;
import com.massivecraft.mcore5.Permission; import com.massivecraft.mcore5.Permission;
import com.massivecraft.mcore5.cmd.HelpCommand; import com.massivecraft.mcore5.cmd.HelpCommand;
import com.massivecraft.mcore5.cmd.MCommand;
import com.massivecraft.mcore5.cmd.req.ReqHasPerm; import com.massivecraft.mcore5.cmd.req.ReqHasPerm;
public class CmdUsysAspect extends UsysCommand public class CmdUsysAspect extends MCommand
{ {
public CmdUsysAspectList cmdUsysAspectList = new CmdUsysAspectList(); public CmdUsysAspectList cmdUsysAspectList = new CmdUsysAspectList();
public CmdUsysAspectShow cmdUsysAspectShow = new CmdUsysAspectShow(); public CmdUsysAspectShow cmdUsysAspectShow = new CmdUsysAspectShow();

View File

@ -4,13 +4,14 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.massivecraft.mcore5.Permission; import com.massivecraft.mcore5.Permission;
import com.massivecraft.mcore5.cmd.MCommand;
import com.massivecraft.mcore5.cmd.arg.ARInteger; import com.massivecraft.mcore5.cmd.arg.ARInteger;
import com.massivecraft.mcore5.cmd.req.ReqHasPerm; import com.massivecraft.mcore5.cmd.req.ReqHasPerm;
import com.massivecraft.mcore5.usys.Aspect; import com.massivecraft.mcore5.usys.Aspect;
import com.massivecraft.mcore5.usys.AspectColl; import com.massivecraft.mcore5.usys.AspectColl;
import com.massivecraft.mcore5.util.Txt; import com.massivecraft.mcore5.util.Txt;
public class CmdUsysAspectList extends UsysCommand public class CmdUsysAspectList extends MCommand
{ {
public CmdUsysAspectList() public CmdUsysAspectList()
{ {

View File

@ -1,12 +1,13 @@
package com.massivecraft.mcore5.usys.cmd; package com.massivecraft.mcore5.usys.cmd;
import com.massivecraft.mcore5.Permission; import com.massivecraft.mcore5.Permission;
import com.massivecraft.mcore5.cmd.MCommand;
import com.massivecraft.mcore5.cmd.arg.ARAspect; import com.massivecraft.mcore5.cmd.arg.ARAspect;
import com.massivecraft.mcore5.cmd.req.ReqHasPerm; import com.massivecraft.mcore5.cmd.req.ReqHasPerm;
import com.massivecraft.mcore5.usys.Aspect; import com.massivecraft.mcore5.usys.Aspect;
import com.massivecraft.mcore5.util.Txt; import com.massivecraft.mcore5.util.Txt;
public class CmdUsysAspectShow extends UsysCommand public class CmdUsysAspectShow extends MCommand
{ {
public CmdUsysAspectShow() public CmdUsysAspectShow()
{ {

View File

@ -1,13 +1,14 @@
package com.massivecraft.mcore5.usys.cmd; package com.massivecraft.mcore5.usys.cmd;
import com.massivecraft.mcore5.Permission; import com.massivecraft.mcore5.Permission;
import com.massivecraft.mcore5.cmd.MCommand;
import com.massivecraft.mcore5.cmd.arg.ARAspect; import com.massivecraft.mcore5.cmd.arg.ARAspect;
import com.massivecraft.mcore5.cmd.arg.ARMultiverse; import com.massivecraft.mcore5.cmd.arg.ARMultiverse;
import com.massivecraft.mcore5.cmd.req.ReqHasPerm; import com.massivecraft.mcore5.cmd.req.ReqHasPerm;
import com.massivecraft.mcore5.usys.Aspect; import com.massivecraft.mcore5.usys.Aspect;
import com.massivecraft.mcore5.usys.Multiverse; import com.massivecraft.mcore5.usys.Multiverse;
public class CmdUsysAspectUse extends UsysCommand public class CmdUsysAspectUse extends MCommand
{ {
public CmdUsysAspectUse() public CmdUsysAspectUse()
{ {

View File

@ -2,9 +2,10 @@ package com.massivecraft.mcore5.usys.cmd;
import com.massivecraft.mcore5.Permission; import com.massivecraft.mcore5.Permission;
import com.massivecraft.mcore5.cmd.HelpCommand; import com.massivecraft.mcore5.cmd.HelpCommand;
import com.massivecraft.mcore5.cmd.MCommand;
import com.massivecraft.mcore5.cmd.req.ReqHasPerm; import com.massivecraft.mcore5.cmd.req.ReqHasPerm;
public class CmdUsysMultiverse extends UsysCommand public class CmdUsysMultiverse extends MCommand
{ {
public CmdUsysMultiverseList cmdUsysMultiverseList = new CmdUsysMultiverseList(); public CmdUsysMultiverseList cmdUsysMultiverseList = new CmdUsysMultiverseList();
public CmdUsysMultiverseShow cmdUsysMultiverseShow = new CmdUsysMultiverseShow(); public CmdUsysMultiverseShow cmdUsysMultiverseShow = new CmdUsysMultiverseShow();

View File

@ -2,11 +2,12 @@ package com.massivecraft.mcore5.usys.cmd;
import com.massivecraft.mcore5.MCore; import com.massivecraft.mcore5.MCore;
import com.massivecraft.mcore5.Permission; import com.massivecraft.mcore5.Permission;
import com.massivecraft.mcore5.cmd.MCommand;
import com.massivecraft.mcore5.cmd.arg.ARMultiverse; import com.massivecraft.mcore5.cmd.arg.ARMultiverse;
import com.massivecraft.mcore5.cmd.req.ReqHasPerm; import com.massivecraft.mcore5.cmd.req.ReqHasPerm;
import com.massivecraft.mcore5.usys.Multiverse; import com.massivecraft.mcore5.usys.Multiverse;
public class CmdUsysMultiverseDel extends UsysCommand public class CmdUsysMultiverseDel extends MCommand
{ {
public CmdUsysMultiverseDel() public CmdUsysMultiverseDel()
{ {

View File

@ -4,13 +4,14 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.massivecraft.mcore5.Permission; import com.massivecraft.mcore5.Permission;
import com.massivecraft.mcore5.cmd.MCommand;
import com.massivecraft.mcore5.cmd.arg.ARInteger; import com.massivecraft.mcore5.cmd.arg.ARInteger;
import com.massivecraft.mcore5.cmd.req.ReqHasPerm; import com.massivecraft.mcore5.cmd.req.ReqHasPerm;
import com.massivecraft.mcore5.usys.Multiverse; import com.massivecraft.mcore5.usys.Multiverse;
import com.massivecraft.mcore5.usys.MultiverseColl; import com.massivecraft.mcore5.usys.MultiverseColl;
import com.massivecraft.mcore5.util.Txt; import com.massivecraft.mcore5.util.Txt;
public class CmdUsysMultiverseList extends UsysCommand public class CmdUsysMultiverseList extends MCommand
{ {
public CmdUsysMultiverseList() public CmdUsysMultiverseList()
{ {

View File

@ -1,10 +1,11 @@
package com.massivecraft.mcore5.usys.cmd; package com.massivecraft.mcore5.usys.cmd;
import com.massivecraft.mcore5.Permission; import com.massivecraft.mcore5.Permission;
import com.massivecraft.mcore5.cmd.MCommand;
import com.massivecraft.mcore5.cmd.req.ReqHasPerm; import com.massivecraft.mcore5.cmd.req.ReqHasPerm;
import com.massivecraft.mcore5.usys.MultiverseColl; import com.massivecraft.mcore5.usys.MultiverseColl;
public class CmdUsysMultiverseNew extends UsysCommand public class CmdUsysMultiverseNew extends MCommand
{ {
public CmdUsysMultiverseNew() public CmdUsysMultiverseNew()
{ {

View File

@ -5,13 +5,14 @@ import java.util.List;
import com.massivecraft.mcore5.MCore; import com.massivecraft.mcore5.MCore;
import com.massivecraft.mcore5.Permission; import com.massivecraft.mcore5.Permission;
import com.massivecraft.mcore5.cmd.MCommand;
import com.massivecraft.mcore5.cmd.arg.ARMultiverse; import com.massivecraft.mcore5.cmd.arg.ARMultiverse;
import com.massivecraft.mcore5.cmd.req.ReqHasPerm; import com.massivecraft.mcore5.cmd.req.ReqHasPerm;
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.Txt; import com.massivecraft.mcore5.util.Txt;
public class CmdUsysMultiverseShow extends UsysCommand public class CmdUsysMultiverseShow extends MCommand
{ {
public CmdUsysMultiverseShow() public CmdUsysMultiverseShow()
{ {

View File

@ -2,9 +2,10 @@ package com.massivecraft.mcore5.usys.cmd;
import com.massivecraft.mcore5.Permission; import com.massivecraft.mcore5.Permission;
import com.massivecraft.mcore5.cmd.HelpCommand; import com.massivecraft.mcore5.cmd.HelpCommand;
import com.massivecraft.mcore5.cmd.MCommand;
import com.massivecraft.mcore5.cmd.req.ReqHasPerm; import com.massivecraft.mcore5.cmd.req.ReqHasPerm;
public class CmdUsysUniverse extends UsysCommand public class CmdUsysUniverse extends MCommand
{ {
public CmdUsysUniverseNew cmdUsysUniverseNew = new CmdUsysUniverseNew(); public CmdUsysUniverseNew cmdUsysUniverseNew = new CmdUsysUniverseNew();
public CmdUsysUniverseDel cmdUsysUniverseDel = new CmdUsysUniverseDel(); public CmdUsysUniverseDel cmdUsysUniverseDel = new CmdUsysUniverseDel();

View File

@ -2,11 +2,12 @@ package com.massivecraft.mcore5.usys.cmd;
import com.massivecraft.mcore5.MCore; import com.massivecraft.mcore5.MCore;
import com.massivecraft.mcore5.Permission; import com.massivecraft.mcore5.Permission;
import com.massivecraft.mcore5.cmd.MCommand;
import com.massivecraft.mcore5.cmd.arg.ARMultiverse; import com.massivecraft.mcore5.cmd.arg.ARMultiverse;
import com.massivecraft.mcore5.cmd.req.ReqHasPerm; import com.massivecraft.mcore5.cmd.req.ReqHasPerm;
import com.massivecraft.mcore5.usys.Multiverse; import com.massivecraft.mcore5.usys.Multiverse;
public class CmdUsysUniverseClear extends UsysCommand public class CmdUsysUniverseClear extends MCommand
{ {
public CmdUsysUniverseClear() public CmdUsysUniverseClear()
{ {

View File

@ -2,11 +2,12 @@ package com.massivecraft.mcore5.usys.cmd;
import com.massivecraft.mcore5.MCore; import com.massivecraft.mcore5.MCore;
import com.massivecraft.mcore5.Permission; import com.massivecraft.mcore5.Permission;
import com.massivecraft.mcore5.cmd.MCommand;
import com.massivecraft.mcore5.cmd.arg.ARMultiverse; import com.massivecraft.mcore5.cmd.arg.ARMultiverse;
import com.massivecraft.mcore5.cmd.req.ReqHasPerm; import com.massivecraft.mcore5.cmd.req.ReqHasPerm;
import com.massivecraft.mcore5.usys.Multiverse; import com.massivecraft.mcore5.usys.Multiverse;
public class CmdUsysUniverseDel extends UsysCommand public class CmdUsysUniverseDel extends MCommand
{ {
public CmdUsysUniverseDel() public CmdUsysUniverseDel()
{ {

View File

@ -1,11 +1,12 @@
package com.massivecraft.mcore5.usys.cmd; package com.massivecraft.mcore5.usys.cmd;
import com.massivecraft.mcore5.Permission; import com.massivecraft.mcore5.Permission;
import com.massivecraft.mcore5.cmd.MCommand;
import com.massivecraft.mcore5.cmd.arg.ARMultiverse; import com.massivecraft.mcore5.cmd.arg.ARMultiverse;
import com.massivecraft.mcore5.cmd.req.ReqHasPerm; import com.massivecraft.mcore5.cmd.req.ReqHasPerm;
import com.massivecraft.mcore5.usys.Multiverse; import com.massivecraft.mcore5.usys.Multiverse;
public class CmdUsysUniverseNew extends UsysCommand public class CmdUsysUniverseNew extends MCommand
{ {
public CmdUsysUniverseNew() public CmdUsysUniverseNew()
{ {

View File

@ -1,11 +1,12 @@
package com.massivecraft.mcore5.usys.cmd; package com.massivecraft.mcore5.usys.cmd;
import com.massivecraft.mcore5.Permission; import com.massivecraft.mcore5.Permission;
import com.massivecraft.mcore5.cmd.MCommand;
import com.massivecraft.mcore5.cmd.arg.ARMultiverse; import com.massivecraft.mcore5.cmd.arg.ARMultiverse;
import com.massivecraft.mcore5.cmd.req.ReqHasPerm; import com.massivecraft.mcore5.cmd.req.ReqHasPerm;
import com.massivecraft.mcore5.usys.Multiverse; import com.massivecraft.mcore5.usys.Multiverse;
public class CmdUsysWorld extends UsysCommand public class CmdUsysWorld extends MCommand
{ {
public CmdUsysWorld() public CmdUsysWorld()
{ {

View File

@ -1,20 +0,0 @@
package com.massivecraft.mcore5.usys.cmd;
import com.massivecraft.mcore5.MCore;
import com.massivecraft.mcore5.cmd.MCommand;
public abstract class UsysCommand extends MCommand
{
public MCore p;
public UsysCommand()
{
super();
this.p = MCore.p;
}
@Override
public MCore p()
{
return MCore.p;
}
}

View File

@ -18,6 +18,7 @@ import java.util.TreeSet;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.CommandSender;
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;
@ -37,6 +38,8 @@ import com.massivecraft.mcore5.MCore;
import com.massivecraft.mcore5.util.extractor.Extractor; import com.massivecraft.mcore5.util.extractor.Extractor;
import com.massivecraft.mcore5.util.extractor.ExtractorPlayer; import com.massivecraft.mcore5.util.extractor.ExtractorPlayer;
import com.massivecraft.mcore5.util.extractor.ExtractorPlayerName; import com.massivecraft.mcore5.util.extractor.ExtractorPlayerName;
import com.massivecraft.mcore5.util.extractor.ExtractorSender;
import com.massivecraft.mcore5.util.extractor.ExtractorSenderId;
import com.massivecraft.mcore5.util.extractor.ExtractorWorld; import com.massivecraft.mcore5.util.extractor.ExtractorWorld;
import com.massivecraft.mcore5.util.extractor.ExtractorWorldName; import com.massivecraft.mcore5.util.extractor.ExtractorWorldName;
@ -366,9 +369,13 @@ public class MUtil
static static
{ {
registerExtractor(World.class, "world", new ExtractorWorld()); registerExtractor(CommandSender.class, "sender", ExtractorSender.get());
registerExtractor(String.class, "worldName", new ExtractorWorldName()); registerExtractor(String.class, "senderId", ExtractorSenderId.get());
registerExtractor(Player.class, "player", new ExtractorPlayer());
registerExtractor(String.class, "playerName", new ExtractorPlayerName()); registerExtractor(Player.class, "player", ExtractorPlayer.get());
registerExtractor(String.class, "playerName", ExtractorPlayerName.get());
registerExtractor(World.class, "world", ExtractorWorld.get());
registerExtractor(String.class, "worldName", ExtractorWorldName.get());
} }
} }

View File

@ -6,6 +6,7 @@ import java.util.Map.Entry;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.permissions.Permissible;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.permissions.PermissionDefault;
@ -18,30 +19,34 @@ public class PermUtil
// HAS // HAS
// -------------------------------------------- // // -------------------------------------------- //
public static boolean has(CommandSender sender, Permission permission) public static boolean has(Permissible permissable, Permission permission)
{ {
return has(sender, permission.getName()); return has(permissable, permission.getName());
} }
public static boolean has(CommandSender sender, String perm) public static boolean has(Permissible permissable, String perm)
{ {
if (sender == null) return false; if (permissable == null) return false;
return sender.hasPermission(perm); return permissable.hasPermission(perm);
} }
public static boolean has(CommandSender sender, Permission permission, boolean verbose) public static boolean has(Permissible permissable, Permission permission, boolean verbose)
{ {
return has(sender, permission.getName(), verbose); return has(permissable, permission.getName(), verbose);
} }
public static boolean has(CommandSender sender, String perm, boolean verbose) public static boolean has(Permissible permissable, String perm, boolean verbose)
{ {
if (has(sender, perm)) if (has(permissable, perm))
{ {
return true; return true;
} }
else if (verbose && sender != null) else if (verbose && permissable != null)
{ {
if (permissable instanceof CommandSender)
{
CommandSender sender = (CommandSender)permissable;
sender.sendMessage(getForbiddenMessage(perm)); sender.sendMessage(getForbiddenMessage(perm));
} }
}
return false; return false;
} }

View File

@ -0,0 +1,570 @@
package com.massivecraft.mcore5.util;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Pattern;
import net.minecraft.server.v1_4_6.MinecraftServer;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Server;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.RemoteConsoleCommandSender;
import org.bukkit.craftbukkit.v1_4_6.CraftServer;
import org.bukkit.entity.Player;
import com.massivecraft.mcore5.sender.FakeBlockCommandSender;
import com.massivecraft.mcore5.store.SenderColl;
/**
* This Util was created to fill out the void between the Player interface and other CommandSenders.
*
* +++ The ID +++
* We add an ID <--> CommandSender lookup feature.
* Each player has an id which is the name of the player. Players are retrievable by id using Bukkit.getPlayerExact().
* Other command senders have no true id. We make it so they have.
* Non-player-sender-ids always start with and ampersand (@). This is to avoid clashes with regular player names.
* The id is simply "@"+CommandSender.getName() with exception for the block command sender which we call "@block".
* Non standard CommandSenders must be manually registered to the util using the register method.
*
* +++ The DisplayName and ListName +++
* CommandSenders can have DisplayName and ListName just like normal Player.
*
* +++ Online/Offline +++
* Players may be Online/Offline. We allow CommandSenders to be Online/Offline as well.
* This is simply done by stating that everything non-player in online all the time.
* The ConsoleCommandSender is for example always online and never offline.
*
* +++ Easy sendMessage and dispatchCommand +++
* This feature isn't new "fake fields" like the ones above.
* Its a suite of useful utility methods for sending messages and dispatching commands as a certain sender.
*
*/
public class SenderUtil
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
// The id prefix
public final static String IDPREFIX = "@";
// Ids for standard-non-players
public final static String ID_CONSOLE = IDPREFIX+"console";
public final static String ID_RCON = IDPREFIX+"rcon";
public final static String ID_BLOCK = IDPREFIX+"block";
// Names for standard-non-players
public final static String VANILLA_CONSOLE_NAME = "CONSOLE";
public final static String VANILLA_RCON_NAME = "Rcon";
public final static String VANILLA_BLOCK_NAME = "@";
// Player id pattern, the regex for a valid minecraft username.
public final static Pattern playerNamePattern = Pattern.compile("^[a-zA-Z0-9_]{2,16}$");
// -------------------------------------------- //
// REGISTRY
// -------------------------------------------- //
protected static Map<String, CommandSender> idToSender = new TreeMap<String, CommandSender>(String.CASE_INSENSITIVE_ORDER);
protected static Map<String, String> idToDisplayName = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
protected static Map<String, String> idToListName = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
public static synchronized boolean register(CommandSender sender)
{
if (sender == null) return false;
String id = getSenderId(sender);
CommandSender current = idToSender.get(id);
if (current != null) return current == sender;
idToSender.put(id, sender);
SenderColl.setSenderRefferences(id, sender);
return true;
}
public static Map<String, CommandSender> getIdToSender()
{
return Collections.unmodifiableMap(idToSender);
}
static
{
// Register
register(getConsole());
register(getRcon());
register(getBlock());
// Display Name
setDisplayName(ID_CONSOLE, ChatColor.RED.toString()+ID_CONSOLE.toUpperCase());
setDisplayName(ID_RCON, ChatColor.RED.toString()+ID_RCON.toUpperCase());
setDisplayName(ID_BLOCK, ChatColor.RED.toString()+ID_BLOCK.toUpperCase());
// List Name
setListName(ID_CONSOLE, ChatColor.RED.toString()+ID_CONSOLE.toUpperCase());
setListName(ID_RCON, ChatColor.RED.toString()+ID_RCON.toUpperCase());
setListName(ID_BLOCK, ChatColor.RED.toString()+ID_BLOCK.toUpperCase());
}
// -------------------------------------------- //
// ID TYPE CHECKING
// -------------------------------------------- //
public static boolean isSenderId(Object o)
{
return o instanceof String;
}
public static boolean isPlayerId(Object o)
{
if (!isSenderId(o)) return false;
return playerNamePattern.matcher((String) o).matches();
}
public static boolean isConsoleId(Object o)
{
return ID_CONSOLE.equals(o);
}
public static boolean isRconId(Object o)
{
return ID_RCON.equals(o);
}
public static boolean isBlockId(Object o)
{
return ID_BLOCK.equals(o);
}
public static boolean isNonplayerId(Object o)
{
if (!isSenderId(o)) return false;
if (isPlayerId(o)) return false;
return true;
}
public static boolean isStandardNonplayerId(Object o)
{
if (isConsoleId(o)) return true;
if (isRconId(o)) return true;
if (isBlockId(o)) return true;
return false;
}
public static boolean isNonstandardNonplayerId(Object o)
{
if (!isSenderId(o)) return false;
if (isStandardNonplayerId(o)) return false;
if (isPlayerId(o)) return false;
return true;
}
// -------------------------------------------- //
// SENDER/OBJECT TYPE CHECKING
// -------------------------------------------- //
public static boolean isSender(Object o)
{
// The object must be a CommandSender and musn't be null.
return o instanceof CommandSender;
}
public static boolean isPlayer(Object o)
{
return o instanceof Player;
}
public static boolean isConsole(Object o)
{
if (!(o instanceof ConsoleCommandSender)) return false;
if (!VANILLA_CONSOLE_NAME.equals(((CommandSender)o).getName())) return false;
return true;
}
public static boolean isRcon(Object o)
{
if (!(o instanceof RemoteConsoleCommandSender)) return false;
if (!VANILLA_RCON_NAME.equals(((CommandSender)o).getName())) return false;
return true;
}
public static boolean isBlock(Object o)
{
if (!(o instanceof BlockCommandSender)) return false;
if (!VANILLA_BLOCK_NAME.equals(((CommandSender)o).getName())) return false;
return true;
}
public static boolean isNonplayer(Object o)
{
if (!isSender(o)) return false;
if (isPlayer(o)) return false;
return true;
}
public static boolean isStandardNonplayer(Object o)
{
if (isConsole(o)) return true;
if (isRcon(o)) return true;
if (isBlock(o)) return true;
return false;
}
public static boolean isNonstandardNonplayer(Object o)
{
if (!isSender(o)) return false;
if (isStandardNonplayer(o)) return false;
if (isPlayer(o)) return false;
return true;
}
// -------------------------------------------- //
// GET ID
// -------------------------------------------- //
public static String getSenderId(Object o)
{
if (!isSender(o)) return null;
if (isPlayer(o)) return ((CommandSender)o).getName();
if (isConsole(o)) return ID_CONSOLE;
if (isRcon(o)) return ID_RCON;
if (isBlock(o)) return ID_BLOCK;
return IDPREFIX+((CommandSender)o).getName();
}
// -------------------------------------------- //
// GET SENDER
// -------------------------------------------- //
// ACTUALL LOGIC
public static synchronized CommandSender getSender(String senderId)
{
if (senderId == null) return null;
if (isPlayerId(senderId))
{
return Bukkit.getPlayerExact(senderId);
}
return idToSender.get(senderId);
}
// ID STUFF
public static Player getPlayer(String senderId)
{
return getAsPlayer(getSender(senderId));
}
public static ConsoleCommandSender getConsole(String senderId)
{
return getAsConsole(getSender(senderId));
}
public static RemoteConsoleCommandSender getRcon(String senderId)
{
return getAsRcon(getSender(senderId));
}
public static BlockCommandSender getBlock(String senderId)
{
return getAsBlock(getSender(senderId));
}
// MARCHAL STUFF
public static CommandSender getAsSender(Object o)
{
if (!isSender(o)) return null;
return (CommandSender) o;
}
public static Player getAsPlayer(Object o)
{
if (!isPlayer(o)) return null;
return (Player) o;
}
public static ConsoleCommandSender getAsConsole(Object o)
{
if (!isConsole(o)) return null;
return (ConsoleCommandSender) o;
}
public static RemoteConsoleCommandSender getAsRcon(Object o)
{
if (!isRcon(o)) return null;
return (RemoteConsoleCommandSender) o;
}
public static BlockCommandSender getAsBlock(Object o)
{
if (!isBlock(o)) return null;
return (BlockCommandSender) o;
}
// -------------------------------------------- //
// GET STANDARD-NON-PLAYERS
// -------------------------------------------- //
public static ConsoleCommandSender getConsole()
{
return Bukkit.getConsoleSender();
}
public static RemoteConsoleCommandSender getRcon()
{
Server server = Bukkit.getServer();
CraftServer craftServer = (CraftServer)server;
MinecraftServer minecraftServer = craftServer.getServer();
return minecraftServer.remoteConsole;
}
public static BlockCommandSender getBlock()
{
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));
}
/*public static LinkedHashSet<String> getOnlineIds(String senderId)
{
return false;
}*/
// -------------------------------------------- //
// DISPLAY NAME
// -------------------------------------------- //
public static String getDisplayName(String senderId)
{
String ret = idToDisplayName.get(senderId);
if (ret != null) return ret;
Player player = Bukkit.getPlayer(senderId);
if (player == null) return senderId;
return player.getDisplayName();
}
public static void setDisplayName(String senderId, String displayName)
{
idToDisplayName.put(senderId, displayName);
Player player = Bukkit.getPlayer(senderId);
if (player == null) return;
player.setDisplayName(displayName);
}
public static String getDisplayName(CommandSender sender)
{
return getDisplayName(getSenderId(sender));
}
public static void setDisplayName(CommandSender sender, String displayName)
{
setDisplayName(getSenderId(sender), displayName);
}
// -------------------------------------------- //
// LIST NAME
// -------------------------------------------- //
public static String getListName(String senderId)
{
String ret = idToListName.get(senderId);
if (ret != null) return ret;
Player player = Bukkit.getPlayer(senderId);
if (player == null) return senderId;
return player.getPlayerListName();
}
public static void setListName(String senderId, String displayName)
{
idToListName.put(senderId, displayName);
Player player = Bukkit.getPlayer(senderId);
if (player == null) return;
player.setPlayerListName(displayName);
}
public static String getListName(CommandSender sender)
{
return getListName(getSenderId(sender));
}
public static void setListName(CommandSender sender, String displayName)
{
setListName(getSenderId(sender), displayName);
}
// -------------------------------------------- //
// CONVENIENCE CMD
// -------------------------------------------- //
public static boolean cmd(CommandSender sender, String cmd)
{
return Bukkit.dispatchCommand(sender, cmd);
}
// -------------------------------------------- //
// CONVENIENCE SEND MESSAGE
// -------------------------------------------- //
// sender
public static boolean sendMessage(CommandSender sender, String message)
{
if (sender == null) return false;
sender.sendMessage(message);
return true;
}
public static boolean sendMessage(CommandSender sender, String... messages)
{
if (sender == null) return false;
sender.sendMessage(messages);
return true;
}
public static boolean sendMessage(CommandSender sender, Collection<String> messages)
{
if (sender == null) return false;
for (String message : messages)
{
sender.sendMessage(message);
}
return true;
}
// senderId
public static boolean sendMessage(String senderId, String message)
{
return sendMessage(getSender(senderId), message);
}
public static boolean sendMessage(String senderId, String... messages)
{
return sendMessage(getSender(senderId), messages);
}
public static boolean sendMessage(String senderId, Collection<String> messages)
{
return sendMessage(getSender(senderId), messages);
}
// -------------------------------------------- //
// CONVENIENCE MSG
// -------------------------------------------- //
// sender
public static boolean msg(CommandSender sender, String msg)
{
return sendMessage(sender, Txt.parse(msg));
}
public static boolean msg(CommandSender sender, String msg, Object... args)
{
return sendMessage(sender, Txt.parse(msg, args));
}
public static boolean msg(CommandSender sender, Collection<String> msgs)
{
if (sender == null) return false;
for (String msg : msgs)
{
msg(sender, msg);
}
return true;
}
// senderId
public static boolean msg(String senderId, String msg)
{
return msg(getSender(senderId), msg);
}
public static boolean msg(String senderId, String msg, Object... args)
{
return msg(getSender(senderId), msg, args);
}
public static boolean msg(String senderId, Collection<String> msgs)
{
return msg(getSender(senderId), msgs);
}
// -------------------------------------------- //
// CONVENIENCE GAME-MODE
// -------------------------------------------- //
public static GameMode getGameMode(String senderId, GameMode def)
{
Player player = getPlayer(senderId);
if (player == null) return def;
return player.getGameMode();
}
public static boolean isGameMode(String senderId, GameMode gm, boolean def)
{
Player player = getPlayer(senderId);
if (player == null) return def;
return player.getGameMode() == gm;
}
public static GameMode getGameMode(CommandSender sender, GameMode def)
{
return getGameMode(getSenderId(sender), def);
}
public static boolean isGameMode(CommandSender sender, GameMode gm, boolean def)
{
return isGameMode(getSenderId(sender), gm, def);
}
}

View File

@ -1,9 +1,9 @@
package com.massivecraft.mcore5.util.extractor; package com.massivecraft.mcore5.util.extractor;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBreakEvent;
@ -27,59 +27,90 @@ import org.bukkit.event.vehicle.VehicleExitEvent;
import com.massivecraft.mcore5.PS; import com.massivecraft.mcore5.PS;
import com.massivecraft.mcore5.store.PlayerEntity; import com.massivecraft.mcore5.store.PlayerEntity;
import com.massivecraft.mcore5.store.SenderEntity;
import com.massivecraft.mcore5.util.SenderUtil;
public class ExtractorLogic public class ExtractorLogic
{ {
// -------------------------------------------- //
// SENDER
// -------------------------------------------- //
public static CommandSender sender(String o) { return SenderUtil.getSender(o); }
public static CommandSender sender(PlayerEvent o) { return o.getPlayer(); }
public static CommandSender sender(BlockBreakEvent o) { return o.getPlayer(); }
public static CommandSender sender(BlockDamageEvent o) { return o.getPlayer(); }
public static CommandSender sender(BlockIgniteEvent o) { return o.getPlayer(); }
public static CommandSender sender(BlockPlaceEvent o) { return o.getPlayer(); }
public static CommandSender sender(SignChangeEvent o) { return o.getPlayer(); }
public static CommandSender sender(EnchantItemEvent o) { return o.getEnchanter(); }
public static CommandSender sender(PrepareItemEnchantEvent o) { return o.getEnchanter(); }
public static CommandSender sender(Entity o) { if (o instanceof CommandSender) return (CommandSender)o; return null; }
public static CommandSender sender(EntityEvent o) { return sender(o.getEntity()); }
public static CommandSender sender(InventoryClickEvent o) { return sender(o.getWhoClicked()); }
public static CommandSender sender(InventoryCloseEvent o) { return sender(o.getPlayer()); }
public static CommandSender sender(InventoryOpenEvent o) { return sender(o.getPlayer()); }
public static CommandSender sender(HangingBreakByEntityEvent o) { return sender(o.getRemover()); }
public static CommandSender sender(VehicleDamageEvent o) { return sender(o.getAttacker()); }
public static CommandSender sender(VehicleDestroyEvent o) { return sender(o.getAttacker()); }
public static CommandSender sender(VehicleEnterEvent o) { return sender(o.getEntered()); }
public static CommandSender sender(VehicleExitEvent o) { return sender(o.getExited()); }
public static CommandSender sender(VehicleEvent o) { return sender(o.getVehicle().getPassenger()); }
public static CommandSender senderFromObject(Object o)
{
if (o == null) return null;
if (o instanceof CommandSender) return (CommandSender)o;
if (o instanceof String) return sender((String)o);
if (o instanceof PlayerEvent) return sender((PlayerEvent)o);
if (o instanceof BlockBreakEvent) return sender((BlockBreakEvent)o);
if (o instanceof BlockDamageEvent) return sender((BlockDamageEvent)o);
if (o instanceof BlockIgniteEvent) return sender((BlockIgniteEvent)o);
if (o instanceof BlockPlaceEvent) return sender((BlockPlaceEvent)o);
if (o instanceof SignChangeEvent) return sender((SignChangeEvent)o);
if (o instanceof EnchantItemEvent) return sender((EnchantItemEvent)o);
if (o instanceof PrepareItemEnchantEvent) return sender((PrepareItemEnchantEvent)o);
if (o instanceof Entity) return sender((Entity)o);
if (o instanceof EntityEvent) return sender((EntityEvent)o);
if (o instanceof InventoryClickEvent) return sender((InventoryClickEvent)o);
if (o instanceof InventoryCloseEvent) return sender((InventoryCloseEvent)o);
if (o instanceof InventoryOpenEvent) return sender((InventoryOpenEvent)o);
if (o instanceof HangingBreakByEntityEvent) return sender((HangingBreakByEntityEvent)o);
if (o instanceof VehicleDamageEvent) return sender((VehicleDamageEvent)o);
if (o instanceof VehicleDestroyEvent) return sender((VehicleDestroyEvent)o);
if (o instanceof VehicleEnterEvent) return sender((VehicleEnterEvent)o);
if (o instanceof VehicleExitEvent) return sender((VehicleExitEvent)o);
if (o instanceof VehicleEvent) return sender((VehicleEvent)o);
return null;
}
// -------------------------------------------- //
// SENDER ID
// -------------------------------------------- //
public static String senderIdFromObject(Object o)
{
if (o == null) return null;
if (o instanceof String) return (String)o;
if (o instanceof SenderEntity) return ((SenderEntity<?>)o).getId();
if (o instanceof PlayerEntity) return ((PlayerEntity<?>)o).getId();
CommandSender sender = senderFromObject(o);
if (sender == null) return null;
return SenderUtil.getSenderId(sender);
}
// -------------------------------------------- // // -------------------------------------------- //
// PLAYER // PLAYER
// -------------------------------------------- // // -------------------------------------------- //
public static Player player(String o) { return Bukkit.getPlayerExact(o); }
public static Player player(PlayerEvent o) { return o.getPlayer(); }
public static Player player(BlockBreakEvent o) { return o.getPlayer(); }
public static Player player(BlockDamageEvent o) { return o.getPlayer(); }
public static Player player(BlockIgniteEvent o) { return o.getPlayer(); }
public static Player player(BlockPlaceEvent o) { return o.getPlayer(); }
public static Player player(SignChangeEvent o) { return o.getPlayer(); }
public static Player player(EnchantItemEvent o) { return o.getEnchanter(); }
public static Player player(PrepareItemEnchantEvent o) { return o.getEnchanter(); }
public static Player player(Entity o) { if (o instanceof Player) return (Player)o; return null; }
public static Player player(EntityEvent o) { return player(o.getEntity()); }
public static Player player(InventoryClickEvent o) { return player(o.getWhoClicked()); }
public static Player player(InventoryCloseEvent o) { return player(o.getPlayer()); }
public static Player player(InventoryOpenEvent o) { return player(o.getPlayer()); }
public static Player player(HangingBreakByEntityEvent o) { return player(o.getRemover()); }
public static Player player(VehicleDamageEvent o) { return player(o.getAttacker()); }
public static Player player(VehicleDestroyEvent o) { return player(o.getAttacker()); }
public static Player player(VehicleEnterEvent o) { return player(o.getEntered()); }
public static Player player(VehicleExitEvent o) { return player(o.getExited()); }
public static Player player(VehicleEvent o) { return player(o.getVehicle().getPassenger()); }
public static Player playerFromObject(Object o) public static Player playerFromObject(Object o)
{ {
if (o instanceof Player) return (Player)o; CommandSender sender = senderFromObject(o);
if (sender instanceof Player) return (Player)sender;
if (o instanceof String) return player((String)o);
if (o instanceof PlayerEvent) return player((PlayerEvent)o);
if (o instanceof BlockBreakEvent) return player((BlockBreakEvent)o);
if (o instanceof BlockDamageEvent) return player((BlockDamageEvent)o);
if (o instanceof BlockIgniteEvent) return player((BlockIgniteEvent)o);
if (o instanceof BlockPlaceEvent) return player((BlockPlaceEvent)o);
if (o instanceof SignChangeEvent) return player((SignChangeEvent)o);
if (o instanceof EnchantItemEvent) return player((EnchantItemEvent)o);
if (o instanceof PrepareItemEnchantEvent) return player((PrepareItemEnchantEvent)o);
if (o instanceof Entity) return player((Entity)o);
if (o instanceof EntityEvent) return player((EntityEvent)o);
if (o instanceof InventoryClickEvent) return player((InventoryClickEvent)o);
if (o instanceof InventoryCloseEvent) return player((InventoryCloseEvent)o);
if (o instanceof InventoryOpenEvent) return player((InventoryOpenEvent)o);
if (o instanceof HangingBreakByEntityEvent) return player((HangingBreakByEntityEvent)o);
if (o instanceof VehicleDamageEvent) return player((VehicleDamageEvent)o);
if (o instanceof VehicleDestroyEvent) return player((VehicleDestroyEvent)o);
if (o instanceof VehicleEnterEvent) return player((VehicleEnterEvent)o);
if (o instanceof VehicleExitEvent) return player((VehicleExitEvent)o);
if (o instanceof VehicleEvent) return player((VehicleEvent)o);
return null; return null;
} }
@ -89,11 +120,10 @@ public class ExtractorLogic
public static String playerNameFromObject(Object o) public static String playerNameFromObject(Object o)
{ {
if (o instanceof String) return (String)o; String senderId = senderIdFromObject(o);
if (o instanceof PlayerEntity) return ((PlayerEntity<?>)o).getId(); //if (SenderUtil.isPlayerId(senderId)) return senderId;
Player player = playerFromObject(o); //return null;
if (player == null) return null; return senderId;
return player.getName();
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -7,4 +7,12 @@ public class ExtractorPlayer implements Extractor
{ {
return ExtractorLogic.playerFromObject(o); return ExtractorLogic.playerFromObject(o);
} }
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
private static ExtractorPlayer i = new ExtractorPlayer();
public static ExtractorPlayer get() { return i; }
} }

View File

@ -7,4 +7,12 @@ public class ExtractorPlayerName implements Extractor
{ {
return ExtractorLogic.playerNameFromObject(o); return ExtractorLogic.playerNameFromObject(o);
} }
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
private static ExtractorPlayerName i = new ExtractorPlayerName();
public static ExtractorPlayerName get() { return i; }
} }

View File

@ -0,0 +1,18 @@
package com.massivecraft.mcore5.util.extractor;
public class ExtractorSender implements Extractor
{
@Override
public Object extract(Object o)
{
return ExtractorLogic.senderFromObject(o);
}
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
private static ExtractorSender i = new ExtractorSender();
public static ExtractorSender get() { return i; }
}

View File

@ -0,0 +1,18 @@
package com.massivecraft.mcore5.util.extractor;
public class ExtractorSenderId implements Extractor
{
@Override
public Object extract(Object o)
{
return ExtractorLogic.senderIdFromObject(o);
}
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
private static ExtractorSenderId i = new ExtractorSenderId();
public static ExtractorSenderId get() { return i; }
}

View File

@ -7,4 +7,12 @@ public class ExtractorWorld implements Extractor
{ {
return ExtractorLogic.worldFromObject(o); return ExtractorLogic.worldFromObject(o);
} }
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
private static ExtractorWorld i = new ExtractorWorld();
public static ExtractorWorld get() { return i; }
} }

View File

@ -7,4 +7,12 @@ public class ExtractorWorldName implements Extractor
{ {
return ExtractorLogic.worldNameFromObject(o); return ExtractorLogic.worldNameFromObject(o);
} }
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
private static ExtractorWorldName i = new ExtractorWorldName();
public static ExtractorWorldName get() { return i; }
} }