Java Code Examples for com.velocitypowered.api.network.ProtocolVersion#compareTo()

The following examples show how to use com.velocitypowered.api.network.ProtocolVersion#compareTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TabCompleteRequest.java    From Velocity with MIT License 6 votes vote down vote up
@Override
public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
  if (version.compareTo(MINECRAFT_1_13) >= 0) {
    this.transactionId = ProtocolUtils.readVarInt(buf);
    this.command = ProtocolUtils.readString(buf, VANILLA_MAX_TAB_COMPLETE_LEN);
  } else {
    this.command = ProtocolUtils.readString(buf, VANILLA_MAX_TAB_COMPLETE_LEN);
    if (version.compareTo(MINECRAFT_1_9) >= 0) {
      this.assumeCommand = buf.readBoolean();
    }
    this.hasPosition = buf.readBoolean();
    if (hasPosition) {
      this.position = buf.readLong();
    }
  }
}
 
Example 2
Source File: TabCompleteRequest.java    From Velocity with MIT License 6 votes vote down vote up
@Override
public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
  if (command == null) {
    throw new IllegalStateException("Command is not specified");
  }

  if (version.compareTo(MINECRAFT_1_13) >= 0) {
    ProtocolUtils.writeVarInt(buf, transactionId);
    ProtocolUtils.writeString(buf, command);
  } else {
    ProtocolUtils.writeString(buf, command);
    if (version.compareTo(MINECRAFT_1_9) >= 0) {
      buf.writeBoolean(assumeCommand);
    }
    buf.writeBoolean(hasPosition);
    if (hasPosition) {
      buf.writeLong(position);
    }
  }
}
 
Example 3
Source File: Respawn.java    From Velocity with MIT License 6 votes vote down vote up
@Override
public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) {
    ProtocolUtils.writeString(buf, dimensionInfo.getRegistryIdentifier());
    ProtocolUtils.writeString(buf, dimensionInfo.getLevelName());
  } else {
    buf.writeInt(dimension);
  }
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_13_2) <= 0) {
    buf.writeByte(difficulty);
  }
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) {
    buf.writeLong(partialHashedSeed);
  }
  buf.writeByte(gamemode);
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) {
    buf.writeByte(previousGamemode);
    buf.writeBoolean(dimensionInfo.isDebugType());
    buf.writeBoolean(dimensionInfo.isFlat());
    buf.writeBoolean(shouldKeepPlayerData);
  } else {
    ProtocolUtils.writeString(buf, levelType);
  }
}
 
Example 4
Source File: ConnectedPlayer.java    From Velocity with MIT License 6 votes vote down vote up
/**
 * Determines whether or not we can forward a plugin message onto the client.
 * @param version the Minecraft protocol version
 * @param message the plugin message to forward to the client
 * @return {@code true} if the message can be forwarded, {@code false} otherwise
 */
public boolean canForwardPluginMessage(ProtocolVersion version, PluginMessage message) {
  boolean minecraftOrFmlMessage;

  // By default, all internal Minecraft and Forge channels are forwarded from the server.
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_12_2) <= 0) {
    String channel = message.getChannel();
    minecraftOrFmlMessage = channel.startsWith("MC|")
        || channel.startsWith(LegacyForgeConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL)
        || PluginMessageUtil.isLegacyRegister(message)
        || PluginMessageUtil.isLegacyUnregister(message);
  } else {
    minecraftOrFmlMessage = message.getChannel().startsWith("minecraft:");
  }

  // Otherwise, we need to see if the player already knows this channel or it's known by the
  // proxy.
  return minecraftOrFmlMessage || knownChannels.contains(message.getChannel());
}
 
Example 5
Source File: Chat.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
  if (message == null) {
    throw new IllegalStateException("Message is not specified");
  }
  ProtocolUtils.writeString(buf, message);
  if (direction == ProtocolUtils.Direction.CLIENTBOUND) {
    buf.writeByte(type);
    if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) {
      ProtocolUtils.writeUuid(buf, sender == null ? EMPTY_SENDER : sender);
    }
  }
}
 
Example 6
Source File: ResourcePackResponse.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public void decode(ByteBuf buf, Direction direction, ProtocolVersion protocolVersion) {
  if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_9_4) <= 0) {
    this.hash = ProtocolUtils.readString(buf);
  }
  this.status = Status.values()[ProtocolUtils.readVarInt(buf)];
}
 
Example 7
Source File: ServerLoginSuccess.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
  if (uuid == null) {
    throw new IllegalStateException("No UUID specified!");
  }
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) {
    ProtocolUtils.writeUuidIntArray(buf, uuid);
  } else {
    ProtocolUtils.writeString(buf, uuid.toString());
  }
  if (username == null) {
    throw new IllegalStateException("No username specified!");
  }
  ProtocolUtils.writeString(buf, username);
}
 
Example 8
Source File: ServerLoginSuccess.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) {
    uuid = ProtocolUtils.readUuidIntArray(buf);
  } else {
    uuid = UUID.fromString(ProtocolUtils.readString(buf, 36));
  }
  username = ProtocolUtils.readString(buf, 16);
}
 
Example 9
Source File: ClientSettings.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
  if (locale == null) {
    throw new IllegalStateException("No locale specified");
  }
  ProtocolUtils.writeString(buf, locale);
  buf.writeByte(viewDistance);
  ProtocolUtils.writeVarInt(buf, chatVisibility);
  buf.writeBoolean(chatColors);
  buf.writeByte(skinParts);

  if (version.compareTo(ProtocolVersion.MINECRAFT_1_9) >= 0) {
    ProtocolUtils.writeVarInt(buf, mainHand);
  }
}
 
Example 10
Source File: ClientSettings.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
  this.locale = ProtocolUtils.readString(buf, 16);
  this.viewDistance = buf.readByte();
  this.chatVisibility = ProtocolUtils.readVarInt(buf);
  this.chatColors = buf.readBoolean();
  this.skinParts = buf.readUnsignedByte();

  if (version.compareTo(ProtocolVersion.MINECRAFT_1_9) >= 0) {
    this.mainHand = ProtocolUtils.readVarInt(buf);
  }
}
 
Example 11
Source File: KeepAlive.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_12_2) >= 0) {
    buf.writeLong(randomId);
  } else {
    ProtocolUtils.writeVarInt(buf, (int) randomId);
  }
}
 
Example 12
Source File: VelocityChannelRegistrar.java    From Velocity with MIT License 5 votes vote down vote up
/**
 * Returns all the channel names to register depending on the Minecraft protocol version.
 * @param protocolVersion the protocol version in use
 * @return the list of channels to register
 */
public Collection<String> getChannelsForProtocol(ProtocolVersion protocolVersion) {
  if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0) {
    return getModernChannelIds();
  }
  return getLegacyChannelIds();
}
 
Example 13
Source File: Respawn.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
  String dimensionIdentifier = null;
  String levelName = null;
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) {
    dimensionIdentifier = ProtocolUtils.readString(buf);
    levelName = ProtocolUtils.readString(buf);
  } else {
    this.dimension = buf.readInt();
  }
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_13_2) <= 0) {
    this.difficulty = buf.readUnsignedByte();
  }
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) {
    this.partialHashedSeed = buf.readLong();
  }
  this.gamemode = buf.readByte();
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) {
    this.previousGamemode = buf.readByte();
    boolean isDebug = buf.readBoolean();
    boolean isFlat = buf.readBoolean();
    this.dimensionInfo = new DimensionInfo(dimensionIdentifier, levelName, isFlat, isDebug);
    this.shouldKeepPlayerData = buf.readBoolean();
  } else {
    this.levelType = ProtocolUtils.readString(buf, 16);
  }
}
 
Example 14
Source File: PluginMessage.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
  if (channel == null) {
    throw new IllegalStateException("Channel is not specified.");
  }
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0) {
    ProtocolUtils.writeString(buf, transformLegacyToModernChannel(this.channel));
  } else {
    ProtocolUtils.writeString(buf, this.channel);
  }
  buf.writeBytes(data);
}
 
Example 15
Source File: PluginMessage.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
  this.channel = ProtocolUtils.readString(buf);
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0) {
    this.channel = transformLegacyToModernChannel(this.channel);
  }
  this.data = new byte[buf.readableBytes()];
  buf.readBytes(data);
}
 
Example 16
Source File: ResourcePackResponse.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public void encode(ByteBuf buf, Direction direction, ProtocolVersion protocolVersion) {
  if (protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_9_4) <= 0) {
    ProtocolUtils.writeString(buf, hash);
  }
  ProtocolUtils.writeVarInt(buf, status.ordinal());
}
 
Example 17
Source File: PluginMessageUtil.java    From Velocity with MIT License 5 votes vote down vote up
/**
 * Constructs a channel (un)register packet.
 * @param protocolVersion the client/server's protocol version
 * @param channels the channels to register
 * @return the plugin message to send
 */
public static PluginMessage constructChannelsPacket(ProtocolVersion protocolVersion,
                                                    Collection<String> channels) {
  Preconditions.checkNotNull(channels, "channels");
  Preconditions.checkArgument(channels.size() > 0, "no channels specified");
  String channelName = protocolVersion.compareTo(ProtocolVersion.MINECRAFT_1_13) >= 0
      ? REGISTER_CHANNEL : REGISTER_CHANNEL_LEGACY;
  PluginMessage message = new PluginMessage();
  message.setChannel(channelName);
  message.setData(String.join("\0", channels).getBytes(StandardCharsets.UTF_8));
  return message;
}
 
Example 18
Source File: JoinGame.java    From Velocity with MIT License 4 votes vote down vote up
@Override
public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
  this.entityId = buf.readInt();
  this.gamemode = buf.readByte();
  String dimensionIdentifier = null;
  String levelName = null;
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) {
    this.previousGamemode = buf.readByte();
    ImmutableSet<String> levelNames = ImmutableSet.copyOf(ProtocolUtils.readStringArray(buf));
    ImmutableSet<DimensionData> readData = DimensionRegistry.fromGameData(ProtocolUtils.readCompoundTag(buf));
    this.dimensionRegistry = new DimensionRegistry(readData, levelNames);
    dimensionIdentifier = ProtocolUtils.readString(buf);
    levelName = ProtocolUtils.readString(buf);
  } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_9_1) >= 0) {
    this.dimension = buf.readInt();
  } else {
    this.dimension = buf.readByte();
  }
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_13_2) <= 0) {
    this.difficulty = buf.readUnsignedByte();
  }
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) {
    this.partialHashedSeed = buf.readLong();
  }
  this.maxPlayers = buf.readUnsignedByte();
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) < 0) {
    this.levelType = ProtocolUtils.readString(buf, 16);
  }
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_14) >= 0) {
    this.viewDistance = ProtocolUtils.readVarInt(buf);
  }
  this.reducedDebugInfo = buf.readBoolean();
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) {
    this.showRespawnScreen = buf.readBoolean();
  }
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) {
    boolean isDebug = buf.readBoolean();
    boolean isFlat = buf.readBoolean();
    this.dimensionInfo = new DimensionInfo(dimensionIdentifier, levelName, isFlat, isDebug);
  }
}
 
Example 19
Source File: JoinGame.java    From Velocity with MIT License 4 votes vote down vote up
@Override
public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {
  buf.writeInt(entityId);
  buf.writeByte(gamemode);
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) {
    buf.writeByte(previousGamemode);
    ProtocolUtils.writeStringArray(buf, dimensionRegistry.getLevelNames().toArray(
            new String[dimensionRegistry.getLevelNames().size()]));
    ProtocolUtils.writeCompoundTag(buf, dimensionRegistry.encodeRegistry());
    ProtocolUtils.writeString(buf, dimensionInfo.getRegistryIdentifier());
    ProtocolUtils.writeString(buf, dimensionInfo.getLevelName());
  } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_9_1) >= 0) {
    buf.writeInt(dimension);
  } else {
    buf.writeByte(dimension);
  }
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_13_2) <= 0) {
    buf.writeByte(difficulty);
  }
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) {
    buf.writeLong(partialHashedSeed);
  }
  buf.writeByte(maxPlayers);
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) < 0) {
    if (levelType == null) {
      throw new IllegalStateException("No level type specified.");
    }
    ProtocolUtils.writeString(buf, levelType);
  }
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_14) >= 0) {
    ProtocolUtils.writeVarInt(buf,viewDistance);
  }
  buf.writeBoolean(reducedDebugInfo);
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) {
    buf.writeBoolean(showRespawnScreen);
  }
  if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) {
    buf.writeBoolean(dimensionInfo.isDebugType());
    buf.writeBoolean(dimensionInfo.isFlat());
  }
}
 
Example 20
Source File: StateRegistry.java    From Velocity with MIT License 4 votes vote down vote up
<P extends MinecraftPacket> void register(Class<P> clazz, Supplier<P> packetSupplier,
                                          PacketMapping... mappings) {
  if (mappings.length == 0) {
    throw new IllegalArgumentException("At least one mapping must be provided.");
  }

  for (int i = 0; i < mappings.length; i++) {
    PacketMapping current = mappings[i];
    PacketMapping next = (i + 1 < mappings.length) ? mappings[i + 1] : current;
    ProtocolVersion from = current.protocolVersion;
    ProtocolVersion to = current == next ? getLast(SUPPORTED_VERSIONS) : next.protocolVersion;

    if (from.compareTo(to) >= 0 && from != getLast(SUPPORTED_VERSIONS)) {
      throw new IllegalArgumentException(String.format(
          "Next mapping version (%s) should be lower then current (%s)", to, from));
    }

    for (ProtocolVersion protocol : EnumSet.range(from, to)) {
      if (protocol == to && next != current) {
        break;
      }
      ProtocolRegistry registry = this.versions.get(protocol);
      if (registry == null) {
        throw new IllegalArgumentException("Unknown protocol version "
            + current.protocolVersion);
      }

      if (registry.packetIdToSupplier.containsKey(current.id)) {
        throw new IllegalArgumentException("Can not register class " + clazz.getSimpleName()
            + " with id " + current.id + " for " + registry.version
            + " because another packet is already registered");
      }

      if (registry.packetClassToId.containsKey(clazz)) {
        throw new IllegalArgumentException(clazz.getSimpleName()
            + " is already registered for version " + registry.version);
      }

      if (!current.encodeOnly) {
        registry.packetIdToSupplier.put(current.id, packetSupplier);
      }
      registry.packetClassToId.put(clazz, current.id);
    }
  }
}