Files
neoLegacy/Minecraft.World/DoorItem.cpp
T
Tranqlmao ff562d1644 TU46 Test content and patches
TU46 Test content, added missing commits by amending commit and cherry-picking to new branch, ready for iggy guis and tu46 bug fixes
2026-10-22 13:55:39 -04:00

86 lines
3.4 KiB
C++

using namespace std;
#include "stdafx.h"
#include "net.minecraft.world.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.tile.h"
#include "net.minecraft.world.phys.h"
#include "net.minecraft.world.item.h"
#include "Facing.h"
#include "Material.h"
#include "GenericStats.h"
#include "DoorItem.h"
DoorItem::DoorItem(int id, Material *material, const wstring& doorType) : Item(id)
{
this->material = material;
this->doorType = doorType;
maxStackSize = 64;
}
bool DoorItem::useOn(shared_ptr<ItemInstance> instance, shared_ptr<Player> player, Level *level, int x, int y, int z, int face, float clickX, float clickY, float clickZ, bool bTestUseOnOnly)
{
if (face != Facing::UP) return false;
y++;
Tile *tile;
if (doorType == L"doorWood") tile = Tile::wooden_door;
else if (doorType == L"doorIron") tile = Tile::iron_door;
else if (doorType == L"doorSpruce") tile = Tile::spruce_door;
else if (doorType == L"doorBirch") tile = Tile::birch_door;
else if (doorType == L"doorJungle") tile = Tile::jungle_door;
else if (doorType == L"doorAcacia") tile = Tile::acacia_door;
else if (doorType == L"doorDark") tile = Tile::dark_oak_door;
if (!player->mayUseItemAt(x, y, z, face, instance) || !player->mayUseItemAt(x, y + 1, z, face, instance)) return false;
if (!tile->mayPlace(level, x, y, z)) return false;
// 4J-PB - Adding a test only version to allow tooltips to be displayed
if(bTestUseOnOnly) return true;
// 4J-JEV: Hook for durango 'BlockPlaced' event.
player->awardStat(GenericStats::blocksPlaced(tile->id),GenericStats::param_blocksPlaced(tile->id,instance->getAuxValue(),1));
int dir = Mth::floor(((player->yRot + 180) * 4) / 360 - 0.5) & 3;
int stepX = (dir == 0) ? 1 : (dir == 2) ? -1 : 0;
int stepZ = (dir == 1) ? 1 : (dir == 3) ? -1 : 0;
bool hingeRight = (stepX < 0 && clickZ < 0.5f) || (stepX > 0 && clickZ > 0.5f) || (stepZ < 0 && clickX > 0.5f) || (stepZ > 0 && clickX < 0.5f);
place(level, x, y, z, dir, tile, hingeRight);
level->playSound(x + 0.5f, y + 0.5f, z + 0.5f, tile->soundType->getPlaceSound(), (tile->soundType->getVolume() + 1) / 2, tile->soundType->getPitch() * 0.8f);
instance->count--;
return true;
}
void DoorItem::place(Level *level, int x, int y, int z, int dir, Tile *tile, bool hingeRight)
{
int xra = 0;
int zra = 0;
if (dir == 0) zra = +1;
if (dir == 1) xra = -1;
if (dir == 2) zra = -1;
if (dir == 3) xra = +1;
int solidLeft = (level->isSolidBlockingTile(x - xra, y, z - zra) ? 1 : 0) + (level->isSolidBlockingTile(x - xra, y + 1, z - zra) ? 1 : 0);
int solidRight = (level->isSolidBlockingTile(x + xra, y, z + zra) ? 1 : 0) + (level->isSolidBlockingTile(x + xra, y + 1, z + zra) ? 1 : 0);
bool doorLeft = (level->getTile(x - xra, y, z - zra) == tile->id) || (level->getTile(x - xra, y + 1, z - zra) == tile->id);
bool doorRight = (level->getTile(x + xra, y, z + zra) == tile->id) || (level->getTile(x + xra, y + 1, z + zra) == tile->id);
bool flip = hingeRight;
if ((doorLeft && !doorRight) || solidRight > solidLeft) flip = true;
else if ((doorRight && !doorLeft) || solidRight < solidLeft) flip = false;
level->setTileAndData(x, y, z, tile->id, dir, Tile::UPDATE_CLIENTS);
level->setTileAndData(x, y + 1, z, tile->id, 8 | (flip ? 1 : 0), Tile::UPDATE_CLIENTS);
level->updateNeighborsAt(x, y, z, tile->id);
level->updateNeighborsAt(x, y + 1, z, tile->id);
}