diff --git a/data/entity/entity.go b/data/entity/entity.go new file mode 100644 index 0000000..b47b241 --- /dev/null +++ b/data/entity/entity.go @@ -0,0 +1,250 @@ +// Code generated by gen_entity.go DO NOT EDIT. +// Package entity stores information about entities in Minecraft. +package entity + +// ID describes the numeric ID of an entity. +type ID uint32 + +// Entity describes information about a type of entity. +type Entity struct { + ID ID + InternalID uint32 + DisplayName string + Name string + Width float64 + Height float64 + Type string +} + +var ( + AreaEffectCloud = Entity{ID: 0, InternalID: 0, DisplayName: "Area Effect Cloud", Name: "area_effect_cloud", Width: 6, Height: 0.5, Type: "other"} + ArmorStand = Entity{ID: 1, InternalID: 1, DisplayName: "Armor Stand", Name: "armor_stand", Width: 0.5, Height: 1.975, Type: "living"} + Arrow = Entity{ID: 2, InternalID: 2, DisplayName: "Arrow", Name: "arrow", Width: 0.5, Height: 0.5, Type: "projectile"} + Axolotl = Entity{ID: 3, InternalID: 3, DisplayName: "Axolotl", Name: "axolotl", Width: 0.75, Height: 0.42, Type: "animal"} + Bat = Entity{ID: 4, InternalID: 4, DisplayName: "Bat", Name: "bat", Width: 0.5, Height: 0.9, Type: "ambient"} + Bee = Entity{ID: 5, InternalID: 5, DisplayName: "Bee", Name: "bee", Width: 0.7, Height: 0.6, Type: "animal"} + Blaze = Entity{ID: 6, InternalID: 6, DisplayName: "Blaze", Name: "blaze", Width: 0.6, Height: 1.8, Type: "hostile"} + Boat = Entity{ID: 7, InternalID: 7, DisplayName: "Boat", Name: "boat", Width: 1.375, Height: 0.5625, Type: "other"} + Cat = Entity{ID: 8, InternalID: 8, DisplayName: "Cat", Name: "cat", Width: 0.6, Height: 0.7, Type: "animal"} + CaveSpider = Entity{ID: 9, InternalID: 9, DisplayName: "Cave Spider", Name: "cave_spider", Width: 0.7, Height: 0.5, Type: "hostile"} + Chicken = Entity{ID: 10, InternalID: 10, DisplayName: "Chicken", Name: "chicken", Width: 0.4, Height: 0.7, Type: "animal"} + Cod = Entity{ID: 11, InternalID: 11, DisplayName: "Cod", Name: "cod", Width: 0.5, Height: 0.3, Type: "water_creature"} + Cow = Entity{ID: 12, InternalID: 12, DisplayName: "Cow", Name: "cow", Width: 0.9, Height: 1.4, Type: "animal"} + Creeper = Entity{ID: 13, InternalID: 13, DisplayName: "Creeper", Name: "creeper", Width: 0.6, Height: 1.7, Type: "hostile"} + Dolphin = Entity{ID: 14, InternalID: 14, DisplayName: "Dolphin", Name: "dolphin", Width: 0.9, Height: 0.6, Type: "water_creature"} + Donkey = Entity{ID: 15, InternalID: 15, DisplayName: "Donkey", Name: "donkey", Width: 1.3964844, Height: 1.5, Type: "animal"} + DragonFireball = Entity{ID: 16, InternalID: 16, DisplayName: "Dragon Fireball", Name: "dragon_fireball", Width: 1, Height: 1, Type: "projectile"} + Drowned = Entity{ID: 17, InternalID: 17, DisplayName: "Drowned", Name: "drowned", Width: 0.6, Height: 1.95, Type: "hostile"} + ElderGuardian = Entity{ID: 18, InternalID: 18, DisplayName: "Elder Guardian", Name: "elder_guardian", Width: 1.9975, Height: 1.9975, Type: "hostile"} + EndCrystal = Entity{ID: 19, InternalID: 19, DisplayName: "End Crystal", Name: "end_crystal", Width: 2, Height: 2, Type: "other"} + EnderDragon = Entity{ID: 20, InternalID: 20, DisplayName: "Ender Dragon", Name: "ender_dragon", Width: 16, Height: 8, Type: "mob"} + Enderman = Entity{ID: 21, InternalID: 21, DisplayName: "Enderman", Name: "enderman", Width: 0.6, Height: 2.9, Type: "hostile"} + Endermite = Entity{ID: 22, InternalID: 22, DisplayName: "Endermite", Name: "endermite", Width: 0.4, Height: 0.3, Type: "hostile"} + Evoker = Entity{ID: 23, InternalID: 23, DisplayName: "Evoker", Name: "evoker", Width: 0.6, Height: 1.95, Type: "hostile"} + EvokerFangs = Entity{ID: 24, InternalID: 24, DisplayName: "Evoker Fangs", Name: "evoker_fangs", Width: 0.5, Height: 0.8, Type: "other"} + ExperienceOrb = Entity{ID: 25, InternalID: 25, DisplayName: "Experience Orb", Name: "experience_orb", Width: 0.5, Height: 0.5, Type: "other"} + EyeOfEnder = Entity{ID: 26, InternalID: 26, DisplayName: "Eye of Ender", Name: "eye_of_ender", Width: 0.25, Height: 0.25, Type: "other"} + FallingBlock = Entity{ID: 27, InternalID: 27, DisplayName: "Falling Block", Name: "falling_block", Width: 0.98, Height: 0.98, Type: "other"} + FireworkRocket = Entity{ID: 28, InternalID: 28, DisplayName: "Firework Rocket", Name: "firework_rocket", Width: 0.25, Height: 0.25, Type: "projectile"} + Fox = Entity{ID: 29, InternalID: 29, DisplayName: "Fox", Name: "fox", Width: 0.6, Height: 0.7, Type: "animal"} + Ghast = Entity{ID: 30, InternalID: 30, DisplayName: "Ghast", Name: "ghast", Width: 4, Height: 4, Type: "mob"} + Giant = Entity{ID: 31, InternalID: 31, DisplayName: "Giant", Name: "giant", Width: 3.6, Height: 12, Type: "hostile"} + GlowItemFrame = Entity{ID: 32, InternalID: 32, DisplayName: "Glow Item Frame", Name: "glow_item_frame", Width: 0.5, Height: 0.5, Type: "other"} + GlowSquid = Entity{ID: 33, InternalID: 33, DisplayName: "Glow Squid", Name: "glow_squid", Width: 0.8, Height: 0.8, Type: "water_creature"} + Goat = Entity{ID: 34, InternalID: 34, DisplayName: "Goat", Name: "goat", Width: 0.9, Height: 1.3, Type: "animal"} + Guardian = Entity{ID: 35, InternalID: 35, DisplayName: "Guardian", Name: "guardian", Width: 0.85, Height: 0.85, Type: "hostile"} + Hoglin = Entity{ID: 36, InternalID: 36, DisplayName: "Hoglin", Name: "hoglin", Width: 1.3964844, Height: 1.4, Type: "animal"} + Horse = Entity{ID: 37, InternalID: 37, DisplayName: "Horse", Name: "horse", Width: 1.3964844, Height: 1.6, Type: "animal"} + Husk = Entity{ID: 38, InternalID: 38, DisplayName: "Husk", Name: "husk", Width: 0.6, Height: 1.95, Type: "hostile"} + Illusioner = Entity{ID: 39, InternalID: 39, DisplayName: "Illusioner", Name: "illusioner", Width: 0.6, Height: 1.95, Type: "hostile"} + IronGolem = Entity{ID: 40, InternalID: 40, DisplayName: "Iron Golem", Name: "iron_golem", Width: 1.4, Height: 2.7, Type: "mob"} + Item = Entity{ID: 41, InternalID: 41, DisplayName: "Item", Name: "item", Width: 0.25, Height: 0.25, Type: "other"} + ItemFrame = Entity{ID: 42, InternalID: 42, DisplayName: "Item Frame", Name: "item_frame", Width: 0.5, Height: 0.5, Type: "other"} + Fireball = Entity{ID: 43, InternalID: 43, DisplayName: "Fireball", Name: "fireball", Width: 1, Height: 1, Type: "projectile"} + LeashKnot = Entity{ID: 44, InternalID: 44, DisplayName: "Leash Knot", Name: "leash_knot", Width: 0.375, Height: 0.5, Type: "other"} + LightningBolt = Entity{ID: 45, InternalID: 45, DisplayName: "Lightning Bolt", Name: "lightning_bolt", Width: 0, Height: 0, Type: "other"} + Llama = Entity{ID: 46, InternalID: 46, DisplayName: "Llama", Name: "llama", Width: 0.9, Height: 1.87, Type: "animal"} + LlamaSpit = Entity{ID: 47, InternalID: 47, DisplayName: "Llama Spit", Name: "llama_spit", Width: 0.25, Height: 0.25, Type: "projectile"} + MagmaCube = Entity{ID: 48, InternalID: 48, DisplayName: "Magma Cube", Name: "magma_cube", Width: 2.04, Height: 2.04, Type: "mob"} + Marker = Entity{ID: 49, InternalID: 49, DisplayName: "Marker", Name: "marker", Width: 0, Height: 0, Type: "other"} + Minecart = Entity{ID: 50, InternalID: 50, DisplayName: "Minecart", Name: "minecart", Width: 0.98, Height: 0.7, Type: "other"} + ChestMinecart = Entity{ID: 51, InternalID: 51, DisplayName: "Minecart with Chest", Name: "chest_minecart", Width: 0.98, Height: 0.7, Type: "other"} + CommandBlockMinecart = Entity{ID: 52, InternalID: 52, DisplayName: "Minecart with Command Block", Name: "command_block_minecart", Width: 0.98, Height: 0.7, Type: "other"} + FurnaceMinecart = Entity{ID: 53, InternalID: 53, DisplayName: "Minecart with Furnace", Name: "furnace_minecart", Width: 0.98, Height: 0.7, Type: "other"} + HopperMinecart = Entity{ID: 54, InternalID: 54, DisplayName: "Minecart with Hopper", Name: "hopper_minecart", Width: 0.98, Height: 0.7, Type: "other"} + SpawnerMinecart = Entity{ID: 55, InternalID: 55, DisplayName: "Minecart with Spawner", Name: "spawner_minecart", Width: 0.98, Height: 0.7, Type: "other"} + TntMinecart = Entity{ID: 56, InternalID: 56, DisplayName: "Minecart with TNT", Name: "tnt_minecart", Width: 0.98, Height: 0.7, Type: "other"} + Mule = Entity{ID: 57, InternalID: 57, DisplayName: "Mule", Name: "mule", Width: 1.3964844, Height: 1.6, Type: "animal"} + Mooshroom = Entity{ID: 58, InternalID: 58, DisplayName: "Mooshroom", Name: "mooshroom", Width: 0.9, Height: 1.4, Type: "animal"} + Ocelot = Entity{ID: 59, InternalID: 59, DisplayName: "Ocelot", Name: "ocelot", Width: 0.6, Height: 0.7, Type: "animal"} + Painting = Entity{ID: 60, InternalID: 60, DisplayName: "Painting", Name: "painting", Width: 0.5, Height: 0.5, Type: "other"} + Panda = Entity{ID: 61, InternalID: 61, DisplayName: "Panda", Name: "panda", Width: 1.3, Height: 1.25, Type: "animal"} + Parrot = Entity{ID: 62, InternalID: 62, DisplayName: "Parrot", Name: "parrot", Width: 0.5, Height: 0.9, Type: "animal"} + Phantom = Entity{ID: 63, InternalID: 63, DisplayName: "Phantom", Name: "phantom", Width: 0.9, Height: 0.5, Type: "mob"} + Pig = Entity{ID: 64, InternalID: 64, DisplayName: "Pig", Name: "pig", Width: 0.9, Height: 0.9, Type: "animal"} + Piglin = Entity{ID: 65, InternalID: 65, DisplayName: "Piglin", Name: "piglin", Width: 0.6, Height: 1.95, Type: "hostile"} + PiglinBrute = Entity{ID: 66, InternalID: 66, DisplayName: "Piglin Brute", Name: "piglin_brute", Width: 0.6, Height: 1.95, Type: "hostile"} + Pillager = Entity{ID: 67, InternalID: 67, DisplayName: "Pillager", Name: "pillager", Width: 0.6, Height: 1.95, Type: "hostile"} + PolarBear = Entity{ID: 68, InternalID: 68, DisplayName: "Polar Bear", Name: "polar_bear", Width: 1.4, Height: 1.4, Type: "animal"} + Tnt = Entity{ID: 69, InternalID: 69, DisplayName: "Primed TNT", Name: "tnt", Width: 0.98, Height: 0.98, Type: "other"} + Pufferfish = Entity{ID: 70, InternalID: 70, DisplayName: "Pufferfish", Name: "pufferfish", Width: 0.7, Height: 0.7, Type: "water_creature"} + Rabbit = Entity{ID: 71, InternalID: 71, DisplayName: "Rabbit", Name: "rabbit", Width: 0.4, Height: 0.5, Type: "animal"} + Ravager = Entity{ID: 72, InternalID: 72, DisplayName: "Ravager", Name: "ravager", Width: 1.95, Height: 2.2, Type: "hostile"} + Salmon = Entity{ID: 73, InternalID: 73, DisplayName: "Salmon", Name: "salmon", Width: 0.7, Height: 0.4, Type: "water_creature"} + Sheep = Entity{ID: 74, InternalID: 74, DisplayName: "Sheep", Name: "sheep", Width: 0.9, Height: 1.3, Type: "animal"} + Shulker = Entity{ID: 75, InternalID: 75, DisplayName: "Shulker", Name: "shulker", Width: 1, Height: 1, Type: "mob"} + ShulkerBullet = Entity{ID: 76, InternalID: 76, DisplayName: "Shulker Bullet", Name: "shulker_bullet", Width: 0.3125, Height: 0.3125, Type: "projectile"} + Silverfish = Entity{ID: 77, InternalID: 77, DisplayName: "Silverfish", Name: "silverfish", Width: 0.4, Height: 0.3, Type: "hostile"} + Skeleton = Entity{ID: 78, InternalID: 78, DisplayName: "Skeleton", Name: "skeleton", Width: 0.6, Height: 1.99, Type: "hostile"} + SkeletonHorse = Entity{ID: 79, InternalID: 79, DisplayName: "Skeleton Horse", Name: "skeleton_horse", Width: 1.3964844, Height: 1.6, Type: "animal"} + Slime = Entity{ID: 80, InternalID: 80, DisplayName: "Slime", Name: "slime", Width: 2.04, Height: 2.04, Type: "mob"} + SmallFireball = Entity{ID: 81, InternalID: 81, DisplayName: "Small Fireball", Name: "small_fireball", Width: 0.3125, Height: 0.3125, Type: "projectile"} + SnowGolem = Entity{ID: 82, InternalID: 82, DisplayName: "Snow Golem", Name: "snow_golem", Width: 0.7, Height: 1.9, Type: "mob"} + Snowball = Entity{ID: 83, InternalID: 83, DisplayName: "Snowball", Name: "snowball", Width: 0.25, Height: 0.25, Type: "projectile"} + SpectralArrow = Entity{ID: 84, InternalID: 84, DisplayName: "Spectral Arrow", Name: "spectral_arrow", Width: 0.5, Height: 0.5, Type: "projectile"} + Spider = Entity{ID: 85, InternalID: 85, DisplayName: "Spider", Name: "spider", Width: 1.4, Height: 0.9, Type: "hostile"} + Squid = Entity{ID: 86, InternalID: 86, DisplayName: "Squid", Name: "squid", Width: 0.8, Height: 0.8, Type: "water_creature"} + Stray = Entity{ID: 87, InternalID: 87, DisplayName: "Stray", Name: "stray", Width: 0.6, Height: 1.99, Type: "hostile"} + Strider = Entity{ID: 88, InternalID: 88, DisplayName: "Strider", Name: "strider", Width: 0.9, Height: 1.7, Type: "animal"} + Egg = Entity{ID: 89, InternalID: 89, DisplayName: "Thrown Egg", Name: "egg", Width: 0.25, Height: 0.25, Type: "projectile"} + EnderPearl = Entity{ID: 90, InternalID: 90, DisplayName: "Thrown Ender Pearl", Name: "ender_pearl", Width: 0.25, Height: 0.25, Type: "projectile"} + ExperienceBottle = Entity{ID: 91, InternalID: 91, DisplayName: "Thrown Bottle o' Enchanting", Name: "experience_bottle", Width: 0.25, Height: 0.25, Type: "projectile"} + Potion = Entity{ID: 92, InternalID: 92, DisplayName: "Potion", Name: "potion", Width: 0.25, Height: 0.25, Type: "projectile"} + Trident = Entity{ID: 93, InternalID: 93, DisplayName: "Trident", Name: "trident", Width: 0.5, Height: 0.5, Type: "projectile"} + TraderLlama = Entity{ID: 94, InternalID: 94, DisplayName: "Trader Llama", Name: "trader_llama", Width: 0.9, Height: 1.87, Type: "animal"} + TropicalFish = Entity{ID: 95, InternalID: 95, DisplayName: "Tropical Fish", Name: "tropical_fish", Width: 0.5, Height: 0.4, Type: "water_creature"} + Turtle = Entity{ID: 96, InternalID: 96, DisplayName: "Turtle", Name: "turtle", Width: 1.2, Height: 0.4, Type: "animal"} + Vex = Entity{ID: 97, InternalID: 97, DisplayName: "Vex", Name: "vex", Width: 0.4, Height: 0.8, Type: "hostile"} + Villager = Entity{ID: 98, InternalID: 98, DisplayName: "Villager", Name: "villager", Width: 0.6, Height: 1.95, Type: "passive"} + Vindicator = Entity{ID: 99, InternalID: 99, DisplayName: "Vindicator", Name: "vindicator", Width: 0.6, Height: 1.95, Type: "hostile"} + WanderingTrader = Entity{ID: 100, InternalID: 100, DisplayName: "Wandering Trader", Name: "wandering_trader", Width: 0.6, Height: 1.95, Type: "passive"} + Witch = Entity{ID: 101, InternalID: 101, DisplayName: "Witch", Name: "witch", Width: 0.6, Height: 1.95, Type: "hostile"} + Wither = Entity{ID: 102, InternalID: 102, DisplayName: "Wither", Name: "wither", Width: 0.9, Height: 3.5, Type: "hostile"} + WitherSkeleton = Entity{ID: 103, InternalID: 103, DisplayName: "Wither Skeleton", Name: "wither_skeleton", Width: 0.7, Height: 2.4, Type: "hostile"} + WitherSkull = Entity{ID: 104, InternalID: 104, DisplayName: "Wither Skull", Name: "wither_skull", Width: 0.3125, Height: 0.3125, Type: "projectile"} + Wolf = Entity{ID: 105, InternalID: 105, DisplayName: "Wolf", Name: "wolf", Width: 0.6, Height: 0.85, Type: "animal"} + Zoglin = Entity{ID: 106, InternalID: 106, DisplayName: "Zoglin", Name: "zoglin", Width: 1.3964844, Height: 1.4, Type: "hostile"} + Zombie = Entity{ID: 107, InternalID: 107, DisplayName: "Zombie", Name: "zombie", Width: 0.6, Height: 1.95, Type: "hostile"} + ZombieHorse = Entity{ID: 108, InternalID: 108, DisplayName: "Zombie Horse", Name: "zombie_horse", Width: 1.3964844, Height: 1.6, Type: "animal"} + ZombieVillager = Entity{ID: 109, InternalID: 109, DisplayName: "Zombie Villager", Name: "zombie_villager", Width: 0.6, Height: 1.95, Type: "hostile"} + ZombifiedPiglin = Entity{ID: 110, InternalID: 110, DisplayName: "Zombified Piglin", Name: "zombified_piglin", Width: 0.6, Height: 1.95, Type: "hostile"} + Player = Entity{ID: 111, InternalID: 111, DisplayName: "Player", Name: "player", Width: 0.6, Height: 1.8, Type: "player"} + FishingBobber = Entity{ID: 112, InternalID: 112, DisplayName: "Fishing Bobber", Name: "fishing_bobber", Width: 0.25, Height: 0.25, Type: "projectile"} +) + +// ByID is an index of minecraft entities by their ID. +var ByID = map[ID]*Entity{ + 0: &AreaEffectCloud, + 1: &ArmorStand, + 2: &Arrow, + 3: &Axolotl, + 4: &Bat, + 5: &Bee, + 6: &Blaze, + 7: &Boat, + 8: &Cat, + 9: &CaveSpider, + 10: &Chicken, + 11: &Cod, + 12: &Cow, + 13: &Creeper, + 14: &Dolphin, + 15: &Donkey, + 16: &DragonFireball, + 17: &Drowned, + 18: &ElderGuardian, + 19: &EndCrystal, + 20: &EnderDragon, + 21: &Enderman, + 22: &Endermite, + 23: &Evoker, + 24: &EvokerFangs, + 25: &ExperienceOrb, + 26: &EyeOfEnder, + 27: &FallingBlock, + 28: &FireworkRocket, + 29: &Fox, + 30: &Ghast, + 31: &Giant, + 32: &GlowItemFrame, + 33: &GlowSquid, + 34: &Goat, + 35: &Guardian, + 36: &Hoglin, + 37: &Horse, + 38: &Husk, + 39: &Illusioner, + 40: &IronGolem, + 41: &Item, + 42: &ItemFrame, + 43: &Fireball, + 44: &LeashKnot, + 45: &LightningBolt, + 46: &Llama, + 47: &LlamaSpit, + 48: &MagmaCube, + 49: &Marker, + 50: &Minecart, + 51: &ChestMinecart, + 52: &CommandBlockMinecart, + 53: &FurnaceMinecart, + 54: &HopperMinecart, + 55: &SpawnerMinecart, + 56: &TntMinecart, + 57: &Mule, + 58: &Mooshroom, + 59: &Ocelot, + 60: &Painting, + 61: &Panda, + 62: &Parrot, + 63: &Phantom, + 64: &Pig, + 65: &Piglin, + 66: &PiglinBrute, + 67: &Pillager, + 68: &PolarBear, + 69: &Tnt, + 70: &Pufferfish, + 71: &Rabbit, + 72: &Ravager, + 73: &Salmon, + 74: &Sheep, + 75: &Shulker, + 76: &ShulkerBullet, + 77: &Silverfish, + 78: &Skeleton, + 79: &SkeletonHorse, + 80: &Slime, + 81: &SmallFireball, + 82: &SnowGolem, + 83: &Snowball, + 84: &SpectralArrow, + 85: &Spider, + 86: &Squid, + 87: &Stray, + 88: &Strider, + 89: &Egg, + 90: &EnderPearl, + 91: &ExperienceBottle, + 92: &Potion, + 93: &Trident, + 94: &TraderLlama, + 95: &TropicalFish, + 96: &Turtle, + 97: &Vex, + 98: &Villager, + 99: &Vindicator, + 100: &WanderingTrader, + 101: &Witch, + 102: &Wither, + 103: &WitherSkeleton, + 104: &WitherSkull, + 105: &Wolf, + 106: &Zoglin, + 107: &Zombie, + 108: &ZombieHorse, + 109: &ZombieVillager, + 110: &ZombifiedPiglin, + 111: &Player, + 112: &FishingBobber, +} diff --git a/data/entity/gen/gen_entity.go b/data/entity/gen/gen_entity.go new file mode 100644 index 0000000..6a6603d --- /dev/null +++ b/data/entity/gen/gen_entity.go @@ -0,0 +1,152 @@ +// gen_entity.go generates entity information. + +package main + +import ( + "encoding/json" + "fmt" + "go/ast" + "go/format" + "go/token" + "net/http" + "os" + "reflect" + "strconv" + + "github.com/iancoleman/strcase" +) + +const ( + infoURL = "https://raw.githubusercontent.com/PrismarineJS/minecraft-data/master/data/pc/1.17/entities.json" +) + +type Entity struct { + ID uint32 `json:"id"` + InternalID uint32 `json:"internalId"` + DisplayName string `json:"displayName"` + Name string `json:"name"` + + Width float64 `json:"width"` + Height float64 `json:"height"` + + Type string `json:"type"` +} + +func downloadInfo() ([]Entity, error) { + resp, err := http.Get(infoURL) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + var data []Entity + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, err + } + return data, nil +} + +func makeEntityDeclaration(entities []Entity) *ast.DeclStmt { + out := &ast.DeclStmt{Decl: &ast.GenDecl{Tok: token.VAR}} + + for _, e := range entities { + t := reflect.TypeOf(e) + fields := make([]ast.Expr, t.NumField()) + + for i := 0; i < t.NumField(); i++ { + ft := t.Field(i) + + var val ast.Expr + switch ft.Type.Kind() { + case reflect.Uint32, reflect.Int: + val = &ast.BasicLit{Kind: token.INT, Value: fmt.Sprint(reflect.ValueOf(e).Field(i))} + case reflect.Float64: + val = &ast.BasicLit{Kind: token.FLOAT, Value: fmt.Sprint(reflect.ValueOf(e).Field(i))} + case reflect.String: + val = &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(reflect.ValueOf(e).Field(i).String())} + case reflect.Bool: + val = &ast.BasicLit{Kind: token.IDENT, Value: fmt.Sprint(reflect.ValueOf(e).Field(i).Bool())} + + case reflect.Slice: + val = &ast.CompositeLit{ + Type: &ast.ArrayType{ + Elt: &ast.BasicLit{Kind: token.IDENT, Value: ft.Type.Elem().Name()}, + }, + } + v := reflect.ValueOf(e).Field(i) + switch ft.Type.Elem().Kind() { + case reflect.Uint32, reflect.Int: + for x := 0; x < v.Len(); x++ { + val.(*ast.CompositeLit).Elts = append(val.(*ast.CompositeLit).Elts, &ast.BasicLit{ + Kind: token.INT, + Value: fmt.Sprint(v.Index(x)), + }) + } + } + } + + fields[i] = &ast.KeyValueExpr{ + Key: &ast.Ident{Name: ft.Name}, + Value: val, + } + } + + out.Decl.(*ast.GenDecl).Specs = append(out.Decl.(*ast.GenDecl).Specs, &ast.ValueSpec{ + Names: []*ast.Ident{{Name: strcase.ToCamel(e.Name)}}, + Values: []ast.Expr{ + &ast.CompositeLit{ + Type: &ast.Ident{Name: reflect.TypeOf(e).Name()}, + Elts: fields, + }, + }, + }) + } + + return out +} + +//go:generate go run $GOFILE +//go:generate go fmt ../entity.go +func main() { + entities, err := downloadInfo() + if err != nil { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(1) + } + + f, err := os.Create("../entity.go") + if err != nil { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(1) + } + defer f.Close() + + fmt.Fprintln(f, `// Code generated by gen_entity.go DO NOT EDIT. +// Package entity stores information about entities in Minecraft. +package entity +// ID describes the numeric ID of an entity. +type ID uint32 + +// Entity describes information about a type of entity. +type Entity struct { + ID ID + InternalID uint32 + DisplayName string + Name string + Width float64 + Height float64 + Type string +}`) + format.Node(f, token.NewFileSet(), makeEntityDeclaration(entities)) + + fmt.Fprintln(f) + fmt.Fprintln(f) + fmt.Fprintln(f, "// ByID is an index of minecraft entities by their ID.") + fmt.Fprintln(f, "var ByID = map[ID]*Entity{") + for _, e := range entities { + fmt.Fprintf(f, " %d: &%s,\n", e.ID, strcase.ToCamel(e.Name)) + } + fmt.Fprintln(f, "}") + + fmt.Fprintln(f) +}