Skip to content

Commit

Permalink
Merge pull request #71
Browse files Browse the repository at this point in the history
Fix Elite Crafting Table Condition
  • Loading branch information
WolfyScript authored Feb 10, 2022
2 parents e0db05b + 5cff677 commit 56d1bbf
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,20 @@ public boolean isValid(Set<Material> materials) {
return materials.isEmpty() || cachedRecipes.stream().anyMatch(recipe1 -> recipe1.getResult().getChoices().stream().anyMatch(customItem -> materials.contains(customItem.getItemStack().getType())));
}

public boolean isValid(EliteWorkbench cache) {
var data = cache.getEliteWorkbenchData();
public boolean isValid(EliteWorkbench eliteWorkbench) {
var data = eliteWorkbench.getData();
return cachedRecipes.parallelStream().anyMatch(cachedRecipe -> {
if (cachedRecipe instanceof CraftingRecipe<?, ?> && (RecipeType.Container.ELITE_CRAFTING.isInstance(cachedRecipe) || data.isAdvancedRecipes())) {
if (RecipeType.Container.ELITE_CRAFTING.isInstance(cachedRecipe)) {
EliteWorkbenchCondition condition = cachedRecipe.getConditions().getEliteCraftingTableCondition();
if (condition != null && !condition.getEliteWorkbenches().contains(data.getNamespacedKey())) {
Conditions conditions = cachedRecipe.getConditions();
if (conditions.has(EliteWorkbenchCondition.KEY) && !conditions.getByType(EliteWorkbenchCondition.class).getEliteWorkbenches().contains(eliteWorkbench.getCustomItem().getNamespacedKey())) {
return false;
}
if (cachedRecipe instanceof AbstractRecipeShapeless<?, ?> shapeless) {
return shapeless.getIngredients().size() <= cache.getCurrentGridSize() * cache.getCurrentGridSize();
return shapeless.getIngredients().size() <= eliteWorkbench.getCurrentGridSize() * eliteWorkbench.getCurrentGridSize();
} else {
CraftingRecipeEliteShaped recipe1 = (CraftingRecipeEliteShaped) cachedRecipe;
return recipe1.getShape().length <= cache.getCurrentGridSize() && recipe1.getShape()[0].length() <= cache.getCurrentGridSize();
return recipe1.getShape().length <= eliteWorkbench.getCurrentGridSize() && recipe1.getShape()[0].length() <= eliteWorkbench.getCurrentGridSize();
}
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
package me.wolfyscript.customcrafting.data.cache;

import me.wolfyscript.customcrafting.configs.custom_data.EliteWorkbenchData;
import me.wolfyscript.utilities.api.inventory.custom_items.CustomItem;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

public class EliteWorkbench {

private byte currentGridSize;
private EliteWorkbenchData eliteWorkbench;
private EliteWorkbenchData data;
private CustomItem customItem;
private ItemStack result;
private ItemStack[] contents;

Expand Down Expand Up @@ -63,11 +65,16 @@ public void setResult(ItemStack result) {
this.result = result;
}

public EliteWorkbenchData getEliteWorkbenchData() {
return eliteWorkbench;
public EliteWorkbenchData getData() {
return data;
}

public void setEliteWorkbenchData(EliteWorkbenchData eliteWorkbench) {
this.eliteWorkbench = eliteWorkbench;
public CustomItem getCustomItem() {
return customItem;
}

public void setCustomItemAndData(CustomItem customItem, EliteWorkbenchData eliteWorkbench) {
this.customItem = customItem;
this.data = eliteWorkbench;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ButtonSlotCrafting extends ItemInputButton<CCCache> {
(cache, guiHandler, player, inventory, itemStack, slot, b) -> {
EliteWorkbench eliteWorkbench = cache.getEliteWorkbench();
eliteWorkbench.getContents()[recipeSlot] = inventory.getItem(slot);
ItemStack result = customCrafting.getCraftManager().preCheckRecipe(eliteWorkbench.getContents(), player, inventory, true, eliteWorkbench.getEliteWorkbenchData().isAdvancedRecipes());
ItemStack result = customCrafting.getCraftManager().preCheckRecipe(eliteWorkbench.getContents(), player, inventory, true, eliteWorkbench.getData().isAdvancedRecipes());
eliteWorkbench.setResult(result);
}, null,
(hashMap, cache, guiHandler, player, inventory, itemStack, slot, help) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ButtonSlotResult extends ItemInputButton<CCCache> {
}
}, (cache, guiHandler, player, inventory, itemStack, slot, b) -> {
EliteWorkbench eliteWorkbench = cache.getEliteWorkbench();
EliteWorkbenchData eliteWorkbenchData = eliteWorkbench.getEliteWorkbenchData();
EliteWorkbenchData eliteWorkbenchData = eliteWorkbench.getData();
ItemStack result = customCrafting.getCraftManager().preCheckRecipe(eliteWorkbench.getContents(), player, inventory, true, eliteWorkbenchData.isAdvancedRecipes());
eliteWorkbench.setResult(result);
}, (hashMap, cache, guiHandler, player, inventory, itemStack, slot, help) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public boolean onClose(GuiHandler<CCCache> guiHandler, GUIInventory<CCCache> gui
}
}
}
eliteWorkbench.setEliteWorkbenchData(null);
eliteWorkbench.setCustomItemAndData(null, null);
eliteWorkbench.setResult(new ItemStack(Material.AIR));
eliteWorkbench.setContents(null);
eliteWorkbench.setCurrentGridSize((byte) 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import me.wolfyscript.customcrafting.data.CCCache;
import me.wolfyscript.customcrafting.utils.NamespacedKeyUtils;
import me.wolfyscript.utilities.api.WolfyUtilities;
import me.wolfyscript.utilities.api.inventory.gui.GuiHandler;
import me.wolfyscript.utilities.util.NamespacedKey;
import me.wolfyscript.utilities.util.world.WorldUtils;
import org.bukkit.event.Event;
Expand All @@ -50,11 +51,12 @@ public void onInteract(PlayerInteractEvent event) {
if (block != null && WorldUtils.getWorldCustomItemStore().isStored(block.getLocation())) {
var customItem = NamespacedKeyUtils.getCustomItem(block);
if (customItem != null) {
EliteWorkbenchData eliteWorkbench = (EliteWorkbenchData) customItem.getCustomData(CustomCrafting.ELITE_CRAFTING_TABLE_DATA);
if (eliteWorkbench != null && eliteWorkbench.isEnabled()) {
var eliteCraftingTableData = (EliteWorkbenchData) customItem.getCustomData(CustomCrafting.ELITE_CRAFTING_TABLE_DATA);
if (eliteCraftingTableData != null && eliteCraftingTableData.isEnabled()) {
event.setCancelled(true);
((CCCache) api.getInventoryAPI().getGuiHandler(event.getPlayer()).getCustomCache()).getEliteWorkbench().setEliteWorkbenchData(eliteWorkbench.clone());
api.getInventoryAPI().getGuiHandler(event.getPlayer()).openWindow(new NamespacedKey("crafting", "crafting_grid" + eliteWorkbench.getGridSize()));
GuiHandler<CCCache> guiHandler = api.getInventoryAPI(CCCache.class).getGuiHandler(event.getPlayer());
guiHandler.getCustomCache().getEliteWorkbench().setCustomItemAndData(customItem, eliteCraftingTableData.clone());
guiHandler.openWindow(new NamespacedKey("crafting", "crafting_grid" + eliteCraftingTableData.getGridSize()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package me.wolfyscript.customcrafting.recipes.conditions;

import me.wolfyscript.customcrafting.data.CCCache;
import me.wolfyscript.lib.com.fasterxml.jackson.annotation.JsonAlias;
import me.wolfyscript.lib.com.fasterxml.jackson.annotation.JsonIgnore;
import me.wolfyscript.lib.com.fasterxml.jackson.annotation.JsonProperty;
Expand Down Expand Up @@ -74,8 +75,11 @@ public boolean check(CustomRecipe<?> recipe, Conditions.Data data) {
if (RecipeType.Container.ELITE_CRAFTING.isInstance(recipe)) {
if (data.getBlock() != null) {
CustomItem customItem = NamespacedKeyUtils.getCustomItem(data.getBlock());
if (customItem != null && customItem.getApiReference() instanceof WolfyUtilitiesRef wolfyUtilsRef) {
return eliteWorkbenches.contains(wolfyUtilsRef.getNamespacedKey()) && ((EliteWorkbenchData) customItem.getCustomData(CustomCrafting.ELITE_CRAFTING_TABLE_DATA)).isEnabled();
if (customItem != null) {
EliteWorkbenchData eliteWorkbench = (EliteWorkbenchData) customItem.getCustomData(CustomCrafting.ELITE_CRAFTING_TABLE_DATA);
if (eliteWorkbench != null && eliteWorkbench.isEnabled()) {
return eliteWorkbenches.contains(customItem.getNamespacedKey()) && ((EliteWorkbenchData) customItem.getCustomData(CustomCrafting.ELITE_CRAFTING_TABLE_DATA)).isEnabled();
}
}
}
return false;
Expand Down

0 comments on commit 56d1bbf

Please sign in to comment.