the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 221 lines 4.3 kB view raw
1#include "stdafx.h" 2#include "com.mojang.nbt.h" 3#include "PistonPieceEntity.h" 4#include "PistonMovingPiece.h" 5#include "net.minecraft.world.level.h" 6#include "Facing.h" 7#include "Tile.h" 8 9 10 11PistonPieceEntity::PistonPieceEntity() 12{ 13 // for the tile entity loader 14 15 // 4J - added initialisers 16 this->id = 0; 17 this->data = 0; 18 this->facing = 0; 19 this->extending = 0; 20 this->_isSourcePiston = 0; 21 progress = 0.0f; 22 progressO = 0.0f; 23} 24 25PistonPieceEntity::PistonPieceEntity(int id, int data, int facing, bool extending, bool isSourcePiston) : TileEntity() 26{ 27 // 4J - added initialisers 28 progress = 0.0f; 29 progressO = 0.0f; 30 31 this->id = id; 32 this->data = data; 33 this->facing = facing; 34 this->extending = extending; 35 this->_isSourcePiston = isSourcePiston; 36} 37 38int PistonPieceEntity::getId() 39{ 40 return id; 41} 42 43int PistonPieceEntity::getData() 44{ 45 return data; 46} 47 48bool PistonPieceEntity::isExtending() 49{ 50 return extending; 51} 52 53int PistonPieceEntity::getFacing() 54{ 55 return facing; 56} 57 58bool PistonPieceEntity::isSourcePiston() 59{ 60 return _isSourcePiston; 61} 62 63float PistonPieceEntity::getProgress(float a) 64{ 65 if (a > 1) 66 { 67 a = 1; 68 } 69 return progressO + (progress - progressO) * a; 70} 71 72float PistonPieceEntity::getXOff(float a) 73{ 74 if (extending) 75 { 76 return (getProgress(a) - 1.0f) * Facing::STEP_X[facing]; 77 } 78 else 79 { 80 return (1.0f - getProgress(a)) * Facing::STEP_X[facing]; 81 } 82} 83 84float PistonPieceEntity::getYOff(float a) 85{ 86 if (extending) 87 { 88 return (getProgress(a) - 1.0f) * Facing::STEP_Y[facing]; 89 } 90 else 91 { 92 return (1.0f - getProgress(a)) * Facing::STEP_Y[facing]; 93 } 94} 95 96float PistonPieceEntity::getZOff(float a) 97{ 98 if (extending) 99 { 100 return (getProgress(a) - 1.0f) * Facing::STEP_Z[facing]; 101 } 102 else 103 { 104 return (1.0f - getProgress(a)) * Facing::STEP_Z[facing]; 105 } 106} 107 108void PistonPieceEntity::moveCollidedEntities(float progress, float amount) 109{ 110 if (extending) 111 { 112 progress = 1.0f - progress; 113 } 114 else 115 { 116 progress = progress - 1.0f; 117 } 118 119 AABB *aabb = Tile::pistonMovingPiece->getAABB(level, x, y, z, id, progress, facing); 120 if (aabb != NULL) 121 { 122 vector<shared_ptr<Entity> > *entities = level->getEntities(nullptr, aabb); 123 if (!entities->empty()) 124 { 125 vector< shared_ptr<Entity> > collisionHolder; 126 for( AUTO_VAR(it, entities->begin()); it != entities->end(); it++ ) 127 { 128 collisionHolder.push_back(*it); 129 } 130 131 for( AUTO_VAR(it, collisionHolder.begin()); it != collisionHolder.end(); it++ ) 132 { 133 (*it)->move(amount * Facing::STEP_X[facing], 134 amount * Facing::STEP_Y[facing], 135 amount * Facing::STEP_Z[facing]); 136 } 137 } 138 } 139} 140 141void PistonPieceEntity::finalTick() 142{ 143 if (progressO < 1 && level != NULL) 144 { 145 progressO = progress = 1; 146 level->removeTileEntity(x, y, z); 147 setRemoved(); 148 if (level->getTile(x, y, z) == Tile::pistonMovingPiece_Id) 149 { 150 level->setTileAndData(x, y, z, id, data, Tile::UPDATE_ALL); 151 level->neighborChanged(x, y, z, id); 152 } 153 } 154} 155 156void PistonPieceEntity::tick() 157{ 158 progressO = progress; 159 160 if (progressO >= 1) 161 { 162 moveCollidedEntities(1, 4 / 16.f); 163 level->removeTileEntity(x, y, z); 164 setRemoved(); 165 if (level->getTile(x, y, z) == Tile::pistonMovingPiece_Id) 166 { 167 level->setTileAndData(x, y, z, id, data, Tile::UPDATE_ALL); 168 level->neighborChanged(x, y, z, id); 169 } 170 return; 171 } 172 173 progress += .5f; 174 if (progress >= 1) 175 { 176 progress = 1; 177 } 178 179 if (extending) 180 { 181 moveCollidedEntities(progress, (progress - progressO) + 1.0f / 16.0f); 182 } 183} 184 185void PistonPieceEntity::load(CompoundTag *tag) 186{ 187 TileEntity::load(tag); 188 189 id = tag->getInt(L"blockId"); 190 data = tag->getInt(L"blockData"); 191 facing = tag->getInt(L"facing"); 192 progressO = progress = tag->getFloat(L"progress"); 193 extending = tag->getBoolean(L"extending"); 194} 195 196void PistonPieceEntity::save(CompoundTag *tag) 197{ 198 TileEntity::save(tag); 199 200 tag->putInt(L"blockId", id); 201 tag->putInt(L"blockData", data); 202 tag->putInt(L"facing", facing); 203 tag->putFloat(L"progress", progressO); 204 tag->putBoolean(L"extending", extending); 205} 206 207// 4J Added 208shared_ptr<TileEntity> PistonPieceEntity::clone() 209{ 210 shared_ptr<PistonPieceEntity> result = shared_ptr<PistonPieceEntity>( new PistonPieceEntity() ); 211 TileEntity::clone(result); 212 213 result->id = id; 214 result->data = data; 215 result->facing = facing; 216 result->extending = extending; 217 result->_isSourcePiston = _isSourcePiston; 218 result->progress = progress; 219 result->progressO = progressO; 220 return result; 221}