Skip to content

Commit

Permalink
Add zustand and create an unused sessionStore.
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Jul 23, 2022
1 parent 18b0681 commit 5322c8a
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Publishing to the Google Play Store is eventually planned for the betas and fina

**Note:** iOS support is currently untested and may have bugs and/or performance issues, since I don't have a Mac to properly support iOS as a target platform. Contributions to improve iOS support are welcome though!

Currently, you must build EnderChat yourself from source. However, IPA files are planned to be uploaded in the future for each EnderChat release (similar to Android), which can then be sideloaded using techniques like AltStore.
IPAs are available to download for each EnderChat release [here.](https://github.com/retrixe/EnderChat/releases) These can be sideloaded on your iPhone using techniques like AltStore.

There are no plans to publish EnderChat to the iOS App Store for now. ***While the license allows redistribution, I request people not to abuse this privilege and publish EnderChat to the App Store without my permission (for now).***

Expand Down
34 changes: 33 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"react-native-vector-icons": "^9.2.0",
"react-native-webview": "^11.18.2",
"semaphore-async-await": "^1.5.1",
"stream-browserify": "^3.0.0"
"stream-browserify": "^3.0.0",
"zustand": "^4.0.0-rc.3"
},
"devDependencies": {
"@babel/core": "^7.13.10",
Expand Down
1 change: 0 additions & 1 deletion src/context/accountsContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Consider using Recoil instead of Context?
import React from 'react'

export interface Account {
Expand Down
1 change: 0 additions & 1 deletion src/context/connectionContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Consider using Recoil instead of Context?
import React from 'react'
import { MinecraftChat } from '../minecraft/chatToJsx'
import { ServerConnection } from '../minecraft/connection'
Expand Down
1 change: 0 additions & 1 deletion src/context/serversContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Consider using Recoil instead of Context?
import React from 'react'
import { protocolMap } from '../minecraft/utils'

Expand Down
24 changes: 24 additions & 0 deletions src/context/sessionStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import create from 'zustand'
import { Certificate } from '../minecraft/api/mojang'

export interface Session {
certificate?: Certificate
accessToken: string
}

export interface Sessions {
[uuid: string]: Session
}

export type SetSession = (uuid: string, session: Session) => void

const useSessionStore = create<{
sessions: Sessions
setSession: SetSession
}>(set => ({
sessions: {},
setSession: (uuid: string, session: Session) =>
set(state => ({ sessions: { ...state.sessions, [uuid]: session } }))
}))

export default useSessionStore
1 change: 0 additions & 1 deletion src/context/settingsContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Consider using Recoil instead of Context?
import React from 'react'

export interface Settings {
Expand Down
31 changes: 17 additions & 14 deletions src/screens/chat/packetHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import { ServerConnection } from '../../minecraft/connection'
import { concatPacketData, Packet } from '../../minecraft/packet'
import { protocolMap, readVarInt, writeVarInt } from '../../minecraft/utils'

const enderChatPrefix = '\u00A74[\u00A7cEnderChat\u00A74] \u00A7c'
const parseMessageErr = 'An error occurred when parsing chat.'
const inventoryCloseErr = 'An error occurred when closing an inventory window.'
const respawnErr = 'An error occurred when trying to respawn after death.'
const deathRespawnMessage = enderChatPrefix + 'You died! Respawning...'
const sendMessageErr = 'Failed to send message to server!'
const healthMessage =
export const enderChatPrefix = '\u00A74[\u00A7cEnderChat\u00A74] \u00A7c'
export const parseMessageError = 'An error occurred when parsing chat.'
export const inventoryCloseError =
'An error occurred when closing an inventory window.'
export const respawnError =
'An error occurred when trying to respawn after death.'
export const deathRespawnMessage = enderChatPrefix + 'You died! Respawning...'
export const sendMessageError = 'Failed to send message to server!'
export const healthMessage =
enderChatPrefix + "You're losing health! \u00A7b%prev \u00A7f-> \u00A7c%new"

export default (
export const packetHandler =
(
healthRef: React.MutableRefObject<number | null>,
loggedInRef: React.MutableRefObject<boolean>,
setLoggedIn: React.Dispatch<React.SetStateAction<boolean>>,
Expand All @@ -38,12 +41,12 @@ export default (
0x03,
concatPacketData([joinMessage.substring(0, charLimit)])
)
.catch(handleError(addMessage, sendMessageErr))
.catch(handleError(addMessage, sendMessageError))
}
if (sendSpawnCommand) {
connection
.writePacket(0x03, concatPacketData(['/spawn']))
.catch(handleError(addMessage, sendMessageErr))
.catch(handleError(addMessage, sendMessageError))
}
}

Expand All @@ -60,7 +63,7 @@ export default (
addMessage(parseValidJson(chatJson))
}
} catch (e) {
handleError(addMessage, parseMessageErr)(e)
handleError(addMessage, parseMessageError)(e)
}
} else if (
packet.id === 0x30 /* Player Chat Message (clientbound) */ &&
Expand All @@ -82,7 +85,7 @@ export default (
addMessage(parseValidJson(chatJson))
}
} catch (e) {
handleError(addMessage, parseMessageErr)(e)
handleError(addMessage, parseMessageError)(e)
}
} else if (packet.id === 0x2e /* Open Window */) {
// Just close the window.
Expand All @@ -91,7 +94,7 @@ export default (
buf.writeUInt8(windowId)
connection // Close Window (serverbound)
.writePacket(0x09, buf)
.catch(handleError(addMessage, inventoryCloseErr))
.catch(handleError(addMessage, inventoryCloseError))
} else if (packet.id === 0x35 /* Death Combat Event */) {
const [, playerIdLen] = readVarInt(packet.data)
const offset = playerIdLen + 4 // Entity ID
Expand All @@ -106,7 +109,7 @@ export default (
// LOW-TODO: Should this be manual, or a dialog, like MC?
connection // Client Status
.writePacket(0x04, writeVarInt(0))
.catch(handleError(addMessage, respawnErr))
.catch(handleError(addMessage, respawnError))
} else if (packet.id === 0x52 /* Update Health */) {
const newHealth = packet.data.readFloatBE(0)
if (healthRef.current != null && healthRef.current > newHealth) {
Expand Down
11 changes: 9 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7129,7 +7129,7 @@
"react-shallow-renderer" "^16.13.1"
"scheduler" "^0.21.0"

"react@*", "react@^16.0.0 || ^17.0.0", "react@^16.8.0 || ^17.0.0 || ^18.0.0", "react@^17.0.0", "react@^18.0.0", "react@16 || 17", "[email protected]":
"react@*", "react@^16.0.0 || ^17.0.0", "react@^16.8.0 || ^17.0.0 || ^18.0.0", "react@^17.0.0", "react@^18.0.0", "react@>=16.8", "react@16 || 17", "[email protected]":
"integrity" "sha512-x+VL6wbT4JRVPm7EGxXhZ8w8LTROaxPXOqhlGyVSrv0sB1jkyFGgXxJ8LVoPRLvPR6/CIZGFmfzqUa2NYeMr2A=="
"resolved" "https://registry.npmjs.org/react/-/react-18.0.0.tgz"
"version" "18.0.0"
Expand Down Expand Up @@ -8365,7 +8365,7 @@
"resolved" "https://registry.npmjs.org/urix/-/urix-0.1.0.tgz"
"version" "0.1.0"

"use-sync-external-store@^1.0.0":
"use-sync-external-store@^1.0.0", "[email protected]":
"integrity" "sha512-eEgnFxGQ1Ife9bzYs6VLi8/4X6CObHMw9Qr9tPY43iKwsPw8xE8+EFsf/2cFZ5S3esXgpWgtSCtLNS41F+sKPA=="
"resolved" "https://registry.npmjs.org/use-sync-external-store/-/use-sync-external-store-1.2.0.tgz"
"version" "1.2.0"
Expand Down Expand Up @@ -8719,3 +8719,10 @@
"string-width" "^4.2.0"
"y18n" "^5.0.5"
"yargs-parser" "^20.2.2"

"zustand@^4.0.0-rc.3":
"integrity" "sha512-vc7TKQJC6T8lx5EupnuUTWXahuRLYjrK9oWLvY+u65ZkLDjocgxDGMrl5VBuxyjXCXyJ3kquN3l/Y2yUj5ZDEg=="
"resolved" "https://registry.npmjs.org/zustand/-/zustand-4.0.0-rc.3.tgz"
"version" "4.0.0-rc.3"
dependencies:
"use-sync-external-store" "1.2.0"

0 comments on commit 5322c8a

Please sign in to comment.