use of com.minecolonies.api.research.util.ResearchConstants.RECIPES in project minecolonies by Minecolonies.
the class AbstractCraftingBuildingModule method checkForWorkerSpecificRecipes.
@Override
public void checkForWorkerSpecificRecipes() {
final IRecipeManager recipeManager = IColonyManager.getInstance().getRecipeManager();
for (final CustomRecipe newRecipe : CustomRecipeManager.getInstance().getRecipes(getCustomRecipeKey())) {
final IRecipeStorage recipeStorage = newRecipe.getRecipeStorage();
final IToken<?> recipeToken = recipeManager.checkOrAddRecipe(recipeStorage);
if (newRecipe.isValidForBuilding(building)) {
IToken<?> duplicateFound = null;
boolean forceReplace = false;
for (IToken<?> token : recipes) {
if (token == recipeToken) {
duplicateFound = token;
break;
}
final IRecipeStorage storage = recipeManager.getRecipes().get(token);
// Let's verify that this recipe doesn't exist in an improved form
if (storage != null && storage.getPrimaryOutput().equals(recipeStorage.getPrimaryOutput(), true)) {
List<ItemStorage> recipeInput1 = storage.getCleanedInput();
List<ItemStorage> recipeInput2 = recipeStorage.getCleanedInput();
if (recipeInput1.size() != recipeInput2.size()) {
continue;
}
if (recipeInput1.size() > 1) {
recipeInput1.sort(Comparator.comparing(item -> Objects.hash(item.hashCode(), item.getAmount())));
recipeInput2.sort(Comparator.comparing(item -> Objects.hash(item.hashCode(), item.getAmount())));
}
boolean allMatch = true;
for (int i = 0; i < recipeInput1.size(); i++) {
if (!recipeInput1.get(i).getItem().equals(recipeInput2.get(i).getItem())) {
allMatch = false;
break;
}
}
if (allMatch) {
duplicateFound = token;
if (storage.getRecipeType() instanceof ClassicRecipe && recipeStorage.getRecipeType() instanceof MultiOutputRecipe) {
// This catches the old custom recipes without a RecipeSource
forceReplace = true;
}
if (storage.getRecipeSource() != null && storage.getRecipeSource().equals(recipeStorage.getRecipeSource())) {
// This will only happen if the tokens don't match, aka: the recipe has changed.
forceReplace = true;
}
break;
}
}
}
if (duplicateFound == null) {
addRecipeToList(recipeToken, true);
building.getColony().getRequestManager().onColonyUpdate(request -> request.getRequest() instanceof IDeliverable && ((IDeliverable) request.getRequest()).matches(recipeStorage.getPrimaryOutput()));
markDirty();
} else if ((forceReplace || newRecipe.getMustExist()) && !(duplicateFound.equals(recipeToken))) {
// We found the base recipe for a multi-recipe, replace it with the multi-recipe
replaceRecipe(duplicateFound, recipeToken);
building.getColony().getRequestManager().onColonyUpdate(request -> request.getRequest() instanceof IDeliverable && ((IDeliverable) request.getRequest()).matches(recipeStorage.getPrimaryOutput()));
// Clean up old 'classic' recipes that the new multi-recipe replaces
final List<ItemStack> alternates = recipeStorage.getAlternateOutputs();
for (IToken<?> token : recipes) {
final IRecipeStorage storage = recipeManager.getRecipes().get(token);
if (storage.getRecipeType() instanceof ClassicRecipe && ItemStackUtils.compareItemStackListIgnoreStackSize(alternates, storage.getPrimaryOutput(), false, true)) {
removeRecipe(token);
}
}
building.getColony().getRequestManager().onColonyUpdate(request -> request.getRequest() instanceof IDeliverable && recipeStorage.getAlternateOutputs().stream().anyMatch(i -> ((IDeliverable) request.getRequest()).matches(i)));
markDirty();
}
} else {
if (recipes.contains(recipeToken)) {
removeRecipe(recipeToken);
markDirty();
}
}
}
}
use of com.minecolonies.api.research.util.ResearchConstants.RECIPES in project minecolonies by Minecolonies.
the class AbstractCraftingBuildingModule method getFirstFulfillableRecipe.
@Override
public IRecipeStorage getFirstFulfillableRecipe(final Predicate<ItemStack> stackPredicate, final int count, final boolean considerReservation) {
for (final IToken<?> token : recipes) {
if (disabledRecipes.contains(token)) {
continue;
}
final IRecipeStorage storage = IColonyManager.getInstance().getRecipeManager().getRecipes().get(token);
if (storage != null && (stackPredicate.test(storage.getPrimaryOutput()) || storage.getAlternateOutputs().stream().anyMatch(i -> stackPredicate.test(i)))) {
final Set<IItemHandler> handlers = new HashSet<>();
for (final ICitizenData workerEntity : building.getAllAssignedCitizen()) {
handlers.add(workerEntity.getInventory());
}
IRecipeStorage toTest = storage.getRecipeType() instanceof MultiOutputRecipe ? storage.getClassicForMultiOutput(stackPredicate) : storage;
if (toTest.canFullFillRecipe(count, considerReservation ? reservedStacks() : Collections.emptyMap(), new ArrayList<>(handlers), building)) {
return toTest;
}
}
}
return null;
}
use of com.minecolonies.api.research.util.ResearchConstants.RECIPES in project minecolonies by ldtteam.
the class AbstractCraftingBuildingModule method checkForWorkerSpecificRecipes.
@Override
public void checkForWorkerSpecificRecipes() {
final IRecipeManager recipeManager = IColonyManager.getInstance().getRecipeManager();
for (final CustomRecipe newRecipe : CustomRecipeManager.getInstance().getRecipes(getCustomRecipeKey())) {
final IRecipeStorage recipeStorage = newRecipe.getRecipeStorage();
final IToken<?> recipeToken = recipeManager.checkOrAddRecipe(recipeStorage);
if (newRecipe.isValidForBuilding(building)) {
IToken<?> duplicateFound = null;
boolean forceReplace = false;
for (IToken<?> token : recipes) {
if (token == recipeToken) {
duplicateFound = token;
break;
}
final IRecipeStorage storage = recipeManager.getRecipes().get(token);
// Let's verify that this recipe doesn't exist in an improved form
if (storage != null && storage.getPrimaryOutput().equals(recipeStorage.getPrimaryOutput(), true)) {
List<ItemStorage> recipeInput1 = storage.getCleanedInput();
List<ItemStorage> recipeInput2 = recipeStorage.getCleanedInput();
if (recipeInput1.size() != recipeInput2.size()) {
continue;
}
if (recipeInput1.size() > 1) {
recipeInput1.sort(Comparator.comparing(item -> Objects.hash(item.hashCode(), item.getAmount())));
recipeInput2.sort(Comparator.comparing(item -> Objects.hash(item.hashCode(), item.getAmount())));
}
boolean allMatch = true;
for (int i = 0; i < recipeInput1.size(); i++) {
if (!recipeInput1.get(i).getItem().equals(recipeInput2.get(i).getItem())) {
allMatch = false;
break;
}
}
if (allMatch) {
duplicateFound = token;
if (storage.getRecipeType() instanceof ClassicRecipe && recipeStorage.getRecipeType() instanceof MultiOutputRecipe) {
// This catches the old custom recipes without a RecipeSource
forceReplace = true;
}
if (storage.getRecipeSource() != null && storage.getRecipeSource().equals(recipeStorage.getRecipeSource())) {
// This will only happen if the tokens don't match, aka: the recipe has changed.
forceReplace = true;
}
break;
}
}
}
if (duplicateFound == null) {
addRecipeToList(recipeToken, true);
building.getColony().getRequestManager().onColonyUpdate(request -> request.getRequest() instanceof IDeliverable && ((IDeliverable) request.getRequest()).matches(recipeStorage.getPrimaryOutput()));
markDirty();
} else if ((forceReplace || newRecipe.getMustExist()) && !(duplicateFound.equals(recipeToken))) {
// We found the base recipe for a multi-recipe, replace it with the multi-recipe
replaceRecipe(duplicateFound, recipeToken);
building.getColony().getRequestManager().onColonyUpdate(request -> request.getRequest() instanceof IDeliverable && ((IDeliverable) request.getRequest()).matches(recipeStorage.getPrimaryOutput()));
// Clean up old 'classic' recipes that the new multi-recipe replaces
final List<ItemStack> alternates = recipeStorage.getAlternateOutputs();
for (IToken<?> token : recipes) {
final IRecipeStorage storage = recipeManager.getRecipes().get(token);
if (storage.getRecipeType() instanceof ClassicRecipe && ItemStackUtils.compareItemStackListIgnoreStackSize(alternates, storage.getPrimaryOutput(), false, true)) {
removeRecipe(token);
}
}
building.getColony().getRequestManager().onColonyUpdate(request -> request.getRequest() instanceof IDeliverable && recipeStorage.getAlternateOutputs().stream().anyMatch(i -> ((IDeliverable) request.getRequest()).matches(i)));
markDirty();
}
} else {
if (recipes.contains(recipeToken)) {
removeRecipe(recipeToken);
markDirty();
}
}
}
}
use of com.minecolonies.api.research.util.ResearchConstants.RECIPES in project minecolonies by ldtteam.
the class AbstractCraftingBuildingModule method getFirstFulfillableRecipe.
@Override
public IRecipeStorage getFirstFulfillableRecipe(final Predicate<ItemStack> stackPredicate, final int count, final boolean considerReservation) {
for (final IToken<?> token : recipes) {
if (disabledRecipes.contains(token)) {
continue;
}
final IRecipeStorage storage = IColonyManager.getInstance().getRecipeManager().getRecipes().get(token);
if (storage != null && (stackPredicate.test(storage.getPrimaryOutput()) || storage.getAlternateOutputs().stream().anyMatch(i -> stackPredicate.test(i)))) {
final Set<IItemHandler> handlers = new HashSet<>();
for (final ICitizenData workerEntity : building.getAllAssignedCitizen()) {
handlers.add(workerEntity.getInventory());
}
IRecipeStorage toTest = storage.getRecipeType() instanceof MultiOutputRecipe ? storage.getClassicForMultiOutput(stackPredicate) : storage;
if (toTest.canFullFillRecipe(count, considerReservation ? reservedStacks() : Collections.emptyMap(), new ArrayList<>(handlers), building)) {
return toTest;
}
}
}
return null;
}
Aggregations