Moved session management.

This commit is contained in:
Robin Kupper
2011-11-09 16:46:59 +01:00
parent 6faa69244e
commit 5bb729a37f
6 changed files with 43 additions and 41 deletions

View File

@@ -1,8 +1,8 @@
package de.diddiz.LogBlock;
import static de.diddiz.LogBlock.Session.getSession;
import static de.diddiz.util.BukkitUtils.giveTool;
import static de.diddiz.util.BukkitUtils.saveSpawnHeight;
import static de.diddiz.util.BukkitUtils.senderName;
import static de.diddiz.util.Utils.isInt;
import static org.bukkit.Bukkit.getLogger;
import java.io.Closeable;
@@ -137,7 +137,7 @@ public class CommandsHandler implements CommandExecutor
} else if (config.toolsByName.get(command) != null) {
if (sender instanceof Player) {
final Player player = (Player)sender;
final Session session = logblock.getSession(player.getName());
final Session session = Session.getSession(player.getName());
final Tool tool = config.toolsByName.get(command);
final ToolData toolData = session.toolData.get(tool);
if (args.length == 1) {
@@ -203,9 +203,9 @@ public class CommandsHandler implements CommandExecutor
else
sender.sendMessage(ChatColor.RED + "You have to specify a page");
} else if (command.equals("next") || command.equals("+"))
showPage(sender, logblock.getSession(senderName(sender)).page + 1);
showPage(sender, getSession(sender).page + 1);
else if (command.equals("prev") || command.equals("-"))
showPage(sender, logblock.getSession(senderName(sender)).page - 1);
showPage(sender, getSession(sender).page - 1);
else if (args[0].equalsIgnoreCase("savequeue")) {
if (logblock.hasPermission(sender, "logblock.rollback"))
new CommandSaveQueue(sender, null, true);
@@ -270,7 +270,7 @@ public class CommandsHandler implements CommandExecutor
if (args.length == 2 || isInt(args[1])) {
final int pos = Integer.parseInt(args[1]) - 1;
final Player player = (Player)sender;
final Session session = logblock.getSession(player.getName());
final Session session = getSession(player);
if (session.lookupCache != null)
if (pos >= 0 && pos < session.lookupCache.length) {
final Location loc = session.lookupCache[pos].getLocation();
@@ -308,7 +308,7 @@ public class CommandsHandler implements CommandExecutor
}
private void showPage(CommandSender sender, int page) {
final Session session = logblock.getSession(senderName(sender));
final Session session = getSession(sender);
if (session.lookupCache != null && session.lookupCache.length > 0) {
final int startpos = (page - 1) * config.linesPerPage;
if (page > 0 && startpos <= session.lookupCache.length - 1) {
@@ -409,7 +409,7 @@ public class CommandsHandler implements CommandExecutor
final LookupCacheElementFactory factory = new LookupCacheElementFactory(params, sender instanceof Player ? 2 / 3f : 1);
while (rs.next())
blockchanges.add(factory.getLookupCacheElement(rs));
logblock.getSession(senderName(sender)).lookupCache = blockchanges.toArray(new LookupCacheElement[blockchanges.size()]);
getSession(sender).lookupCache = blockchanges.toArray(new LookupCacheElement[blockchanges.size()]);
if (blockchanges.size() > config.linesPerPage)
sender.sendMessage(ChatColor.DARK_AQUA.toString() + blockchanges.size() + " changes found." + (blockchanges.size() == config.linesLimit ? " Use 'limit -1' to see all changes." : ""));
if (params.sum != SummarizationMode.NONE)
@@ -417,7 +417,7 @@ public class CommandsHandler implements CommandExecutor
showPage(sender, 1);
} else {
sender.sendMessage(ChatColor.DARK_AQUA + "No results found.");
logblock.getSession(senderName(sender)).lookupCache = null;
getSession(sender).lookupCache = null;
}
} catch (final Exception ex) {
sender.sendMessage(ChatColor.RED + "Exception, check error log");
@@ -594,7 +594,7 @@ public class CommandsHandler implements CommandExecutor
return;
}
editor.start();
logblock.getSession(senderName(sender)).lookupCache = editor.errors;
getSession(sender).lookupCache = editor.errors;
sender.sendMessage(ChatColor.GREEN + "Rollback finished successfully (" + editor.getElapsedTime() + " ms, " + editor.getSuccesses() + "/" + changes + " blocks" + (editor.getErrors() > 0 ? ", " + ChatColor.RED + editor.getErrors() + " errors" + ChatColor.GREEN : "") + (editor.getBlacklistCollisions() > 0 ? ", " + editor.getBlacklistCollisions() + " blacklist collisions" : "") + ")");
if (!params.silent && config.askClearLogAfterRollback && logblock.hasPermission(sender, "logblock.clearlog") && questioner != null && sender instanceof Player) {
Thread.sleep(1000);

View File

@@ -1,5 +1,7 @@
package de.diddiz.LogBlock;
import static de.diddiz.LogBlock.Session.getSession;
import static de.diddiz.LogBlock.Session.hasSession;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.ChatColor;
@@ -38,7 +40,7 @@ class LBToolListener extends PlayerListener
final Player player = event.getPlayer();
if (tool != null && (action == Action.RIGHT_CLICK_BLOCK || action == Action.LEFT_CLICK_BLOCK) && worlds.containsKey(player.getWorld().getName().hashCode()) && logblock.hasPermission(player, "logblock.tools." + tool.name)) {
final ToolBehavior behavior = action == Action.RIGHT_CLICK_BLOCK ? tool.rightClickBehavior : tool.leftClickBehavior;
final ToolData toolData = logblock.getSession(player.getName()).toolData.get(tool);
final ToolData toolData = getSession(player).toolData.get(tool);
if (behavior != ToolBehavior.NONE && toolData.enabled) {
final Block block = event.getClickedBlock();
final QueryParams params = toolData.params;
@@ -101,8 +103,8 @@ class LBToolListener extends PlayerListener
@Override
public void onPlayerChangedWorld(PlayerChangedWorldEvent event) {
final Player player = event.getPlayer();
final Session session = logblock.getSession(player.getName(), false);
if (session != null)
if (hasSession(player)) {
final Session session = getSession(player);
for (final Entry<Tool, ToolData> entry : session.toolData.entrySet()) {
final Tool tool = entry.getKey();
final ToolData toolData = entry.getValue();
@@ -112,5 +114,6 @@ class LBToolListener extends PlayerListener
player.sendMessage(ChatColor.GREEN + "Tool disabled.");
}
}
}
}
}

View File

@@ -10,9 +10,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.logging.Level;
import org.bukkit.ChatColor;
@@ -41,7 +39,6 @@ public class LogBlock extends JavaPlugin
private Timer timer = null;
private PermissionHandler permissions = null;
private boolean errorAtLoading = false, noDb = false, connected = true;
private final Map<String, Session> sessions = new HashMap<String, Session>();
public static LogBlock getInstance() {
return logblock;
@@ -288,19 +285,6 @@ public class LogBlock extends JavaPlugin
}
}
public Session getSession(String playerName) {
return getSession(playerName, true);
}
public Session getSession(String playerName, boolean create) {
Session session = sessions.get(playerName.toLowerCase());
if (session == null && create) {
session = new Session(this, getServer().getPlayer(playerName));
sessions.put(playerName.toLowerCase(), session);
}
return session;
}
/**
* @param params
* QueryParams that contains the needed columns (all other will be filled with default values) and the params. World is required.

View File

@@ -1,8 +1,8 @@
package de.diddiz.LogBlock;
import static de.diddiz.LogBlock.Session.getSession;
import static de.diddiz.util.BukkitUtils.friendlyWorldname;
import static de.diddiz.util.BukkitUtils.getBlockEquivalents;
import static de.diddiz.util.BukkitUtils.senderName;
import static de.diddiz.util.MaterialName.materialName;
import static de.diddiz.util.Utils.isInt;
import static de.diddiz.util.Utils.join;
@@ -264,7 +264,7 @@ public class QueryParams implements Cloneable
if (args == null || args.size() == 0)
throw new IllegalArgumentException("No parameters specified.");
final Player player = sender instanceof Player ? (Player)sender : null;
final Session session = prepareToolQuery ? null : logblock.getSession(senderName(sender));
final Session session = prepareToolQuery ? null : getSession(sender);
if (player != null && world == null)
world = player.getWorld();
for (int i = 0; i < args.size(); i++) {

View File

@@ -1,22 +1,47 @@
package de.diddiz.LogBlock;
import static org.bukkit.Bukkit.getServer;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Session
{
private static final Map<String, Session> sessions = new HashMap<String, Session>();
public QueryParams lastQuery = null;
public LookupCacheElement[] lookupCache = null;
public int page = 1;
public Map<Tool, ToolData> toolData;
Session(LogBlock logblock, Player player) {
private Session(Player player) {
toolData = new HashMap<Tool, ToolData>();
final LogBlock logblock = LogBlock.getInstance();
if (player != null)
for (final Tool tool : logblock.getLBConfig().toolsByType.values())
toolData.put(tool, new ToolData(tool, logblock, player));
}
public static boolean hasSession(CommandSender sender) {
return sessions.containsKey(sender.getName().toLowerCase());
}
public static boolean hasSession(String playerName) {
return sessions.containsKey(playerName.toLowerCase());
}
public static Session getSession(CommandSender sender) {
return getSession(sender.getName());
}
public static Session getSession(String playerName) {
Session session = sessions.get(playerName.toLowerCase());
if (session == null) {
session = new Session(getServer().getPlayer(playerName));
sessions.put(playerName.toLowerCase(), session);
}
return session;
}
}
class ToolData

View File

@@ -13,8 +13,6 @@ import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import org.bukkit.block.ContainerBlock;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
@@ -120,14 +118,6 @@ public class BukkitUtils
return entity.getClass().getSimpleName().substring(5);
}
public static String senderName(CommandSender sender) {
if (sender instanceof Player)
return ((Player)sender).getName();
if (sender instanceof ConsoleCommandSender)
return "console";
return sender.getClass().getSimpleName();
}
public static void giveTool(Player player, int type) {
final Inventory inv = player.getInventory();
if (inv.contains(type))