2011-10-05 12:13:54 +02:00
package com.massivecraft.factions.integration ;
2012-02-03 02:31:33 +01:00
import org.bukkit.Bukkit ;
2012-02-22 20:35:26 +01:00
import org.bukkit.ChatColor ;
2011-10-05 12:13:54 +02:00
import org.bukkit.entity.Player ;
2012-02-03 02:31:33 +01:00
import org.bukkit.event.EventHandler ;
import org.bukkit.event.EventPriority ;
import org.bukkit.event.Listener ;
2012-02-22 20:35:26 +01:00
import org.bukkit.Location ;
2012-02-22 18:43:53 +01:00
import org.bukkit.plugin.Plugin ;
2011-10-05 12:13:54 +02:00
2012-02-02 14:29:00 +01:00
import com.massivecraft.factions.Conf ;
2013-04-09 13:00:09 +02:00
import com.massivecraft.factions.Factions ;
2012-05-02 04:45:10 +02:00
import com.massivecraft.factions.listeners.FactionsChatListener ;
2011-10-05 12:13:54 +02:00
2012-02-22 20:35:26 +01:00
import com.earth2me.essentials.IEssentials ;
import com.earth2me.essentials.Teleport ;
import com.earth2me.essentials.Trade ;
2011-10-05 12:13:54 +02:00
import com.earth2me.essentials.chat.EssentialsChat ;
2012-02-03 02:31:33 +01:00
import com.earth2me.essentials.chat.EssentialsLocalChatEvent ;
2011-10-05 12:13:54 +02:00
2012-02-03 02:31:33 +01:00
/ *
* This Essentials integration handler is for newer 3 . x . x versions of Essentials which don ' t have " IEssentialsChatListener "
2012-02-22 18:43:53 +01:00
* If an older version is detected in the setup ( ) method below , handling is passed off to EssentialsOldVersionFeatures
2012-02-03 02:31:33 +01:00
* /
2012-03-13 14:43:45 +01:00
// silence deprecation warnings with this old interface
@SuppressWarnings ( " deprecation " )
2011-10-08 22:03:44 +02:00
public class EssentialsFeatures
{
2011-10-05 12:13:54 +02:00
private static EssentialsChat essChat ;
2012-02-22 20:35:26 +01:00
private static IEssentials essentials ;
2011-10-05 12:13:54 +02:00
2012-02-22 18:43:53 +01:00
public static void setup ( )
{
2012-02-22 20:35:26 +01:00
// integrate main essentials plugin
// TODO: this is the old Essentials method not supported in 3.0... probably needs to eventually be moved to EssentialsOldVersionFeatures and new method implemented
if ( essentials = = null )
{
Plugin ess = Bukkit . getPluginManager ( ) . getPlugin ( " Essentials " ) ;
if ( ess ! = null & & ess . isEnabled ( ) )
essentials = ( IEssentials ) ess ;
}
// integrate chat
2012-02-22 18:43:53 +01:00
if ( essChat ! = null ) return ;
Plugin test = Bukkit . getServer ( ) . getPluginManager ( ) . getPlugin ( " EssentialsChat " ) ;
if ( test = = null | | ! test . isEnabled ( ) ) return ;
essChat = ( EssentialsChat ) test ;
// try newer Essentials 3.x integration method
try
{
Class . forName ( " com.earth2me.essentials.chat.EssentialsLocalChatEvent " ) ;
integrateChat ( essChat ) ;
}
catch ( ClassNotFoundException ex )
{
// no? try older Essentials 2.x integration method
try
{
EssentialsOldVersionFeatures . integrateChat ( essChat ) ;
}
catch ( NoClassDefFoundError ex2 ) { /* no known integration method, then */ }
}
}
public static void unhookChat ( )
{
if ( essChat = = null ) return ;
try
{
EssentialsOldVersionFeatures . unhookChat ( ) ;
}
catch ( NoClassDefFoundError ex ) { }
}
2012-02-22 20:35:26 +01:00
// return false if feature is disabled or Essentials isn't available
public static boolean handleTeleport ( Player player , Location loc )
{
if ( ! Conf . homesTeleportCommandEssentialsIntegration | | essentials = = null ) return false ;
Teleport teleport = ( Teleport ) essentials . getUser ( player ) . getTeleport ( ) ;
Trade trade = new Trade ( Conf . econCostHome , essentials ) ;
try
{
teleport . teleport ( loc , trade ) ;
}
catch ( Exception e )
{
player . sendMessage ( ChatColor . RED . toString ( ) + e . getMessage ( ) ) ;
}
return true ;
}
2012-02-22 18:43:53 +01:00
2011-10-08 22:03:44 +02:00
public static void integrateChat ( EssentialsChat instance )
{
2011-10-05 12:13:54 +02:00
essChat = instance ;
2011-10-08 22:03:44 +02:00
try
{
2013-04-09 13:00:09 +02:00
Bukkit . getServer ( ) . getPluginManager ( ) . registerEvents ( new LocalChatListener ( ) , Factions . p ) ;
Factions . p . log ( " Found and will integrate chat with newer " + essChat . getDescription ( ) . getFullName ( ) ) ;
2011-10-05 12:13:54 +02:00
}
2011-10-08 22:03:44 +02:00
catch ( NoSuchMethodError ex )
{
2011-10-05 12:13:54 +02:00
essChat = null ;
}
}
2012-02-03 02:31:33 +01:00
private static class LocalChatListener implements Listener
2011-10-08 22:03:44 +02:00
{
2012-02-03 02:31:33 +01:00
@EventHandler ( priority = EventPriority . NORMAL )
public void onPlayerChat ( EssentialsLocalChatEvent event )
2011-10-08 22:03:44 +02:00
{
2012-02-03 02:31:33 +01:00
Player speaker = event . getPlayer ( ) ;
String format = event . getFormat ( ) ;
2012-05-02 04:45:10 +02:00
format = FactionsChatListener . parseTags ( format , speaker ) ;
2012-02-03 02:31:33 +01:00
event . setFormat ( format ) ;
// NOTE: above doesn't do relation coloring. if/when we can get a local recipient list from EssentialsLocalChatEvent, we'll probably
// want to pass it on to FactionsPlayerListener.onPlayerChat(PlayerChatEvent event) rather than duplicating code
2011-10-05 12:13:54 +02:00
}
}
}