use of dev.rosewood.rosestacker.stack.settings.entity.MushroomCowStackSettings in project RoseStacker by Rosewood-Development.
the class EntityListener method handleEntityTransformation.
private void handleEntityTransformation(EntityTransformEvent event) {
if (this.stackManager.isWorldDisabled(event.getEntity().getWorld()))
return;
if (!this.stackManager.isEntityStackingEnabled())
return;
if (event.getEntity() instanceof Slime) {
if (PersistentDataUtils.isAiDisabled((LivingEntity) event.getEntity()))
event.getTransformedEntities().stream().map(x -> (Slime) x).forEach(PersistentDataUtils::removeEntityAi);
return;
}
if (!(event.getEntity() instanceof LivingEntity) || !(event.getTransformedEntity() instanceof LivingEntity) || event.getEntity().getType() == event.getTransformedEntity().getType() || !this.stackManager.isEntityStacked((LivingEntity) event.getEntity()))
return;
StackedEntity stackedEntity = this.stackManager.getStackedEntity((LivingEntity) event.getEntity());
if (stackedEntity.getStackSize() == 1)
return;
LivingEntity transformedEntity = (LivingEntity) event.getTransformedEntity();
if (Setting.ENTITY_TRANSFORM_ENTIRE_STACK.getBoolean()) {
NMSHandler nmsHandler = NMSAdapter.getHandler();
StackedEntityDataEntry<?> serialized = nmsHandler.getEntityAsNBT(transformedEntity);
event.setCancelled(true);
// Handle mooshroom shearing
if (event.getEntityType() == EntityType.MUSHROOM_COW) {
MushroomCowStackSettings stackSettings = (MushroomCowStackSettings) stackedEntity.getStackSettings();
int mushroomsDropped = 5;
if (stackSettings.shouldDropAdditionalMushroomsPerCowInStack())
mushroomsDropped += (stackedEntity.getStackSize() - 1) * stackSettings.getExtraMushroomsPerCowInStack();
Material dropType = ((MushroomCow) event.getEntity()).getVariant() == Variant.BROWN ? Material.BROWN_MUSHROOM : Material.RED_MUSHROOM;
this.stackManager.preStackItems(GuiUtil.getMaterialAmountAsItemStacks(dropType, mushroomsDropped), event.getEntity().getLocation());
}
boolean aiDisabled = PersistentDataUtils.isAiDisabled((LivingEntity) event.getEntity());
event.getEntity().remove();
Bukkit.getScheduler().scheduleSyncDelayedTask(this.rosePlugin, () -> {
this.stackManager.setEntityStackingTemporarilyDisabled(true);
LivingEntity newEntity = nmsHandler.createEntityFromNBT(serialized, transformedEntity.getLocation(), true, transformedEntity.getType());
if (aiDisabled)
PersistentDataUtils.removeEntityAi(newEntity);
StackedEntity newStack = this.stackManager.createEntityStack(newEntity, false);
this.stackManager.setEntityStackingTemporarilyDisabled(false);
if (newStack == null)
return;
for (StackedEntityDataEntry<?> serializedEntity : stackedEntity.getStackedEntityNBT().getAll()) {
LivingEntity entity = nmsHandler.createEntityFromNBT(serializedEntity, transformedEntity.getLocation(), false, transformedEntity.getType());
if (aiDisabled)
PersistentDataUtils.removeEntityAi(entity);
newStack.increaseStackSize(entity);
}
});
} else {
// Make sure disabled AI gets transferred
if (PersistentDataUtils.isAiDisabled((LivingEntity) event.getEntity()))
PersistentDataUtils.removeEntityAi((LivingEntity) event.getTransformedEntity());
if (event.getTransformReason() == TransformReason.LIGHTNING) {
// Wait for lightning to disappear
Bukkit.getScheduler().scheduleSyncDelayedTask(this.rosePlugin, stackedEntity::decreaseStackSize, 20);
} else {
Bukkit.getScheduler().runTask(this.rosePlugin, stackedEntity::decreaseStackSize);
}
}
}
Aggregations