Skip to content

Commit b1c8cf4

Browse files
committed
handle entity passengers
1 parent 2b76b8f commit b1c8cf4

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

spigot/src/main/java/de/clickism/clickmobs/mob/PickupManager.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
import org.bukkit.persistence.PersistentDataType;
3030
import org.bukkit.plugin.java.JavaPlugin;
3131

32-
import java.util.Arrays;
32+
import java.util.*;
33+
import java.util.stream.Collectors;
3334

3435
import static de.clickism.clickmobs.ClickMobsConfig.*;
3536

@@ -152,12 +153,32 @@ public boolean isMob(ItemStack item) {
152153
}
153154

154155
public ItemStack toItemStack(LivingEntity entity) {
155-
ItemStack item = createItem(entity);
156+
entity = getRootEntity(entity);
157+
Set<LivingEntity> passengers = getAllPassengers(entity);
158+
ItemStack item = createItem(entity, passengers);
156159
writeData(entity, item);
157160
entity.remove();
161+
passengers.forEach(Entity::remove);
158162
return item;
159163
}
160164

165+
private Set<LivingEntity> getAllPassengers(LivingEntity entity) {
166+
Set<LivingEntity> passengers = new HashSet<>();
167+
entity.getPassengers().forEach(passenger -> {
168+
if (!(passenger instanceof LivingEntity livingEntity)) return;
169+
passengers.add(livingEntity);
170+
passengers.addAll(getAllPassengers(livingEntity));
171+
});
172+
return passengers;
173+
}
174+
175+
private LivingEntity getRootEntity(LivingEntity entity) {
176+
if (entity.getVehicle() instanceof LivingEntity vehicle) {
177+
return getRootEntity(vehicle);
178+
}
179+
return entity;
180+
}
181+
161182
private void writeData(LivingEntity entity, ItemStack item) {
162183
ItemMeta meta = item.getItemMeta();
163184
if (meta == null) return;
@@ -186,9 +207,14 @@ public LivingEntity spawnFromItemStack(ItemStack item, Location location) {
186207
return entity;
187208
}
188209

189-
private ItemStack createItem(LivingEntity entity) {
210+
private ItemStack createItem(LivingEntity entity, Set<LivingEntity> passengers) {
190211
String entityName = formatEntity(entity);
191212
String name = getName(entity);
213+
if (!passengers.isEmpty()) {
214+
name = name + " §7(+ " + passengers.stream()
215+
.map(PickupManager::getName)
216+
.collect(Collectors.joining(" + ")) + ")";
217+
}
192218
ItemStack item = new ItemStack(Material.PLAYER_HEAD);
193219
ItemMeta meta = item.getItemMeta();
194220
if (meta == null) throw new IllegalArgumentException("ItemMeta is null");

0 commit comments

Comments
 (0)