2020-05-23 21:50:04 +00:00
/ *
2022-01-01 19:03:05 +00:00
* Copyright ( c ) 2019 - 2022 GeyserMC . http : //geysermc.org
2020-05-23 21:50:04 +00:00
*
2020-07-30 20:10:15 +00:00
* Permission is hereby granted , free of charge , to any person obtaining a copy
* of this software and associated documentation files ( the " Software " ) , to deal
* in the Software without restriction , including without limitation the rights
* to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
* copies of the Software , and to permit persons to whom the Software is
* furnished to do so , subject to the following conditions :
2020-05-23 21:50:04 +00:00
*
2020-07-30 20:10:15 +00:00
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software .
2020-05-23 21:50:04 +00:00
*
2020-07-30 20:10:15 +00:00
* THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
* IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
* LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE .
2020-05-23 21:50:04 +00:00
*
2020-07-30 20:10:15 +00:00
* @author GeyserMC
* @link https : //github.com/GeyserMC/Geyser
2020-05-23 21:50:04 +00:00
* /
2021-11-20 21:34:30 +00:00
package org.geysermc.geyser.platform.spigot ;
2020-05-23 21:50:04 +00:00
import lombok.AllArgsConstructor ;
import org.bukkit.Bukkit ;
import org.bukkit.entity.Player ;
import org.bukkit.event.server.ServerListPingEvent ;
import org.bukkit.util.CachedServerIcon ;
2021-11-20 23:29:46 +00:00
import org.geysermc.geyser.ping.GeyserPingInfo ;
2021-11-20 21:34:30 +00:00
import org.geysermc.geyser.network.MinecraftProtocol ;
import org.geysermc.geyser.ping.IGeyserPingPassthrough ;
2020-05-23 21:50:04 +00:00
2021-06-21 01:42:22 +00:00
import javax.annotation.Nonnull ;
2020-05-23 21:50:04 +00:00
import java.net.InetAddress ;
2020-12-07 19:04:50 +00:00
import java.net.InetSocketAddress ;
2020-05-23 21:50:04 +00:00
import java.util.Collections ;
import java.util.Iterator ;
@AllArgsConstructor
2020-05-26 14:05:25 +00:00
public class GeyserSpigotPingPassthrough implements IGeyserPingPassthrough {
2020-05-23 21:50:04 +00:00
2020-05-26 14:05:25 +00:00
private final GeyserSpigotLogger logger ;
2020-05-23 21:50:04 +00:00
@Override
2020-12-07 19:04:50 +00:00
public GeyserPingInfo getPingInformation ( InetSocketAddress inetSocketAddress ) {
2020-05-23 21:50:04 +00:00
try {
2020-12-07 19:04:50 +00:00
ServerListPingEvent event = new GeyserPingEvent ( inetSocketAddress . getAddress ( ) , Bukkit . getMotd ( ) , Bukkit . getOnlinePlayers ( ) . size ( ) , Bukkit . getMaxPlayers ( ) ) ;
2020-05-23 21:50:04 +00:00
Bukkit . getPluginManager ( ) . callEvent ( event ) ;
2020-07-04 21:35:48 +00:00
GeyserPingInfo geyserPingInfo = new GeyserPingInfo ( event . getMotd ( ) ,
new GeyserPingInfo . Players ( event . getMaxPlayers ( ) , event . getNumPlayers ( ) ) ,
2021-11-14 05:07:24 +00:00
new GeyserPingInfo . Version ( Bukkit . getVersion ( ) , MinecraftProtocol . getJavaProtocolVersion ( ) ) // thanks Spigot for not exposing this, just default to latest
2020-07-04 21:35:48 +00:00
) ;
Bukkit . getOnlinePlayers ( ) . stream ( ) . map ( Player : : getName ) . forEach ( geyserPingInfo . getPlayerList ( ) : : add ) ;
2020-05-23 21:50:04 +00:00
return geyserPingInfo ;
} catch ( Exception e ) {
2021-12-23 04:05:56 +00:00
logger . debug ( " Error while getting Bukkit ping passthrough: " + e ) ;
return null ;
2020-05-23 21:50:04 +00:00
}
}
// These methods are unimplemented on spigot api by default so we add stubs so plugins don't complain
private static class GeyserPingEvent extends ServerListPingEvent {
public GeyserPingEvent ( InetAddress address , String motd , int numPlayers , int maxPlayers ) {
super ( address , motd , numPlayers , maxPlayers ) ;
}
@Override
public void setServerIcon ( CachedServerIcon icon ) throws IllegalArgumentException , UnsupportedOperationException {
}
2021-06-21 01:42:22 +00:00
@Nonnull
2020-05-23 21:50:04 +00:00
@Override
public Iterator < Player > iterator ( ) throws UnsupportedOperationException {
2021-06-21 01:42:22 +00:00
return Collections . emptyIterator ( ) ;
2020-05-23 21:50:04 +00:00
}
}
}