22
33import fr .traqueur .recipes .api .hook .Hook ;
44import fr .traqueur .recipes .impl .PrepareCraftListener ;
5- import fr .traqueur .recipes .impl .domains .recipes .RecipeConfiguration ;
65import fr .traqueur .recipes .impl .domains .ItemRecipe ;
6+ import fr .traqueur .recipes .impl .domains .recipes .RecipeConfiguration ;
77import fr .traqueur .recipes .impl .updater .Updater ;
8- import org .bukkit .Bukkit ;
98import org .bukkit .configuration .file .YamlConfiguration ;
109import org .bukkit .plugin .java .JavaPlugin ;
1110
1211import java .io .File ;
1312import java .io .IOException ;
13+ import java .net .URL ;
1414import java .nio .file .Files ;
1515import java .nio .file .Path ;
16+ import java .security .CodeSource ;
1617import java .util .ArrayList ;
1718import java .util .List ;
19+ import java .util .jar .JarEntry ;
20+ import java .util .jar .JarInputStream ;
1821import java .util .stream .Stream ;
1922
2023/**
@@ -38,19 +41,13 @@ public final class RecipesAPI {
3841 */
3942 private final List <ItemRecipe > recipes ;
4043
41- /**
42- * The scheduler
43- */
44- private final com .tcoded .folialib .impl .PlatformScheduler scheduler ;
45-
46-
4744 /**
4845 * Create a new instance of RecipesAPI with yml support enabled
4946 * @param plugin The plugin instance
5047 * @param debug If the debug mode is enabled
5148 */
5249 public RecipesAPI (JavaPlugin plugin , boolean debug ) {
53- this (plugin , debug , true , null );
50+ this (plugin , debug , true );
5451 }
5552
5653 /**
@@ -59,49 +56,57 @@ public RecipesAPI(JavaPlugin plugin, boolean debug) {
5956 * @param debug If the debug mode is enabled
6057 * @param enableYmlSupport If the yml support is enabled
6158 */
62- public RecipesAPI (JavaPlugin plugin , boolean debug , boolean enableYmlSupport , com . tcoded . folialib . impl . PlatformScheduler scheduler ) {
59+ public RecipesAPI (JavaPlugin plugin , boolean debug , boolean enableYmlSupport ) {
6360 this .debug = debug ;
6461 this .plugin = plugin ;
6562 this .recipes = new ArrayList <>();
66- this .scheduler = scheduler ;
6763
6864 RecipeType .registerPlugin (plugin );
6965
7066 plugin .getServer ().getPluginManager ().registerEvents (new PrepareCraftListener (this ), plugin );
71- this .unregisterRecipes ();
72- this .runNextTick (() -> {
73-
74- if (this .debug ) {
75- Hook .HOOKS .stream ()
76- .filter (hook -> hook .isEnable (plugin ))
77- .forEach (hook -> this .plugin .getLogger ().info ("Hook enabled: " + hook .getPluginName ()));
78- }
7967
80- if (enableYmlSupport ) {
81- var recipeFolder = new File (plugin .getDataFolder (), "recipes/" );
82- if (!recipeFolder .exists () && !recipeFolder .mkdirs ()) {
83- plugin .getLogger ().warning ("Could not create recipes folder." );
84- return ;
85- }
86- this .addConfiguredRecipes (recipeFolder );
68+ if (enableYmlSupport ) {
69+ var recipeFolder = new File (plugin .getDataFolder (), "recipes/" );
70+ if (!recipeFolder .exists () && !recipeFolder .mkdirs ()) {
71+ plugin .getLogger ().warning ("Could not create recipes folder." );
72+ return ;
8773 }
88- });
74+ this .loadDefaultRecipes ();
75+ this .addConfiguredRecipes (recipeFolder );
76+ }
8977
9078 if (this .debug ) {
79+ Hook .HOOKS .stream ()
80+ .filter (hook -> hook .isEnable (plugin ))
81+ .forEach (hook -> this .plugin .getLogger ().info ("Hook enabled: " + hook .getPluginName ()));
82+
9183 Updater .update ("RecipesAPI" );
9284 }
9385 }
9486
9587 /**
96- * Run a task on the next tick
97- * @param runnable The task to run
88+ * Load the default recipes from the jar
9889 */
99- private void runNextTick (Runnable runnable ) {
100- //Permits to use FoliaLib's scheduler if it's present in the plugin
101- if (scheduler != null ) {
102- this .scheduler .runNextTick ((t ) -> runnable .run ());
103- } else {
104- Bukkit .getScheduler ().runTaskLater (plugin , runnable , 1 );
90+ private void loadDefaultRecipes () {
91+ try {
92+ CodeSource src = getClass ().getProtectionDomain ().getCodeSource ();
93+ if (src != null ) {
94+ URL jar = src .getLocation ();
95+ try (JarInputStream jarStream = new JarInputStream (jar .openStream ())) {
96+ JarEntry entry ;
97+ while ((entry = jarStream .getNextJarEntry ()) != null ) {
98+ if (entry .getName ().startsWith ("recipes/" ) && entry .getName ().endsWith (".yml" )) {
99+ File outFile = new File (plugin .getDataFolder (), entry .getName ());
100+ if (!outFile .exists ()) {
101+ plugin .saveResource (entry .getName (), false );
102+ }
103+ }
104+ }
105+ }
106+ }
107+ } catch (IOException e ) {
108+ plugin .getLogger ().warning ("Could not load default recipes." );
109+ plugin .getServer ().getPluginManager ().disablePlugin (plugin );
105110 }
106111 }
107112
@@ -110,14 +115,16 @@ private void runNextTick(Runnable runnable) {
110115 * @param recipeFolder The folder containing the recipes
111116 */
112117 private void addConfiguredRecipes (File recipeFolder ) {
118+
113119 try (Stream <Path > stream = Files .walk (recipeFolder .toPath ())) {
114120 stream .skip (1 )
115121 .map (Path ::toFile )
116122 .filter (File ::isFile )
117123 .filter (e -> e .getName ().endsWith (".yml" ))
118124 .forEach (this ::loadRecipe );
119125 } catch (IOException exception ) {
120- exception .printStackTrace ();
126+ plugin .getLogger ().warning ("Could not load recipes." );
127+ plugin .getServer ().getPluginManager ().disablePlugin (plugin );
121128 }
122129 }
123130
@@ -126,9 +133,6 @@ private void addConfiguredRecipes(File recipeFolder) {
126133 * @param file The file to load the recipe from
127134 */
128135 private void loadRecipe (File file ) {
129- if (!new File (this .plugin .getDataFolder (), "recipes/" + file .getName ()).exists ()) {
130- this .plugin .saveResource ("recipes/" + file .getName (), false );
131- }
132136 YamlConfiguration configuration = YamlConfiguration .loadConfiguration (file );
133137 var recipe = new RecipeConfiguration (this .plugin , file .getName ().replace (".yml" , "" ), configuration )
134138 .build ();
@@ -140,17 +144,23 @@ private void loadRecipe(File file) {
140144 */
141145 public void unregisterRecipes () {
142146 for (ItemRecipe recipe : recipes ) {
143- this . removeRecipe (recipe );
147+ plugin . getServer (). removeRecipe (recipe . getKey () );
144148 }
149+ recipes .clear ();
145150 }
146151
147152 /**
148153 * Add a recipe to the list of recipes
149154 * @param recipe The recipe to add
150155 */
151156 public void addRecipe (ItemRecipe recipe ) {
157+ if (recipes .stream ().anyMatch (r -> r .getKey ().equals (recipe .getKey ()))) {
158+ throw new IllegalArgumentException ("Recipe already registered" );
159+ }
152160 this .recipes .add (recipe );
153- plugin .getServer ().addRecipe (recipe .toBukkitRecipe ());
161+ if (plugin .getServer ().getRecipe (recipe .getKey ()) == null ) {
162+ plugin .getServer ().addRecipe (recipe .toBukkitRecipe ());
163+ }
154164 if (this .debug ) {
155165 plugin .getLogger ().info ("Registering recipe: " + recipe .getKey ());
156166 }
0 commit comments