|
29 | 29 | import org.bukkit.persistence.PersistentDataType; |
30 | 30 | import org.bukkit.plugin.java.JavaPlugin; |
31 | 31 |
|
32 | | -import java.util.Arrays; |
| 32 | +import java.util.*; |
| 33 | +import java.util.stream.Collectors; |
33 | 34 |
|
34 | 35 | import static de.clickism.clickmobs.ClickMobsConfig.*; |
35 | 36 |
|
@@ -152,12 +153,32 @@ public boolean isMob(ItemStack item) { |
152 | 153 | } |
153 | 154 |
|
154 | 155 | 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); |
156 | 159 | writeData(entity, item); |
157 | 160 | entity.remove(); |
| 161 | + passengers.forEach(Entity::remove); |
158 | 162 | return item; |
159 | 163 | } |
160 | 164 |
|
| 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 | + |
161 | 182 | private void writeData(LivingEntity entity, ItemStack item) { |
162 | 183 | ItemMeta meta = item.getItemMeta(); |
163 | 184 | if (meta == null) return; |
@@ -186,9 +207,14 @@ public LivingEntity spawnFromItemStack(ItemStack item, Location location) { |
186 | 207 | return entity; |
187 | 208 | } |
188 | 209 |
|
189 | | - private ItemStack createItem(LivingEntity entity) { |
| 210 | + private ItemStack createItem(LivingEntity entity, Set<LivingEntity> passengers) { |
190 | 211 | String entityName = formatEntity(entity); |
191 | 212 | String name = getName(entity); |
| 213 | + if (!passengers.isEmpty()) { |
| 214 | + name = name + " §7(+ " + passengers.stream() |
| 215 | + .map(PickupManager::getName) |
| 216 | + .collect(Collectors.joining(" + ")) + ")"; |
| 217 | + } |
192 | 218 | ItemStack item = new ItemStack(Material.PLAYER_HEAD); |
193 | 219 | ItemMeta meta = item.getItemMeta(); |
194 | 220 | if (meta == null) throw new IllegalArgumentException("ItemMeta is null"); |
|
0 commit comments