the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 296 lines 6.3 kB view raw
1#include "stdafx.h" 2#include "net.minecraft.world.phys.h" 3#include "net.minecraft.world.entity.h" 4#include "net.minecraft.world.level.tile.h" 5#include "net.minecraft.world.level.h" 6#include "net.minecraft.world.damagesource.h" 7#include "com.mojang.nbt.h" 8#include "HangingEntity.h" 9 10 11 12void HangingEntity::_init(Level *level) 13{ 14 checkInterval = 0; 15 dir = 0; 16 xTile = yTile = zTile = 0; 17 this->heightOffset = 0; 18 this->setSize(0.5f, 0.5f); 19} 20 21HangingEntity::HangingEntity(Level *level) : Entity( level ) 22{ 23 _init(level); 24 25} 26 27HangingEntity::HangingEntity(Level *level, int xTile, int yTile, int zTile, int dir) : Entity( level ) 28{ 29 _init(level); 30 this->xTile = xTile; 31 this->yTile = yTile; 32 this->zTile = zTile; 33} 34 35void HangingEntity::setDir(int dir) 36{ 37 this->dir = dir; 38 yRotO = yRot = (float)(dir * 90); 39 40 float w = (float)getWidth(); 41 float h = (float)getHeight(); 42 float d = (float)getWidth(); 43 44 if (dir == Direction::NORTH || dir == Direction::SOUTH) 45 { 46 d = 0.5f; 47 yRot = yRotO = (float)(Direction::DIRECTION_OPPOSITE[dir] * 90); 48 } 49 else 50 { 51 w = 0.5f; 52 } 53 54 w /= 32.0f; 55 h /= 32.0f; 56 d /= 32.0f; 57 58 float x = xTile + 0.5f; 59 float y = yTile + 0.5f; 60 float z = zTile + 0.5f; 61 62 float fOffs = 0.5f + 1.0f / 16.0f; 63 64 if (dir == Direction::NORTH) z -= fOffs; 65 if (dir == Direction::WEST) x -= fOffs; 66 if (dir == Direction::SOUTH) z += fOffs; 67 if (dir == Direction::EAST) x += fOffs; 68 69 if (dir == Direction::NORTH) x -= offs(getWidth()); 70 if (dir == Direction::WEST) z += offs(getWidth()); 71 if (dir == Direction::SOUTH) x += offs(getWidth()); 72 if (dir == Direction::EAST) z -= offs(getWidth()); 73 y += offs(getHeight()); 74 75 setPos(x, y, z); 76 77 float ss = -(0.5f / 16.0f); 78 79 // 4J Stu - Due to rotations the bb couold be set with a lower bound x/z being higher than the higher bound 80 float x0 = x - w - ss; 81 float x1 = x + w + ss; 82 float y0 = y - h - ss; 83 float y1 = y + h + ss; 84 float z0 = z - d - ss; 85 float z1 = z + d + ss; 86 bb->set(min(x0,x1), min(y0,y1), min(z0,z1), max(x0,x1), max(y0,y1), max(z0,z1)); 87} 88 89float HangingEntity::offs(int w) 90{ 91 if (w == 32) return 0.5f; 92 if (w == 64) return 0.5f; 93 return 0.0f; 94} 95 96void HangingEntity::tick() 97{ 98 xo = x; 99 yo = y; 100 zo = z; 101 if (checkInterval++ == 20 * 5 && !level->isClientSide) 102 { 103 checkInterval = 0; 104 if (!removed && !survives()) 105 { 106 remove(); 107 dropItem(nullptr); 108 } 109 } 110} 111 112bool HangingEntity::survives() 113{ 114 if (level->getCubes(shared_from_this(), bb)->size()!=0)//isEmpty()) 115 { 116 return false; 117 } 118 else 119 { 120 int ws = max(1, getWidth() / 16); 121 int hs = max(1, getHeight() / 16); 122 123 int xt = xTile; 124 int yt = yTile; 125 int zt = zTile; 126 if (dir == Direction::NORTH) xt = Mth::floor(x - getWidth() / 32.0f); 127 if (dir == Direction::WEST) zt = Mth::floor(z - getWidth() / 32.0f); 128 if (dir == Direction::SOUTH) xt = Mth::floor(x - getWidth() / 32.0f); 129 if (dir == Direction::EAST) zt = Mth::floor(z - getWidth() / 32.0f); 130 yt = Mth::floor(y - getHeight() / 32.0f); 131 132 for (int ss = 0; ss < ws; ss++) 133 { 134 for (int yy = 0; yy < hs; yy++) 135 { 136 Material *m; 137 if (dir == Direction::NORTH || dir == Direction::SOUTH) 138 { 139 m = level->getMaterial(xt + ss, yt + yy, zTile); 140 } 141 else 142 { 143 m = level->getMaterial(xTile, yt + yy, zt + ss); 144 } 145 if (!m->isSolid()) 146 { 147 return false; 148 } 149 } 150 151 vector<shared_ptr<Entity> > *entities = level->getEntities(shared_from_this(), bb); 152 153 if (entities != NULL && entities->size() > 0) 154 { 155 AUTO_VAR(itEnd, entities->end()); 156 for (AUTO_VAR(it, entities->begin()); it != itEnd; it++) 157 { 158 shared_ptr<Entity> e = (*it); 159 if( e->instanceof(eTYPE_HANGING_ENTITY) ) 160 { 161 return false; 162 } 163 } 164 } 165 } 166 } 167 return true; 168} 169 170bool HangingEntity::isPickable() 171{ 172 return true; 173} 174 175bool HangingEntity::skipAttackInteraction(shared_ptr<Entity> source) 176{ 177 if(source->GetType()==eTYPE_PLAYER) 178 { 179 return hurt(DamageSource::playerAttack(dynamic_pointer_cast<Player>( source)), 0); 180 } 181 return false; 182} 183 184bool HangingEntity::hurt(DamageSource *source, float damage) 185{ 186 if (isInvulnerable()) return false; 187 if (!removed && !level->isClientSide) 188 { 189 if (dynamic_cast<EntityDamageSource *>(source) != NULL) 190 { 191 shared_ptr<Entity> sourceEntity = source->getDirectEntity(); 192 193 if ( (sourceEntity != NULL) && sourceEntity->instanceof(eTYPE_PLAYER) && !dynamic_pointer_cast<Player>(sourceEntity)->isAllowedToHurtEntity(shared_from_this()) ) 194 { 195 return false; 196 } 197 } 198 199 remove(); 200 markHurt(); 201 202 shared_ptr<Player> player = nullptr; 203 shared_ptr<Entity> e = source->getEntity(); 204 if ( (e!=NULL) && e->instanceof(eTYPE_PLAYER) ) // check if it's serverplayer or player 205 { 206 player = dynamic_pointer_cast<Player>( e ); 207 } 208 209 if (player != NULL && player->abilities.instabuild) 210 { 211 return true; 212 } 213 214 dropItem(nullptr); 215 } 216 return true; 217} 218 219// 4J - added noEntityCubes parameter 220void HangingEntity::move(double xa, double ya, double za, bool noEntityCubes) 221{ 222 if (!level->isClientSide && !removed && (xa * xa + ya * ya + za * za) > 0) 223 { 224 remove(); 225 dropItem(nullptr); 226 } 227} 228 229void HangingEntity::push(double xa, double ya, double za) 230{ 231 if (!level->isClientSide && !removed && (xa * xa + ya * ya + za * za) > 0) 232 { 233 remove(); 234 dropItem(nullptr); 235 } 236} 237 238void HangingEntity::addAdditonalSaveData(CompoundTag *tag) 239{ 240 tag->putByte(L"Direction", (byte) dir); 241 tag->putInt(L"TileX", xTile); 242 tag->putInt(L"TileY", yTile); 243 tag->putInt(L"TileZ", zTile); 244 245 // Back compat 246 switch (dir) 247 { 248 case Direction::NORTH: 249 tag->putByte(L"Dir", (byte) 0); 250 break; 251 case Direction::WEST: 252 tag->putByte(L"Dir", (byte) 1); 253 break; 254 case Direction::SOUTH: 255 tag->putByte(L"Dir", (byte) 2); 256 break; 257 case Direction::EAST: 258 tag->putByte(L"Dir", (byte) 3); 259 break; 260 } 261} 262 263void HangingEntity::readAdditionalSaveData(CompoundTag *tag) 264{ 265 if (tag->contains(L"Direction")) 266 { 267 dir = tag->getByte(L"Direction"); 268 } 269 else 270 { 271 switch (tag->getByte(L"Dir")) 272 { 273 case 0: 274 dir = Direction::NORTH; 275 break; 276 case 1: 277 dir = Direction::WEST; 278 break; 279 case 2: 280 dir = Direction::SOUTH; 281 break; 282 case 3: 283 dir = Direction::EAST; 284 break; 285 } 286 } 287 xTile = tag->getInt(L"TileX"); 288 yTile = tag->getInt(L"TileY"); 289 zTile = tag->getInt(L"TileZ"); 290 setDir(dir); 291} 292 293bool HangingEntity::repositionEntityAfterLoad() 294{ 295 return false; 296}