Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipe Book Category Icon NBT/Stack Support #147

Merged
merged 1 commit into from Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -26,12 +26,15 @@
import me.wolfyscript.customcrafting.recipes.CustomRecipe;
import me.wolfyscript.lib.com.fasterxml.jackson.annotation.JsonAlias;
import me.wolfyscript.lib.com.fasterxml.jackson.annotation.JsonGetter;
import me.wolfyscript.lib.com.fasterxml.jackson.annotation.JsonIgnore;
import me.wolfyscript.lib.com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import me.wolfyscript.lib.com.fasterxml.jackson.annotation.JsonInclude;
import me.wolfyscript.lib.com.fasterxml.jackson.annotation.JsonSetter;
import me.wolfyscript.lib.com.fasterxml.jackson.databind.JsonNode;
import me.wolfyscript.lib.net.kyori.adventure.platform.bukkit.BukkitComponentSerializer;
import me.wolfyscript.utilities.api.nms.network.MCByteBuf;
import me.wolfyscript.utilities.util.NamespacedKey;
import me.wolfyscript.utilities.util.json.jackson.JacksonUtil;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

Expand All @@ -49,13 +52,13 @@ public abstract class CategorySettings {
protected Set<String> folders;
protected Set<NamespacedKey> recipes;
private String id = "";
private Material icon;
private ItemStack icon;
private String name;
private List<String> description;

protected CategorySettings() {
this.name = "";
this.icon = Material.CHEST;
this.icon = new ItemStack(Material.CHEST);
this.groups = new HashSet<>();
this.folders = new HashSet<>();
this.description = new ArrayList<>();
Expand All @@ -65,7 +68,7 @@ protected CategorySettings() {
protected CategorySettings(CategorySettings category) {
this.id = category.id;
this.name = category.name;
this.icon = category.getIcon();
this.icon = category.getIconStack();
this.groups = new HashSet<>(category.groups);
this.folders = new HashSet<>(category.folders);
this.description = new ArrayList<>(category.getDescription());
Expand All @@ -82,16 +85,45 @@ void setId(String id) {
this.id = id;
}

@JsonGetter
public Material getIcon() {
@JsonGetter("icon")
private Object getJsonIconStack() {
if (icon.hasItemMeta() || icon.getAmount() > 1) {
return icon;
}
return icon.getType();
}

@JsonSetter("icon")
private void setJsonIconStack(JsonNode icon) {
if (icon.isTextual()) {
this.icon = new ItemStack(Objects.requireNonNull(Material.matchMaterial(icon.asText())));
} else if (icon.isObject()) {
this.icon = JacksonUtil.getObjectMapper().convertValue(icon, ItemStack.class);
} else {
this.icon = new ItemStack(Material.CHEST);
}
}

public ItemStack getIconStack() {
return icon;
}

@JsonSetter
public void setIcon(Material icon) {
public void setIconStack(ItemStack icon) {
this.icon = icon;
}

@JsonIgnore
@Deprecated
public void setIcon(Material icon) {
this.icon = new ItemStack(icon);
}

@JsonIgnore
@Deprecated
public Material getIcon() {
return icon.getType();
}

@JsonGetter
public String getName() {
return name;
Expand Down Expand Up @@ -143,7 +175,7 @@ public void setRecipes(Set<NamespacedKey> recipes) {
}

public ItemStack createItemStack(CustomCrafting customCrafting) {
var categoryItem = new ItemStack(getIcon());
var categoryItem = getIconStack().clone();
var itemMeta = categoryItem.getItemMeta();
var languageAPI = customCrafting.getApi().getLanguageAPI();
var miniMsg = customCrafting.getApi().getChat().getMiniMessage();
Expand Down
Expand Up @@ -51,7 +51,7 @@ class ButtonCategory extends ActionButton<CCCache> {
}
return true;
}, (values, cache, guiHandler, player, inventory, itemStack, slot, help) -> {
itemStack.setType(category.getIcon());
itemStack = category.getIconStack();
values.put("%name%", category.getName());
values.put("%description%", category.getDescription());
return itemStack;
Expand Down
Expand Up @@ -51,7 +51,7 @@ class ButtonFilter extends ActionButton<CCCache> {
}
return true;
}, (values, cache, guiHandler, player, inventory, itemStack, slot, help) -> {
itemStack.setType(filter.getIcon());
itemStack = filter.getIconStack();
values.put("%name%", filter.getName());
values.put("%description%", filter.getDescription());
return itemStack;
Expand Down
Expand Up @@ -31,6 +31,7 @@
import me.wolfyscript.utilities.api.WolfyUtilities;
import me.wolfyscript.utilities.api.inventory.gui.GuiWindow;
import me.wolfyscript.utilities.api.inventory.gui.button.buttons.ActionButton;
import me.wolfyscript.utilities.util.inventory.ItemUtils;
import org.bukkit.Material;
import org.bukkit.util.StringUtil;

Expand Down Expand Up @@ -79,7 +80,7 @@ class ButtonSaveCategory extends ActionButton<CCCache> {
private static boolean saveCategorySetting(RecipeBookEditor recipeBookEditor, CustomCrafting customCrafting) {
var recipeBook = customCrafting.getConfigHandler().getRecipeBookConfig();
CategorySettings category = recipeBookEditor.getCategorySetting();
if (category.getIcon() == null) {
if (ItemUtils.isAirOrNull(category.getIconStack())) {
return false;
}
if (category instanceof CategoryFilter filter) {
Expand Down
Expand Up @@ -83,15 +83,15 @@ public void onInit() {
btnBld.itemInput(ICON.getKey()).state(state -> state.icon(Material.AIR).action((cache, guiHandler, player, inventory, slot, event) -> {
Bukkit.getScheduler().runTask(customCrafting, () -> {
if (!ItemUtils.isAirOrNull(inventory.getItem(slot))) {
cache.getRecipeBookEditor().getCategorySetting().setIcon(inventory.getItem(slot).getType());
cache.getRecipeBookEditor().getCategorySetting().setIconStack(inventory.getItem(slot));
} else {
cache.getRecipeBookEditor().getCategorySetting().setIcon(Material.AIR);
cache.getRecipeBookEditor().getCategorySetting().setIconStack(new ItemStack(Material.AIR));
}
});
return false;
}).render((cache, guiHandler, player, guiInventory, itemStack, i) -> {
var categorySettings = guiHandler.getCustomCache().getRecipeBookEditor().getCategorySetting();
return CallbackButtonRender.UpdateResult.of(categorySettings != null && categorySettings.getIcon() != null ? new ItemStack(categorySettings.getIcon()) : new ItemStack(Material.AIR));
return CallbackButtonRender.UpdateResult.of(categorySettings != null ? categorySettings.getIconStack() : new ItemStack(Material.AIR));
})).register();
btnBld.chatInput(NAME.getKey()).state(state -> state.icon(Material.NAME_TAG).render((cache, guiHandler, player, guiInventory, itemStack, i) -> CallbackButtonRender.UpdateResult.of(Placeholder.parsed("name", guiHandler.getCustomCache().getRecipeBookEditor().getCategorySetting().getName())))).inputAction((guiHandler, player, s, strings) -> {
guiHandler.getCustomCache().getRecipeBookEditor().getCategorySetting().setName(s);
Expand Down