Commits

Steve Losh committed aa67f93

Handle namedentityspawn packets.

  • Participants
  • Parent commits cd03086

Comments (0)

Files changed (3)

src/clojurecraft/core.clj

     ; We need to log in to find out our bot's entity ID, so we delay creation of the
     ; player until then.
     (let [player-id (:eid (login bot username))
-          player (ref (Entity. player-id nil false 0.0))
+          player (ref (Entity. player-id nil username nil false 0.0))
           bot (assoc bot :player player)]
 
       ; Theoretically another connected bot could fill in the player's entity entry

src/clojurecraft/data.clj

 ; eid -> Integer
 ; loc -> Location
 ;
+; name -> String
+;   The name of the player, or nil if this entity is not a player.
+;
+; holding -> Keyword (Item Type)
+;   The type of item this entity is holding, or nil if this entity is not holding
+;   anything.
+;
 ; despawned -> boolean
 ;   True if the entity has been despawned.
 ;
 ;   something has changed, since otherwise there would be no way to tell
 ;   (despawning would simply remove the object from the entity list
 ;   without modifying it).
-(defrecord Entity [eid loc despawned velocity])
+(defrecord Entity [eid loc name holding despawned velocity])
 
 ; Block
 ;

src/clojurecraft/in.clj

 (def FULL-CHUNK (* 16 16 128))
 (def BLANK-CHUNK-ARRAY (byte-array FULL-CHUNK))
 
+; Convenience Functions ------------------------------------------------------------
+(defn- blank-entity []
+  (Entity. nil
+           (Location. nil nil nil nil nil nil nil)
+           nil nil false 0.0))
+
+
 ; Reading Data ---------------------------------------------------------------------
 (defn- -read-byte-bare [conn]
   (io!
          :action (-read-byte conn)))
 
 (defn- read-packet-namedentityspawn [bot conn]
-  (assoc {}
-         :eid (-read-int conn)
-         :playername (-read-string-ucs2 conn)
-         :x (-read-int conn)
-         :y (-read-int conn)
-         :z (-read-int conn)
-         :rotation (-read-byte conn)
-         :pitch (-read-byte conn)
-         :currentitem (-read-short conn)))
+  (let [payload (assoc {}
+                       :eid (-read-int conn)
+                       :playername (-read-string-ucs2 conn)
+                       :x (-read-int conn)
+                       :y (-read-int conn)
+                       :z (-read-int conn)
+                       :rotation (-read-byte conn)
+                       :pitch (-read-byte conn)
+                       :currentitem (-read-short conn))
+        entity-data {:eid (:eid payload)
+                     :name (:playername payload)
+                     :holding (item-types (:currentitem payload))
+                     :despawned false}
+        location-data {:x (:x payload)
+                       :y (:y payload)
+                       :z (:z payload)}]
+    (dosync
+      (let [eid (:eid payload)
+            entities (:entities (:world bot))
+            entity (or (let [e (@entities eid)] (when e @e)) (blank-entity))
+            new-loc (merge (:loc entity) location-data)
+            new-entity-data (assoc entity-data :loc new-loc)]
+        ; I'm not sure this is right.  We should probably be altering the entity ref
+        ; if it already exists.
+        (alter entities assoc eid (ref (merge entity new-entity-data)))))
+    payload))
 
 (defn- read-packet-pickupspawn [bot conn]
   (assoc {}
             entities (:entities (:world bot))
             entity (@entities eid)]
         (when-not entity
-          (alter entities assoc eid (Entity. eid nil false 0.0)))))
+          (alter entities assoc eid (ref (Entity. eid nil nil nil false 0.0))))))
     payload))
 
 (defn- read-packet-entityrelativemove [bot conn]