use of org.bukkit.entity.Player in project Prism-Bukkit by prism.
the class Executor method onCommand.
/**
*
*/
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
// Set player
Player player = null;
if (sender instanceof Player) {
player = (Player) sender;
}
// Find command
String subcommandName;
if (mode.equals("subcommand") && args.length > 0) {
subcommandName = args[0].toLowerCase();
} else {
subcommandName = cmd.getName();
}
String currentMode = mode;
SubCommand sub = subcommands.get(subcommandName);
if (sub == null) {
sub = subcommands.get(defaultSubcommand);
if (sub == null) {
sender.sendMessage("Invalid command");
return true;
} else {
// The default is used, we must switch back to command mode
currentMode = "command";
}
}
// Ensure they have permission
if (player != null && !(sub.playerHasPermission(player))) {
sender.sendMessage("You do not have permission to use this command");
return true;
} else // Ensure min number of arguments
if ((currentMode.equals("subcommand") && (args.length - 1) < sub.getMinArgs()) || (currentMode.equals("command") && (args.length) < sub.getMinArgs())) {
sender.sendMessage("You're missing arguments for this command");
return true;
}
// Ensure command allows console
if (!(sender instanceof Player)) {
if (!sub.isConsoleAllowed()) {
sender.sendMessage("You must be in-game to use this command");
return true;
}
}
// Pass along call to handler
final CallInfo call = new CallInfo(sender, player, args);
sub.getHandler().handle(call);
return true;
}
use of org.bukkit.entity.Player in project Prism-Bukkit by prism.
the class PreprocessArgs method parseParam.
/**
*
* @param plugin
* @param sender
* @param parameters
* @param registeredParams
* @param foundArgsNames
* @param foundArgsList
* @param arg
* @return
*/
private static ParseResult parseParam(Prism plugin, CommandSender sender, QueryParameters parameters, HashMap<String, PrismParameterHandler> registeredParams, Set<String> foundArgsNames, List<MatchedParam> foundArgsList, String arg) {
ParseResult result = ParseResult.NotFound;
// Match command argument to parameter handler
for (final Entry<String, PrismParameterHandler> entry : registeredParams.entrySet()) {
PrismParameterHandler parameterHandler = entry.getValue();
if (!parameterHandler.applicable(arg, sender)) {
continue;
}
if (!parameterHandler.hasPermission(arg, sender)) {
result = ParseResult.NoPermission;
continue;
}
result = ParseResult.Found;
foundArgsList.add(new MatchedParam(parameterHandler, arg));
foundArgsNames.add(parameterHandler.getName().toLowerCase());
break;
}
// Reject argument that doesn't match anything
if (result == ParseResult.NotFound) {
// We support an alternate player syntax so that people
// can use the tab-complete
// feature of minecraft. Using p: prevents it.
final Player autoFillPlayer = plugin.getServer().getPlayer(arg);
if (autoFillPlayer != null) {
MatchRule match = MatchRule.INCLUDE;
if (arg.startsWith("!")) {
match = MatchRule.EXCLUDE;
}
result = ParseResult.Found;
parameters.addPlayerName(arg.replace("!", ""), match);
}
}
switch(result) {
case NotFound:
if (sender != null)
sender.sendMessage(Prism.messenger.playerError("Unrecognized parameter '" + arg + "'. Use /prism ? for help."));
break;
case NoPermission:
if (sender != null)
sender.sendMessage(Prism.messenger.playerError("No permission for parameter '" + arg + "', skipped."));
break;
default:
break;
}
return result;
}
use of org.bukkit.entity.Player in project Prism-Bukkit by prism.
the class ActionsQuery method lookup.
/**
*
* @return
*/
public QueryResult lookup(QueryParameters parameters, CommandSender sender) {
Player player = null;
if (sender instanceof Player) {
player = (Player) sender;
}
// If lookup, determine if we need to group
shouldGroup = false;
if (parameters.getProcessType().equals(PrismProcessType.LOOKUP)) {
shouldGroup = true;
// What to default to
if (!plugin.getConfig().getBoolean("prism.queries.lookup-auto-group")) {
shouldGroup = false;
}
// Any overriding flags passed?
if (parameters.hasFlag(Flag.NO_GROUP) || parameters.hasFlag(Flag.EXTENDED)) {
shouldGroup = false;
}
}
// Pull results
final List<Handler> actions = new ArrayList<Handler>();
// Build conditions based off final args
final String query = qb.getQuery(parameters, shouldGroup);
if (query != null) {
Connection conn = null;
PreparedStatement s = null;
ResultSet rs = null;
try {
plugin.eventTimer.recordTimedEvent("query started");
conn = Prism.dbc();
// Handle dead connections
if (conn == null || conn.isClosed()) {
if (RecordingManager.failedDbConnectionCount == 0) {
Prism.log("Prism database error. Connection should be there but it's not. Leaving actions to log in queue.");
}
RecordingManager.failedDbConnectionCount++;
sender.sendMessage(Prism.messenger.playerError("Database connection was closed, please wait and try again."));
return new QueryResult(actions, parameters);
} else {
RecordingManager.failedDbConnectionCount = 0;
}
s = conn.prepareStatement(query);
rs = s.executeQuery();
plugin.eventTimer.recordTimedEvent("query returned, building results");
while (rs.next()) {
if (rs.getString(3) == null)
continue;
// Convert action ID to name
// Performance-wise this is a lot faster than table joins
// and the cache data should always be available
String actionName = "";
for (final Entry<String, Integer> entry : Prism.prismActions.entrySet()) {
if (entry.getValue() == rs.getInt(3)) {
actionName = entry.getKey();
}
}
if (actionName.isEmpty()) {
Prism.log("Record contains action ID that doesn't exist in cache: " + rs.getInt(3));
continue;
}
// Get the action handler
final ActionType actionType = Prism.getActionRegistry().getAction(actionName);
if (actionType == null)
continue;
try {
final Handler baseHandler = Prism.getHandlerRegistry().getHandler(actionType.getHandler());
// Convert world ID to name
// Performance-wise this is typically a lot faster than
// table joins
String worldName = "";
for (final Entry<String, Integer> entry : Prism.prismWorlds.entrySet()) {
if (entry.getValue() == rs.getInt(5)) {
worldName = entry.getKey();
}
}
// Set all shared values
baseHandler.setPlugin(plugin);
baseHandler.setType(actionType);
baseHandler.setId(rs.getInt(1));
baseHandler.setUnixEpoch(rs.getString(2));
baseHandler.setPlayerName(rs.getString(4));
baseHandler.setWorldName(worldName);
baseHandler.setX(rs.getInt(6));
baseHandler.setY(rs.getInt(7));
baseHandler.setZ(rs.getInt(8));
baseHandler.setBlockId(rs.getInt(9));
baseHandler.setBlockSubId(rs.getInt(10));
baseHandler.setOldBlockId(rs.getInt(11));
baseHandler.setOldBlockSubId(rs.getInt(12));
baseHandler.setData(rs.getString(13));
baseHandler.setMaterialAliases(Prism.getItems());
// Set aggregate counts if a lookup
int aggregated = 0;
if (shouldGroup) {
aggregated = rs.getInt(14);
}
baseHandler.setAggregateCount(aggregated);
actions.add(baseHandler);
} catch (final Exception e) {
if (!rs.isClosed()) {
Prism.log("Ignoring data from record #" + rs.getInt(1) + " because it caused an error:");
}
e.printStackTrace();
}
}
} catch (final SQLException e) {
plugin.handleDatabaseException(e);
} finally {
if (rs != null)
try {
rs.close();
} catch (final SQLException ignored) {
}
if (s != null)
try {
s.close();
} catch (final SQLException ignored) {
}
if (conn != null)
try {
conn.close();
} catch (final SQLException ignored) {
}
}
}
// Build result object
final QueryResult res = new QueryResult(actions, parameters);
res.setPerPage(parameters.getPerPage());
// need a cache.
if (parameters.getProcessType().equals(PrismProcessType.LOOKUP)) {
String keyName = "console";
if (player != null) {
keyName = player.getName();
}
if (plugin.cachedQueries.containsKey(keyName)) {
plugin.cachedQueries.remove(keyName);
}
plugin.cachedQueries.put(keyName, res);
// We also need to share these results with the -share-with players.
for (final CommandSender sharedPlayer : parameters.getSharedPlayers()) {
plugin.cachedQueries.put(sharedPlayer.getName(), res);
}
}
plugin.eventTimer.recordTimedEvent("results object completed");
// Return it
return res;
}
use of org.bukkit.entity.Player in project Prism-Bukkit by prism.
the class PrismApplierCallback method handle.
/**
*
*/
@Override
public void handle(CommandSender sender, ApplierResult result) {
// Did we move anyone?
final HashMap<Entity, Integer> entitiesMoved = result.getEntitiesMoved();
if (!entitiesMoved.isEmpty()) {
for (final Entry<Entity, Integer> entry : entitiesMoved.entrySet()) {
if (entry.getKey() instanceof Player) {
((Player) entry.getKey()).sendMessage(Prism.messenger.playerSubduedHeaderMsg("Moved you " + entry.getValue() + " blocks to safety due to a rollback."));
}
}
}
// Send player success messages
if (result.getProcessType().equals(PrismProcessType.ROLLBACK)) {
// Build the results message
if (!result.isPreview()) {
String msg = result.getChangesApplied() + " reversals.";
if (result.getChangesSkipped() > 0) {
msg += " " + result.getChangesSkipped() + " skipped.";
}
if (result.getChangesApplied() > 0) {
msg += ChatColor.GRAY + " It's like it never happened.";
}
sender.sendMessage(Prism.messenger.playerHeaderMsg(msg));
} else {
// Build the results message
String msg = "At least " + result.getChangesPlanned() + " planned reversals.";
if (result.getChangesSkipped() > 0) {
msg += " " + result.getChangesSkipped() + " skipped.";
}
if (result.getChangesPlanned() > 0) {
msg += ChatColor.GRAY + " Use /prism preview apply to confirm.";
}
sender.sendMessage(Prism.messenger.playerHeaderMsg(msg));
// Let me know there's no need to cancel/apply
if (result.getChangesPlanned() == 0) {
sender.sendMessage(Prism.messenger.playerHeaderMsg(ChatColor.GRAY + "Nothing to rollback, preview canceled for you."));
}
}
}
// Build the results message
if (result.getProcessType().equals(PrismProcessType.RESTORE)) {
if (!result.isPreview()) {
// Build the results message
String msg = result.getChangesApplied() + " events restored.";
if (result.getChangesSkipped() > 0) {
msg += " " + result.getChangesSkipped() + " skipped.";
}
if (result.getChangesApplied() > 0) {
msg += ChatColor.GRAY + " It's like it was always there.";
}
sender.sendMessage(Prism.messenger.playerHeaderMsg(msg));
} else {
// Build the results message
String msg = result.getChangesPlanned() + " planned restorations.";
if (result.getChangesSkipped() > 0) {
msg += " " + result.getChangesSkipped() + " skipped.";
}
if (result.getChangesPlanned() > 0) {
msg += ChatColor.GRAY + " Use /prism preview apply to confirm.";
}
sender.sendMessage(Prism.messenger.playerHeaderMsg(msg));
// Let me know there's no need to cancel/apply
if (result.getChangesPlanned() == 0) {
sender.sendMessage(Prism.messenger.playerHeaderMsg(ChatColor.GRAY + "Nothing to restore, preview canceled for you."));
}
}
}
// Build the results message
if (result.getProcessType().equals(PrismProcessType.UNDO)) {
// Build the results message
String msg = result.getChangesApplied() + " changes undone.";
if (result.getChangesSkipped() > 0) {
msg += " " + result.getChangesSkipped() + " skipped.";
}
if (result.getChangesApplied() > 0) {
msg += ChatColor.GRAY + " If anyone asks, you never did that.";
}
sender.sendMessage(Prism.messenger.playerHeaderMsg(msg));
}
// Notify shared players of previews
for (final CommandSender sharedPlayer : result.getParameters().getSharedPlayers()) {
sharedPlayer.sendMessage(Prism.messenger.playerHeaderMsg("A preview is being shared with you: " + result.getParameters().getOriginalCommand()));
}
}
use of org.bukkit.entity.Player in project Prism-Bukkit by prism.
the class PlayerIdentification method cacheOnlinePlayerPrimaryKeys.
/**
* Build-load all online players into cache
*/
public static void cacheOnlinePlayerPrimaryKeys() {
String prefix = Prism.config.getString("prism.mysql.prefix");
String[] playerNames;
playerNames = new String[Bukkit.getServer().getOnlinePlayers().size()];
int i = 0;
for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
playerNames[i] = pl.getName();
i++;
}
Connection conn = null;
PreparedStatement s = null;
ResultSet rs = null;
try {
conn = Prism.dbc();
s = conn.prepareStatement("SELECT player_id, player, HEX(player_uuid) FROM " + prefix + "players WHERE player IN (?)");
s.setString(1, "'" + TypeUtils.join(playerNames, "','") + "'");
rs = s.executeQuery();
while (rs.next()) {
PrismPlayer prismPlayer = new PrismPlayer(rs.getInt(1), uuidFromDbString(rs.getString(3)), rs.getString(2));
Prism.debug("Loaded player " + rs.getString(2) + ", id: " + rs.getInt(1) + " into the cache.");
Prism.prismPlayers.put(UUID.fromString(rs.getString(2)), prismPlayer);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
}
if (s != null)
try {
s.close();
} catch (SQLException e) {
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
}
}
}
Aggregations