packet_handling.clj

Branches > client-handling-rewrite > cf28fd5 > files / src / net / bowmap / packet_handling.clj

(ns net.bowmap.packet-handling
  (:require
   [net.bowmap.client.state :as state]
   [net.bowmap.packet :as pk]
   [net.bowmap.packet.client :as cp]
   [net.bowmap.packet.registry :as registry]
   [net.bowmap.packet.server :as sp]))

(defmulti handle-packet
  (fn [pk client]
    (::pk/id pk)))

(defn response
  "Returns a map of a client, and any other keys a packet handler may use.
   Client may be unchanged, or adorned with any information, such as a new state."
  [client pk]
  (let [response (handle-packet pk client)]
    (if (:client response)
      response
      (assoc response :client client))))

(defmethod handle-packet :default [pk client]
  (prn "Unhandled packet:" pk))

(defmethod handle-packet ::sp/handshaking [pk client]
  {:client (assoc client ::state/state (:intent pk))})

(defmethod handle-packet ::sp/status-request [pk client]
  {:response [{::pk/id ::cp/status-response
               :response {:version {:name "1.21.11"
                                    :protocol 774}
                          :players {:max 1000
                                    :online 0}
                          :description {:text "Bowmap!"}
                          :enforcesSecureChat false}}]})

(defmethod handle-packet ::sp/ping-request [pk client]
  {:response [{::pk/id ::cp/pong-response
               :timestamp (:timestamp pk)}]})

(defmethod handle-packet ::sp/login-start [pk client]
  {:response [{::pk/id ::cp/login-success
               :uuid (:uuid pk)
               :username (:username pk)
               :properties []}]})

(defmethod handle-packet ::sp/login-acknowledged [pk client]
  {:client (assoc client ::state/state ::state/config)
   :response [{::pk/id ::cp/known-packs
               :known-packs [{:namespace "minecraft" :id "core" :version "1.12.11"}]}]})

(defmethod handle-packet ::sp/known-packs [pk client]
  {:response (conj (vec registry/registry-packets)
                   {::pk/id ::cp/finish-config})})

(defmethod handle-packet ::sp/finish-config [pk client]
  {:client (assoc client ::state/state ::state/play)
   :keepalive true
   :response [{::pk/id ::cp/login
              :entity-id 0 ;; TODO magic zero :p
              :hardcore? false
              :dimension-names ["world"]
              :max-players 1000
              :view-distance 32
              :simulation-distance 32
              :reduced-debug-info? false
              :enable-respawn-screen? false
              :do-limited-crafting? false
              :dimension-type-id 0
              :dimension-name "world"
              :hashed-seed 203894
              :gamemode :creative
              :previous-gamemode :unknown
              :debug-world? false
              :superflat-world? true
              :has-death-location? false
              :portal-cooldown 10
              :sea-level 60
              :enforce-secure-chat? true}
              {::pk/id ::cp/game-event
               :event :wait-for-chunks}]})