Skip to content

Instantly share code, notes, and snippets.

@kvanbere
Created October 6, 2012 07:44
Show Gist options
  • Save kvanbere/3844340 to your computer and use it in GitHub Desktop.
Save kvanbere/3844340 to your computer and use it in GitHub Desktop.
Converting the Minecraft Coalition data found @ http://mc.kev009.com into Haskell
--
-- Things to do:
-- add missing types, find an acceptable way to render an invalid type (null?)
--
-- Need the Data.Text library later, so make alias
import qualified Data.ByteString as BS
import Data.Serialize
import Data.Word
-- | Helper functions courtesy of IRC
bWord16 :: Word16 -> BS.ByteString
bWord16 = encode
-- |
bUcs2 :: T.Text -> BS.ByteString
bUcs2 t = BS.append (bWord16 $ fromIntegral (T.length t)) (encodeUtf16BE t)
-- |
putUcs2 :: Putter T.Text
putUcs2 = putByteString . bUcs2
-- |
getUcs2 :: Get T.Text
getUcs2 = do
len <- getWord16be
bytes <- getByteString (fromIntegral len * 2)
return $ decodeUtf16BE bytes
-- MouseButton
data MouseButton = RightButton | LeftButton
deriving (Enum, Eq, Show)
instance Serialize MouseButton where
put x = putWord8 $ case x of
RightButton -> 0x00
LeftButton -> 0x01
get = do
b <- getWord8
return $ case of
0x00 -> RightButton
_ -> LeftButton
-- LevelType
data LevelType = Default | Flat | Large
deriving (Enum, Eq, Show)
instance Serialize LevelType where
put x = putUcs2 $ case x of
Default -> "default"
Flat -> "flat"
Large -> "largeBiomes"
get = do
s <- getUcs2
return $ case of
"flat" -> Flat
"largeBiomes" -> Large
_ -> Default
-- GameMode
data GameMode = Survival | Creative | Adventure | Hardcore
deriving (Enum, Eq, Show)
instance Serialize GameMode where
put x = putWord8 $ case x of
Survival -> 0x00
Creative -> 0x01
Adventure -> 0x02
Hardcore -> 0x04
get = do
b <- getWord8
return $ case b of
0x01 -> Creative
0x02 -> Adventure
0x04 -> Hardcore
_ -> Survival
-- Dimension
data Dimension = Overworld | Nether | End
deriving (Enum, Eq, Show)
instance Serialize Dimension where
put x = putWord8 $ case x of
Overworld -> 0x00
End -> 0x01
Nether -> 0xff
get = do
b <- getWord8
return $ case b of
0x01 -> End
0xff -> Nether
_ -> Overworld
-- Difficulty
data Difficulty = Peaceful | Easy | Medium | Hard
deriving (Enum, Eq, Show, Ord)
instance Serialize Difficulty where
put x = putWord8 $ case x of
Peaceful -> 0x00
Easy -> 0x01
Medium -> 0x02
Hard -> 0x03
get = do
b <- getWord8
return $ case b of
0x01 -> Easy
0x02 -> Medium
0x03 -> Hard
_ -> Peaceful
-- Airborne
data Airborne = Grounded | Aloft
deriving (Enum, Eq, Show)
instance Serialize Airborne where
put x = putWord8 $ case x of
Grounded -> 0x00
Aloft -> 0x01
get = do
d <- getWord8
return $ case d of
0x00 -> Grounded
_ -> Aloft
-- PlayerAction
data PlayerAction = StartDigging | CancelDigging | FinishedDigging | CheckBlock | DropItem | ShootArrow
deriving (Enum, Eq, SHow)
instance Serialize PlayerAction where
put x = putWord8 $ case x of
StartDigging -> 0x00
CancelDigging -> 0x01
FinishedDigging -> 0x02
CheckBlock -> 0x03
DropItem -> 0x04
ShootArrow -> 0x05
get = do
b <- getWord8
return $ case b of
0x00 -> StartDigging
0x01 -> CancelDigging
0x03 -> CheckBlock
0x04 -> DropItem
0x05 -> ShootArrow
_ -> FinishedDigging
-- PlayerAnimation
data PlayerAnimation = None | SwingArm | Damaged | LeaveBed | Eating | Crouch | Uncrouch -- todo fix conflicting names, ie: nouns, verbs
deriving (Enum, Eq, Show)
instance Serialize PlayerAnimation where
put x = putWord8 $ case x of
SwingArm -> 0x01
Damaged -> 0x02
LeaveBed -> 003
Eating -> 0x05
Crouch -> 0x68
Uncrouch -> 0x69
get = do
b <- getWord8
return $ case b of
0x01 -> SwingArm
0x02 -> Damaged
0x03 -> LeaveBed
0x05 -> Eating
0x68 -> Crouch
0x69 -> Uncrouch
_ -> None
-- EntityAction
data EntityAction = EntityActionNull | Crouch | Uncrouch | LeaveBed | StartSprint | StopSprint
deriving (Enum, Eq, Show)
instance Serialize EntityAction where
put x = putWord8 $ case x of
Crouch -> 0x01
Uncrouch -> 0x02
LeaveBed -> 0x03
StartSprint -> 0x04
StopSprint -> 0x05
get = do
b <- getWord8
return $ case b of
0x01 -> Crouch
0x02 -> Uncrouch
0x03 -> LeaveBed
0x04 -> StartSprint
0x05 -> StopSprint
_ -> Uncrouch
-- EntityStatus
data EntityStatus = EntityHurt | EntityDead | WolfTaming | WolfTamed | WolfShake | EntityEat | SheepEat
deriving (Enum, Eq, Show)
instance Serialize EntityStatus where
put x = putWord8 $ case x of
EntityHurt -> 0x02
EntityDead -> 0x03
WolfTaming -> 0x06
WolfTamed -> 0x07
WolfShake -> 0x08
EntityEat -> 0x09
SheepEat -> 0x0a
get = do
b <- getWord8
return $ case b of
0x02 -> EntityHurt
0x03 -> EntityDead
0x06 -> WolfTaming
0x07 -> WolfTamed
0x08 -> WolfShake
0x09 -> EntityEat
0x0a -> SheepEat
_ -> EntityHurt -- todo, change
-- EntityType
data EntityType = EntityTypeNull | Creeper | Skeleton | Spider | GiantZombie | Zombie | Slime | Ghast | ZombiePigman | Enderman | CaveSpider | Silverfish | Blaze | MagmaCube | EnderDragon | Pig | Sheep | Cow | Chicken | Squid | Wolf | Mooshroom | Snowman | Ocelot | IronGolem | Villager
deriving (Enum, Eq, Show)
instance Serialize EntityType where
put x = putWord8 $ case x of
Creeper -> 0x32
Skeleton -> 0x33
Spider -> 0x34
GiantZombie -> 0x35
Zombie -> 0x36
Slime -> 0x37
Ghast -> 0x38
ZombiePigman -> 0x39
Enderman -> 0x3a
CaveSpider -> 0x3b
Silverfish -> 0x3c
Blaze -> 0x3d
MagmaCube -> 0x3e
EnderDragon -> 0x3f
Pig -> 0x5a
Sheep -> 0x5b
Cow -> 0x5c
Chicken -> 0x5d
Squid -> 0x5e
Wolf -> 0x5f
Mooshroom -> 0x60
Snowman -> 0x61
Ocelot -> 0x62
IronGolem -> 0x63
Villager -> 0x78
get = do
b <- getWord8
return $ case b of
0x32 -> Creeper
0x33 -> Skeleton
0x34 -> Spider
0x35 -> GiantZombie
0x36 -> Zombie
0x37 -> Slime
0x38 -> Ghast
0x39 -> ZombiePigman
0x3a -> Enderman
0x3b -> CaveSpider
0x3c -> Silverfish
0x3d -> Blaze
0x3e -> MagmaCube
0x3f -> EnderDragon
0x5a -> Pig
0x5b -> Sheep
0x5c -> Cow
0x5d -> Chicken
0x5e -> Squid
0x5f -> Wolf
0x60 -> Mooshroom
0x61 -> Snowman
0x62 -> Ocelot
0x63 -> IronGolem
0x78 -> Villager
_ -> EntityTypeNull -- todo, get better default values
-- SoundEffectType
-- todo
-- ParticleEffectType
-- todo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment