the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 329 lines 15 kB view raw
1#include "stdafx.h" 2#include "Class.h" 3#include "Painting.h" 4#include "System.h" 5#include "Entity.h" 6#include "WitherBoss.h" 7#include "net.minecraft.world.entity.ambient.h" 8#include "net.minecraft.world.entity.animal.h" 9#include "net.minecraft.world.entity.item.h" 10#include "net.minecraft.world.entity.monster.h" 11#include "net.minecraft.world.entity.projectile.h" 12#include "net.minecraft.world.entity.boss.enderdragon.h" 13#include "net.minecraft.world.entity.npc.h" 14#include "net.minecraft.world.entity.h" 15#include "net.minecraft.world.level.h" 16#include "com.mojang.nbt.h" 17#include "EntityIO.h" 18 19unordered_map<wstring, entityCreateFn> *EntityIO::idCreateMap = new unordered_map<wstring, entityCreateFn>; 20unordered_map<eINSTANCEOF, wstring, eINSTANCEOFKeyHash, eINSTANCEOFKeyEq> *EntityIO::classIdMap = new unordered_map<eINSTANCEOF, wstring, eINSTANCEOFKeyHash, eINSTANCEOFKeyEq>; 21unordered_map<int, entityCreateFn> *EntityIO::numCreateMap = new unordered_map<int, entityCreateFn>; 22unordered_map<int, eINSTANCEOF> *EntityIO::numClassMap = new unordered_map<int, eINSTANCEOF>; 23unordered_map<eINSTANCEOF, int, eINSTANCEOFKeyHash, eINSTANCEOFKeyEq> *EntityIO::classNumMap = new unordered_map<eINSTANCEOF, int, eINSTANCEOFKeyHash, eINSTANCEOFKeyEq>; 24unordered_map<wstring, int> *EntityIO::idNumMap = new unordered_map<wstring, int>; 25unordered_map<int, EntityIO::SpawnableMobInfo *> EntityIO::idsSpawnableInCreative; 26 27void EntityIO::setId(entityCreateFn createFn, eINSTANCEOF clas, const wstring &id, int idNum) 28{ 29 idCreateMap->insert( unordered_map<wstring, entityCreateFn>::value_type(id, createFn) ); 30 classIdMap->insert( unordered_map<eINSTANCEOF, wstring, eINSTANCEOFKeyHash, eINSTANCEOFKeyEq>::value_type(clas,id ) ); 31 numCreateMap->insert( unordered_map<int, entityCreateFn>::value_type(idNum, createFn) ); 32 numClassMap->insert( unordered_map<int, eINSTANCEOF>::value_type(idNum, clas) ); 33 classNumMap->insert( unordered_map<eINSTANCEOF, int, eINSTANCEOFKeyHash, eINSTANCEOFKeyEq>::value_type(clas, idNum) ); 34 idNumMap->insert( unordered_map<wstring, int>::value_type(id, idNum) ); 35} 36 37void EntityIO::setId(entityCreateFn createFn, eINSTANCEOF clas, const wstring &id, int idNum, eMinecraftColour color1, eMinecraftColour color2, int nameId) 38{ 39 setId(createFn, clas, id, idNum); 40 41 idsSpawnableInCreative.insert( unordered_map<int, SpawnableMobInfo *>::value_type( idNum, new SpawnableMobInfo(idNum, color1, color2, nameId) ) ); 42} 43 44void EntityIO::staticCtor() 45{ 46 setId(ItemEntity::create, eTYPE_ITEMENTITY, L"Item", 1); 47 setId(ExperienceOrb::create, eTYPE_EXPERIENCEORB, L"XPOrb", 2); 48 49 setId(LeashFenceKnotEntity::create, eTYPE_LEASHFENCEKNOT, L"LeashKnot", 8); 50 setId(Painting::create, eTYPE_PAINTING, L"Painting", 9); 51 setId(Arrow::create, eTYPE_ARROW, L"Arrow", 10); 52 setId(Snowball::create, eTYPE_SNOWBALL, L"Snowball", 11); 53 setId(LargeFireball::create, eTYPE_FIREBALL, L"Fireball", 12); 54 setId(SmallFireball::create, eTYPE_SMALL_FIREBALL, L"SmallFireball", 13); 55 setId(ThrownEnderpearl::create, eTYPE_THROWNENDERPEARL, L"ThrownEnderpearl", 14); 56 setId(EyeOfEnderSignal::create, eTYPE_EYEOFENDERSIGNAL, L"EyeOfEnderSignal", 15); 57 setId(ThrownPotion::create, eTYPE_THROWNPOTION, L"ThrownPotion", 16); 58 setId(ThrownExpBottle::create, eTYPE_THROWNEXPBOTTLE, L"ThrownExpBottle", 17); 59 setId(ItemFrame::create, eTYPE_ITEM_FRAME, L"ItemFrame", 18); 60 setId(WitherSkull::create, eTYPE_WITHER_SKULL, L"WitherSkull", 19); 61 62 setId(PrimedTnt::create, eTYPE_PRIMEDTNT, L"PrimedTnt", 20); 63 setId(FallingTile::create, eTYPE_FALLINGTILE, L"FallingSand", 21); 64 65 setId(FireworksRocketEntity::create, eTYPE_FIREWORKS_ROCKET, L"FireworksRocketEntity", 22); 66 67 setId(Boat::create, eTYPE_BOAT, L"Boat", 41); 68 setId(MinecartRideable::create, eTYPE_MINECART_RIDEABLE, L"MinecartRideable", 42); 69 setId(MinecartChest::create, eTYPE_MINECART_CHEST, L"MinecartChest", 43); 70 setId(MinecartFurnace::create, eTYPE_MINECART_FURNACE, L"MinecartFurnace", 44); 71 setId(MinecartTNT::create, eTYPE_MINECART_TNT, L"MinecartTNT", 45); 72 setId(MinecartHopper::create, eTYPE_MINECART_HOPPER, L"MinecartHopper", 46); 73 setId(MinecartSpawner::create, eTYPE_MINECART_SPAWNER, L"MinecartSpawner", 47); 74 75 setId(Mob::create, eTYPE_MOB, L"Mob", 48); 76 setId(Monster::create, eTYPE_MONSTER, L"Monster", 49); 77 78 setId(Creeper::create, eTYPE_CREEPER, L"Creeper", 50, eMinecraftColour_Mob_Creeper_Colour1, eMinecraftColour_Mob_Creeper_Colour2, IDS_CREEPER); 79 setId(Skeleton::create, eTYPE_SKELETON, L"Skeleton", 51, eMinecraftColour_Mob_Skeleton_Colour1, eMinecraftColour_Mob_Skeleton_Colour2, IDS_SKELETON); 80 setId(Spider::create, eTYPE_SPIDER, L"Spider", 52, eMinecraftColour_Mob_Spider_Colour1, eMinecraftColour_Mob_Spider_Colour2, IDS_SPIDER); 81 setId(Giant::create, eTYPE_GIANT, L"Giant", 53); 82 setId(Zombie::create, eTYPE_ZOMBIE, L"Zombie", 54, eMinecraftColour_Mob_Zombie_Colour1, eMinecraftColour_Mob_Zombie_Colour2, IDS_ZOMBIE); 83 setId(Slime::create, eTYPE_SLIME, L"Slime", 55, eMinecraftColour_Mob_Slime_Colour1, eMinecraftColour_Mob_Slime_Colour2, IDS_SLIME); 84 setId(Ghast::create, eTYPE_GHAST, L"Ghast", 56, eMinecraftColour_Mob_Ghast_Colour1, eMinecraftColour_Mob_Ghast_Colour2, IDS_GHAST); 85 setId(PigZombie::create, eTYPE_PIGZOMBIE, L"PigZombie", 57, eMinecraftColour_Mob_PigZombie_Colour1, eMinecraftColour_Mob_PigZombie_Colour2, IDS_PIGZOMBIE); 86 setId(EnderMan::create, eTYPE_ENDERMAN, L"Enderman", 58, eMinecraftColour_Mob_Enderman_Colour1, eMinecraftColour_Mob_Enderman_Colour2, IDS_ENDERMAN); 87 setId(CaveSpider::create, eTYPE_CAVESPIDER, L"CaveSpider", 59, eMinecraftColour_Mob_CaveSpider_Colour1, eMinecraftColour_Mob_CaveSpider_Colour2, IDS_CAVE_SPIDER); 88 setId(Silverfish::create, eTYPE_SILVERFISH, L"Silverfish", 60, eMinecraftColour_Mob_Silverfish_Colour1, eMinecraftColour_Mob_Silverfish_Colour2, IDS_SILVERFISH); 89 setId(Blaze::create, eTYPE_BLAZE, L"Blaze", 61, eMinecraftColour_Mob_Blaze_Colour1, eMinecraftColour_Mob_Blaze_Colour2, IDS_BLAZE); 90 setId(LavaSlime::create, eTYPE_LAVASLIME, L"LavaSlime", 62, eMinecraftColour_Mob_LavaSlime_Colour1, eMinecraftColour_Mob_LavaSlime_Colour2, IDS_LAVA_SLIME); 91 setId(EnderDragon::create, eTYPE_ENDERDRAGON, L"EnderDragon", 63, eMinecraftColour_Mob_Enderman_Colour1, eMinecraftColour_Mob_Enderman_Colour1, IDS_ENDERDRAGON); 92 setId(WitherBoss::create, eTYPE_WITHERBOSS, L"WitherBoss", 64); 93 setId(Bat::create, eTYPE_BAT, L"Bat", 65, eMinecraftColour_Mob_Bat_Colour1, eMinecraftColour_Mob_Bat_Colour2, IDS_BAT); 94 setId(Witch::create, eTYPE_WITCH, L"Witch", 66, eMinecraftColour_Mob_Witch_Colour1, eMinecraftColour_Mob_Witch_Colour2, IDS_WITCH); 95 96 setId(Pig::create, eTYPE_PIG, L"Pig", 90, eMinecraftColour_Mob_Pig_Colour1, eMinecraftColour_Mob_Pig_Colour2, IDS_PIG); 97 setId(Sheep::create, eTYPE_SHEEP, L"Sheep", 91, eMinecraftColour_Mob_Sheep_Colour1, eMinecraftColour_Mob_Sheep_Colour2, IDS_SHEEP); 98 setId(Cow::create, eTYPE_COW, L"Cow", 92, eMinecraftColour_Mob_Cow_Colour1, eMinecraftColour_Mob_Cow_Colour2, IDS_COW); 99 setId(Chicken::create, eTYPE_CHICKEN, L"Chicken", 93, eMinecraftColour_Mob_Chicken_Colour1, eMinecraftColour_Mob_Chicken_Colour2, IDS_CHICKEN); 100 setId(Squid::create, eTYPE_SQUID, L"Squid", 94, eMinecraftColour_Mob_Squid_Colour1, eMinecraftColour_Mob_Squid_Colour2, IDS_SQUID); 101 setId(Wolf::create, eTYPE_WOLF, L"Wolf", 95, eMinecraftColour_Mob_Wolf_Colour1, eMinecraftColour_Mob_Wolf_Colour2, IDS_WOLF); 102 setId(MushroomCow::create, eTYPE_MUSHROOMCOW, L"MushroomCow", 96, eMinecraftColour_Mob_MushroomCow_Colour1, eMinecraftColour_Mob_MushroomCow_Colour2, IDS_MUSHROOM_COW); 103 setId(SnowMan::create, eTYPE_SNOWMAN, L"SnowMan", 97); 104 setId(Ocelot::create, eTYPE_OCELOT, L"Ozelot", 98, eMinecraftColour_Mob_Ocelot_Colour1, eMinecraftColour_Mob_Ocelot_Colour2, IDS_OZELOT); 105 setId(VillagerGolem::create, eTYPE_VILLAGERGOLEM, L"VillagerGolem", 99); 106 setId(EntityHorse::create, eTYPE_HORSE, L"EntityHorse", 100, eMinecraftColour_Mob_Horse_Colour1, eMinecraftColour_Mob_Horse_Colour2, IDS_HORSE); 107 108 setId(Villager::create, eTYPE_VILLAGER, L"Villager", 120, eMinecraftColour_Mob_Villager_Colour1, eMinecraftColour_Mob_Villager_Colour2, IDS_VILLAGER); 109 110 setId(EnderCrystal::create, eTYPE_ENDER_CRYSTAL, L"EnderCrystal", 200); 111 112 // 4J Added 113 setId(DragonFireball::create, eTYPE_DRAGON_FIREBALL, L"DragonFireball", 1000); 114 115 // 4J-PB - moved to allow the eggs to be named and coloured in the Creative Mode menu 116 // 4J Added for custom spawn eggs 117 setId(EntityHorse::create, eTYPE_HORSE, L"EntityHorse", 100 | ((EntityHorse::TYPE_DONKEY + 1) << 12), eMinecraftColour_Mob_Horse_Colour1, eMinecraftColour_Mob_Horse_Colour2, IDS_DONKEY); 118 setId(EntityHorse::create, eTYPE_HORSE, L"EntityHorse", 100 | ((EntityHorse::TYPE_MULE + 1) << 12), eMinecraftColour_Mob_Horse_Colour1, eMinecraftColour_Mob_Horse_Colour2, IDS_MULE); 119 120#ifndef _CONTENT_PACKAGE 121 setId(EntityHorse::create, eTYPE_HORSE, L"EntityHorse", 100 | ((EntityHorse::TYPE_SKELETON + 1) << 12), eMinecraftColour_Mob_Horse_Colour1, eMinecraftColour_Mob_Horse_Colour2, IDS_SKELETON_HORSE); 122 setId(EntityHorse::create, eTYPE_HORSE, L"EntityHorse", 100 | ((EntityHorse::TYPE_UNDEAD + 1) << 12), eMinecraftColour_Mob_Horse_Colour1, eMinecraftColour_Mob_Horse_Colour2, IDS_ZOMBIE_HORSE); 123 setId(Ocelot::create, eTYPE_OCELOT, L"Ozelot", 98 | ((Ocelot::TYPE_BLACK + 1) << 12), eMinecraftColour_Mob_Ocelot_Colour1, eMinecraftColour_Mob_Ocelot_Colour2, IDS_OZELOT ); 124 setId(Ocelot::create, eTYPE_OCELOT, L"Ozelot", 98 | ((Ocelot::TYPE_RED + 1) << 12), eMinecraftColour_Mob_Ocelot_Colour1, eMinecraftColour_Mob_Ocelot_Colour2, IDS_OZELOT ); 125 setId(Ocelot::create, eTYPE_OCELOT, L"Ozelot", 98 | ((Ocelot::TYPE_SIAMESE + 1) << 12), eMinecraftColour_Mob_Ocelot_Colour1, eMinecraftColour_Mob_Ocelot_Colour2, IDS_OZELOT ); 126 setId(Spider::create, eTYPE_SPIDER, L"Spider", 52 | (2 << 12), eMinecraftColour_Mob_Spider_Colour1, eMinecraftColour_Mob_Spider_Colour2, IDS_SKELETON ); 127#endif 128} 129 130shared_ptr<Entity> EntityIO::newEntity(const wstring& id, Level *level) 131{ 132 shared_ptr<Entity> entity; 133 134 AUTO_VAR(it, idCreateMap->find(id)); 135 if(it != idCreateMap->end() ) 136 { 137 entityCreateFn create = it->second; 138 if (create != NULL) entity = shared_ptr<Entity>(create(level)); 139 if( ( entity != NULL ) && entity->GetType() == eTYPE_ENDERDRAGON ) 140 { 141 dynamic_pointer_cast<EnderDragon>(entity)->AddParts(); // 4J added to finalise creation 142 } 143 } 144 145 return entity; 146} 147 148shared_ptr<Entity> EntityIO::loadStatic(CompoundTag *tag, Level *level) 149{ 150 shared_ptr<Entity> entity; 151 152 if (tag->getString(L"id").compare(L"Minecart") == 0) 153 { 154 // I don't like this any more than you do. Sadly, compatibility... 155 156 switch (tag->getInt(L"Type")) 157 { 158 case Minecart::TYPE_CHEST: 159 tag->putString(L"id", L"MinecartChest"); 160 break; 161 case Minecart::TYPE_FURNACE: 162 tag->putString(L"id", L"MinecartFurnace"); 163 break; 164 case Minecart::TYPE_RIDEABLE: 165 tag->putString(L"id", L"MinecartRideable"); 166 break; 167 } 168 169 tag->remove(L"Type"); 170 } 171 172 AUTO_VAR(it, idCreateMap->find(tag->getString(L"id"))); 173 if(it != idCreateMap->end() ) 174 { 175 entityCreateFn create = it->second; 176 if (create != NULL) entity = shared_ptr<Entity>(create(level)); 177 if( ( entity != NULL ) && entity->GetType() == eTYPE_ENDERDRAGON ) 178 { 179 dynamic_pointer_cast<EnderDragon>(entity)->AddParts(); // 4J added to finalise creation 180 } 181 } 182 183 if (entity != NULL) 184 { 185 entity->load(tag); 186 } 187 else 188 { 189#ifdef _DEBUG 190 app.DebugPrintf("Skipping Entity with id %ls\n", tag->getString(L"id").c_str() ); 191#endif 192 } 193 return entity; 194} 195 196shared_ptr<Entity> EntityIO::newById(int id, Level *level) 197{ 198 shared_ptr<Entity> entity; 199 200 AUTO_VAR(it, numCreateMap->find(id)); 201 if(it != numCreateMap->end() ) 202 { 203 entityCreateFn create = it->second; 204 if (create != NULL) entity = shared_ptr<Entity>(create(level)); 205 if( ( entity != NULL ) && entity->GetType() == eTYPE_ENDERDRAGON ) 206 { 207 dynamic_pointer_cast<EnderDragon>(entity)->AddParts(); // 4J added to finalise creation 208 } 209 } 210 211 if (entity != NULL) 212 { 213 } 214 else 215 { 216 //printf("Skipping Entity with id %d\n", id ) ; 217 } 218 return entity; 219} 220 221shared_ptr<Entity> EntityIO::newByEnumType(eINSTANCEOF eType, Level *level) 222{ 223 shared_ptr<Entity> entity; 224 225 unordered_map<eINSTANCEOF, int, eINSTANCEOFKeyHash, eINSTANCEOFKeyEq>::iterator it = classNumMap->find( eType ); 226 if( it != classNumMap->end() ) 227 { 228 AUTO_VAR(it2, numCreateMap->find(it->second)); 229 if(it2 != numCreateMap->end() ) 230 { 231 entityCreateFn create = it2->second; 232 if (create != NULL) entity = shared_ptr<Entity>(create(level)); 233 if( ( entity != NULL ) && entity->GetType() == eTYPE_ENDERDRAGON ) 234 { 235 dynamic_pointer_cast<EnderDragon>(entity)->AddParts(); // 4J added to finalise creation 236 } 237 } 238 } 239 240 return entity; 241} 242 243int EntityIO::getId(shared_ptr<Entity> entity) 244{ 245 unordered_map<eINSTANCEOF, int, eINSTANCEOFKeyHash, eINSTANCEOFKeyEq>::iterator it = classNumMap->find( entity->GetType() ); 246 return (*it).second; 247} 248 249wstring EntityIO::getEncodeId(shared_ptr<Entity> entity) 250{ 251 unordered_map<eINSTANCEOF, wstring, eINSTANCEOFKeyHash, eINSTANCEOFKeyEq>::iterator it = classIdMap->find( entity->GetType() ); 252 if( it != classIdMap->end() ) 253 return (*it).second; 254 else 255 return L""; 256} 257 258int EntityIO::getId(const wstring &encodeId) 259{ 260 AUTO_VAR(it, idNumMap->find(encodeId)); 261 if (it == idNumMap->end()) 262 { 263 // defaults to pig... 264 return 90; 265 } 266 return it->second; 267} 268 269wstring EntityIO::getEncodeId(int entityIoValue) 270{ 271 //Class<? extends Entity> class1 = numClassMap.get(entityIoValue); 272 //if (class1 != null) 273 //{ 274 //return classIdMap.get(class1); 275 //} 276 277 AUTO_VAR(it, numClassMap->find(entityIoValue)); 278 if(it != numClassMap->end() ) 279 { 280 unordered_map<eINSTANCEOF, wstring, eINSTANCEOFKeyHash, eINSTANCEOFKeyEq>::iterator classIdIt = classIdMap->find( it->second ); 281 if( classIdIt != classIdMap->end() ) 282 return (*classIdIt).second; 283 else 284 return L""; 285 } 286 287 return L""; 288} 289 290int EntityIO::getNameId(int entityIoValue) 291{ 292 int id = -1; 293 294 AUTO_VAR(it, idsSpawnableInCreative.find(entityIoValue)); 295 if(it != idsSpawnableInCreative.end()) 296 { 297 id = it->second->nameId; 298 } 299 300 return id; 301} 302 303eINSTANCEOF EntityIO::getType(const wstring &idString) 304{ 305 AUTO_VAR(it, numClassMap->find(getId(idString))); 306 if(it != numClassMap->end() ) 307 { 308 return it->second; 309 } 310 return eTYPE_NOTSET; 311} 312 313eINSTANCEOF EntityIO::getClass(int id) 314{ 315 AUTO_VAR(it, numClassMap->find(id)); 316 if(it != numClassMap->end() ) 317 { 318 return it->second; 319 } 320 return eTYPE_NOTSET; 321} 322 323int EntityIO::eTypeToIoid(eINSTANCEOF eType) 324{ 325 unordered_map<eINSTANCEOF, int, eINSTANCEOFKeyHash, eINSTANCEOFKeyEq>::iterator it = classNumMap->find( eType ); 326 if( it != classNumMap->end() ) 327 return it->second; 328 return -1; 329}