@@ -187,11 +187,17 @@ void UnitSAO::setAttachment(int parent_id, const std::string &bone, v3f position
187
187
// This breaks some things so we also give the server the most accurate representation
188
188
// even if players only see the client changes.
189
189
190
+ int old_parent = m_attachment_parent_id;
190
191
m_attachment_parent_id = parent_id;
191
192
m_attachment_bone = bone;
192
193
m_attachment_position = position;
193
194
m_attachment_rotation = rotation;
194
195
m_attachment_sent = false ;
196
+
197
+ if (parent_id != old_parent) {
198
+ onDetach (old_parent);
199
+ onAttach (parent_id);
200
+ }
195
201
}
196
202
197
203
void UnitSAO::getAttachment (int *parent_id, std::string *bone, v3f *position,
@@ -203,6 +209,30 @@ void UnitSAO::getAttachment(int *parent_id, std::string *bone, v3f *position,
203
209
*rotation = m_attachment_rotation;
204
210
}
205
211
212
+ void UnitSAO::clearChildAttachments ()
213
+ {
214
+ for (int child_id : m_attachment_child_ids) {
215
+ // Child can be NULL if it was deleted earlier
216
+ if (ServerActiveObject *child = m_env->getActiveObject (child_id))
217
+ child->setAttachment (0 , " " , v3f (0 , 0 , 0 ), v3f (0 , 0 , 0 ));
218
+ }
219
+ m_attachment_child_ids.clear ();
220
+ }
221
+
222
+ void UnitSAO::clearParentAttachment ()
223
+ {
224
+ ServerActiveObject *parent = nullptr ;
225
+ if (m_attachment_parent_id) {
226
+ parent = m_env->getActiveObject (m_attachment_parent_id);
227
+ setAttachment (0 , " " , m_attachment_position, m_attachment_rotation);
228
+ } else {
229
+ setAttachment (0 , " " , v3f (0 , 0 , 0 ), v3f (0 , 0 , 0 ));
230
+ }
231
+ // Do it
232
+ if (parent)
233
+ parent->removeAttachmentChild (m_id);
234
+ }
235
+
206
236
void UnitSAO::addAttachmentChild (int child_id)
207
237
{
208
238
m_attachment_child_ids.insert (child_id);
@@ -218,6 +248,38 @@ const std::unordered_set<int> &UnitSAO::getAttachmentChildIds()
218
248
return m_attachment_child_ids;
219
249
}
220
250
251
+ void UnitSAO::onAttach (int parent_id)
252
+ {
253
+ if (!parent_id)
254
+ return ;
255
+
256
+ ServerActiveObject *parent = m_env->getActiveObject (parent_id);
257
+
258
+ if (!parent || parent->isGone ())
259
+ return ; // Do not try to notify soon gone parent
260
+
261
+ if (parent->getType () == ACTIVEOBJECT_TYPE_LUAENTITY) {
262
+ // Call parent's on_attach field
263
+ m_env->getScriptIface ()->luaentity_on_attach_child (parent_id, this );
264
+ }
265
+ }
266
+
267
+ void UnitSAO::onDetach (int parent_id)
268
+ {
269
+ if (!parent_id)
270
+ return ;
271
+
272
+ ServerActiveObject *parent = m_env->getActiveObject (parent_id);
273
+ if (getType () == ACTIVEOBJECT_TYPE_LUAENTITY)
274
+ m_env->getScriptIface ()->luaentity_on_detach (m_id, parent);
275
+
276
+ if (!parent || parent->isGone ())
277
+ return ; // Do not try to notify soon gone parent
278
+
279
+ if (parent->getType () == ACTIVEOBJECT_TYPE_LUAENTITY)
280
+ m_env->getScriptIface ()->luaentity_on_detach_child (parent_id, this );
281
+ }
282
+
221
283
ObjectProperties* UnitSAO::accessObjectProperties ()
222
284
{
223
285
return &m_prop;
@@ -548,10 +610,6 @@ int LuaEntitySAO::punch(v3f dir,
548
610
return 0 ;
549
611
}
550
612
551
- // It's best that attachments cannot be punched
552
- if (isAttached ())
553
- return 0 ;
554
-
555
613
ItemStack *punchitem = NULL ;
556
614
ItemStack punchitem_static;
557
615
if (puncher) {
@@ -588,8 +646,10 @@ int LuaEntitySAO::punch(v3f dir,
588
646
}
589
647
}
590
648
591
- if (getHP () == 0 ) {
649
+ if (getHP () == 0 && ! isGone () ) {
592
650
m_pending_removal = true ;
651
+ clearParentAttachment ();
652
+ clearChildAttachments ();
593
653
m_env->getScriptIface ()->luaentity_on_death (m_id, puncher);
594
654
}
595
655
@@ -600,9 +660,7 @@ void LuaEntitySAO::rightClick(ServerActiveObject *clicker)
600
660
{
601
661
if (!m_registered)
602
662
return ;
603
- // It's best that attachments cannot be clicked
604
- if (isAttached ())
605
- return ;
663
+
606
664
m_env->getScriptIface ()->luaentity_Rightclick (m_id, clicker);
607
665
}
608
666
@@ -1187,10 +1245,6 @@ int PlayerSAO::punch(v3f dir,
1187
1245
ServerActiveObject *puncher,
1188
1246
float time_from_last_punch)
1189
1247
{
1190
- // It's best that attachments cannot be punched
1191
- if (isAttached ())
1192
- return 0 ;
1193
-
1194
1248
if (!toolcap)
1195
1249
return 0 ;
1196
1250
0 commit comments