the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 135 lines 4.0 kB view raw
1#include "stdafx.h" 2#include "net.minecraft.world.entity.player.h" 3#include "net.minecraft.world.entity.item.h" 4#include "net.minecraft.world.level.h" 5#include "net.minecraft.world.level.tile.h" 6#include "net.minecraft.world.phys.h" 7#include "ItemInstance.h" 8#include "BoatItem.h" 9 10BoatItem::BoatItem(int id) : Item( id ) 11{ 12 maxStackSize = 1; 13} 14 15bool BoatItem::TestUse(shared_ptr<ItemInstance> itemInstance, Level *level, shared_ptr<Player> player) 16{ 17 // 4J-PB - added for tooltips to test use 18 // 4J TODO really we should have the crosshair hitresult telling us if it hit water, and at what distance, so we don't need to do this again 19 // if the player happens to have a boat in their hand 20 21 float xRot = player->xRotO + (player->xRot - player->xRotO); 22 float yRot = player->yRotO + (player->yRot - player->yRotO); 23 24 double x = player->xo + (player->x - player->xo); 25 double y = player->yo + (player->y - player->yo) + 1.62 - player->heightOffset; 26 double z = player->zo + (player->z - player->zo); 27 28 Vec3 *from = Vec3::newTemp(x, y, z); 29 30 float yCos = Mth::cos(-yRot * Mth::RAD_TO_GRAD - PI); 31 float ySin = Mth::sin(-yRot * Mth::RAD_TO_GRAD - PI); 32 float xCos = -Mth::cos(-xRot * Mth::RAD_TO_GRAD); 33 float xSin = Mth::sin(-xRot * Mth::RAD_TO_GRAD); 34 35 float xa = ySin * xCos; 36 float ya = xSin; 37 float za = yCos * xCos; 38 39 double range = 5; 40 Vec3 *to = from->add(xa * range, ya * range, za * range); 41 HitResult *hr = level->clip(from, to, true); 42 if (hr == NULL) return false; 43 44 if (hr->type == HitResult::TILE) 45 { 46 delete hr; 47 return true; 48 } 49 delete hr; 50 return false; 51} 52shared_ptr<ItemInstance> BoatItem::use(shared_ptr<ItemInstance> itemInstance, Level *level, shared_ptr<Player> player) 53{ 54 float a = 1; 55 56 float xRot = player->xRotO + (player->xRot - player->xRotO) * a; 57 float yRot = player->yRotO + (player->yRot - player->yRotO) * a; 58 59 double x = player->xo + (player->x - player->xo) * a; 60 double y = player->yo + (player->y - player->yo) * a + 1.62 - player->heightOffset; 61 double z = player->zo + (player->z - player->zo) * a; 62 63 Vec3 *from = Vec3::newTemp(x, y, z); 64 65 float yCos = Mth::cos(-yRot * Mth::RAD_TO_GRAD - PI); 66 float ySin = Mth::sin(-yRot * Mth::RAD_TO_GRAD - PI); 67 float xCos = -Mth::cos(-xRot * Mth::RAD_TO_GRAD); 68 float xSin = Mth::sin(-xRot * Mth::RAD_TO_GRAD); 69 70 float xa = ySin * xCos; 71 float ya = xSin; 72 float za = yCos * xCos; 73 74 double range = 5; 75 Vec3 *to = from->add(xa * range, ya * range, za * range); 76 HitResult *hr = level->clip(from, to, true); 77 if (hr == NULL) return itemInstance; 78 79 // check entity collision 80 Vec3 *b = player->getViewVector(a); 81 bool hitEntity = false; 82 float overlap = 1; 83 vector<shared_ptr<Entity> > *objects = level->getEntities(player, player->bb->expand(b->x * (range), b->y * (range), b->z * (range))->grow(overlap, overlap, overlap)); 84 //for (int i = 0; i < objects.size(); i++) { 85 for(AUTO_VAR(it, objects->begin()); it != objects->end(); ++it) 86 { 87 shared_ptr<Entity> e = *it; //objects.get(i); 88 if (!e->isPickable()) continue; 89 90 float rr = e->getPickRadius(); 91 AABB *bb = e->bb->grow(rr, rr, rr); 92 if (bb->contains(from)) 93 { 94 hitEntity = true; 95 } 96 } 97 if (hitEntity) 98 { 99 return itemInstance; 100 } 101 102 if (hr->type == HitResult::TILE) 103 { 104 int xt = hr->x; 105 int yt = hr->y; 106 int zt = hr->z; 107 108 if (level->getTile(xt, yt, zt) == Tile::topSnow_Id) yt--; 109 if( level->countInstanceOf(eTYPE_BOAT, true) < Level::MAX_XBOX_BOATS ) // 4J - added limit 110 { 111 shared_ptr<Boat> boat = shared_ptr<Boat>( new Boat(level, xt + 0.5f, yt + 1.0f, zt + 0.5f) ); 112 boat->yRot = ((Mth::floor(player->yRot * 4.0F / 360.0F + 0.5) & 0x3) - 1) * 90; 113 if (!level->getCubes(boat, boat->bb->grow(-.1, -.1, -.1))->empty()) 114 { 115 return itemInstance; 116 } 117 if (!level->isClientSide) 118 { 119 level->addEntity(boat); 120 } 121 if (!player->abilities.instabuild) 122 { 123 itemInstance->count--; 124 } 125 } 126 else 127 { 128 // display a message to say max boats has been hit 129 player->displayClientMessage(IDS_MAX_BOATS ); 130 } 131 } 132 delete hr; 133 134 return itemInstance; 135}