23
23
24
24
namespace pocketmine \entity \object ;
25
25
26
+ use pocketmine \block \Water ;
26
27
use pocketmine \entity \Entity ;
27
28
use pocketmine \event \entity \ItemDespawnEvent ;
28
29
use pocketmine \event \entity \ItemSpawnEvent ;
29
30
use pocketmine \event \inventory \InventoryPickupItemEvent ;
30
31
use pocketmine \item \Item ;
32
+ use pocketmine \math \AxisAlignedBB ;
33
+ use pocketmine \network \mcpe \protocol \ActorEventPacket ;
31
34
use pocketmine \network \mcpe \protocol \AddItemActorPacket ;
32
35
use pocketmine \network \mcpe \protocol \TakeItemActorPacket ;
33
36
use pocketmine \Player ;
@@ -52,7 +55,7 @@ class ItemEntity extends Entity{
52
55
protected $ gravity = 0.04 ;
53
56
protected $ drag = 0.02 ;
54
57
55
- public $ canCollide = false ;
58
+ public $ canCollide = true ;
56
59
57
60
/** @var int */
58
61
protected $ age = 0 ;
@@ -61,13 +64,13 @@ protected function initEntity() : void{
61
64
parent ::initEntity ();
62
65
63
66
$ this ->setMaxHealth (5 );
67
+ $ this ->setImmobile (true );
64
68
$ this ->setHealth ($ this ->namedtag ->getShort ("Health " , (int ) $ this ->getHealth ()));
65
69
$ this ->age = $ this ->namedtag ->getShort ("Age " , $ this ->age );
66
70
$ this ->pickupDelay = $ this ->namedtag ->getShort ("PickupDelay " , $ this ->pickupDelay );
67
71
$ this ->owner = $ this ->namedtag ->getString ("Owner " , $ this ->owner );
68
72
$ this ->thrower = $ this ->namedtag ->getString ("Thrower " , $ this ->thrower );
69
73
70
-
71
74
$ itemTag = $ this ->namedtag ->getCompoundTag ("Item " );
72
75
if ($ itemTag === null ){
73
76
throw new \UnexpectedValueException ("Invalid " . get_class ($ this ) . " entity: expected \"Item \" NBT tag not found " );
@@ -95,6 +98,32 @@ public function entityBaseTick(int $tickDiff = 1) : bool{
95
98
$ this ->pickupDelay = 0 ;
96
99
}
97
100
101
+ if ($ this ->ticksLived % 60 === 0 ){
102
+ foreach ($ this ->level ->getCollidingEntities ($ this ->getBoundingBox ()->expandedCopy (1 , 1 , 1 ), $ this ) as $ entity ){
103
+ if ($ entity instanceof ItemEntity){
104
+ $ item = $ this ->getItem ();
105
+ if ($ item ->getCount () < $ item ->getMaxStackSize ()){
106
+ if ($ entity ->getItem ()->equals ($ item , true , true )){
107
+ $ nextAmount = $ item ->getCount () + $ entity ->getItem ()->getCount ();
108
+ if ($ nextAmount <= $ item ->getMaxStackSize ()){
109
+ if ($ this ->ticksLived > $ entity ->ticksLived ){
110
+ $ entity ->flagForDespawn ();
111
+
112
+ $ item ->setCount ($ nextAmount );
113
+ $ this ->broadcastEntityEvent (ActorEventPacket::ITEM_ENTITY_MERGE , $ nextAmount );
114
+ }else {
115
+ $ this ->flagForDespawn ();
116
+
117
+ $ entity ->getItem ()->setCount ($ nextAmount );
118
+ $ entity ->broadcastEntityEvent (ActorEventPacket::ITEM_ENTITY_MERGE , $ nextAmount );
119
+ }
120
+ }
121
+ }
122
+ }
123
+ }
124
+ }
125
+ }
126
+
98
127
$ this ->age += $ tickDiff ;
99
128
if ($ this ->age > 6000 ){
100
129
$ ev = new ItemDespawnEvent ($ this );
@@ -111,6 +140,10 @@ public function entityBaseTick(int $tickDiff = 1) : bool{
111
140
return $ hasUpdate ;
112
141
}
113
142
143
+ protected function getDefaultDrag () : float {
144
+ return 0.02 ;
145
+ }
146
+
114
147
protected function tryChangeMovement () : void {
115
148
$ this ->checkObstruction ($ this ->x , $ this ->y , $ this ->z );
116
149
parent ::tryChangeMovement ();
@@ -120,6 +153,28 @@ protected function applyDragBeforeGravity() : bool{
120
153
return true ;
121
154
}
122
155
156
+ protected function applyGravity () : void {
157
+ $ bb = $ this ->getBoundingBox ();
158
+ $ waterCount = 0 ;
159
+
160
+ for ($ j = 0 ; $ j < 5 ; ++$ j ){
161
+ $ d1 = $ bb ->minY + ($ bb ->maxY - $ bb ->minY ) * $ j / 5 + 0.4 ;
162
+ $ d3 = $ bb ->minY + ($ bb ->maxY - $ bb ->minY ) * ($ j + 1 ) / 5 + 1 ;
163
+
164
+ $ bb2 = new AxisAlignedBB ($ bb ->minX , $ d1 , $ bb ->minZ , $ bb ->maxX , $ d3 , $ bb ->maxZ );
165
+
166
+ if ($ this ->level ->isLiquidInBoundingBox ($ bb2 , new Water ())){
167
+ $ waterCount += 0.2 ;
168
+ }
169
+ }
170
+
171
+ if ($ waterCount > 0 ){
172
+ $ this ->motion ->y += 0.002 * ($ waterCount * 2 - 1 );
173
+ }else {
174
+ $ this ->motion ->y -= $ this ->gravity ;
175
+ }
176
+ }
177
+
123
178
public function saveNBT () : void {
124
179
parent ::saveNBT ();
125
180
$ this ->namedtag ->setTag ($ this ->item ->nbtSerialize (-1 , "Item " ));
@@ -142,11 +197,7 @@ public function getItem() : Item{
142
197
}
143
198
144
199
public function canCollideWith (Entity $ entity ) : bool {
145
- return false ;
146
- }
147
-
148
- public function canBeCollidedWith () : bool {
149
- return false ;
200
+ return parent ::canCollideWith ($ entity ) and $ entity instanceof ItemEntity;
150
201
}
151
202
152
203
/**
0 commit comments