use of com.infinityraider.agricraft.farming.mutation.Mutation in project AgriCraft by AgriCraft.
the class JsonHelper method wrap.
@Nonnull
public static Optional<IAgriMutation> wrap(@Nullable AgriMutation mutation) {
// Step I. Abort If Null Mutation.
if (mutation == null) {
return Optional.empty();
}
// Step II. Determine Chance.
final double chance = mutation.getChance();
// Step III. Determine ID.
final String mutationId = mutation.getChild().getId().replace("_plant", "_mutation");
// Step IV. Determine Child.
final Optional<IAgriPlant> child = AgriApi.getPlantRegistry().get(mutation.getChild().getId());
// Step V. Abort If Child Missing.
if (!child.isPresent()) {
return Optional.empty();
}
// Step VI. Determine Parents.
final Optional<IAgriPlant> parentOne = AgriApi.getPlantRegistry().get(mutation.getParent1().getId());
final Optional<IAgriPlant> parentTwo = AgriApi.getPlantRegistry().get(mutation.getParent2().getId());
// Step VII. Abort If Missing Parent.
if ((!parentOne.isPresent()) && (!parentTwo.isPresent())) {
return Optional.empty();
}
// Step VIII. Create New Mutation
return Optional.of(new Mutation(mutationId, chance, child.get(), parentOne.get(), parentTwo.get()));
}
use of com.infinityraider.agricraft.farming.mutation.Mutation in project AgriCraft by AgriCraft.
the class AgriMutationRegistry method add.
@Override
public boolean add(@Nonnull String id, double chance, @Nonnull String childId, @Nonnull List<String> parentIds) {
// Step I. Validate Parameters.
Objects.requireNonNull(id, "The id of a mutation may not be null!");
Objects.requireNonNull(childId, "The id of the child plant for a mutation may not be null!");
Objects.requireNonNull(parentIds, "The supplied list of parents for a mutation may not be null!");
// Step II. Validate Parents.
parentIds.forEach(parentId -> Objects.requireNonNull(parentId, "The id of a parent for a mutation may not be null!"));
// Step III. Map Child.
final IAgriPlant childPlant = AgriApi.getPlantRegistry().get(id).orElse(null);
// Step IV. Abort If Child Missing.
if (childPlant == null) {
// We tried, so don't throw error, just return false.
return false;
}
// Step V. Allocate Parent Plant List.
final List<IAgriPlant> parentPlants = new ArrayList<>(parentIds.size());
// Step VI. Map Parents, Aborting If Missing.
for (String parentId : parentIds) {
final IAgriPlant parentPlant = AgriApi.getPlantRegistry().get(parentId).orElse(null);
if (parentPlant != null) {
parentPlants.add(parentPlant);
} else {
return false;
}
}
// Step VII. Create the new mutation.
final IAgriMutation mutation = new Mutation(id, chance, childPlant, parentPlants);
// Step VIII. Register the new muation.
return this.add(mutation);
}
Aggregations