Skip to content

Commit

Permalink
misc: Use StandaloneYamlConfigManager to manage recipe config
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Oct 8, 2024
1 parent 7469a9e commit 84bf814
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 143 deletions.
131 changes: 20 additions & 111 deletions src/main/java/xiamomc/morph/misc/recipe/RecipeManager.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package xiamomc.morph.misc.recipe;

import com.google.common.base.Charsets;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xiamomc.morph.MorphPluginObject;
import xiamomc.morph.config.MorphConfigManager;
import xiamomc.morph.utilities.ItemUtils;
import xiamomc.morph.utilities.PluginAssetUtils;
import xiamomc.pluginbase.Annotations.Initializer;

import java.io.*;
Expand All @@ -25,10 +22,7 @@

public class RecipeManager extends MorphPluginObject
{
@Nullable
private YamlConfiguration yamlConfiguration;

private final File configFile = new File(plugin.getDataFolder(), "recipes.yml");
private final StandaloneYamlConfigManager configManager = new RecipeYamlConfigManager(new File(plugin.getDataFolder(), "recipes.yml"), "recipes.yml");

private boolean allowCrafting = false;
private boolean unShaped = false;
Expand All @@ -38,87 +32,33 @@ public class RecipeManager extends MorphPluginObject
private String resultName = "~UNSET";
private List<String> resultLore = new ObjectArrayList<>();

public void reload()
{
var newConfig = new YamlConfiguration();

if (!configFile.exists())
{
if (!copyInternalRecipeResource())
{
logger.error("Can't create file to save configuration! Not reloading recipes...");
return;
}
}

try
{
newConfig.load(configFile);
}
catch (Throwable e)
{
logger.error("Unable to load recipe configuration: " + e.getMessage());
return;
}

this.yamlConfiguration = newConfig;
readValuesFromConfig(newConfig);
prepareRecipe();
}

private void readValuesFromConfig(YamlConfiguration config)
@Initializer
private void load(MorphConfigManager configManager)
{
allowCrafting = config.getBoolean(RecipeOptions.ALLOW_DISGUISE_TOOL_CRAFTING.toString(), false);
unShaped = config.getBoolean(RecipeOptions.DISGUISE_TOOL_CRAFTING_UNSHAPED.toString(), false);
shape = config.getStringList(RecipeOptions.DISGUISE_TOOL_CRAFTING_SHAPE.toString());
resultMaterialId = config.getString(RecipeOptions.DISGUISE_TOOL_RESULT_MATERIAL.toString(), "~UNSET");
resultName = config.getString(RecipeOptions.DISGUISE_TOOL_RESULT_NAME.toString(), "~UNSET");
resultLore = config.getStringList(RecipeOptions.DISGUISE_TOOL_RESULT_LORE.toString());

var materialSection = config.getConfigurationSection(RecipeOptions.DISGUISE_TOOL_CRAFTING_MATERIALS.toString());

if (materialSection != null)
{
materialSection.getKeys(false).forEach(key ->
{
var value = materialSection.getString(key, "~UNSET");
materials.put(key, value);
});
}
reload();
}

private boolean copyInternalRecipeResource()
public void reload()
{
try
{
if (!configFile.createNewFile())
return false;
this.configManager.reload();

try (var writer = new OutputStreamWriter(new FileOutputStream(configFile), Charsets.UTF_8))
{
writer.write(PluginAssetUtils.getFileStrings("recipes.yml"));
}
catch (Throwable t)
{
logger.error("Can't write content: " + t.getMessage());
return false;
}

return true;
}
catch (Throwable t)
{
logger.error("Can't create config file: " + t.getMessage());
}

return true;
readValuesFromConfig(this.configManager);
prepareRecipe();
}

@Initializer
private void load(MorphConfigManager configManager)
private void readValuesFromConfig(StandaloneYamlConfigManager configManager)
{
if (this.yamlConfiguration == null)
reload();
allowCrafting = configManager.getOrDefault(RecipeOptions.ALLOW_DISGUISE_TOOL_CRAFTING);
unShaped = configManager.getOrDefault(RecipeOptions.DISGUISE_TOOL_CRAFTING_UNSHAPED);
shape = configManager.getList(RecipeOptions.DISGUISE_TOOL_CRAFTING_SHAPE);
resultMaterialId = configManager.getOrDefault(RecipeOptions.DISGUISE_TOOL_RESULT_MATERIAL);
resultName = configManager.getOrDefault(RecipeOptions.DISGUISE_TOOL_RESULT_NAME);
resultLore = configManager.getList(RecipeOptions.DISGUISE_TOOL_RESULT_LORE);
var material = configManager.getMap(RecipeOptions.DISGUISE_TOOL_CRAFTING_MATERIALS);
this.materials.clear();

if (material != null)
this.materials.putAll(material);
}

@Nullable
Expand Down Expand Up @@ -239,35 +179,4 @@ private void test_dumpExsampleConfig()
materials.put("A", Material.BEDROCK.key().asString());
materials.put("B", Material.ACACIA_BOAT.key().asString());
}

private void test_saveConfig()
{
if (yamlConfiguration == null)
{
logger.error("Null config!");
return;
}

yamlConfiguration.set(RecipeOptions.ALLOW_DISGUISE_TOOL_CRAFTING.toString(), allowCrafting);
yamlConfiguration.set(RecipeOptions.DISGUISE_TOOL_CRAFTING_UNSHAPED.toString(), this.unShaped);
yamlConfiguration.set(RecipeOptions.DISGUISE_TOOL_CRAFTING_SHAPE.toString(), this.shape);
yamlConfiguration.set(RecipeOptions.DISGUISE_TOOL_RESULT_MATERIAL.toString(), this.resultMaterialId);
yamlConfiguration.set(RecipeOptions.DISGUISE_TOOL_RESULT_NAME.toString(), this.resultName);
yamlConfiguration.set(RecipeOptions.DISGUISE_TOOL_RESULT_LORE.toString(), this.resultLore);

this.materials.forEach((str, id) ->
{
var node = RecipeOptions.DISGUISE_TOOL_CRAFTING_MATERIALS.toString() + "." + str;
yamlConfiguration.set(node, id);
});

try
{
yamlConfiguration.save(configFile);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}
1 change: 0 additions & 1 deletion src/main/java/xiamomc/morph/misc/recipe/RecipeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class RecipeOptions
public static final ConfigOption<String> DISGUISE_TOOL_RESULT_MATERIAL = new ConfigOption<>(skillItemNode().append("result_material"), "minecraft:feather");
public static final ConfigOption<String> DISGUISE_TOOL_RESULT_NAME = new ConfigOption<>(skillItemNode().append("result_item_name"), "~UNSET");
public static final ConfigOption<List<String>> DISGUISE_TOOL_RESULT_LORE = new ConfigOption<>(skillItemNode().append("result_item_lore"), new ArrayList<>());
public static final ConfigOption<Integer> CONFIG_VERSION = new ConfigOption<>(ConfigNode.create().append("version"), 0);

private static ConfigNode craftingNode()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package xiamomc.morph.misc.recipe;

import com.google.common.base.Charsets;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import org.jetbrains.annotations.Nullable;
import xiamomc.morph.utilities.PluginAssetUtils;
import xiamomc.pluginbase.Configuration.ConfigOption;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.List;

public class RecipeYamlConfigManager extends StandaloneYamlConfigManager
{
public RecipeYamlConfigManager(File file, @Nullable String internalResourceName)
{
super(file, internalResourceName);
}

/**
* Copy internal resource to the location
*
* @return Whether this operation was successful
*/
@Override
protected boolean copyInternalResource()
{
try
{
if (!configFile.createNewFile())
return false;

try (var writer = new OutputStreamWriter(new FileOutputStream(configFile), Charsets.UTF_8))
{
writer.write(PluginAssetUtils.getFileStrings("recipes.yml"));
}
catch (Throwable t)
{
logger.error("Can't write content: " + t.getMessage());
return false;
}

return true;
}
catch (Throwable t)
{
logger.error("Can't create config file: " + t.getMessage());
}

return true;
}

@Override
protected int getExpectedConfigVersion()
{
return 1;
}

private final List<ConfigOption<?>> options = new ObjectArrayList<>(List.of(
RecipeOptions.DISGUISE_TOOL_CRAFTING_SHAPE,
RecipeOptions.DISGUISE_TOOL_RESULT_LORE,
RecipeOptions.DISGUISE_TOOL_RESULT_NAME,
RecipeOptions.ALLOW_DISGUISE_TOOL_CRAFTING,
RecipeOptions.DISGUISE_TOOL_CRAFTING_MATERIALS,
RecipeOptions.DISGUISE_TOOL_CRAFTING_UNSHAPED,
RecipeOptions.DISGUISE_TOOL_RESULT_MATERIAL
));

@Override
protected List<ConfigOption<?>> getAllOptions()
{
return options;
}
}
Loading

0 comments on commit 84bf814

Please sign in to comment.