Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ repositories {
}

dependencies {
compileOnly "org.spigotmc:spigot-api:1.21-R0.1-SNAPSHOT"
compileOnly "com.github.technicallycoded:FoliaLib:main-SNAPSHOT"
compileOnly "org.spigotmc:spigot-api:1.21.3-R0.1-SNAPSHOT"

// Hooks
compileOnly 'io.th0rgal:oraxen:1.181.0'
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=1.4.4
version=2.0.0
92 changes: 51 additions & 41 deletions src/main/java/fr/traqueur/recipes/api/RecipesAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

import fr.traqueur.recipes.api.hook.Hook;
import fr.traqueur.recipes.impl.PrepareCraftListener;
import fr.traqueur.recipes.impl.domains.recipes.RecipeConfiguration;
import fr.traqueur.recipes.impl.domains.ItemRecipe;
import fr.traqueur.recipes.impl.domains.recipes.RecipeConfiguration;
import fr.traqueur.recipes.impl.updater.Updater;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.stream.Stream;

/**
Expand All @@ -38,19 +41,13 @@ public final class RecipesAPI {
*/
private final List<ItemRecipe> recipes;

/**
* The scheduler
*/
private final com.tcoded.folialib.impl.PlatformScheduler scheduler;


/**
* Create a new instance of RecipesAPI with yml support enabled
* @param plugin The plugin instance
* @param debug If the debug mode is enabled
*/
public RecipesAPI(JavaPlugin plugin, boolean debug) {
this(plugin, debug, true, null);
this(plugin, debug, true);
}

/**
Expand All @@ -59,49 +56,57 @@ public RecipesAPI(JavaPlugin plugin, boolean debug) {
* @param debug If the debug mode is enabled
* @param enableYmlSupport If the yml support is enabled
*/
public RecipesAPI(JavaPlugin plugin, boolean debug, boolean enableYmlSupport, com.tcoded.folialib.impl.PlatformScheduler scheduler) {
public RecipesAPI(JavaPlugin plugin, boolean debug, boolean enableYmlSupport) {
this.debug = debug;
this.plugin = plugin;
this.recipes = new ArrayList<>();
this.scheduler = scheduler;

RecipeType.registerPlugin(plugin);

plugin.getServer().getPluginManager().registerEvents(new PrepareCraftListener(this), plugin);
this.unregisterRecipes();
this.runNextTick(() -> {

if(this.debug) {
Hook.HOOKS.stream()
.filter(hook -> hook.isEnable(plugin))
.forEach(hook -> this.plugin.getLogger().info("Hook enabled: " + hook.getPluginName()));
}

if(enableYmlSupport) {
var recipeFolder = new File(plugin.getDataFolder(), "recipes/");
if (!recipeFolder.exists() && !recipeFolder.mkdirs()) {
plugin.getLogger().warning("Could not create recipes folder.");
return;
}
this.addConfiguredRecipes(recipeFolder);
if(enableYmlSupport) {
var recipeFolder = new File(plugin.getDataFolder(), "recipes/");
if (!recipeFolder.exists() && !recipeFolder.mkdirs()) {
plugin.getLogger().warning("Could not create recipes folder.");
return;
}
});
this.loadDefaultRecipes();
this.addConfiguredRecipes(recipeFolder);
}

if(this.debug) {
Hook.HOOKS.stream()
.filter(hook -> hook.isEnable(plugin))
.forEach(hook -> this.plugin.getLogger().info("Hook enabled: " + hook.getPluginName()));

Updater.update("RecipesAPI");
}
}

/**
* Run a task on the next tick
* @param runnable The task to run
* Load the default recipes from the jar
*/
private void runNextTick(Runnable runnable) {
//Permits to use FoliaLib's scheduler if it's present in the plugin
if(scheduler != null) {
this.scheduler.runNextTick((t) -> runnable.run());
} else {
Bukkit.getScheduler().runTaskLater(plugin, runnable, 1);
private void loadDefaultRecipes() {
try {
CodeSource src = getClass().getProtectionDomain().getCodeSource();
if (src != null) {
URL jar = src.getLocation();
try (JarInputStream jarStream = new JarInputStream(jar.openStream())) {
JarEntry entry;
while ((entry = jarStream.getNextJarEntry()) != null) {
if (entry.getName().startsWith("recipes/") && entry.getName().endsWith(".yml")) {
File outFile = new File(plugin.getDataFolder(), entry.getName());
if (!outFile.exists()) {
plugin.saveResource(entry.getName(), false);
}
}
}
}
}
} catch (IOException e) {
plugin.getLogger().warning("Could not load default recipes.");
plugin.getServer().getPluginManager().disablePlugin(plugin);
}
}

Expand All @@ -110,14 +115,16 @@ private void runNextTick(Runnable runnable) {
* @param recipeFolder The folder containing the recipes
*/
private void addConfiguredRecipes(File recipeFolder) {

try (Stream<Path> stream = Files.walk(recipeFolder.toPath())) {
stream.skip(1)
.map(Path::toFile)
.filter(File::isFile)
.filter(e -> e.getName().endsWith(".yml"))
.forEach(this::loadRecipe);
} catch (IOException exception) {
exception.printStackTrace();
plugin.getLogger().warning("Could not load recipes.");
plugin.getServer().getPluginManager().disablePlugin(plugin);
}
}

Expand All @@ -126,9 +133,6 @@ private void addConfiguredRecipes(File recipeFolder) {
* @param file The file to load the recipe from
*/
private void loadRecipe(File file) {
if(!new File(this.plugin.getDataFolder(), "recipes/" + file.getName()).exists()) {
this.plugin.saveResource("recipes/" + file.getName(), false);
}
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file);
var recipe = new RecipeConfiguration(this.plugin, file.getName().replace(".yml", ""), configuration)
.build();
Expand All @@ -140,17 +144,23 @@ private void loadRecipe(File file) {
*/
public void unregisterRecipes() {
for (ItemRecipe recipe : recipes) {
this.removeRecipe(recipe);
plugin.getServer().removeRecipe(recipe.getKey());
}
recipes.clear();
}

/**
* Add a recipe to the list of recipes
* @param recipe The recipe to add
*/
public void addRecipe(ItemRecipe recipe) {
if (recipes.stream().anyMatch(r -> r.getKey().equals(recipe.getKey()))) {
throw new IllegalArgumentException("Recipe already registered");
}
this.recipes.add(recipe);
plugin.getServer().addRecipe(recipe.toBukkitRecipe());
if(plugin.getServer().getRecipe(recipe.getKey()) == null) {
plugin.getServer().addRecipe(recipe.toBukkitRecipe());
}
if(this.debug) {
plugin.getLogger().info("Registering recipe: " + recipe.getKey());
}
Expand Down
28 changes: 0 additions & 28 deletions src/main/java/fr/traqueur/recipes/api/domains/BaseIngredient.java

This file was deleted.

35 changes: 25 additions & 10 deletions src/main/java/fr/traqueur/recipes/api/domains/Ingredient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,42 @@
import org.bukkit.inventory.RecipeChoice;

/**
* Represents an ingredient.
* Base class for ingredients.
*/
public interface Ingredient {
public abstract class Ingredient {

/**
* Check if the item is similar to the ingredient.
* @param item The item to check.
* @return true if the item is similar to the ingredient, false otherwise.
* The sign of the ingredient.
*/
boolean isSimilar(ItemStack item);
private final Character sign;

/**
* Get the choice of the ingredient.
* @return The choice of the ingredient.
* Constructor.
* @param sign The sign of the ingredient.
*/
RecipeChoice choice();
public Ingredient(Character sign) {
this.sign = sign;
}

/**
* Get the sign of the ingredient.
* @return The sign of the ingredient.
*/
Character sign();
public Character sign() {
return this.sign;
}


/**
* Check if the item is similar to the ingredient.
* @param item The item to check.
* @return true if the item is similar to the ingredient, false otherwise.
*/
public abstract boolean isSimilar(ItemStack item);

/**
* Get the choice of the ingredient.
* @return The choice of the ingredient.
*/
public abstract RecipeChoice choice();
}
6 changes: 3 additions & 3 deletions src/main/java/fr/traqueur/recipes/api/hook/Hook.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.traqueur.recipes.api.hook;

import fr.traqueur.recipes.api.domains.BaseIngredient;
import fr.traqueur.recipes.api.domains.Ingredient;
import fr.traqueur.recipes.impl.hook.Hooks;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
Expand Down Expand Up @@ -38,15 +38,15 @@ static void addHook(Hook hook) {
* @param sign The sign of the ingredient
* @return The ingredient
*/
BaseIngredient getIngredient(String data, Character sign);
Ingredient getIngredient(String data, Character sign);

/**
* Check if the plugin is enabled
* @param plugin The plugin which use the API
* @return If the plugin is enabled
*/
default boolean isEnable(JavaPlugin plugin) {
return plugin.getServer().getPluginManager().isPluginEnabled(getPluginName());
return plugin.getServer().getPluginManager().getPlugin(getPluginName()) != null;
}

ItemStack getItemStack(String resultPart);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
import fr.traqueur.recipes.api.domains.Ingredient;
import fr.traqueur.recipes.impl.domains.ItemRecipe;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockCookEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.event.inventory.PrepareSmithingEvent;
import org.bukkit.inventory.*;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.traqueur.recipes.impl.domains.ingredients;

import fr.traqueur.recipes.api.domains.BaseIngredient;
import fr.traqueur.recipes.api.domains.Ingredient;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
Expand All @@ -11,7 +11,7 @@
/**
* This class represents an ingredient that is an item stack
*/
public class ItemStackIngredient extends BaseIngredient {
public class ItemStackIngredient extends Ingredient {

/**
* The item of the ingredient
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package fr.traqueur.recipes.impl.domains.ingredients;

import fr.traqueur.recipes.api.domains.BaseIngredient;
import fr.traqueur.recipes.api.domains.Ingredient;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;

/**
* A material ingredient
*/
public class MaterialIngredient extends BaseIngredient {
public class MaterialIngredient extends Ingredient {

/**
* The material of the ingredient
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package fr.traqueur.recipes.impl.domains.ingredients;

import fr.traqueur.recipes.api.domains.BaseIngredient;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.Objects;

/**
* This class represents an ingredient that is an item stack with strict comparison
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.traqueur.recipes.impl.domains.ingredients;

import fr.traqueur.recipes.api.domains.BaseIngredient;
import fr.traqueur.recipes.api.domains.Ingredient;
import org.bukkit.Material;
import org.bukkit.Tag;
import org.bukkit.inventory.ItemStack;
Expand All @@ -9,7 +9,7 @@
/**
* This class represents an ingredient that is a tag
*/
public class TagIngredient extends BaseIngredient {
public class TagIngredient extends Ingredient {

/**
* The tag of the ingredient
Expand Down
Loading
Loading