コード例 #1
0
func (this *requestNotifyPlayerCodec) Decode(reader io.Reader) (request Request, err error) {
	requestNotifyPlayer := new(RequestNotifyPlayer)
	requestNotifyPlayer.Add, err = packet.ReadBool(reader)
	if err != nil {
		return
	}
	requestNotifyPlayer.Player, err = packet.ReadString(reader)
	if err != nil {
		return
	}
	requestNotifyPlayer.Uuid, err = packet.ReadUUID(reader)
	if err != nil {
		return
	}
	request = requestNotifyPlayer
	return
}
コード例 #2
0
func (this *packetClientPlayerListCodec) Decode(reader io.Reader) (decode packet.Packet, err error) {
	packetClientPlayerList := new(PacketClientPlayerList)
	packetClientPlayerList.Action, err = packet.ReadVarInt(reader)
	if err != nil {
		return
	}
	itemLength, err := packet.ReadVarInt(reader)
	if err != nil {
		return
	}
	if itemLength < 0 {
		err = errors.New(fmt.Sprintf("Decode, Item length is below zero: %d", itemLength))
		return
	}
	if itemLength > 65535 {
		err = errors.New(fmt.Sprintf("Decode, Item length is above maximum: %d", itemLength))
		return
	}
	packetClientPlayerList.Items = make([]PacketClientPlayerListItem, itemLength)
	for i, _ := range packetClientPlayerList.Items {
		item := &packetClientPlayerList.Items[i]
		item.UUID, err = packet.ReadUUID(reader)
		if err != nil {
			return
		}
		switch packetClientPlayerList.Action {
		case PACKET_CLIENT_PLAYER_LIST_ACTION_ADD:
			addPlayer := PacketClientPlayerListAddPlayer{}
			addPlayer.Name, err = packet.ReadString(reader)
			if err != nil {
				return
			}
			var propertiesLength int
			propertiesLength, err = packet.ReadVarInt(reader)
			if err != nil {
				return
			}
			if propertiesLength < 0 {
				err = errors.New(fmt.Sprintf("Decode, Properties length is below zero: %d", propertiesLength))
				return
			}
			if propertiesLength > 65535 {
				err = errors.New(fmt.Sprintf("Decode, Properties length is above maximum: %d", propertiesLength))
				return
			}
			addPlayer.Properties = make([]PacketClientPlayerListAddPlayerProperty, propertiesLength)
			for j, _ := range addPlayer.Properties {
				property := &addPlayer.Properties[j]
				property.Name, err = packet.ReadString(reader)
				if err != nil {
					return
				}
				property.Value, err = packet.ReadString(reader)
				if err != nil {
					return
				}
				var signed bool
				signed, err = packet.ReadBool(reader)
				if err != nil {
					return
				}
				if signed {
					property.Signature, err = packet.ReadString(reader)
					if err != nil {
						return
					}
				}
			}
			addPlayer.Gamemode, err = packet.ReadVarInt(reader)
			if err != nil {
				return
			}
			addPlayer.Latency, err = packet.ReadVarInt(reader)
			if err != nil {
				return
			}
			var hasDisplayName bool
			hasDisplayName, err = packet.ReadBool(reader)
			if err != nil {
				return
			}
			if hasDisplayName {
				addPlayer.DisplayName, err = packet.ReadString(reader)
				if err != nil {
					return
				}
			}
			item.Info = addPlayer
		case PACKET_CLIENT_PLAYER_LIST_ACTION_UPDATE_GAMEMODE:
			updateGamemode := PacketClientPlayerListUpdateGamemode{}
			updateGamemode.Gamemode, err = packet.ReadVarInt(reader)
			if err != nil {
				return
			}
			item.Info = updateGamemode
		case PACKET_CLIENT_PLAYER_LIST_ACTION_UPDATE_LATENCY:
			updateLatency := PacketClientPlayerListUpdateLatency{}
			updateLatency.Latency, err = packet.ReadVarInt(reader)
			if err != nil {
				return
			}
			item.Info = updateLatency
		case PACKET_CLIENT_PLAYER_LIST_ACTION_UPDATE_DISPLAY_NAME:
			updateDisplayName := PacketClientPlayerListUpdateDisplayName{}
			var hasDisplayName bool
			hasDisplayName, err = packet.ReadBool(reader)
			if err != nil {
				return
			}
			if hasDisplayName {
				updateDisplayName.DisplayName, err = packet.ReadString(reader)
				if err != nil {
					return
				}
			}
			item.Info = updateDisplayName
		case PACKET_CLIENT_PLAYER_LIST_ACTION_REMOVE:
			// no payload
		default:
			err = errors.New(fmt.Sprintf("Decode, PacketClientPlayerList action is not valid: %d", packetClientPlayerList.Action))
		}
	}
	decode = packetClientPlayerList
	return
}