Skip to content

Commit

Permalink
Add Custom craft and its config GUI. (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrea authored Apr 7, 2024
2 parents 1925580 + 30095e3 commit 74f36ec
Show file tree
Hide file tree
Showing 24 changed files with 1,582 additions and 389 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "xyz.alexcrea"
version = "1.3.2-A2"
version = "1.4.0a"

repositories {
mavenCentral()
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.configuration.file.YamlConfiguration;
import xyz.alexcrea.cuanvil.group.EnchantConflictManager;
import xyz.alexcrea.cuanvil.group.ItemGroupManager;
import xyz.alexcrea.cuanvil.recipe.CustomAnvilRecipeManager;
import xyz.alexcrea.cuanvil.util.MetricsUtil;

import java.io.File;
Expand All @@ -18,12 +19,14 @@ public abstract class ConfigHolder {
public static ItemGroupConfigHolder ITEM_GROUP_HOLDER;
public static ConflictConfigHolder CONFLICT_HOLDER;
public static UnitRepairHolder UNIT_REPAIR_HOLDER;
public static CustomAnvilCraftHolder CUSTOM_RECIPE_HOLDER;

public static boolean loadConfig() {
DEFAULT_CONFIG = new DefaultConfigHolder();
ITEM_GROUP_HOLDER = new ItemGroupConfigHolder();
CONFLICT_HOLDER = new ConflictConfigHolder();
UNIT_REPAIR_HOLDER = new UnitRepairHolder();
CUSTOM_RECIPE_HOLDER = new CustomAnvilCraftHolder();

boolean result = reloadAllFromDisk(true);
if (result) {
Expand All @@ -41,6 +44,9 @@ public static boolean reloadAllFromDisk(boolean hardfail) {
sucess = CONFLICT_HOLDER.reloadFromDisk(hardfail);
if (!sucess) return false;
sucess = UNIT_REPAIR_HOLDER.reloadFromDisk(hardfail);
if (!sucess) return false;
sucess = CUSTOM_RECIPE_HOLDER.reloadFromDisk(hardfail);

return sucess;
}

Expand Down Expand Up @@ -83,6 +89,7 @@ protected File getLastBackup() {

// Save logic
public boolean saveToDisk(boolean doBackup) {
CustomAnvil.Companion.log("Saving "+getConfigFileName());
if (doBackup) {
if (!saveBackup()) {
CustomAnvil.instance.getLogger().severe("Could not save backup. see above.");
Expand All @@ -104,6 +111,7 @@ public boolean saveToDisk(boolean doBackup) {
return false;
}

CustomAnvil.Companion.log(getConfigFileName()+" saved successfully");
return true;
}

Expand Down Expand Up @@ -242,6 +250,7 @@ public void reload() {
public static class UnitRepairHolder extends ResourceConfigHolder {
private final static String ITEM_GROUP_FILE_NAME = "unit_repair_item";


private UnitRepairHolder() {
super(ITEM_GROUP_FILE_NAME);
}
Expand All @@ -253,4 +262,25 @@ public void reload() {
}


// Class for custom anvil craft
public static class CustomAnvilCraftHolder extends ResourceConfigHolder {
private final static String CUSTOM_RECIPE_FILE_NAME = "custom_recipes";
CustomAnvilRecipeManager recipeManager;

private CustomAnvilCraftHolder() {
super(CUSTOM_RECIPE_FILE_NAME);
}

public CustomAnvilRecipeManager getRecipeManager() {
return recipeManager;
}

@Override
public void reload() {
this.recipeManager = new CustomAnvilRecipeManager();
this.recipeManager.prepareRecipes(this.configuration);
}
}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.alexcrea.cuanvil.gui.config;
package xyz.alexcrea.cuanvil.gui.config.global;

import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.pane.Orientable;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package xyz.alexcrea.cuanvil.gui.config.global;

import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import org.bukkit.Material;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.config.settings.subsetting.CustomRecipeSubSettingGui;
import xyz.alexcrea.cuanvil.recipe.AnvilCustomRecipe;
import xyz.alexcrea.cuanvil.util.CasedStringUtil;

import java.util.Arrays;
import java.util.List;

public class CustomRecipeConfigGui extends MappedElementListConfigGui<AnvilCustomRecipe, CustomRecipeSubSettingGui> {


public final static CustomRecipeConfigGui INSTANCE = new CustomRecipeConfigGui();

static {
INSTANCE.init();
}

private CustomRecipeConfigGui() {
super("Custom Recipe Config");

}

@Override
protected ItemStack createItemForGeneric(AnvilCustomRecipe recipe) {
// Get base item to display
ItemStack craftResultItem = recipe.getResultItem();
ItemStack displaydItem;
if(craftResultItem == null){
displaydItem = new ItemStack(Material.BARRIER);
}else{
displaydItem = craftResultItem.clone();
}

// edit displayed item
ItemMeta meta = displaydItem.getItemMeta();

meta.setDisplayName("\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(recipe.getName()) + " \u00A7fCustom recipe");
meta.addItemFlags(ItemFlag.values());

boolean shouldWork = recipe.validate();

meta.setLore(Arrays.asList(
"\u00A77Should work: \u00A7"+(shouldWork ? "aYes" : "cNo"),
"\u00A77Exact count: \u00A7"+(recipe.getExactCount() ? "aYes" : "cNo"),
"\u00A77Recipe Xp Cost: \u00A7e"+recipe.getXpCostPerCraft()

));

displaydItem.setItemMeta(meta);
return displaydItem;
}

@Override
protected CustomRecipeSubSettingGui newInstanceOfGui(AnvilCustomRecipe generic, GuiItem item) {
return new CustomRecipeSubSettingGui(this, generic, item);
}

@Override
protected String genericDisplayedName() {
return "custom recipe";
}

@Override
protected AnvilCustomRecipe createAndSaveNewEmptyGeneric(String name) {
// Create new empty conflict and display it to the admin
AnvilCustomRecipe recipe = new AnvilCustomRecipe(
name,
AnvilCustomRecipe.Companion.getDEFAULT_EXACT_COUNT_CONFIG(),
AnvilCustomRecipe.Companion.getDEFAULT_XP_COST_CONFIG(),
AnvilCustomRecipe.Companion.getDEFAULT_LEFT_ITEM_CONFIG(),
AnvilCustomRecipe.Companion.getDEFAULT_RIGHT_ITEM_CONFIG(),
AnvilCustomRecipe.Companion.getDEFAULT_RESULT_ITEM_CONFIG());

ConfigHolder.CUSTOM_RECIPE_HOLDER.getRecipeManager().cleanAddNew(recipe);

// Save recipe to file
recipe.saveToFile();

return recipe;
}


@Override
protected List<AnvilCustomRecipe> getEveryDisplayableInstanceOfGeneric() {
return ConfigHolder.CUSTOM_RECIPE_HOLDER.getRecipeManager().getRecipeList();
}
}
Loading

0 comments on commit 74f36ec

Please sign in to comment.