the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 368 lines 8.9 kB view raw
1#include "stdafx.h" 2#include "net.minecraft.world.entity.projectile.h" 3#include "net.minecraft.world.level.h" 4#include "net.minecraft.world.level.redstone.h" 5#include "net.minecraft.world.phys.h" 6#include "net.minecraft.h" 7#include "ButtonTile.h" 8#include "SoundTypes.h" 9 10ButtonTile::ButtonTile(int id, bool sensitive) : Tile(id, Material::decoration,isSolidRender()) 11{ 12 this->setTicking(true); 13 this->sensitive = sensitive; 14} 15 16Icon *ButtonTile::getTexture(int face, int data) 17{ 18 if(id == Tile::button_wood_Id) return Tile::wood->getTexture(Facing::UP); 19 else return Tile::stone->getTexture(Facing::UP); 20} 21 22AABB *ButtonTile::getAABB(Level *level, int x, int y, int z) 23{ 24 return NULL; 25} 26 27int ButtonTile::getTickDelay(Level *level) 28{ 29 return sensitive ? 30 : 20; 30} 31 32bool ButtonTile::blocksLight() 33{ 34 return false; 35} 36 37bool ButtonTile::isSolidRender(bool isServerLevel) 38{ 39 return false; 40} 41 42bool ButtonTile::isCubeShaped() 43{ 44 return false; 45} 46 47bool ButtonTile::mayPlace(Level *level, int x, int y, int z, int face) 48{ 49 if (face == 2 && level->isSolidBlockingTile(x, y, z + 1)) return true; 50 if (face == 3 && level->isSolidBlockingTile(x, y, z - 1)) return true; 51 if (face == 4 && level->isSolidBlockingTile(x + 1, y, z)) return true; 52 if (face == 5 && level->isSolidBlockingTile(x - 1, y, z)) return true; 53 return false; 54} 55 56bool ButtonTile::mayPlace(Level *level, int x, int y, int z) 57{ 58 if (level->isSolidBlockingTile(x - 1, y, z)) 59 { 60 return true; 61 } 62 else if (level->isSolidBlockingTile(x + 1, y, z)) 63 { 64 return true; 65 } 66 else if (level->isSolidBlockingTile(x, y, z - 1)) 67 { 68 return true; 69 } 70 else if (level->isSolidBlockingTile(x, y, z + 1)) 71 { 72 return true; 73 } 74 return false; 75} 76 77int ButtonTile::getPlacedOnFaceDataValue(Level *level, int x, int y, int z, int face, float clickX, float clickY, float clickZ, int itemValue) 78{ 79 int dir = level->getData(x, y, z); 80 81 int oldFlip = dir & 8; 82 dir &= 7; 83 84 if (face == 2 && level->isSolidBlockingTile(x, y, z + 1)) dir = 4; 85 else if (face == 3 && level->isSolidBlockingTile(x, y, z - 1)) dir = 3; 86 else if (face == 4 && level->isSolidBlockingTile(x + 1, y, z)) dir = 2; 87 else if (face == 5 && level->isSolidBlockingTile(x - 1, y, z)) dir = 1; 88 else dir = findFace(level, x, y, z); 89 90 return dir + oldFlip; 91} 92 93int ButtonTile::findFace(Level *level, int x, int y, int z) 94{ 95 if (level->isSolidBlockingTile(x - 1, y, z)) 96 { 97 return 1; 98 } 99 else if (level->isSolidBlockingTile(x + 1, y, z)) 100 { 101 return 2; 102 } 103 else if (level->isSolidBlockingTile(x, y, z - 1)) 104 { 105 return 3; 106 } 107 else if (level->isSolidBlockingTile(x, y, z + 1)) 108 { 109 return 4; 110 } 111 return 1; 112} 113 114void ButtonTile::neighborChanged(Level *level, int x, int y, int z, int type) 115{ 116 if (checkCanSurvive(level, x, y, z)) 117 { 118 int dir = level->getData(x, y, z) & 7; 119 bool replace = false; 120 121 if (!level->isSolidBlockingTile(x - 1, y, z) && dir == 1) replace = true; 122 if (!level->isSolidBlockingTile(x + 1, y, z) && dir == 2) replace = true; 123 if (!level->isSolidBlockingTile(x, y, z - 1) && dir == 3) replace = true; 124 if (!level->isSolidBlockingTile(x, y, z + 1) && dir == 4) replace = true; 125 126 if (replace) 127 { 128 spawnResources(level, x, y, z, level->getData(x, y, z), 0); 129 level->removeTile(x, y, z); 130 } 131 } 132} 133 134bool ButtonTile::checkCanSurvive(Level *level, int x, int y, int z) 135{ 136 if (!mayPlace(level, x, y, z)) 137 { 138 this->spawnResources(level, x, y, z, level->getData(x, y, z), 0); 139 level->removeTile(x, y, z); 140 return false; 141 } 142 return true; 143} 144 145void ButtonTile::updateShape(LevelSource *level, int x, int y, int z, int forceData, shared_ptr<TileEntity> forceEntity) // 4J added forceData, forceEntity param 146{ 147 int data = level->getData(x, y, z); 148 updateShape(data); 149} 150 151void ButtonTile::updateShape(int data) 152{ 153 int dir = data & 7; 154 bool pressed = (data & 8) > 0; 155 156 float h0 = 6 / 16.0f; 157 float h1 = 10 / 16.0f; 158 float r = 3 / 16.0f; 159 float d = 2 / 16.0f; 160 if (pressed) d = 1 / 16.0f; 161 162 if (dir == 1) 163 { 164 setShape(0, h0, 0.5f - r, d, h1, 0.5f + r); 165 } 166 else if (dir == 2) 167 { 168 setShape(1 - d, h0, 0.5f - r, 1, h1, 0.5f + r); 169 } 170 else if (dir == 3) 171 { 172 setShape(0.5f - r, h0, 0, 0.5f + r, h1, d); 173 } 174 else if (dir == 4) 175 { 176 setShape(0.5f - r, h0, 1 - d, 0.5f + r, h1, 1); 177 } 178} 179 180void ButtonTile::attack(Level *level, int x, int y, int z, shared_ptr<Player> player) 181{ 182 //use(level, x, y, z, player, 0, 0, 0, 0); 183} 184 185// 4J-PB - Adding a TestUse for tooltip display 186bool ButtonTile::TestUse() 187{ 188 return true; 189} 190 191bool ButtonTile::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 192{ 193 if (soundOnly) 194 { 195 // 4J - added - just do enough to play the sound 196 level->playSound(x + 0.5, y + 0.5, z + 0.5, eSoundType_RANDOM_CLICK, 0.3f, 0.6f); 197 return false; 198 } 199 200 int data = level->getData(x, y, z); 201 int dir = data & 7; 202 int open = 8 - (data & 8); 203 if (open == 0) return true; 204 205 level->setData(x, y, z, dir + open, Tile::UPDATE_ALL); 206 level->setTilesDirty(x, y, z, x, y, z); 207 208 level->playSound(x + 0.5, y + 0.5, z + 0.5, eSoundType_RANDOM_CLICK, 0.3f, 0.6f); 209 210 updateNeighbours(level, x, y, z, dir); 211 212 level->addToTickNextTick(x, y, z, id, getTickDelay(level)); 213 214 return true; 215} 216 217void ButtonTile::onRemove(Level *level, int x, int y, int z, int id, int data) 218{ 219 if ((data & 8) > 0) 220 { 221 int dir = data & 7; 222 updateNeighbours(level, x, y, z, dir); 223 } 224 Tile::onRemove(level, x, y, z, id, data); 225} 226 227int ButtonTile::getSignal(LevelSource *level, int x, int y, int z, int dir) 228{ 229 return (level->getData(x, y, z) & 8) > 0 ? Redstone::SIGNAL_MAX : Redstone::SIGNAL_NONE; 230} 231 232int ButtonTile::getDirectSignal(LevelSource *level, int x, int y, int z, int dir) 233{ 234 int data = level->getData(x, y, z); 235 if ((data & 8) == 0) return Redstone::SIGNAL_NONE; 236 int myDir = data & 7; 237 238 if (myDir == 5 && dir == 1) return Redstone::SIGNAL_MAX; 239 if (myDir == 4 && dir == 2) return Redstone::SIGNAL_MAX; 240 if (myDir == 3 && dir == 3) return Redstone::SIGNAL_MAX; 241 if (myDir == 2 && dir == 4) return Redstone::SIGNAL_MAX; 242 if (myDir == 1 && dir == 5) return Redstone::SIGNAL_MAX; 243 244 return false; 245} 246 247bool ButtonTile::isSignalSource() 248{ 249 return true; 250} 251 252void ButtonTile::tick(Level *level, int x, int y, int z, Random *random) 253{ 254 if (level->isClientSide) return; 255 int data = level->getData(x, y, z); 256 if ((data & 8) == 0) 257 { 258 return; 259 } 260 if(sensitive) 261 { 262 checkPressed(level, x, y, z); 263 } 264 else 265 { 266 level->setData(x, y, z, data & 7, Tile::UPDATE_ALL); 267 268 int dir = data & 7; 269 updateNeighbours(level, x, y, z, dir); 270 271 level->playSound(x + 0.5, y + 0.5, z + 0.5, eSoundType_RANDOM_CLICK, 0.3f, 0.5f); 272 level->setTilesDirty(x, y, z, x, y, z); 273 } 274} 275 276void ButtonTile::updateDefaultShape() 277{ 278 float x = 3 / 16.0f; 279 float y = 2 / 16.0f; 280 float z = 2 / 16.0f; 281 setShape(0.5f - x, 0.5f - y, 0.5f - z, 0.5f + x, 0.5f + y, 0.5f + z); 282} 283 284void ButtonTile::entityInside(Level *level, int x, int y, int z, shared_ptr<Entity> entity) 285{ 286 if (level->isClientSide) return; 287 if (!sensitive) return; 288 289 if ((level->getData(x, y, z) & 8) != 0) 290 { 291 return; 292 } 293 294 checkPressed(level, x, y, z); 295} 296 297void ButtonTile::checkPressed(Level *level, int x, int y, int z) 298{ 299 int data = level->getData(x, y, z); 300 int dir = data & 7; 301 bool wasPressed = (data & 8) != 0; 302 bool shouldBePressed; 303 304 updateShape(data); 305 Tile::ThreadStorage *tls = (Tile::ThreadStorage *)TlsGetValue(Tile::tlsIdxShape); 306 vector<shared_ptr<Entity> > *entities = level->getEntitiesOfClass(typeid(Arrow), AABB::newTemp(x + tls->xx0, y + tls->yy0, z + tls->zz0, x + tls->xx1, y + tls->yy1, z + tls->zz1)); 307 shouldBePressed = !entities->empty(); 308 delete entities; 309 310 if (shouldBePressed && !wasPressed) 311 { 312 level->setData(x, y, z, dir | 8, Tile::UPDATE_ALL); 313 updateNeighbours(level, x, y, z, dir); 314 level->setTilesDirty(x, y, z, x, y, z); 315 316 level->playSound(x + 0.5, y + 0.5, z + 0.5, eSoundType_RANDOM_CLICK, 0.3f, 0.6f); 317 } 318 if (!shouldBePressed && wasPressed) 319 { 320 level->setData(x, y, z, dir, Tile::UPDATE_ALL); 321 updateNeighbours(level, x, y, z, dir); 322 level->setTilesDirty(x, y, z, x, y, z); 323 324 level->playSound(x + 0.5, y + 0.5, z + 0.5, eSoundType_RANDOM_CLICK, 0.3f, 0.5f); 325 } 326 327 if (shouldBePressed) 328 { 329 level->addToTickNextTick(x, y, z, id, getTickDelay(level)); 330 } 331} 332 333void ButtonTile::updateNeighbours(Level *level, int x, int y, int z, int dir) 334{ 335 level->updateNeighborsAt(x, y, z, id); 336 337 if (dir == 1) 338 { 339 level->updateNeighborsAt(x - 1, y, z, id); 340 } 341 else if (dir == 2) 342 { 343 level->updateNeighborsAt(x + 1, y, z, id); 344 } 345 else if (dir == 3) 346 { 347 level->updateNeighborsAt(x, y, z - 1, id); 348 } 349 else if (dir == 4) 350 { 351 level->updateNeighborsAt(x, y, z + 1, id); 352 } 353 else 354 { 355 level->updateNeighborsAt(x, y - 1, z, id); 356 } 357} 358 359bool ButtonTile::shouldTileTick(Level *level, int x,int y,int z) 360{ 361 int currentData = level->getData(x, y, z); 362 return (currentData & 8) != 0; 363} 364 365void ButtonTile::registerIcons(IconRegister *iconRegister) 366{ 367 // None 368}