Skip to content

Commit d451490

Browse files
committed
Feat/improve (#27)
* fix: ia hook (#23) * feat: improve * feat: remove some useless usage of variable
1 parent 7d11b10 commit d451490

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

src/main/java/fr/traqueur/recipes/api/RecipeType.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fr.traqueur.recipes.api;
22

33
import org.bukkit.NamespacedKey;
4+
import org.bukkit.plugin.Plugin;
45
import org.bukkit.plugin.java.JavaPlugin;
56

67
import java.util.List;
@@ -47,7 +48,7 @@ public enum RecipeType {
4748
/**
4849
* The plugin that registered this enum.
4950
*/
50-
private static JavaPlugin plugin;
51+
private static Plugin plugin;
5152

5253
/**
5354
* The maximum number of ingredients that can be used in this recipe.
@@ -83,7 +84,7 @@ public NamespacedKey getNamespacedKey(String key) {
8384
* Registers the plugin that is using this enum.
8485
* @param plugin the plugin
8586
*/
86-
public static void registerPlugin(JavaPlugin plugin) {
87+
public static void registerPlugin(Plugin plugin) {
8788
RecipeType.plugin = plugin;
8889
}
8990

src/main/java/fr/traqueur/recipes/api/RecipesAPI.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import fr.traqueur.recipes.impl.domains.recipes.RecipeConfiguration;
77
import fr.traqueur.recipes.impl.updater.Updater;
88
import org.bukkit.configuration.file.YamlConfiguration;
9+
import org.bukkit.plugin.Plugin;
910
import org.bukkit.plugin.java.JavaPlugin;
1011

1112
import java.io.File;
@@ -29,7 +30,7 @@ public final class RecipesAPI {
2930
/**
3031
* The plugin instance
3132
*/
32-
private final JavaPlugin plugin;
33+
private final Plugin plugin;
3334

3435
/**
3536
* If the debug mode is enabled
@@ -46,7 +47,7 @@ public final class RecipesAPI {
4647
* @param plugin The plugin instance
4748
* @param debug If the debug mode is enabled
4849
*/
49-
public RecipesAPI(JavaPlugin plugin, boolean debug) {
50+
public RecipesAPI(Plugin plugin, boolean debug) {
5051
this(plugin, debug, true);
5152
}
5253

@@ -56,7 +57,7 @@ public RecipesAPI(JavaPlugin plugin, boolean debug) {
5657
* @param debug If the debug mode is enabled
5758
* @param enableYmlSupport If the yml support is enabled
5859
*/
59-
public RecipesAPI(JavaPlugin plugin, boolean debug, boolean enableYmlSupport) {
60+
public RecipesAPI(Plugin plugin, boolean debug, boolean enableYmlSupport) {
6061
this.debug = debug;
6162
this.plugin = plugin;
6263
this.recipes = new ArrayList<>();
@@ -77,7 +78,7 @@ public RecipesAPI(JavaPlugin plugin, boolean debug, boolean enableYmlSupport) {
7778

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

8384
Updater.update("RecipesAPI");
@@ -134,7 +135,7 @@ private void addConfiguredRecipes(File recipeFolder) {
134135
*/
135136
private void loadRecipe(File file) {
136137
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file);
137-
var recipe = new RecipeConfiguration(this.plugin, file.getName().replace(".yml", ""), configuration)
138+
var recipe = new RecipeConfiguration(file.getName().replace(".yml", ""), configuration)
138139
.build();
139140
this.addRecipe(recipe);
140141
}
@@ -190,7 +191,7 @@ public List<ItemRecipe> getRecipes() {
190191
* Get the plugin instance
191192
* @return The plugin instance
192193
*/
193-
public JavaPlugin getPlugin() {
194+
public Plugin getPlugin() {
194195
return plugin;
195196
}
196197

src/main/java/fr/traqueur/recipes/api/hook/Hook.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import fr.traqueur.recipes.api.domains.Ingredient;
44
import fr.traqueur.recipes.impl.hook.Hooks;
5+
import org.bukkit.Bukkit;
56
import org.bukkit.inventory.ItemStack;
67
import org.bukkit.plugin.java.JavaPlugin;
78

@@ -42,12 +43,16 @@ static void addHook(Hook hook) {
4243

4344
/**
4445
* Check if the plugin is enabled
45-
* @param plugin The plugin which use the API
4646
* @return If the plugin is enabled
4747
*/
48-
default boolean isEnable(JavaPlugin plugin) {
49-
return plugin.getServer().getPluginManager().getPlugin(getPluginName()) != null;
48+
default boolean isEnable() {
49+
return Bukkit.getPluginManager().getPlugin(getPluginName()) != null;
5050
}
5151

52+
/**
53+
* Get the ItemStack from a result part
54+
* @param resultPart The result part to get the ItemStack from
55+
* @return The ItemStack from the result part
56+
*/
5257
ItemStack getItemStack(String resultPart);
5358
}

src/main/java/fr/traqueur/recipes/impl/domains/recipes/RecipeConfiguration.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,20 @@ public class RecipeConfiguration implements Recipe {
8080

8181
/**
8282
* The constructor of the recipe.
83-
* @param plugin the plugin of the recipe.
8483
* @param name the name of the recipe.
8584
* @param configuration the configuration of the recipe.
8685
*/
87-
public RecipeConfiguration(JavaPlugin plugin, String name, YamlConfiguration configuration) {
88-
this(plugin, name, "", configuration);
86+
public RecipeConfiguration(String name, YamlConfiguration configuration) {
87+
this(name, "", configuration);
8988
}
9089

9190
/**
9291
* The constructor of the recipe.
93-
* @param plugin the plugin of the recipe.
9492
* @param name the name of the recipe.
9593
* @param path the path of the recipe.
9694
* @param configuration the configuration of the recipe.
9795
*/
98-
public RecipeConfiguration(JavaPlugin plugin, String name, String path, YamlConfiguration configuration) {
96+
public RecipeConfiguration(String name, String path, YamlConfiguration configuration) {
9997
this.name = name.replace(".yml", "");
10098
if(!path.endsWith(".") && !path.isEmpty()) {
10199
path += ".";
@@ -140,7 +138,7 @@ public RecipeConfiguration(JavaPlugin plugin, String name, String path, YamlConf
140138
yield new ItemStackIngredient(this.getItemStack(data[1]), sign);
141139
}
142140
default -> Hook.HOOKS.stream()
143-
.filter(hook -> hook.isEnable(plugin))
141+
.filter(Hook::isEnable)
144142
.filter(hook -> hook.getPluginName().equalsIgnoreCase(data[0]))
145143
.findFirst()
146144
.orElseThrow(() -> new IllegalArgumentException("The data " + data[0] + " isn't valid."))
@@ -163,7 +161,7 @@ public RecipeConfiguration(JavaPlugin plugin, String name, String path, YamlConf
163161
case "material" -> new ItemStack(this.getMaterial(resultParts[1]));
164162
case "item", "base64" -> this.getItemStack(resultParts[1]);
165163
default -> Hook.HOOKS.stream()
166-
.filter(hook -> hook.isEnable(plugin))
164+
.filter(Hook::isEnable)
167165
.filter(hook -> hook.getPluginName().equalsIgnoreCase(resultParts[0]))
168166
.findFirst()
169167
.orElseThrow(() -> new IllegalArgumentException("The result " + strItem + " isn't valid."))

0 commit comments

Comments
 (0)