aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/Common/GameRules/UpdatePlayerRuleDefinition.cpp
blob: 99aee99b05a344dc7a8004b71bc9428e8c0c2f53 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "stdafx.h"
#include "UpdatePlayerRuleDefinition.h"
#include "ConsoleGameRules.h"
#include "..\..\..\Minecraft.World\Pos.h"
#include "..\..\..\Minecraft.World\StringHelpers.h"
#include "..\..\..\Minecraft.World\net.minecraft.world.entity.player.h"
#include "..\..\..\Minecraft.World\net.minecraft.world.food.h"
#include "..\..\..\Minecraft.World\net.minecraft.world.item.h"

UpdatePlayerRuleDefinition::UpdatePlayerRuleDefinition()
{
	m_bUpdateHealth = m_bUpdateFood = m_bUpdateYRot = false;;
	m_health = 0;
	m_food = 0;
	m_spawnPos = nullptr;
	m_yRot = 0.0f;
}

UpdatePlayerRuleDefinition::~UpdatePlayerRuleDefinition()
{
	for(auto& item : m_items)
	{
		delete item;
	}
}

void UpdatePlayerRuleDefinition::writeAttributes(DataOutputStream *dos, UINT numAttributes)
{
	int attrCount = 3;
	if(m_bUpdateHealth) ++attrCount;
	if(m_bUpdateFood) ++attrCount;
	if(m_bUpdateYRot) ++attrCount;
	GameRuleDefinition::writeAttributes(dos, numAttributes + attrCount );

	ConsoleGameRules::write(dos, ConsoleGameRules::eGameRuleAttr_spawnX);
	dos->writeUTF(std::to_wstring(m_spawnPos->x));
	ConsoleGameRules::write(dos, ConsoleGameRules::eGameRuleAttr_spawnY);
	dos->writeUTF(std::to_wstring(m_spawnPos->y));
	ConsoleGameRules::write(dos, ConsoleGameRules::eGameRuleAttr_spawnZ);
	dos->writeUTF(std::to_wstring(m_spawnPos->z));

	if(m_bUpdateYRot)
	{
		ConsoleGameRules::write(dos, ConsoleGameRules::eGameRuleAttr_yRot);
		dos->writeUTF(std::to_wstring(m_yRot));
	}
	if(m_bUpdateHealth)
	{
		ConsoleGameRules::write(dos, ConsoleGameRules::eGameRuleAttr_food);
		dos->writeUTF(std::to_wstring(m_health));
	}
	if(m_bUpdateFood)
	{
		ConsoleGameRules::write(dos, ConsoleGameRules::eGameRuleAttr_health);
		dos->writeUTF(std::to_wstring(m_food));
	}
}

void UpdatePlayerRuleDefinition::getChildren(vector<GameRuleDefinition *> *children)
{
	GameRuleDefinition::getChildren(children);
	for(auto& item : m_items)
		children->push_back(item);
}

GameRuleDefinition *UpdatePlayerRuleDefinition::addChild(ConsoleGameRules::EGameRuleType ruleType)
{
	GameRuleDefinition *rule = nullptr;
	if(ruleType == ConsoleGameRules::eGameRuleType_AddItem)
	{
		rule = new AddItemRuleDefinition();
		m_items.push_back(static_cast<AddItemRuleDefinition *>(rule));
	}
	else
	{
#ifndef _CONTENT_PACKAGE
		wprintf(L"UpdatePlayerRuleDefinition: Attempted to add invalid child rule - %d\n", ruleType );
#endif
	}
	return rule;
}

void UpdatePlayerRuleDefinition::addAttribute(const wstring &attributeName, const wstring &attributeValue)
{
	if(attributeName.compare(L"spawnX") == 0)
	{
		if(m_spawnPos == nullptr) m_spawnPos = new Pos();
		int value = _fromString<int>(attributeValue);
		m_spawnPos->x = value;
		app.DebugPrintf("UpdatePlayerRuleDefinition: Adding parameter spawnX=%d\n",value);
	}
	else if(attributeName.compare(L"spawnY") == 0)
	{
		if(m_spawnPos == nullptr) m_spawnPos = new Pos();
		int value = _fromString<int>(attributeValue);
		m_spawnPos->y = value;
		app.DebugPrintf("UpdatePlayerRuleDefinition: Adding parameter spawnY=%d\n",value);
	}
	else if(attributeName.compare(L"spawnZ") == 0)
	{
		if(m_spawnPos == nullptr) m_spawnPos = new Pos();
		int value = _fromString<int>(attributeValue);
		m_spawnPos->z = value;
		app.DebugPrintf("UpdatePlayerRuleDefinition: Adding parameter spawnZ=%d\n",value);
	}
	else if(attributeName.compare(L"health") == 0)
	{
		int value = _fromString<int>(attributeValue);
		m_health = value;
		m_bUpdateHealth = true;
		app.DebugPrintf("UpdatePlayerRuleDefinition: Adding parameter health=%d\n",value);
	}
	else if(attributeName.compare(L"food") == 0)
	{
		int value = _fromString<int>(attributeValue);
		m_food = value;
		m_bUpdateFood = true;
		app.DebugPrintf("UpdatePlayerRuleDefinition: Adding parameter health=%d\n",value);
	}
	else if(attributeName.compare(L"yRot") == 0)
	{
		float value = _fromString<float>(attributeValue);
		m_yRot = value;
		m_bUpdateYRot = true;
		app.DebugPrintf("UpdatePlayerRuleDefinition: Adding parameter yRot=%f\n",value);
	}
	else
	{
		GameRuleDefinition::addAttribute(attributeName, attributeValue);
	}
}

void UpdatePlayerRuleDefinition::postProcessPlayer(shared_ptr<Player> player)
{
	if(m_bUpdateHealth)
	{
		player->lastHealth = m_health;
		player->setHealth(m_health);
	}

	if(m_bUpdateFood)
	{
		player->getFoodData()->setFoodLevel(m_food);
	}

	double x = player->x;
	double y = player->y;
	double z = player->z;
	float yRot = player->yRot;
	float xRot = player->xRot;
	if(m_spawnPos != nullptr)
	{
		x = m_spawnPos->x;
		y = m_spawnPos->y;
		z = m_spawnPos->z;
	}

	if(m_bUpdateYRot)
	{
		yRot = m_yRot;
	}

	if(m_spawnPos != nullptr || m_bUpdateYRot) player->absMoveTo(x,y,z,yRot,xRot);

	for(auto& addItem : m_items)
	{
		addItem->addItemToContainer(player->inventory, -1);
	}
}