Properly implement server list ping.
This commit is contained in:
parent
83085ec4f4
commit
660f5e67f5
@ -1,7 +1,7 @@
|
||||
package io.izzel.arclight.common.mixin.core.network.status;
|
||||
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import io.izzel.arclight.common.bridge.entity.player.ServerPlayerEntityBridge;
|
||||
import io.izzel.arclight.common.mod.util.ArclightPingEvent;
|
||||
import net.minecraft.entity.player.ServerPlayerEntity;
|
||||
import net.minecraft.network.IPacket;
|
||||
import net.minecraft.network.NetworkManager;
|
||||
@ -12,11 +12,6 @@ import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.util.SharedConstants;
|
||||
import net.minecraft.util.text.StringTextComponent;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.craftbukkit.v.CraftServer;
|
||||
import org.bukkit.craftbukkit.v.util.CraftIconCache;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.CachedServerIcon;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.spigotmc.SpigotConfig;
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
@ -24,12 +19,9 @@ import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Redirect;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
@Mixin(ServerStatusNetHandler.class)
|
||||
public class ServerStatusNetHandlerMixin {
|
||||
@ -39,74 +31,7 @@ public class ServerStatusNetHandlerMixin {
|
||||
@Redirect(method = "processServerQuery", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/NetworkManager;sendPacket(Lnet/minecraft/network/IPacket;)V"))
|
||||
private void arclight$handleServerPing(NetworkManager networkManager, IPacket<?> packetIn) {
|
||||
Object[] players = this.server.getPlayerList().players.toArray();
|
||||
class ServerListPingEvent extends org.bukkit.event.server.ServerListPingEvent {
|
||||
|
||||
CraftIconCache icon;
|
||||
|
||||
ServerListPingEvent() {
|
||||
super(((InetSocketAddress) networkManager.getRemoteAddress()).getAddress(), server.getMOTD(), server.getPlayerList().getMaxPlayers());
|
||||
this.icon = ((CraftServer) Bukkit.getServer()).getServerIcon();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setServerIcon(CachedServerIcon icon) {
|
||||
if (!(icon instanceof CraftIconCache)) {
|
||||
throw new IllegalArgumentException(icon + " was not created by " + CraftServer.class);
|
||||
}
|
||||
this.icon = (CraftIconCache) icon;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Iterator<Player> iterator() throws UnsupportedOperationException {
|
||||
class Itr implements Iterator<Player> {
|
||||
|
||||
int i;
|
||||
int ret = Integer.MIN_VALUE;
|
||||
ServerPlayerEntity player;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (this.player != null) {
|
||||
return true;
|
||||
}
|
||||
Object[] currentPlayers = players;
|
||||
for (int length = currentPlayers.length, i = this.i; i < length; ++i) {
|
||||
ServerPlayerEntity player = (ServerPlayerEntity) currentPlayers[i];
|
||||
if (player != null) {
|
||||
this.i = i + 1;
|
||||
this.player = player;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player next() {
|
||||
if (!this.hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
ServerPlayerEntity player = this.player;
|
||||
this.player = null;
|
||||
this.ret = this.i - 1;
|
||||
return ((ServerPlayerEntityBridge) player).bridge$getBukkitEntity();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
Object[] currentPlayers = players;
|
||||
int i = this.ret;
|
||||
if (i < 0 || currentPlayers[i] == null) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
currentPlayers[i] = null;
|
||||
}
|
||||
}
|
||||
return new Itr();
|
||||
}
|
||||
}
|
||||
ServerListPingEvent event = new ServerListPingEvent();
|
||||
ArclightPingEvent event = new ArclightPingEvent(networkManager, server);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
List<GameProfile> profiles = new ArrayList<>(players.length);
|
||||
Object[] array;
|
||||
@ -128,6 +53,7 @@ public class ServerStatusNetHandlerMixin {
|
||||
ping.setPlayers(playerSample);
|
||||
int version = SharedConstants.getVersion().getProtocolVersion();
|
||||
ping.setVersion(new ServerStatusResponse.Version(this.server.getServerModName() + " " + this.server.getMinecraftVersion(), version));
|
||||
ping.setForgeData(this.server.getServerStatusResponse().getForgeData());
|
||||
networkManager.sendPacket(new SServerInfoPacket(ping));
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,85 @@
|
||||
package io.izzel.arclight.common.mod.util;
|
||||
|
||||
import io.izzel.arclight.common.bridge.entity.player.ServerPlayerEntityBridge;
|
||||
import net.minecraft.entity.player.ServerPlayerEntity;
|
||||
import net.minecraft.network.NetworkManager;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.craftbukkit.v.CraftServer;
|
||||
import org.bukkit.craftbukkit.v.util.CraftIconCache;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.server.ServerListPingEvent;
|
||||
import org.bukkit.util.CachedServerIcon;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class ArclightPingEvent extends ServerListPingEvent {
|
||||
|
||||
public CraftIconCache icon;
|
||||
private final Object[] players;
|
||||
|
||||
public ArclightPingEvent(NetworkManager networkManager, MinecraftServer server) {
|
||||
super(((InetSocketAddress) networkManager.getRemoteAddress()).getAddress(), server.getMOTD(), server.getPlayerList().getMaxPlayers());
|
||||
this.icon = ((CraftServer) Bukkit.getServer()).getServerIcon();
|
||||
this.players = server.getPlayerList().players.toArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setServerIcon(CachedServerIcon icon) {
|
||||
if (!(icon instanceof CraftIconCache)) {
|
||||
throw new IllegalArgumentException(icon + " was not created by " + CraftServer.class);
|
||||
}
|
||||
this.icon = (CraftIconCache) icon;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Iterator<Player> iterator() throws UnsupportedOperationException {
|
||||
return new Iterator<Player>() {
|
||||
int i;
|
||||
int ret = Integer.MIN_VALUE;
|
||||
ServerPlayerEntity player;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (this.player != null) {
|
||||
return true;
|
||||
}
|
||||
Object[] currentPlayers = players;
|
||||
for (int length = currentPlayers.length, i = this.i; i < length; ++i) {
|
||||
ServerPlayerEntity player = (ServerPlayerEntity) currentPlayers[i];
|
||||
if (player != null) {
|
||||
this.i = i + 1;
|
||||
this.player = player;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player next() {
|
||||
if (!this.hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
ServerPlayerEntity player = this.player;
|
||||
this.player = null;
|
||||
this.ret = this.i - 1;
|
||||
return ((ServerPlayerEntityBridge) player).bridge$getBukkitEntity();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
Object[] currentPlayers = players;
|
||||
int i = this.ret;
|
||||
if (i < 0 || currentPlayers[i] == null) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
currentPlayers[i] = null;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user