Skip to content

Commit

Permalink
Respect message length limit (256, 100 for <1.11).
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Jan 27, 2022
1 parent bec582f commit da74e34
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const App = () => {
const [connection, setConnection] = React.useState<Connection | undefined>()

const [settings, setSettings] = useJsonAsyncStorage<Settings>('@settings', {
// TODO: Better defaults and settings.
// TODO: Better defaults and settings e.g. join message is 140 chars (bad for <1.11).
joinMessage:
'I connected using EnderChat, an ad-free, easy to use and well-built ' +
'alternative to ChatCraft for Android! Even this message can be disabled!',
Expand Down
5 changes: 4 additions & 1 deletion src/components/Setting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ const Setting = <T extends string | boolean>({
value,
onClick,
setValue,
multiline
multiline,
maxLength
}: {
name: string
value: T
onClick?: () => void
setValue?: (newValue: T) => void
multiline?: boolean
maxLength?: number
}) => {
const da = useDarkMode()
const [modalOpen, setModalOpen] = useState(false)
Expand All @@ -44,6 +46,7 @@ const Setting = <T extends string | boolean>({
{typeof value === 'string' && setValue && (
<TextFieldDialog
name={name}
maxLength={maxLength}
placeholder={name}
multiline={multiline}
modalOpen={modalOpen}
Expand Down
3 changes: 3 additions & 0 deletions src/components/TextFieldDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import useDarkMode from '../context/useDarkMode'
const TextFieldDialog = ({
name,
multiline,
maxLength,
modalOpen,
closeModal,
placeholder,
Expand All @@ -17,6 +18,7 @@ const TextFieldDialog = ({
}: {
name: string
modalOpen: boolean
maxLength?: number
multiline?: boolean
placeholder: string
closeModal: () => void
Expand All @@ -38,6 +40,7 @@ const TextFieldDialog = ({
<Text style={styles.modalTitle}>{name}</Text>
<TextField
value={modalContent}
maxLength={maxLength}
multiline={multiline}
placeholder={placeholder}
onChangeText={setModalContent}
Expand Down
24 changes: 14 additions & 10 deletions src/minecraft/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,32 @@ export declare interface ServerConnection {
((event: string, listener: Function) => this)
}

export interface ConnectionOptions {
host: string
port: number
username: string
protocolVersion: number
selectedProfile?: string
accessToken?: string
}

export class ServerConnection extends events.EventEmitter {
bufferedData: Buffer = Buffer.from([])
compressionThreshold = -1
compressionEnabled = false
loggedIn = false
closed = false
socket: net.Socket
options: ConnectionOptions
disconnectTimer?: NodeJS.Timeout
disconnectReason?: string
aesDecipher?: Decipher
aesCipher?: Cipher

constructor(socket: net.Socket) {
constructor(socket: net.Socket, options: ConnectionOptions) {
super()
this.socket = socket
this.options = options
}

async writePacket(
Expand Down Expand Up @@ -73,18 +84,11 @@ export class ServerConnection extends events.EventEmitter {
}
}

const initiateConnection = async (opts: {
host: string
port: number
username: string
protocolVersion: number
selectedProfile?: string
accessToken?: string
}) => {
const initiateConnection = async (opts: ConnectionOptions) => {
const [host, port] = await resolveHostname(opts.host, opts.port)
return await new Promise<ServerConnection>((resolve, reject) => {
const socket = net.createConnection({ host, port })
const conn = new ServerConnection(socket)
const conn = new ServerConnection(socket, opts)
let resolved = false
const { accessToken, selectedProfile } = opts
socket.on('connect', () => {
Expand Down
13 changes: 11 additions & 2 deletions src/screens/ChatScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ const ChatScreen = ({ navigation }: { navigation: ChatNavigationProp }) => {
const [message, setMessage] = useState('')
const loggedInRef = useRef(false)

const colorMap = mojangColorMap
const charLimit =
connection && connection.connection.options.protocolVersion >= 306 // 16w38a
? 256
: 100
const colorMap = darkMode ? mojangColorMap : lightColorMap
const addMessage = (text: MinecraftChat) =>
setMessages(m => {
const trunc = m.length > 500 ? m.slice(0, 499) : m
Expand All @@ -104,7 +108,10 @@ const ChatScreen = ({ navigation }: { navigation: ChatNavigationProp }) => {
)
if (settings.sendJoinMessage) {
connection.connection
.writePacket(0x03, concatPacketData([settings.joinMessage]))
.writePacket(
0x03,
concatPacketData([settings.joinMessage.substring(charLimit)])
)
.catch(errorHandler)
}
if (settings.sendSpawnCommand) {
Expand All @@ -131,6 +138,7 @@ const ChatScreen = ({ navigation }: { navigation: ChatNavigationProp }) => {
}
}, [
colorMap,
charLimit,
connection,
settings.joinMessage,
settings.sendJoinMessage,
Expand Down Expand Up @@ -208,6 +216,7 @@ const ChatScreen = ({ navigation }: { navigation: ChatNavigationProp }) => {
<View style={darkMode ? styles.textAreaDark : styles.textArea}>
<TextField
value={message}
maxLength={charLimit}
onChangeText={setMessage}
style={styles.textField}
onSubmitEditing={sendMessage}
Expand Down
1 change: 1 addition & 0 deletions src/screens/settings/SettingScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const SettingScreen = (props: { button?: JSX.Element }) => {
{/* <Text>Hermes in use: {(global as any).HermesInternal ? 'true' : 'false'}</Text> */}
<Setting
multiline
maxLength={256}
name='Join message'
value={settings.joinMessage}
setValue={joinMessage => setSettings({ joinMessage })}
Expand Down

0 comments on commit da74e34

Please sign in to comment.