aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/SignTileEntity.cpp
blob: 598621eb5abb334be3a01b2e548af6da42f90bb7 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "stdafx.h"
#include "com.mojang.nbt.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.entity.item.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.world.entity.h"
#include "net.minecraft.world.phys.h"
#include "net.minecraft.network.packet.h"
#include "SignTileEntity.h"
#include <xuiapp.h>
#include "..\Minecraft.Client\ClientConnection.h"
#include "..\Minecraft.Client\Minecraft.h"
#include "..\Minecraft.Client\ServerLevel.h"
#include "..\Minecraft.World\Level.h"



const int SignTileEntity::MAX_LINE_LENGTH = 15;

SignTileEntity::SignTileEntity() : TileEntity()
{
	m_wsmessages[0] = L"";
	m_wsmessages[1] = L"";
	m_wsmessages[2] = L"";
	m_wsmessages[3] = L"";
	m_bVerified=true;
	m_bCensored=false;

	m_iSelectedLine = -1;

	_isEditable = true;

	playerWhoMayEdit = nullptr;
}

SignTileEntity::~SignTileEntity()
{
	// TODO ORBIS_STUBBED;
#ifndef __ORBIS__
	// 4J-PB - we don't need to verify strings anymore - InputManager.CancelQueuedVerifyStrings(&SignTileEntity::StringVerifyCallback,(LPVOID)this);
#endif
}

void SignTileEntity::save(CompoundTag *tag) 
{
	TileEntity::save(tag);
	tag->putString(L"Text1", m_wsmessages[0] );
	tag->putString(L"Text2", m_wsmessages[1] );
	tag->putString(L"Text3", m_wsmessages[2] );
	tag->putString(L"Text4", m_wsmessages[3] );
#ifndef _CONTENT_PACKAGE
	OutputDebugStringW(L"### - Saving a sign with text - \n");
	for(int i=0;i<4;i++)
	{
		OutputDebugStringW(m_wsmessages[i].c_str());
		OutputDebugStringW(L"\n");
	}
#endif
}

void SignTileEntity::load(CompoundTag *tag)
{
	_isEditable = false;
	TileEntity::load(tag);
	for (int i = 0; i < MAX_SIGN_LINES; i++) 
	{
		wchar_t *buf = new wchar_t[256];
		swprintf(buf, 256, L"Text%d", (i+1) );
		m_wsmessages[i] = tag->getString( buf );
		if (m_wsmessages[i].length() > MAX_LINE_LENGTH) m_wsmessages[i] = m_wsmessages[i].substr(0, MAX_LINE_LENGTH);
	}
#ifndef _CONTENT_PACKAGE
	OutputDebugStringW(L"### - Loaded a sign with text - \n");
	for(int i=0;i<4;i++)
	{
		OutputDebugStringW(m_wsmessages[i].c_str());
		OutputDebugStringW(L"\n");
	}
#endif

	// 4J Stu - Fix for #13531 - Bug: Signs do not Censor after loading a save
	// Set verified as false so that it can be re-verified
	m_bVerified=false;

	setChanged();
}

shared_ptr<Packet> SignTileEntity::getUpdatePacket()
{
	wstring copy[MAX_SIGN_LINES];
	for (int i = 0; i < MAX_SIGN_LINES; i++) 
	{
		copy[i] = m_wsmessages[i];
	}
	return std::make_shared<SignUpdatePacket>(x, y, z, m_bVerified, m_bCensored, copy);
}

bool SignTileEntity::isEditable() 
{
	return _isEditable;
}

void SignTileEntity::setEditable(bool isEditable)
{
	this->_isEditable = isEditable;
	if (!isEditable)
	{
		playerWhoMayEdit = nullptr;
	}
}

void SignTileEntity::setAllowedPlayerEditor(shared_ptr<Player> player)
{
	playerWhoMayEdit = player;
}

shared_ptr<Player> SignTileEntity::getPlayerWhoMayEdit()
{
	return playerWhoMayEdit;
}

void SignTileEntity::setChanged()
{
	Minecraft *pMinecraft=Minecraft::GetInstance();

	// 4J-PB - For TU14 we are allowed to not verify strings anymore !
	m_bVerified=true;
	/*
	if(!g_NetworkManager.IsLocalGame() && !m_bVerified)
	//if (pMinecraft->level->isClientSide)
	{
		WCHAR *wcMessages[MAX_SIGN_LINES];
		for (int i = 0; i < MAX_SIGN_LINES; ++i) 
		{
			wcMessages[i]=new WCHAR [MAX_LINE_LENGTH+1];
			ZeroMemory(wcMessages[i],sizeof(WCHAR)*(MAX_LINE_LENGTH+1));	
			if(m_wsmessages[i].length()>0)
			{
				memcpy(wcMessages[i],m_wsmessages[i].c_str(),m_wsmessages[i].length()*sizeof(WCHAR));
			}
		}
		// at this point, we can ask the online string verifier if our sign text is ok
#ifdef __ORBIS__
			m_bVerified=true;
#else

		if(!InputManager.VerifyStrings((WCHAR**)&wcMessages,MAX_SIGN_LINES,&SignTileEntity::StringVerifyCallback,(LPVOID)this))
		{
			// Nothing to verify
			m_bVerified=true;
		}
		for(unsigned int i = 0; i < MAX_SIGN_LINES; ++i)
		{
			delete [] wcMessages[i];
		}
#endif
	}
	else
	{
		// set the sign to allowed (local game)
		m_bVerified=true;
	}
	*/
}


void SignTileEntity::SetMessage(int iIndex,wstring &wsText) 
{
	if (wsText.length() > MAX_LINE_LENGTH)  // MAX_LINE_LENGTH == 15
    {
        wsText = wsText.substr(0, MAX_LINE_LENGTH);
#ifdef _DEBUG
        OutputDebugStringW(L"Sign text truncated to 15 characters\n");
#endif
    }
	m_wsmessages[iIndex]=wsText;
}

// 4J-PB - added for string verification
int SignTileEntity::StringVerifyCallback(LPVOID lpParam,STRING_VERIFY_RESPONSE *pResults)
{
	// results will be in m_pStringVerifyResponse
	SignTileEntity *pClass=static_cast<SignTileEntity *>(lpParam);

	pClass->m_bVerified=true;
	pClass->m_bCensored=false;
	for(int i=0;i<pResults->wNumStrings;i++)
	{
		if(pResults->pStringResult[i]!=ERROR_SUCCESS)
		{
			pClass->m_bCensored=true;
		}
	}

	if(!pClass->level->isClientSide)
	{
		ServerLevel *serverLevel = static_cast<ServerLevel *>(pClass->level);
		// 4J Stu - This callback gets called on the main thread, but tried to access things on the server thread. Change to go through the protected method.
		//pClass->level->sendTileUpdated(pClass->x, pClass->y, pClass->z);
		serverLevel->queueSendTileUpdate(pClass->x, pClass->y, pClass->z);
	}

	return 0;
}

// 4J Added
shared_ptr<TileEntity> SignTileEntity::clone()
{
	shared_ptr<SignTileEntity> result = std::make_shared<SignTileEntity>();
	TileEntity::clone(result);

	result->m_wsmessages[0] = m_wsmessages[0];
	result->m_wsmessages[1] = m_wsmessages[1];
	result->m_wsmessages[2] = m_wsmessages[2];
	result->m_wsmessages[3] = m_wsmessages[3];
	result->m_bVerified = m_bVerified;
	result->m_bCensored = m_bCensored;
	return result;
}