the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 334 lines 8.6 kB view raw
1#include "stdafx.h" 2#include "net.minecraft.world.level.h" 3#include "net.minecraft.world.level.redstone.h" 4#include "net.minecraft.h" 5#include "LeverTile.h" 6 7LeverTile::LeverTile(int id) : Tile(id, Material::decoration,isSolidRender()) 8{ 9} 10 11AABB *LeverTile::getAABB(Level *level, int x, int y, int z) 12{ 13 return NULL; 14} 15 16bool LeverTile::blocksLight() 17{ 18 return false; 19} 20 21bool LeverTile::isSolidRender(bool isServerLevel) 22{ 23 return false; 24} 25 26bool LeverTile::isCubeShaped() 27{ 28 return false; 29} 30 31int LeverTile::getRenderShape() 32{ 33 return Tile::SHAPE_LEVER; 34} 35 36bool LeverTile::mayPlace(Level *level, int x, int y, int z, int face) 37{ 38 if (face == Facing::DOWN && level->isSolidBlockingTile(x, y + 1, z)) return true; 39 if (face == Facing::UP && level->isTopSolidBlocking(x, y - 1, z)) return true; 40 if (face == Facing::NORTH && level->isSolidBlockingTile(x, y, z + 1)) return true; 41 if (face == Facing::SOUTH && level->isSolidBlockingTile(x, y, z - 1)) return true; 42 if (face == Facing::WEST && level->isSolidBlockingTile(x + 1, y, z)) return true; 43 if (face == Facing::EAST && level->isSolidBlockingTile(x - 1, y, z)) return true; 44 return false; 45} 46 47bool LeverTile::mayPlace(Level *level, int x, int y, int z) 48{ 49 if (level->isSolidBlockingTile(x - 1, y, z)) 50 { 51 return true; 52 } else if (level->isSolidBlockingTile(x + 1, y, z)) 53 { 54 return true; 55 } else if (level->isSolidBlockingTile(x, y, z - 1)) 56 { 57 return true; 58 } else if (level->isSolidBlockingTile(x, y, z + 1)) 59 { 60 return true; 61 } else if (level->isTopSolidBlocking(x, y - 1, z)) 62 { 63 return true; 64 } 65 else if (level->isSolidBlockingTile(x, y + 1, z)) 66 { 67 return true; 68 } 69 return false; 70} 71 72int LeverTile::getPlacedOnFaceDataValue(Level *level, int x, int y, int z, int face, float clickX, float clickY, float clickZ, int itemValue) 73{ 74 int dir = itemValue; 75 76 int oldFlip = dir & 8; 77 dir &= 7; 78 79 dir = -1; 80 81 if (face == Facing::DOWN && level->isSolidBlockingTile(x, y + 1, z)) dir = 0; 82 if (face == Facing::UP && level->isTopSolidBlocking(x, y - 1, z)) dir = 5; 83 if (face == Facing::NORTH && level->isSolidBlockingTile(x, y, z + 1)) dir = 4; 84 if (face == Facing::SOUTH && level->isSolidBlockingTile(x, y, z - 1)) dir = 3; 85 if (face == Facing::WEST && level->isSolidBlockingTile(x + 1, y, z)) dir = 2; 86 if (face == Facing::EAST && level->isSolidBlockingTile(x - 1, y, z)) dir = 1; 87 88 return dir + oldFlip; 89} 90 91void LeverTile::setPlacedBy(Level *level, int x, int y, int z, shared_ptr<LivingEntity> by, shared_ptr<ItemInstance> itemInstance) 92{ 93 int data = level->getData(x, y, z); 94 int dir = data & 7; 95 int flip = data & 8; 96 97 if (dir == getLeverFacing(Facing::UP)) 98 { 99 if ((Mth::floor(by->yRot * 4 / (360) + 0.5) & 1) == 0) 100 { 101 level->setData(x, y, z, 5 | flip, Tile::UPDATE_CLIENTS); 102 } 103 else 104 { 105 level->setData(x, y, z, 6 | flip, Tile::UPDATE_CLIENTS); 106 } 107 } 108 else if (dir == getLeverFacing(Facing::DOWN)) 109 { 110 if ((Mth::floor(by->yRot * 4 / (360) + 0.5) & 1) == 0) 111 { 112 level->setData(x, y, z, 7 | flip, Tile::UPDATE_CLIENTS); 113 } 114 else 115 { 116 level->setData(x, y, z, 0 | flip, Tile::UPDATE_CLIENTS); 117 } 118 } 119} 120 121int LeverTile::getLeverFacing(int facing) 122{ 123 switch (facing) 124 { 125 case Facing::DOWN: 126 return 0; 127 case Facing::UP: 128 return 5; 129 case Facing::NORTH: 130 return 4; 131 case Facing::SOUTH: 132 return 3; 133 case Facing::WEST: 134 return 2; 135 case Facing::EAST: 136 return 1; 137 } 138 return -1; 139} 140 141void LeverTile::neighborChanged(Level *level, int x, int y, int z, int type) 142{ 143 if (checkCanSurvive(level, x, y, z)) 144 { 145 int dir = level->getData(x, y, z) & 7; 146 bool replace = false; 147 148 if (!level->isSolidBlockingTile(x - 1, y, z) && dir == 1) replace = true; 149 if (!level->isSolidBlockingTile(x + 1, y, z) && dir == 2) replace = true; 150 if (!level->isSolidBlockingTile(x, y, z - 1) && dir == 3) replace = true; 151 if (!level->isSolidBlockingTile(x, y, z + 1) && dir == 4) replace = true; 152 if (!level->isTopSolidBlocking(x, y - 1, z) && dir == 5) replace = true; 153 if (!level->isTopSolidBlocking(x, y - 1, z) && dir == 6) replace = true; 154 if (!level->isSolidBlockingTile(x, y + 1, z) && dir == 0) replace = true; 155 if (!level->isSolidBlockingTile(x, y + 1, z) && dir == 7) replace = true; 156 157 if (replace) 158 { 159 spawnResources(level, x, y, z, level->getData(x, y, z), 0); 160 level->removeTile(x, y, z); 161 } 162 } 163 164} 165 166bool LeverTile::checkCanSurvive(Level *level, int x, int y, int z) 167{ 168 if (!mayPlace(level, x, y, z)) 169 { 170 spawnResources(level, x, y, z, level->getData(x, y, z), 0); 171 level->removeTile(x, y, z); 172 return false; 173 } 174 return true; 175} 176 177void LeverTile::updateShape(LevelSource *level, int x, int y, int z, int forceData, shared_ptr<TileEntity> forceEntity) // 4J added forceData, forceEntity param 178{ 179 int dir = level->getData(x, y, z) & 7; 180 float r = 3 / 16.0f; 181 if (dir == 1) 182 { 183 setShape(0, 0.2f, 0.5f - r, r * 2, 0.8f, 0.5f + r); 184 } 185 else if (dir == 2) 186 { 187 setShape(1 - r * 2, 0.2f, 0.5f - r, 1, 0.8f, 0.5f + r); 188 } 189 else if (dir == 3) 190 { 191 setShape(0.5f - r, 0.2f, 0, 0.5f + r, 0.8f, r * 2); 192 } 193 else if (dir == 4) 194 { 195 setShape(0.5f - r, 0.2f, 1 - r * 2, 0.5f + r, 0.8f, 1); 196 } 197 else if (dir == 5 || dir == 6) 198 { 199 r = 4 / 16.0f; 200 setShape(0.5f - r, 0.0f, 0.5f - r, 0.5f + r, 0.6f, 0.5f + r); 201 } 202 else if (dir == 0 || dir == 7) 203 { 204 r = 4 / 16.0f; 205 setShape(0.5f - r, 0.4f, 0.5f - r, 0.5f + r, 1.0f, 0.5f + r); 206 } 207} 208 209// 4J-PB - Adding a TestUse for tooltip display 210bool LeverTile::TestUse() 211{ 212 return true; 213} 214 215bool LeverTile::use(Level *level, int x, int y, int z, shared_ptr<Player> player, int clickedFace, float clickX, float clickY, float clickZ, bool soundOnly/*=false*/) // 4J added soundOnly param 216{ 217 if( soundOnly ) 218 { 219 // 4J - added - just do enough to play the sound 220 int data = level->getData(x, y, z); 221 int dir = data & 7; 222 int open = 8 - (data & 8); 223 level->playSound(x + 0.5, y + 0.5, z + 0.5, eSoundType_RANDOM_CLICK, 0.3f, open > 0 ? 0.6f : 0.5f); 224 return false; 225 } 226 if (level->isClientSide) 227 { 228 // 4J - added stuff to play sound in this case too 229 int data = level->getData(x, y, z); 230 int dir = data & 7; 231 int open = 8 - (data & 8); 232 level->playSound(x + 0.5, y + 0.5, z + 0.5, eSoundType_RANDOM_CLICK, 0.3f, open > 0 ? 0.6f : 0.5f); 233 234 return true; 235 } 236 int data = level->getData(x, y, z); 237 int dir = data & 7; 238 int open = 8 - (data & 8); 239 240 level->setData(x, y, z, dir + open, Tile::UPDATE_ALL); 241 level->setTilesDirty(x, y, z, x, y, z); 242 243 level->playSound(x + 0.5, y + 0.5, z + 0.5, eSoundType_RANDOM_CLICK, 0.3f, open > 0 ? 0.6f : 0.5f); 244 245 level->updateNeighborsAt(x, y, z, id); 246 if (dir == 1) 247 { 248 level->updateNeighborsAt(x - 1, y, z, id); 249 } 250 else if (dir == 2) 251 { 252 level->updateNeighborsAt(x + 1, y, z, id); 253 } 254 else if (dir == 3) 255 { 256 level->updateNeighborsAt(x, y, z - 1, id); 257 } 258 else if (dir == 4) 259 { 260 level->updateNeighborsAt(x, y, z + 1, id); 261 } 262 else if (dir == 5 || dir == 6) 263 { 264 level->updateNeighborsAt(x, y - 1, z, id); 265 } 266 else if (dir == 0 || dir == 7) 267 { 268 level->updateNeighborsAt(x, y + 1, z, id); 269 } 270 271 return true; 272} 273 274void LeverTile::onRemove(Level *level, int x, int y, int z, int id, int data) 275{ 276 if ((data & 8) > 0) 277 { 278 level->updateNeighborsAt(x, y, z, this->id); 279 int dir = data & 7; 280 if (dir == 1) 281 { 282 level->updateNeighborsAt(x - 1, y, z, this->id); 283 } 284 else if (dir == 2) 285 { 286 level->updateNeighborsAt(x + 1, y, z, this->id); 287 } 288 else if (dir == 3) 289 { 290 level->updateNeighborsAt(x, y, z - 1, this->id); 291 } 292 else if (dir == 4) 293 { 294 level->updateNeighborsAt(x, y, z + 1, this->id); 295 } 296 else if (dir == 5 || dir == 6) 297 { 298 level->updateNeighborsAt(x, y - 1, z, this->id); 299 } 300 else if (dir == 0 || dir == 7) 301 { 302 level->updateNeighborsAt(x, y + 1, z, this->id); 303 } 304 } 305 Tile::onRemove(level, x, y, z, id, data); 306} 307 308int LeverTile::getSignal(LevelSource *level, int x, int y, int z, int dir) 309{ 310 return (level->getData(x, y, z) & 8) > 0 ? Redstone::SIGNAL_MAX : Redstone::SIGNAL_NONE; 311} 312 313int LeverTile::getDirectSignal(LevelSource *level, int x, int y, int z, int dir) 314{ 315 int data = level->getData(x, y, z); 316 if ((data & 8) == 0) return Redstone::SIGNAL_NONE; 317 int myDir = data & 7; 318 319 if (myDir == 0 && dir == 0) return Redstone::SIGNAL_MAX; 320 if (myDir == 7 && dir == 0) return Redstone::SIGNAL_MAX; 321 if (myDir == 6 && dir == 1) return Redstone::SIGNAL_MAX; 322 if (myDir == 5 && dir == 1) return Redstone::SIGNAL_MAX; 323 if (myDir == 4 && dir == 2) return Redstone::SIGNAL_MAX; 324 if (myDir == 3 && dir == 3) return Redstone::SIGNAL_MAX; 325 if (myDir == 2 && dir == 4) return Redstone::SIGNAL_MAX; 326 if (myDir == 1 && dir == 5) return Redstone::SIGNAL_MAX; 327 328 return Redstone::SIGNAL_NONE; 329} 330 331bool LeverTile::isSignalSource() 332{ 333 return true; 334}