SetupPacketHandler class

Пакет: com.hypixel.hytale.server.core.io.handlers

Файл: com/hypixel/hytale/server/core/io/handlers/SetupPacketHandler.java

extends: GenericConnectionPacketHandler

Поля (22)

МодификаторыТипИмя
private PlayerCommonAssets assets
break
break
break
break
return
WorldSettings var10
Asset[] var11
HytaleServerConfig var12
HytaleServerConfig.TimeoutProfile var2
IEventDispatcher var2
CompletableFuture var2
CompletableFuture var2
PlayerSetupConnectEvent var3
HytaleServerConfig.TimeoutProfile var3
HytaleServerConfig.TimeoutProfile var3
ClientReferral var4
PlayerRef var5
ChannelConnection var6
Ref var7
World var8
CompletableFuture var9

Методы (18)

МодификаторыВозвратСигнатура
abstract throw new IllegalArgumentExceptionthrow new IllegalArgumentException("Received duplicate RequestAssets!")
abstract throw new IllegalArgumentExceptionthrow new IllegalArgumentException("Hasn't received RequestAssets yet!")
abstract throw new RuntimeExceptionthrow new RuntimeException("Exception when player was joining", var1x)
abstract throw new RuntimeExceptionthrow new RuntimeException("Exception when player adding to universe", var1x)
public void handlevoid handle(@Nonnull ClientDisconnect var1)
public void handlevoid handle(@Nonnull RequestAssets var1)
public void handlevoid handle(@Nonnull ViewRadius var1)
public void handlevoid handle(@Nonnull SetupFinalize var1)
if if(var4 != null)
if if(var5 != null)
if if(var7 != null)
if if(var8 != null)
if if(Constants.SINGLEPLAYER)
if if(this.receivedRequest)
if if(!this.receivedRequest)
if if(this.setupFinalized)
if if(this.auth == null)
abstract super super(var1, var2, var3)

Исходный код

Показать/скрыть
class="kw">package com.hypixel.hytale.server.core.io.handlers;

class="kw">import com.hypixel.hytale.common.util.CompletableFutureUtil;
class="kw">import com.hypixel.hytale.component.Ref;
class="kw">import com.hypixel.hytale.event.IEventDispatcher;
class="kw">import com.hypixel.hytale.logger.HytaleLogger;
class="kw">import com.hypixel.hytale.math.util.MathUtil;
class="kw">import com.hypixel.hytale.protocol.Asset;
class="kw">import com.hypixel.hytale.protocol.NetworkChannel;
class="kw">import com.hypixel.hytale.protocol.ToServerPacket;
class="kw">import com.hypixel.hytale.protocol.io.ChannelConnection;
class="kw">import com.hypixel.hytale.protocol.io.ConnectionHandler;
class="kw">import com.hypixel.hytale.protocol.packets.auth.ClientReferral;
class="kw">import com.hypixel.hytale.protocol.packets.connection.ClientDisconnect;
class="kw">import com.hypixel.hytale.protocol.packets.connection.DisconnectType;
class="kw">import com.hypixel.hytale.protocol.packets.interface_.ServerInfo;
class="kw">import com.hypixel.hytale.protocol.packets.setup.RequestAssets;
class="kw">import com.hypixel.hytale.protocol.packets.setup.SetupFinalize;
class="kw">import com.hypixel.hytale.protocol.packets.setup.ViewRadius;
class="kw">import com.hypixel.hytale.protocol.packets.setup.WorldLoadFinished;
class="kw">import com.hypixel.hytale.protocol.packets.setup.WorldLoadProgress;
class="kw">import com.hypixel.hytale.protocol.packets.setup.WorldSettings;
class="kw">import com.hypixel.hytale.server.core.Constants;
class="kw">import com.hypixel.hytale.server.core.HytaleServer;
class="kw">import com.hypixel.hytale.server.core.HytaleServerConfig;
class="kw">import com.hypixel.hytale.server.core.Message;
class="kw">import com.hypixel.hytale.server.core.asset.AssetRegistryLoader;
class="kw">import com.hypixel.hytale.server.core.asset.common.CommonAssetModule;
class="kw">import com.hypixel.hytale.server.core.asset.common.PlayerCommonAssets;
class="kw">import com.hypixel.hytale.server.core.asset.common.events.SendCommonAssetsEvent;
class="kw">import com.hypixel.hytale.server.core.auth.PlayerAuthentication;
class="kw">import com.hypixel.hytale.server.core.event.events.player.PlayerSetupConnectEvent;
class="kw">import com.hypixel.hytale.server.core.event.events.player.PlayerSetupDisconnectEvent;
class="kw">import com.hypixel.hytale.server.core.io.ProtocolVersion;
class="kw">import com.hypixel.hytale.server.core.modules.i18n.I18nModule;
class="kw">import com.hypixel.hytale.server.core.modules.singleplayer.SingleplayerModule;
class="kw">import com.hypixel.hytale.server.core.universe.PlayerRef;
class="kw">import com.hypixel.hytale.server.core.universe.Universe;
class="kw">import com.hypixel.hytale.server.core.universe.world.World;
class="kw">import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
class="kw">import com.hypixel.hytale.server.core.util.DumpUtil;
class="kw">import java.util.concurrent.CompletableFuture;
class="kw">import java.util.logging.Level;
class="kw">import javax.annotation.Nonnull;
class="kw">import javax.annotation.Nullable;

class="kw">public class SetupPacketHandler class="kw">extends GenericConnectionPacketHandler {
   class="kw">private PlayerCommonAssets assets;
   class="kw">private boolean receivedRequest;
   class="kw">private boolean setupFinalized;
   class="kw">private int clientViewRadiusChunks = 6;

   class="kw">public SetupPacketHandler(@Nonnull ChannelConnection var1, @Nonnull ProtocolVersion var2, String var3, @Nonnull PlayerAuthentication var4) {
      super(var1, var2, var3);
      this.auth = var4;
   }

   @Nonnull
   @Override
   class="kw">public String getIdentifier() {
      class="kw">return "{Setup(" + this.getChannel().formatRemoteAddress() + "), " + this.auth.getUsername() + ", " + this.auth.getUuid() + "}";
   }

   @Override
   class="kw">public void registered0(@Nonnull ConnectionHandler var1) {
      HytaleServerConfig.TimeoutProfile var2 = HytaleServer.get().getConfig().getConnectionTimeouts();
      this.enterStage("setup:world-settings", var2.getSetupWorldSettings(), () -> this.assets != null);
      if (this.auth.getReferralSource() != null) {
         HytaleLogger.getLogger()
            .at(Level.INFO)
            .log(
               "Player %s referred from %s:%d with %d bytes of data",
               this.auth.getUsername(),
               this.auth.getReferralSource().host,
               this.auth.getReferralSource().port,
               this.auth.getReferralData() != null ? this.auth.getReferralData().length : 0
            );
      }

      PlayerSetupConnectEvent var3 = HytaleServer.get()
         .getEventBus()
         .<Void, PlayerSetupConnectEvent>dispatchFor(PlayerSetupConnectEvent.class)
         .dispatch(
            new PlayerSetupConnectEvent(
               this, this.auth.getUsername(), this.auth.getUuid(), this.auth, this.auth.getReferralData(), this.auth.getReferralSource()
            )
         );
      if (var3.isCancelled()) {
         this.disconnect(var3.getReason());
      } else {
         ClientReferral var4 = var3.getClientReferral();
         if (var4 != null) {
            this.writeNoCache(var4);
         } else {
            PlayerRef var5 = Universe.get().getPlayer(this.auth.getUuid());
            if (var5 != null) {
               HytaleLogger.getLogger().at(Level.INFO).log("Found match of player %s on %s", this.auth.getUuid(), var5.getUsername());
               ChannelConnection var6 = var5.getPacketHandler().getChannel();
               if (!this.getChannel().isFromSameOrigin(var6)) {
                  this.disconnect(Message.translation("client.general.disconnect.alreadyLoggedIn"));
                  var5.sendMessage(Message.translation("server.io.setuppackethandler.otherLoginAttempt"));
                  class="kw">return;
               }

               Ref var7 = var5.getReference();
               if (var7 != null) {
                  World var8 = var7.getStore().getExternalData().getWorld();
                  if (var8 != null) {
                     CompletableFuture var9 = new CompletableFuture<>();
                     var8.execute(() -> {
                        var5.getPacketHandler().disconnect(Message.translation("server.general.disconnect.loggedInAgain"));
                        var8.execute(() -> var9.complete(null));
                     });
                     var9.join();
                  } else {
                     var5.getPacketHandler().disconnect(Message.translation("server.general.disconnect.loggedInAgain"));
                  }
               }
            }

            this.getChannel().logConnectionTimings("Load Player Config", Level.FINE);
            WorldSettings var10 = new WorldSettings();
            Asset[] var11 = CommonAssetModule.get().getRequiredAssets();
            this.assets = new PlayerCommonAssets(var11);
            var10.requiredAssets = var11;
            this.write(var10);
            HytaleServerConfig var12 = HytaleServer.get().getConfig();
            this.write(new ServerInfo(HytaleServer.get().getServerName(), var12.getMotd(), var12.getMaxPlayers(), var12.getFallbackServer()));
            this.continueStage("setup:assets-request", var2.getSetupAssetsRequest(), () -> this.receivedRequest);
         }
      }
   }

   @Override
   class="kw">public void accept(@Nonnull ToServerPacket var1) {
      class="kw">switch (var1.getId()) {
         case 1:
            this.handle((ClientDisconnect)var1);
            break;
         case 23:
            this.handle((RequestAssets)var1);
            break;
         case 32:
            this.handle((ViewRadius)var1);
            break;
         case 33:
            this.handle((SetupFinalize)var1);
            break;
         class="kw">default:
            this.disconnect(Message.translation("client.general.disconnect.protocol.unexpectedPacket").param("packetId", var1.getId()));
      }
   }

   @Override
   class="kw">public void closed(@Nullable NetworkChannel var1) {
      super.closed(var1);
      IEventDispatcher var2 = HytaleServer.get().getEventBus().dispatchFor(PlayerSetupDisconnectEvent.class);
      if (var2.hasListener()) {
         var2.dispatch(new PlayerSetupDisconnectEvent(this.auth.getUsername(), this.auth.getUuid(), this.auth, this.disconnectReason));
      }

      if (Constants.SINGLEPLAYER) {
         if (Universe.get().getPlayerCount() == 0) {
            HytaleLogger.getLogger().at(Level.INFO).log("No players left on singleplayer server shutting down!");
            HytaleServer.get().shutdownServer();
         } else if (SingleplayerModule.isOwner(this.auth.getUuid())) {
            HytaleLogger.getLogger().at(Level.INFO).log("Owner left the singleplayer server shutting down!");
            Universe.get()
               .getPlayers()
               .forEach(
                  var1x -> var1x.getPacketHandler()
                     .disconnect(Message.translation("server.general.disconnect.singleplayerOwnerLeft").param("username", this.auth.getUsername()))
               );
            HytaleServer.get().shutdownServer();
         }
      }
   }

   class="kw">public void handle(@Nonnull ClientDisconnect var1) {
      this.disconnectReason.setClientDisconnectType(var1.type);
      HytaleLogger.getLogger()
         .at(Level.INFO)
         .log(
            "%s - %s at %s left with reason: %s - %s",
            this.auth.getUuid(),
            this.auth.getUsername(),
            this.getChannel().formatRemoteAddress(),
            var1.type.name(),
            var1.reason.name()
         );
      this.getChannel().closeApplicationConnection();
      if (var1.type == DisconnectType.Crash
         && Constants.SINGLEPLAYER
         && (Universe.get().getPlayerCount() == 0 || SingleplayerModule.isOwner(this.auth.getUuid()))) {
         DumpUtil.dump(true, false);
      }
   }

   class="kw">public void handle(@Nonnull RequestAssets var1) {
      if (this.receivedRequest) {
         throw new IllegalArgumentException("Received duplicate RequestAssets!");
      }

      this.receivedRequest = true;
      this.getChannel().logConnectionTimings("Request Assets", Level.FINE);
      CompletableFuture var2 = CompletableFutureUtil._catch(
         HytaleServer.get()
            .getEventBus()
            .<Void, SendCommonAssetsEvent>dispatchForAsync(SendCommonAssetsEvent.class)
            .dispatch(new SendCommonAssetsEvent(this, var1.assets))
            .thenAccept(var1x -> {
               if (this.getChannel().isActive()) {
                  this.getChannel().logConnectionTimings("Send Common Assets", Level.FINE);
                  this.assets.sent(var1x.getRequestedAssets());
                  AssetRegistryLoader.sendAssets(this);
                  I18nModule.get().sendTranslations(this, this.language);
                  this.getChannel().logConnectionTimings("Send Config Assets", Level.FINE);
                  this.write(new WorldLoadProgress(Message.translation("client.general.worldLoad.loadingWorld").getFormattedMessage(), 0, 0));
                  this.write(new WorldLoadFinished());
               }
            })
            .exceptionally(var1x -> {
               if (!this.getChannel().isActive()) {
                  class="kw">return null;
               }

               this.disconnect(Message.translation("client.general.disconnect.loginException"));
               throw new RuntimeException("Exception when player was joining", var1x);
            })
      );
      HytaleServerConfig.TimeoutProfile var3 = HytaleServer.get().getConfig().getConnectionTimeouts();
      this.continueStage("setup:send-assets", var3.getSetupSendAssets(), () -> var2.isDone() || !var2.cancel(true));
   }

   class="kw">public void handle(@Nonnull ViewRadius var1) {
      this.clientViewRadiusChunks = MathUtil.ceil(var1.value / 32.0F);
   }

   class="kw">public void handle(@Nonnull SetupFinalize var1) {
      if (!this.receivedRequest) {
         throw new IllegalArgumentException("Hasn't received RequestAssets yet!");
      }

      this.getChannel().logConnectionTimings("Setup Finalize", Level.FINE);
      if (this.getChannel().isActive()) {
         if (this.setupFinalized) {
            this.disconnect(Message.translation("client.general.disconnect.protocol.unexpectedPacket"));
         } else {
            this.setupFinalized = true;
            if (this.auth == null) {
               this.disconnect(Message.translation("client.general.disconnect.authenticationRequired"));
            } else {
               CompletableFuture var2 = CompletableFutureUtil._catch(
                  Universe.get()
                     .addPlayer(this.getChannel(), this.language, this.protocolVersion, this.auth, this.clientViewRadiusChunks)
                     .thenAccept(var1x -> {
                        if (this.getChannel().isActive()) {
                           this.getChannel().logConnectionTimings("Add To Universe", Level.FINE);
                           this.clearTimeout();
                        }
                     })
                     .exceptionally(var1x -> {
                        if (!this.getChannel().isActive()) {
                           class="kw">return null;
                        }

                        this.disconnect(Message.translation("client.general.disconnect.universeException"));
                        throw new RuntimeException("Exception when player adding to universe", var1x);
                     })
               );
               HytaleServerConfig.TimeoutProfile var3 = HytaleServer.get().getConfig().getConnectionTimeouts();
               this.continueStage("setup:add-to-universe", var3.getSetupAddToUniverse(), () -> var2.isDone() || !var2.cancel(true));
            }
         }
      }
   }
}