pastebin

Paste #omH -- näytä pelkkänä tekstinä -- uusi tämän pohjalta

Värjäys: Tyyli: ensimmäinen rivinumero: Tabin korvaus:

  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
From 8831669505905dd9cd415711063f705d8e7ce02c Mon Sep 17 00:00:00 2001
From: Perttu Ahola <celeron55@gmail.com>
Date: Sun, 4 Aug 2013 00:45:49 +0300
Subject: [PATCH] Allow mods to listen to cheat detections using
 minetest.register_on_cheat()

---
 builtin/misc_register.lua       |  1 +
 doc/lua_api.txt                 |  3 +++
 src/content_sao.cpp             |  5 ++++-
 src/content_sao.h               |  3 ++-
 src/script/cpp_api/s_player.cpp | 16 ++++++++++++++++
 src/script/cpp_api/s_player.h   |  1 +
 src/server.cpp                  | 14 +++++++++++++-
 7 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/builtin/misc_register.lua b/builtin/misc_register.lua
index 2d25568..d5e0867 100644
--- a/builtin/misc_register.lua
+++ b/builtin/misc_register.lua
@@ -319,4 +319,5 @@ minetest.registered_on_respawnplayers, minetest.register_on_respawnplayer = make
 minetest.registered_on_joinplayers, minetest.register_on_joinplayer = make_registration()
 minetest.registered_on_leaveplayers, minetest.register_on_leaveplayer = make_registration()
 minetest.registered_on_player_receive_fields, minetest.register_on_player_receive_fields = make_registration_reverse()
+minetest.registered_on_cheats, minetest.register_on_cheat = make_registration()
 
diff --git a/doc/lua_api.txt b/doc/lua_api.txt
index 67ff823..d345887 100644
--- a/doc/lua_api.txt
+++ b/doc/lua_api.txt
@@ -1142,6 +1142,9 @@ minetest.register_on_joinplayer(func(ObjectRef))
 ^ Called when a player joins the game
 minetest.register_on_leaveplayer(func(ObjectRef))
 ^ Called when a player leaves the game
+minetest.register_on_cheat(func(ObjectRef, cheat))
+^ Called when a player cheats
+^ cheat: {type="moved_too_fast"/"interacted_too_far"/"finished_unknown_dig"/"dug_unbreakable"/"dug_too_fast"}
 minetest.register_on_chat_message(func(name, message))
 ^ Called always when a player says something
 minetest.register_on_player_receive_fields(func(player, formname, fields))
diff --git a/src/content_sao.cpp b/src/content_sao.cpp
index 8d46d42..799c279 100644
--- a/src/content_sao.cpp
+++ b/src/content_sao.cpp
@@ -1454,8 +1454,9 @@ void PlayerSAO::disconnected()
 	return gob_cmd_set_properties(m_prop);
 }
 
-void PlayerSAO::checkMovementCheat()
+bool PlayerSAO::checkMovementCheat()
 {
+	bool cheated = false;
 	if(isAttached() || m_is_singleplayer ||
 			g_settings->getBool("disable_anticheat"))
 	{
@@ -1503,8 +1504,10 @@ void PlayerSAO::checkMovementCheat()
 					<<std::endl;
 			m_player->setPosition(m_last_good_position);
 			m_moved = true;
+			cheated = true;
 		}
 	}
+	return cheated;
 }
 
 bool PlayerSAO::getCollisionBox(aabb3f *toset) {
diff --git a/src/content_sao.h b/src/content_sao.h
index 9640e5f..413fd3e 100644
--- a/src/content_sao.h
+++ b/src/content_sao.h
@@ -262,7 +262,8 @@ class PlayerSAO : public ServerActiveObject
 	{
 		return m_dig_pool;
 	}
-	void checkMovementCheat();
+	// Returns true if cheated
+	bool checkMovementCheat();
 
 	// Other
 
diff --git a/src/script/cpp_api/s_player.cpp b/src/script/cpp_api/s_player.cpp
index e736d74..0dbd525 100644
--- a/src/script/cpp_api/s_player.cpp
+++ b/src/script/cpp_api/s_player.cpp
@@ -81,6 +81,22 @@ void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
 	runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
 }
 
+void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
+		const std::string &cheat_type)
+{
+	SCRIPTAPI_PRECHECKHEADER
+
+	// Get minetest.registered_on_cheats
+	lua_getglobal(L, "minetest");
+	lua_getfield(L, -1, "registered_on_cheats");
+	// Call callbacks
+	objectrefGetOrCreate(player);
+	lua_newtable(L);
+	lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
+	lua_setfield(L, -2, "type");
+	runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
+}
+
 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
 		const std::string &formname,
 		const std::map<std::string, std::string> &fields)
diff --git a/src/script/cpp_api/s_player.h b/src/script/cpp_api/s_player.h
index 663e3c2..c0409a4 100644
--- a/src/script/cpp_api/s_player.h
+++ b/src/script/cpp_api/s_player.h
@@ -34,6 +34,7 @@ class ScriptApiPlayer
 	bool on_respawnplayer(ServerActiveObject *player);
 	void on_joinplayer(ServerActiveObject *player);
 	void on_leaveplayer(ServerActiveObject *player);
+	void on_cheat(ServerActiveObject *player, const std::string &cheat_type);
 
 	void on_playerReceiveFields(ServerActiveObject *player,
 			const std::string &formname,
diff --git a/src/server.cpp b/src/server.cpp
index cdd42c2..7527f17 100644
--- a/src/server.cpp
+++ b/src/server.cpp
@@ -2251,7 +2251,11 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
 		player->control.LMB = (bool)(keyPressed&128);
 		player->control.RMB = (bool)(keyPressed&256);
 
-		playersao->checkMovementCheat();
+		bool cheated = playersao->checkMovementCheat();
+		if(cheated){
+			// Call callbacks
+			m_script->on_cheat(playersao, "moved_too_fast");
+		}
 
 		/*infostream<<"Server::ProcessData(): Moved player "<<peer_id<<" to "
 				<<"("<<position.X<<","<<position.Y<<","<<position.Z<<")"
@@ -2811,6 +2815,8 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
 				RemoteClient *client = getClient(peer_id);
 				v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_under, BS));
 				client->SetBlockNotSent(blockpos);
+				// Call callbacks
+				m_script->on_cheat(playersao, "interacted_too_far");
 				// Do nothing else
 				return;
 			}
@@ -2939,6 +2945,8 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
 								<<PP(nocheat_p)<<" and completed digging "
 								<<PP(p_under)<<"; not digging."<<std::endl;
 						is_valid_dig = false;
+						// Call callbacks
+						m_script->on_cheat(playersao, "finished_unknown_dig");
 					}
 					// Get player's wielded item
 					ItemStack playeritem;
@@ -2964,6 +2972,8 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
 								<<", which is not diggable with tool. not digging."
 								<<std::endl;
 						is_valid_dig = false;
+						// Call callbacks
+						m_script->on_cheat(playersao, "dug_unbreakable");
 					}
 					// Check digging time
 					// If already invalidated, we don't have to
@@ -2987,6 +2997,8 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
 								<<" completed digging "<<PP(p_under)
 								<<"too fast; not digging."<<std::endl;
 						is_valid_dig = false;
+						// Call callbacks
+						m_script->on_cheat(playersao, "dug_too_fast");
 					}
 				}
 
-- 
1.8.3.1