Skip to content

Commit f94aa5f

Browse files
committed
feat(core) : breaking version to remove useless tick later action
1 parent 0655cbb commit f94aa5f

File tree

19 files changed

+109
-135
lines changed

19 files changed

+109
-135
lines changed

build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ repositories {
2525
}
2626

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

3130
// Hooks
3231
compileOnly 'io.th0rgal:oraxen:1.181.0'

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=1.4.4
1+
version=2.0.0

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

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22

33
import fr.traqueur.recipes.api.hook.Hook;
44
import fr.traqueur.recipes.impl.PrepareCraftListener;
5-
import fr.traqueur.recipes.impl.domains.recipes.RecipeConfiguration;
65
import fr.traqueur.recipes.impl.domains.ItemRecipe;
6+
import fr.traqueur.recipes.impl.domains.recipes.RecipeConfiguration;
77
import fr.traqueur.recipes.impl.updater.Updater;
8-
import org.bukkit.Bukkit;
98
import org.bukkit.configuration.file.YamlConfiguration;
109
import org.bukkit.plugin.java.JavaPlugin;
1110

1211
import java.io.File;
1312
import java.io.IOException;
13+
import java.net.URL;
1414
import java.nio.file.Files;
1515
import java.nio.file.Path;
16+
import java.security.CodeSource;
1617
import java.util.ArrayList;
1718
import java.util.List;
19+
import java.util.jar.JarEntry;
20+
import java.util.jar.JarInputStream;
1821
import 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
}

src/main/java/fr/traqueur/recipes/api/domains/BaseIngredient.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/main/java/fr/traqueur/recipes/api/domains/Ingredient.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,42 @@
44
import org.bukkit.inventory.RecipeChoice;
55

66
/**
7-
* Represents an ingredient.
7+
* Base class for ingredients.
88
*/
9-
public interface Ingredient {
9+
public abstract class Ingredient {
1010

1111
/**
12-
* Check if the item is similar to the ingredient.
13-
* @param item The item to check.
14-
* @return true if the item is similar to the ingredient, false otherwise.
12+
* The sign of the ingredient.
1513
*/
16-
boolean isSimilar(ItemStack item);
14+
private final Character sign;
1715

1816
/**
19-
* Get the choice of the ingredient.
20-
* @return The choice of the ingredient.
17+
* Constructor.
18+
* @param sign The sign of the ingredient.
2119
*/
22-
RecipeChoice choice();
20+
public Ingredient(Character sign) {
21+
this.sign = sign;
22+
}
2323

2424
/**
2525
* Get the sign of the ingredient.
2626
* @return The sign of the ingredient.
2727
*/
28-
Character sign();
28+
public Character sign() {
29+
return this.sign;
30+
}
31+
32+
33+
/**
34+
* Check if the item is similar to the ingredient.
35+
* @param item The item to check.
36+
* @return true if the item is similar to the ingredient, false otherwise.
37+
*/
38+
public abstract boolean isSimilar(ItemStack item);
2939

40+
/**
41+
* Get the choice of the ingredient.
42+
* @return The choice of the ingredient.
43+
*/
44+
public abstract RecipeChoice choice();
3045
}

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

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

3-
import fr.traqueur.recipes.api.domains.BaseIngredient;
3+
import fr.traqueur.recipes.api.domains.Ingredient;
44
import fr.traqueur.recipes.impl.hook.Hooks;
55
import org.bukkit.inventory.ItemStack;
66
import org.bukkit.plugin.java.JavaPlugin;
@@ -38,15 +38,15 @@ static void addHook(Hook hook) {
3838
* @param sign The sign of the ingredient
3939
* @return The ingredient
4040
*/
41-
BaseIngredient getIngredient(String data, Character sign);
41+
Ingredient getIngredient(String data, Character sign);
4242

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

5252
ItemStack getItemStack(String resultPart);

src/main/java/fr/traqueur/recipes/impl/PrepareCraftListener.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
import fr.traqueur.recipes.api.domains.Ingredient;
66
import fr.traqueur.recipes.impl.domains.ItemRecipe;
77
import org.bukkit.Material;
8-
import org.bukkit.NamespacedKey;
98
import org.bukkit.event.EventHandler;
109
import org.bukkit.event.Listener;
1110
import org.bukkit.event.block.BlockCookEvent;
1211
import org.bukkit.event.inventory.PrepareItemCraftEvent;
1312
import org.bukkit.event.inventory.PrepareSmithingEvent;
1413
import org.bukkit.inventory.*;
1514

16-
import java.util.*;
15+
import java.util.ArrayList;
16+
import java.util.Arrays;
17+
import java.util.List;
18+
import java.util.Objects;
1719
import java.util.concurrent.atomic.AtomicBoolean;
1820

1921
/**

src/main/java/fr/traqueur/recipes/impl/domains/ingredients/ItemStackIngredient.java

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

3-
import fr.traqueur.recipes.api.domains.BaseIngredient;
3+
import fr.traqueur.recipes.api.domains.Ingredient;
44
import org.bukkit.NamespacedKey;
55
import org.bukkit.inventory.ItemStack;
66
import org.bukkit.inventory.RecipeChoice;
@@ -11,7 +11,7 @@
1111
/**
1212
* This class represents an ingredient that is an item stack
1313
*/
14-
public class ItemStackIngredient extends BaseIngredient {
14+
public class ItemStackIngredient extends Ingredient {
1515

1616
/**
1717
* The item of the ingredient

src/main/java/fr/traqueur/recipes/impl/domains/ingredients/MaterialIngredient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package fr.traqueur.recipes.impl.domains.ingredients;
22

3-
import fr.traqueur.recipes.api.domains.BaseIngredient;
3+
import fr.traqueur.recipes.api.domains.Ingredient;
44
import org.bukkit.Material;
55
import org.bukkit.inventory.ItemStack;
66
import org.bukkit.inventory.RecipeChoice;
77

88
/**
99
* A material ingredient
1010
*/
11-
public class MaterialIngredient extends BaseIngredient {
11+
public class MaterialIngredient extends Ingredient {
1212

1313
/**
1414
* The material of the ingredient

src/main/java/fr/traqueur/recipes/impl/domains/ingredients/StrictItemStackIngredient.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
package fr.traqueur.recipes.impl.domains.ingredients;
22

3-
import fr.traqueur.recipes.api.domains.BaseIngredient;
4-
import org.bukkit.NamespacedKey;
53
import org.bukkit.inventory.ItemStack;
64
import org.bukkit.inventory.RecipeChoice;
7-
import org.bukkit.inventory.meta.ItemMeta;
8-
9-
import java.util.Objects;
105

116
/**
127
* This class represents an ingredient that is an item stack with strict comparison

0 commit comments

Comments
 (0)