aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/RepairResultSlot.cpp
blob: 87d2aab6e9d779b46f052b4da25f9194d11a5111 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "stdafx.h"
#include "net.minecraft.world.inventory.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.tile.h"
#include "net.minecraft.world.entity.player.h"
#include "RepairResultSlot.h"

RepairResultSlot::RepairResultSlot(AnvilMenu *menu, int xt, int yt, int zt, shared_ptr<Container> container, int slot, int x, int y) : Slot(container, slot, x, y)
{
	m_menu = menu;
	this->xt = xt;
	this->yt = yt;
	this->zt = zt;
}

bool RepairResultSlot::mayPlace(shared_ptr<ItemInstance> item)
{
	return false;
}

bool RepairResultSlot::mayPickup(shared_ptr<Player> player)
{
	return (player->abilities.instabuild || player->experienceLevel >= m_menu->cost) && (m_menu->cost > 0 && hasItem());
}

void RepairResultSlot::onTake(shared_ptr<Player> player, shared_ptr<ItemInstance> carried)
{
	if (!player->abilities.instabuild) player->giveExperienceLevels(-m_menu->cost);
	m_menu->repairSlots->setItem(AnvilMenu::INPUT_SLOT, nullptr);
	if (m_menu->repairItemCountCost > 0)
	{
		shared_ptr<ItemInstance> addition = m_menu->repairSlots->getItem(AnvilMenu::ADDITIONAL_SLOT);
		if (addition != nullptr && addition->count > m_menu->repairItemCountCost)
		{
			addition->count -= m_menu->repairItemCountCost;
			m_menu->repairSlots->setItem(AnvilMenu::ADDITIONAL_SLOT, addition);
		}
		else
		{
			m_menu->repairSlots->setItem(AnvilMenu::ADDITIONAL_SLOT, nullptr);
		}
	}
	else
	{
		m_menu->repairSlots->setItem(AnvilMenu::ADDITIONAL_SLOT, nullptr);
	}
	m_menu->cost = 0;

	if (!player->abilities.instabuild && !m_menu->level->isClientSide && m_menu->level->getTile(xt, yt, zt) == Tile::anvil->id && player->getRandom()->nextFloat() < 0.12f)
	{
		int data = m_menu->level->getData(xt, yt, zt);
		int dir = data & 0x3;
		int dmg = data >> 2;

		if (++dmg > 2)
		{
			m_menu->level->removeTile(xt, yt, zt);
			m_menu->level->levelEvent(LevelEvent::SOUND_ANVIL_BROKEN, xt, yt, zt, 0);
		}
		else
		{
			m_menu->level->setData(xt, yt, zt, dir | (dmg << 2), Tile::UPDATE_CLIENTS);
			m_menu->level->levelEvent(LevelEvent::SOUND_ANVIL_USED, xt, yt, zt, 0);
		}
	}
	else if (!m_menu->level->isClientSide)
	{
		m_menu->level->levelEvent(LevelEvent::SOUND_ANVIL_USED, xt, yt, zt, 0);
	}
}

bool RepairResultSlot::mayCombine(shared_ptr<ItemInstance> second)
{
	return false;
}