From 4a963b22f128756340d59ca601b1625637ca9aa5 Mon Sep 17 00:00:00 2001 From: snowleo Date: Tue, 19 Jul 2011 10:34:02 +0200 Subject: [PATCH 1/4] Close database connections on plugin deactivation --- .../essentials/protect/EssentialsProtect.java | 12 +++++++++++- .../essentials/protect/data/IProtectedBlock.java | 1 + .../essentials/protect/data/ProtectedBlockJDBC.java | 5 +++++ .../protect/data/ProtectedBlockMemory.java | 5 +++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java index 216a37564..ed02291cf 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java @@ -12,7 +12,9 @@ import java.beans.PropertyVetoException; import java.util.EnumMap; import java.util.List; import java.util.Map; +import java.util.logging.Filter; import java.util.logging.Level; +import java.util.logging.LogRecord; import java.util.logging.Logger; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -59,7 +61,7 @@ public class EssentialsProtect extends JavaPlugin implements IConf, IProtect pm.registerEvent(Type.LIGHTNING_STRIKE, weatherListener, Priority.Highest, this); pm.registerEvent(Type.THUNDER_CHANGE, weatherListener, Priority.Highest, this); pm.registerEvent(Type.WEATHER_CHANGE, weatherListener, Priority.Highest, this); - + reloadConfig(); ess.addReloadListener(this); if (!this.getDescription().getVersion().equals(ess.getDescription().getVersion())) @@ -96,6 +98,10 @@ public class EssentialsProtect extends JavaPlugin implements IConf, IProtect public void reloadConfig() { + if (storage != null) + { + storage.onPluginDeactivation(); + } for (ProtectConfig protectConfig : ProtectConfig.values()) { if (protectConfig.isList()) @@ -166,6 +172,10 @@ public class EssentialsProtect extends JavaPlugin implements IConf, IProtect public void onDisable() { + if (storage != null) + { + storage.onPluginDeactivation(); + } } public IEssentials getEssentials() diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/IProtectedBlock.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/IProtectedBlock.java index 6580ce7f8..271f1c464 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/data/IProtectedBlock.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/IProtectedBlock.java @@ -11,4 +11,5 @@ public interface IProtectedBlock { public boolean isProtected(Block block, String playerName); public List getOwners(Block block); public int unprotectBlock(Block block); + public void onPluginDeactivation(); } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java index e6b3c7592..e24a71b80 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockJDBC.java @@ -418,4 +418,9 @@ public abstract class ProtectedBlockJDBC implements IProtectedBlock } } } + + public void onPluginDeactivation() + { + cpds.close(); + } } diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java index 2fd32b026..1348b6bc8 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/data/ProtectedBlockMemory.java @@ -245,4 +245,9 @@ public class ProtectedBlockMemory implements IProtectedBlock } return id; } + + public void onPluginDeactivation() + { + storage.onPluginDeactivation(); + } } From 6a156ede687c4a43b042ce6d7aa4ab2be8a19794 Mon Sep 17 00:00:00 2001 From: snowleo Date: Tue, 19 Jul 2011 11:11:59 +0200 Subject: [PATCH 2/4] Removing the annoying c3p0 start message. --- .../essentials/protect/EssentialsProtect.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java index ed02291cf..bae792d06 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java @@ -27,12 +27,26 @@ import org.bukkit.plugin.java.JavaPlugin; public class EssentialsProtect extends JavaPlugin implements IConf, IProtect { private static final Logger LOGGER = Logger.getLogger("Minecraft"); + private static com.mchange.v2.log.MLogger C3P0logger; private final transient Map settingsBoolean = new EnumMap(ProtectConfig.class); private final transient Map settingsString = new EnumMap(ProtectConfig.class); private final transient Map> settingsList = new EnumMap>(ProtectConfig.class); private transient IProtectedBlock storage = null; public transient IEssentials ess = null; + @Override + public void onLoad() + { + C3P0logger = com.mchange.v2.log.MLog.getLogger(com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.class); + C3P0logger.setFilter(new Filter() + { + public boolean isLoggable(LogRecord lr) + { + return lr.getLevel() != Level.INFO; + } + }); + } + public void onEnable() { final PluginManager pm = this.getServer().getPluginManager(); @@ -61,7 +75,7 @@ public class EssentialsProtect extends JavaPlugin implements IConf, IProtect pm.registerEvent(Type.LIGHTNING_STRIKE, weatherListener, Priority.Highest, this); pm.registerEvent(Type.THUNDER_CHANGE, weatherListener, Priority.Highest, this); pm.registerEvent(Type.WEATHER_CHANGE, weatherListener, Priority.Highest, this); - + reloadConfig(); ess.addReloadListener(this); if (!this.getDescription().getVersion().equals(ess.getDescription().getVersion())) From dc99efb0a73f13be687a19e4b77623e6cc0dfe68 Mon Sep 17 00:00:00 2001 From: snowleo Date: Tue, 19 Jul 2011 11:26:23 +0200 Subject: [PATCH 3/4] Rewritten Factions hook, so we don't need Factions in our code. --- .../essentials/chat/EssentialsChat.java | 14 +++- .../chat/EssentialsChatPlayerListener.java | 81 +++++++++++-------- .../chat/IEssentialsChatListener.java | 11 +++ EssentialsChat/src/plugin.yml | 2 +- 4 files changed, 70 insertions(+), 38 deletions(-) create mode 100644 EssentialsChat/src/com/earth2me/essentials/chat/IEssentialsChatListener.java diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java index fcc734de2..2bba47a2a 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java @@ -2,6 +2,8 @@ package com.earth2me.essentials.chat; import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.Util; +import java.util.HashMap; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import org.bukkit.event.Event.Priority; @@ -13,15 +15,17 @@ import org.bukkit.plugin.java.JavaPlugin; public class EssentialsChat extends JavaPlugin { private static final Logger LOGGER = Logger.getLogger("Minecraft"); + private Map chatListener; public void onEnable() { final PluginManager pluginManager = getServer().getPluginManager(); final IEssentials ess = (IEssentials)pluginManager.getPlugin("Essentials"); - EssentialsChatPlayerListener.checkFactions(pluginManager); + chatListener = new HashMap(); + //EssentialsChatPlayerListener.checkFactions(pluginManager); - final EssentialsChatPlayerListener playerListener = new EssentialsChatPlayerListener(getServer(), ess); + final EssentialsChatPlayerListener playerListener = new EssentialsChatPlayerListener(getServer(), ess, chatListener); pluginManager.registerEvent(Type.PLAYER_CHAT, playerListener, Priority.Highest, this); if (!this.getDescription().getVersion().equals(ess.getDescription().getVersion())) { @@ -32,5 +36,11 @@ public class EssentialsChat extends JavaPlugin public void onDisable() { + chatListener.clear(); + } + + public void addEssentialsChatListener(String plugin, IEssentialsChatListener listener) + { + chatListener.put(plugin, listener); } } diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListener.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListener.java index 60af127ad..28adf9c2c 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListener.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListener.java @@ -3,31 +3,30 @@ package com.earth2me.essentials.chat; import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; +import java.util.Map; import java.util.logging.Logger; import org.bukkit.Location; import org.bukkit.Server; import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerChatEvent; -import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerListener; -import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.PluginManager; - -import org.mcteam.factions.Factions; +//import org.mcteam.factions.Factions; public class EssentialsChatPlayerListener extends PlayerListener { private static final Logger LOGGER = Logger.getLogger("Minecraft"); private final transient IEssentials ess; private final transient Server server; - private static Factions factions = null; + private final transient Map listeners; + //private static Factions factions = null; - public EssentialsChatPlayerListener(final Server server, final IEssentials ess) + public EssentialsChatPlayerListener(final Server server, final IEssentials ess, final Map listeners) { this.server = server; this.ess = ess; + this.listeners = listeners; } @Override @@ -38,8 +37,15 @@ public class EssentialsChatPlayerListener extends PlayerListener return; } - if (factions != null && (factions.shouldLetFactionsHandleThisChat(event))) - return; + //if (factions != null && (factions.shouldLetFactionsHandleThisChat(event))) + // return; + for (IEssentialsChatListener listener : listeners.values()) + { + if (listener.shouldHandleThisChat(event)) + { + return; + } + } final User user = ess.getUser(event.getPlayer()); @@ -112,37 +118,42 @@ public class EssentialsChatPlayerListener extends PlayerListener } String message = String.format(event.getFormat(), user.getDisplayName(), event.getMessage()); - if (factions != null) - message = message.replace("{FACTION}", factions.getPlayerFactionTagRelation(event.getPlayer(), p)).replace("{FACTION_TITLE}", factions.getPlayerTitle(event.getPlayer())); + for (IEssentialsChatListener listener : listeners.values()) + { + message = listener.modifyMessage(message); + + } + //if (factions != null) + // message = message.replace("{FACTION}", factions.getPlayerFactionTagRelation(event.getPlayer(), p)).replace("{FACTION_TITLE}", factions.getPlayerTitle(event.getPlayer())); u.sendMessage(message); } } - protected static void checkFactions(PluginManager pm) + /*protected static void checkFactions(PluginManager pm) { - if (factions != null) - return; - - Plugin factionsPlugin = pm.getPlugin("Factions"); - if (factionsPlugin == null) - return; - - factions = (Factions)factionsPlugin; - try - { // make sure Factions is sufficiently up-to-date - if (factions.hookSupportVersion() < 1) - factions = null; - } - catch (NoSuchMethodError ex) - { // if not, we can't work with it, so don't bother - factions = null; - } - - if (factions == null) - return; - - // normally a good thing, but we'll skip it to let Factions handle faction tags for global messages - //factions.handleFactionTagExternally(true); + if (factions != null) + return; + + Plugin factionsPlugin = pm.getPlugin("Factions"); + if (factionsPlugin == null) + return; + + factions = (Factions)factionsPlugin; + try + { // make sure Factions is sufficiently up-to-date + if (factions.hookSupportVersion() < 1) + factions = null; } + catch (NoSuchMethodError ex) + { // if not, we can't work with it, so don't bother + factions = null; + } + + if (factions == null) + return; + + // normally a good thing, but we'll skip it to let Factions handle faction tags for global messages + //factions.handleFactionTagExternally(true); + }*/ } diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/IEssentialsChatListener.java b/EssentialsChat/src/com/earth2me/essentials/chat/IEssentialsChatListener.java new file mode 100644 index 000000000..9cd1157e0 --- /dev/null +++ b/EssentialsChat/src/com/earth2me/essentials/chat/IEssentialsChatListener.java @@ -0,0 +1,11 @@ +package com.earth2me.essentials.chat; + +import org.bukkit.event.player.PlayerChatEvent; + + +public interface IEssentialsChatListener +{ + boolean shouldHandleThisChat(PlayerChatEvent event); + + String modifyMessage(String message); +} diff --git a/EssentialsChat/src/plugin.yml b/EssentialsChat/src/plugin.yml index 4ad77d230..d3f5d38df 100644 --- a/EssentialsChat/src/plugin.yml +++ b/EssentialsChat/src/plugin.yml @@ -7,4 +7,4 @@ website: http://www.earth2me.net:8001/ description: Provides chat control features for Essentials. Requires Permissions. authors: [Zenexer, ementalo, Aelux, Brettflan, KimKandor, snowleo, ceulemans, Xeology] depend: [Essentials] -softdepend: [Factions] \ No newline at end of file +#softdepend: [Factions] \ No newline at end of file From e805d1047f1c1a694b9a095a8a2d2b23e7caa2f0 Mon Sep 17 00:00:00 2001 From: snowleo Date: Tue, 19 Jul 2011 11:29:16 +0200 Subject: [PATCH 4/4] The event.getPlayer() is needed for the hook. --- .../earth2me/essentials/chat/EssentialsChatPlayerListener.java | 2 +- .../com/earth2me/essentials/chat/IEssentialsChatListener.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListener.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListener.java index 28adf9c2c..634dc7157 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListener.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayerListener.java @@ -120,7 +120,7 @@ public class EssentialsChatPlayerListener extends PlayerListener for (IEssentialsChatListener listener : listeners.values()) { - message = listener.modifyMessage(message); + message = listener.modifyMessage(event, message); } //if (factions != null) diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/IEssentialsChatListener.java b/EssentialsChat/src/com/earth2me/essentials/chat/IEssentialsChatListener.java index 9cd1157e0..e8467fea7 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/IEssentialsChatListener.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/IEssentialsChatListener.java @@ -7,5 +7,5 @@ public interface IEssentialsChatListener { boolean shouldHandleThisChat(PlayerChatEvent event); - String modifyMessage(String message); + String modifyMessage(PlayerChatEvent event, String message); }