GITHUB:https://github.com/Mayomi/GeoIPMessage 代码 (Java): package com.mayomi.geoiploginmessage; import org.bukkit.ChatColor; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; import uk.org.whoami.geoip.GeoIPLookup; import uk.org.whoami.geoip.GeoIPTools; public class Main extends JavaPlugin implements Listener { private GeoIPLookup geo = null; private String Leave; private String Login; private String LoginNoGeoIP; @Override public void onEnable( ) { initConfig( ); initGeoLiteCity( ); getLogger().info("感谢使用 GeoIPMessage"); getServer( ).getPluginManager( ).registerEvents( this, this ); } private void initGeoLiteCity( ) /* 读取 GeoLiteCity.dat 文件部分 */ { if( !Login.contains( "{CITY}" ) ) { getLogger( ).info( "请在config.yml 的login中加入 {CITY} 代码 , 不然将不会正确显示地区" ); return; } Boolean catchedException = false; try { Plugin plugin = this.getServer( ).getPluginManager( ).getPlugin( "GeoIPTools" ); if( plugin != null ) { geo = ((GeoIPTools) plugin).getGeoIPLookup( ); } } catch( NullPointerException e ) { catchedException = true; getLogger( ).warning( "读取插件GeoIPTools发生了一个错误: " + e.getMessage( ) ); } finally { if( !catchedException && geo == null ) { getLogger( ).warning( "无法读取前置插件GeoIPTools." ); } } } private void initConfig( ) /* 写入 config.yml 文件部分 */ { final FileConfiguration config = getConfig( ); Login = config.getString( "login", "&6来自 &2{CITY} &6的玩家 &2{PLAYER} &6加入了游戏." ); LoginNoGeoIP = config.getString( "login-nogeoip", "&6玩家 &2{PLAYER} &6加入了游戏." ); Leave = config.getString( "leave", "&5玩家 &2{PLAYER} &5离开了游戏." ); config.set( "login", Login ); config.set( "login-nogeoip", LoginNoGeoIP ); config.set( "leave", Leave ); saveConfig( ); Leave = ChatColor.translateAlternateColorCodes( '&', Leave ); Login = ChatColor.translateAlternateColorCodes( '&', Login ); LoginNoGeoIP = ChatColor.translateAlternateColorCodes( '&', LoginNoGeoIP ); } @EventHandler( ignoreCancelled = true ) public void onPlayerJoin( PlayerJoinEvent event ) /* 玩家加入事件 */ { Player player = event.getPlayer( ); String message = LoginNoGeoIP; if( geo != null ) { String city = geo.getLocation( player.getAddress( ).getAddress( ) ).city; /* 获取城市 */ String country = geo.getLocation( player.getAddress( ).getAddress( ) ).countryName; /* 获取国家 */ if( !city.equals( "N/A" ) ) { message = Login.replace( "{CITY}", country + "," + city ); } if( !country.equals( "N/A" ) ) { message = Login.replace( "{CITY}", country + "," + city ); } } event.setJoinMessage( message.replace( "{PLAYER}", player.getDisplayName( ) ) ); } @EventHandler( ignoreCancelled = true ) public void onPlayerQuit( PlayerQuitEvent event ) /* 玩家退出事件 */ { if( Leave.isEmpty( ) ) { return; } event.setQuitMessage( Leave.replace( "{PLAYER}", event.getPlayer( ).getDisplayName( ) ) ); } }