c9bd266

Bowmap · bowmap.git · Tickets

Branches > main > c9bd266 > files / src

Build id and schema maps in serverbound and clientbound packet namespaces

Grahp, 2026-09-14

Commit: c9bd266703243ec5f93321eb31fc34795b1b3575
Parent: 36cd69a

3 changed files:

src/net/bowmap/server/protocol/clientbound_packets.clj modified (view file)

@@ -179,3 +179,6 @@     ::waypoint nil     ::clear-dialog nil     ::show-dialog-play nil]})++(def phase->id->numeric-id (update-vals clientbound-packets #(zipmap (take-nth 2 %) (range))))+(def phase->id->schema (update-vals clientbound-packets #(some-vals (apply hash-map %))))

src/net/bowmap/server/protocol/codec.clj modified (view file)

@@ -1,30 +1,20 @@ (ns net.bowmap.server.protocol.codec   (:require-   [net.bowmap.server.protocol.clientbound-packets :refer [clientbound-packets]]+   [net.bowmap.server.protocol.clientbound-packets :as clientbound]    [net.bowmap.server.protocol.frame :refer [frame]]    [net.bowmap.server.protocol.packet :as packet]    [net.bowmap.server.protocol.phase :as phase]    [net.bowmap.server.protocol.readers :as readers]-   [net.bowmap.server.protocol.serverbound-packets :refer [serverbound-packets]]+   [net.bowmap.server.protocol.serverbound-packets :as serverbound]    [net.bowmap.server.protocol.writers :as writers])) -(defn- find-indexed-  "Takes a seq of pairs and returns both the index of the first pair-   'keyed' by the given id and its 'value' in linear time.-   E.g. (find-indexed [:a [] :b {} :c #{}] :b) -> [1 {}]"-  [id pairs]-  (some (fn [[i [k v]]]-          (when (= id k)-            [i v]))-        (map-indexed vector (partition 2 pairs))))- (defn encode-packet   "Encodes a packet into a frame.    Throws when the packet cannot be encoded."   [{::phase/keys! [phase]} {::packet/keys! [id] :as packet}]-  (let [schemas (clientbound-packets phase)-        [numeric-id schema] (find-indexed id schemas)-        payload-writer (if (some? schema)+  (let [numeric-id (req! (clientbound/phase->id->numeric-id phase) id)+        schema (req! (clientbound/phase->id->schema phase) id)+        payload-writer (if (and numeric-id schema)                          (writers/to-writer schema)                          (throw (ex-info "No such packet with that id!" {:phase phase :numeric-id numeric-id})))]     (-> (frame)@@ -36,8 +26,8 @@    Can throw if the frame does not contain a valid packet."   [{:keys! [::phase/phase]} frame]   (let [numeric-id (readers/->varint frame)-        schemas (serverbound-packets phase)-        [id schema] (nth (partition 2 schemas) numeric-id)+        id (req! (serverbound/phase->numeric-id->id phase) numeric-id)+        schema (req! (serverbound/phase->id->schemas phase) id)         payload-reader (readers/to-reader schema)]     (assoc (payload-reader frame)            ::packet/id id)))

src/net/bowmap/server/protocol/serverbound_packets.clj modified (view file)

@@ -101,3 +101,6 @@     ::use-item-on []     ::use-item []     ::custom-click-action []]})++(def phase->numeric-id->id (update-vals serverbound-packets #(zipmap (range) (take-nth 2 %))))+(def phase->id->schemas (update-vals serverbound-packets #(apply hash-map %)))