the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 162 lines 4.3 kB view raw
1#include "stdafx.h" 2#include "net.minecraft.world.level.h" 3#include "net.minecraft.world.level.tile.h" 4#include "net.minecraft.world.phys.h" 5#include "net.minecraft.world.item.h" 6#include "net.minecraft.world.effect.h" 7#include "SharedConstants.h" 8#include "JavaMath.h" 9#include "ThrownPotion.h" 10 11 12 13const double ThrownPotion::SPLASH_RANGE = 4.0; 14const double ThrownPotion::SPLASH_RANGE_SQ = ThrownPotion::SPLASH_RANGE * ThrownPotion::SPLASH_RANGE; 15 16void ThrownPotion::_init() 17{ 18 // 4J Stu - This function call had to be moved here from the Entity ctor to ensure that 19 // the derived version of the function is called 20 this->defineSynchedData(); 21 22 potionItem = nullptr; 23} 24 25ThrownPotion::ThrownPotion(Level *level) : Throwable(level) 26{ 27 _init(); 28} 29 30ThrownPotion::ThrownPotion(Level *level, shared_ptr<LivingEntity> mob, int potionValue) : Throwable(level,mob) 31{ 32 _init(); 33 34 potionItem = shared_ptr<ItemInstance>( new ItemInstance(Item::potion, 1, potionValue)); 35} 36 37ThrownPotion::ThrownPotion(Level *level, shared_ptr<LivingEntity> mob, shared_ptr<ItemInstance> potion) : Throwable(level, mob) 38{ 39 _init(); 40 41 potionItem = potion; 42} 43 44ThrownPotion::ThrownPotion(Level *level, double x, double y, double z, int potionValue) : Throwable(level,x,y,z) 45{ 46 _init(); 47 48 potionItem = shared_ptr<ItemInstance>( new ItemInstance(Item::potion, 1, potionValue)); 49} 50 51ThrownPotion::ThrownPotion(Level *level, double x, double y, double z, shared_ptr<ItemInstance> potion) : Throwable(level, x, y, z) 52{ 53 _init(); 54 55 potionItem = potion; 56} 57 58float ThrownPotion::getGravity() 59{ 60 return 0.05f; 61} 62 63float ThrownPotion::getThrowPower() 64{ 65 return 0.5f; 66} 67 68float ThrownPotion::getThrowUpAngleOffset() 69{ 70 return -20; 71} 72 73void ThrownPotion::setPotionValue(int potionValue) 74{ 75 if (potionItem == NULL) potionItem = shared_ptr<ItemInstance>( new ItemInstance(Item::potion, 1, 0) ); 76 potionItem->setAuxValue(potionValue); 77} 78 79int ThrownPotion::getPotionValue() 80{ 81 if (potionItem == NULL) potionItem = shared_ptr<ItemInstance>( new ItemInstance(Item::potion, 1, 0) ); 82 return potionItem->getAuxValue(); 83} 84 85void ThrownPotion::onHit(HitResult *res) 86{ 87 if (!level->isClientSide) 88 { 89 vector<MobEffectInstance *> *mobEffects = Item::potion->getMobEffects(potionItem); 90 91 if (mobEffects != NULL && !mobEffects->empty()) 92 { 93 AABB *aoe = bb->grow(SPLASH_RANGE, SPLASH_RANGE / 2, SPLASH_RANGE); 94 vector<shared_ptr<Entity> > *entitiesOfClass = level->getEntitiesOfClass(typeid(LivingEntity), aoe); 95 96 if (entitiesOfClass != NULL && !entitiesOfClass->empty()) 97 { 98 //for (Entity e : entitiesOfClass) 99 for(AUTO_VAR(it, entitiesOfClass->begin()); it != entitiesOfClass->end(); ++it) 100 { 101 //shared_ptr<Entity> e = *it; 102 shared_ptr<LivingEntity> e = dynamic_pointer_cast<LivingEntity>( *it ); 103 double dist = distanceToSqr(e); 104 if (dist < SPLASH_RANGE_SQ) 105 { 106 double scale = 1.0 - (sqrt(dist) / SPLASH_RANGE); 107 if (e == res->entity) 108 { 109 scale = 1; 110 } 111 112 //for (MobEffectInstance effect : mobEffects) 113 for(AUTO_VAR(itMEI, mobEffects->begin()); itMEI != mobEffects->end(); ++itMEI) 114 { 115 MobEffectInstance *effect = *itMEI; 116 int id = effect->getId(); 117 if (MobEffect::effects[id]->isInstantenous()) 118 { 119 MobEffect::effects[id]->applyInstantenousEffect(getOwner(), e, effect->getAmplifier(), scale); 120 } 121 else 122 { 123 int duration = (int) (scale * (double) effect->getDuration() + .5); 124 if (duration > SharedConstants::TICKS_PER_SECOND) 125 { 126 e->addEffect(new MobEffectInstance(id, duration, effect->getAmplifier())); 127 } 128 } 129 } 130 } 131 } 132 } 133 delete entitiesOfClass; 134 } 135 level->levelEvent(LevelEvent::PARTICLES_POTION_SPLASH, (int) Math::round(x), (int) Math::round(y), (int) Math::round(z), getPotionValue() ); 136 137 remove(); 138 } 139} 140 141void ThrownPotion::readAdditionalSaveData(CompoundTag *tag) 142{ 143 Throwable::readAdditionalSaveData(tag); 144 145 if (tag->contains(L"Potion")) 146 { 147 potionItem = ItemInstance::fromTag(tag->getCompound(L"Potion")); 148 } 149 else 150 { 151 setPotionValue(tag->getInt(L"potionValue")); 152 } 153 154 if (potionItem == NULL) remove(); 155} 156 157void ThrownPotion::addAdditonalSaveData(CompoundTag *tag) 158{ 159 Throwable::addAdditonalSaveData(tag); 160 161 if (potionItem != NULL) tag->putCompound(L"Potion", potionItem->save(new CompoundTag())); 162}