Skip to content

Commit

Permalink
Merge branch 'recipe_type_impl'
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfyScript committed Feb 8, 2022
2 parents 4ac5f09 + cd22718 commit 8ce7b52
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 47 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

<groupId>com.wolfyscript.customcrafting</groupId>
<artifactId>customcrafting-spigot</artifactId>
<version>3.16.1.0</version>
<version>3.16.2.0</version>

<properties>
<!-- Generic properties -->
Expand All @@ -52,7 +52,7 @@
<dependency>
<groupId>com.wolfyscript.wolfyutilities</groupId>
<artifactId>wolfyutilities</artifactId>
<version>3.16-SNAPSHOT</version>
<version>3.16.0.0</version>
<scope>provided</scope>
</dependency>
<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ public void onEnable() {
configHandler.loadDefaults();
disableRecipesHandler = new DisableRecipesHandler(this);

writeSeparator();
registerListeners();
registerCommands();
registerInventories();
Expand Down Expand Up @@ -296,7 +295,6 @@ private void writeBanner() {
getLogger().info("| | | [__ | | | |\\/| | |__/ |__| |___ | | |\\ | | __ ");
getLogger().info("|___ |__| ___] | |__| | | |___ | \\ | | | | | | \\| |__]");
getLogger().info(() -> " v" + currentVersion + " " + (PREMIUM ? "Premium" : "Free"));
getLogger().info(" ");
}

public void writeSeparator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

public class LocalStorageLoader extends ResourceLoader {

private static final String PREFIX = "[LOCAL] ";
public static final File DATA_FOLDER = new File(CustomCrafting.inst().getDataFolder() + File.separator + "data");
private static final String ITEMS_FOLDER = "items";
private static final String RECIPES_FOLDER = "recipes";
Expand All @@ -65,23 +66,21 @@ public void load() {
* items/<folder>/<item_name>
*/
api.getConsole().info("- - - - [Local Storage] - - - -");
api.getConsole().info("Searching for namespaces...");
api.getConsole().info(PREFIX + "Looking through data folder...");
String[] dirs = DATA_FOLDER.list();
if (dirs != null) {
api.getConsole().info("Namespaces: [" + String.join(", ", dirs) + "]");
api.getConsole().info(" - ");
api.getConsole().info("Loading items...");
api.getConsole().info(PREFIX + "$msg.startup.recipes.items$");
for (String dir : dirs) {
loadItemsInNamespace(dir);
}
api.getConsole().info("Loading recipes...");
api.getConsole().info(PREFIX + "$msg.startup.recipes.recipes$");
new NewDataLoader(dirs).load();
//Loading old & legacy recipes
//The recipes are only loaded if they are not already loaded in previous stages! So if a new version of a recipe exists, then the older ones are ignored.
new OldDataLoader(dirs).load();
new LegacyDataLoader(dirs).load();

api.getConsole().info("Loaded " + customCrafting.getRegistries().getRecipes().values().size() + " recipes");
api.getConsole().info(PREFIX + "Loaded " + customCrafting.getRegistries().getRecipes().values().size() + " recipes");
api.getConsole().info("");
}
}
Expand Down Expand Up @@ -213,7 +212,7 @@ protected void load() {
for (String dir : dirs) {
loadRecipesInNamespace(dir); //Load new recipe format files
}
api.getConsole().getLogger().info(String.format("[DEFAULT] Loaded %d recipes; skipped: %d error/s, %d already existing", loaded.size(), skippedError.size(), skippedAlreadyExisting.size()));
api.getConsole().getLogger().info(String.format("[LOCAL] Loaded %d recipes; Skipped: %d error/s, %d already existing", loaded.size(), skippedError.size(), skippedAlreadyExisting.size()));
}

private void loadRecipesInNamespace(String namespace) {
Expand All @@ -226,7 +225,7 @@ private void loadRecipesInNamespace(String namespace) {
customCrafting.getRegistries().getRecipes().register(objectMapper.reader(injectableValues).readValue(file.toFile(), CustomRecipe.class));
loaded.add(namespacedKey);
} catch (IOException e) {
ChatUtils.sendRecipeItemLoadingError(namespacedKey.getNamespace(), namespacedKey.getKey(), "", e);
ChatUtils.sendRecipeItemLoadingError(PREFIX, namespacedKey.getNamespace(), namespacedKey.getKey(), e);
skippedError.add(namespacedKey);
}
} else {
Expand All @@ -253,7 +252,7 @@ protected void load() {
loadAndRegisterOldOrLegacyRecipe(RecipeType.Container.ELITE_CRAFTING, dir);
}
}
api.getConsole().getLogger().info(String.format("[LEGACY] Loaded %d recipes; skipped: %d error/s, %d already existing", loaded.size(), skippedError.size(), skippedAlreadyExisting.size()));
api.getConsole().getLogger().info(String.format("[LOCAL_LEGACY] Loaded %d recipes; Skipped: %d error/s, %d already existing", loaded.size(), skippedError.size(), skippedAlreadyExisting.size()));
}
}

Expand All @@ -272,7 +271,7 @@ protected void load() {
}
}
}
api.getConsole().getLogger().info(String.format("[OLD] Loaded %d recipes; skipped: %d error/s, %d already existing", loaded.size(), skippedError.size(), skippedAlreadyExisting.size()));
api.getConsole().getLogger().info(String.format("[LOCAL_OLD] Loaded %d recipes; Skipped: %d error/s, %d already existing", loaded.size(), skippedError.size(), skippedAlreadyExisting.size()));
}

protected List<File> getOldOrLegacyFiles(String subFolder, String type) {
Expand All @@ -298,12 +297,12 @@ protected void loadOldOrLegacyRecipeFiles(RecipeLoader<?> loader, List<File> fil
for (File file : files) {
var name = file.getName();
var namespacedKey = new NamespacedKey(customCrafting, namespace + "/" + name.substring(0, name.lastIndexOf(".")));
if (isReplaceData() || !customCrafting.getRegistries().getRecipes().has(namespacedKey)) {
if (!customCrafting.getRegistries().getRecipes().has(namespacedKey)) {
try {
customCrafting.getRegistries().getRecipes().register(loader.getInstance(namespacedKey, objectMapper.readTree(file)));
loaded.add(namespacedKey);
} catch (IOException | InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
ChatUtils.sendRecipeItemLoadingError(namespacedKey.getNamespace(), namespacedKey.getKey(), loader.getId(), e);
ChatUtils.sendRecipeItemLoadingError("[LOCAL] ", namespacedKey.getNamespace(), namespacedKey.getKey(), e);
skippedError.add(namespacedKey);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,25 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class SQLDatabaseLoader extends DatabaseLoader {

private static final String PREFIX = "[MYSQL] ";
protected List<NamespacedKey> loaded;
protected List<NamespacedKey> skippedError;
protected List<NamespacedKey> skippedAlreadyExisting;

private final SQLDataBase dataBase;

public SQLDatabaseLoader(CustomCrafting customCrafting) {
super(customCrafting, new NamespacedKey(customCrafting, "database_loader"));
this.dataBase = new SQLDataBase(api, config.getDatabaseHost(), config.getDatabaseSchema(), config.getDatabaseUsername(), config.getDatabasePassword(), config.getDatabasePort());
init();
this.loaded = new LinkedList<>();
this.skippedError = new LinkedList<>();
this.skippedAlreadyExisting = new LinkedList<>();
}

public void init() {
Expand Down Expand Up @@ -116,7 +126,10 @@ public boolean delete(CustomItem item) {
}

public void loadRecipes() {
api.getConsole().info("$msg.startup.recipes.recipes$");
loaded.clear();
skippedError.clear();
skippedAlreadyExisting.clear();
api.getConsole().info(PREFIX + "$msg.startup.recipes.recipes$");
try (PreparedStatement recipesQuery = dataBase.open().prepareStatement("SELECT * FROM customcrafting_recipes")) {
ResultSet resultSet = recipesQuery.executeQuery();
if (resultSet == null) {
Expand All @@ -125,20 +138,29 @@ public void loadRecipes() {
while (resultSet.next()) {
String namespace = resultSet.getString("rNamespace");
String key = resultSet.getString("rKey");
CustomRecipe<?> recipe = getRecipe(new NamespacedKey(customCrafting, namespace + "/" + key));
if (recipe != null) {
customCrafting.getRegistries().getRecipes().register(recipe);
NamespacedKey namespacedKey = new NamespacedKey(customCrafting, namespace + "/" + key);
if (isReplaceData() || !customCrafting.getRegistries().getRecipes().has(namespacedKey)) {
CustomRecipe<?> recipe = getRecipe(namespacedKey);
if (recipe != null) {
customCrafting.getRegistries().getRecipes().register(recipe);
loaded.add(namespacedKey);
} else {
skippedError.add(namespacedKey);
}
} else {
skippedAlreadyExisting.add(namespacedKey);
}
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
dataBase.close();
}
api.getConsole().getLogger().info(String.format(PREFIX + "Loaded %d recipes; Skipped: %d error/s, %d already existing", loaded.size(), skippedError.size(), skippedAlreadyExisting.size()));
}

public void loadItems() {
api.getConsole().info("$msg.startup.recipes.items$");
api.getConsole().info(PREFIX + "$msg.startup.recipes.items$");
try (PreparedStatement itemsQuery = dataBase.open().prepareStatement("SELECT * FROM customcrafting_items")) {
ResultSet resultSet = itemsQuery.executeQuery();
if (resultSet == null) return;
Expand All @@ -147,13 +169,16 @@ public void loadItems() {
String key = resultSet.getString("rKey");
String data = resultSet.getString("rData");
if (namespace != null && key != null && data != null && !data.equals("{}")) {
try {
api.getRegistries().getCustomItems().register(new NamespacedKey(customCrafting, namespace + "/" + key), JacksonUtil.getObjectMapper().readValue(data, CustomItem.class));
} catch (JsonProcessingException e) {
api.getConsole().info("Error loading item \"" + namespace + ":" + key + "\": " + e.getMessage());
NamespacedKey namespacedKey = new NamespacedKey(customCrafting, namespace + "/" + key);
if (isReplaceData() || !api.getRegistries().getCustomItems().has(namespacedKey)) {
try {
api.getRegistries().getCustomItems().register(new NamespacedKey(customCrafting, namespace + "/" + key), JacksonUtil.getObjectMapper().readValue(data, CustomItem.class));
} catch (JsonProcessingException e) {
api.getConsole().info(PREFIX + "Error loading item \"" + namespace + ":" + key + "\": " + e.getMessage());
}
}
} else {
api.getConsole().info("Error loading item \"" + namespace + ":" + key + "\". Invalid namespacedkey or data!");
api.getConsole().info(PREFIX + "Error loading item \"" + namespace + ":" + key + "\". Invalid namespacedkey or data!");
}
}
} catch (SQLException ex) {
Expand Down Expand Up @@ -202,7 +227,7 @@ public CustomRecipe<?> getRecipe(NamespacedKey namespacedKey) {
return loader.getInstance(namespacedKey, JacksonUtil.getObjectMapper().readTree(data));
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException | IOException e) {
ChatUtils.sendRecipeItemLoadingError(namespacedKey.getNamespace(), namespacedKey.getKey(), typeID, e);
ChatUtils.sendRecipeItemLoadingError(PREFIX, namespacedKey.getNamespace(), namespacedKey.getKey(), e);
}
}
resultSet.getStatement().close();
Expand All @@ -216,7 +241,7 @@ public void addRecipe(CustomRecipe<?> data) {
try {
PreparedStatement pState = dataBase.open().prepareStatement("INSERT INTO customcrafting_recipes (rNamespace, rKey, rType, rData) VALUES (?, ?, ?, ?)");
setNamespacedKey(pState, data.getNamespacedKey(), 1, 2);
pState.setString(3, "");
pState.setString(3, ""); //No longer save the type. The type is contained in the json data now.
pState.setString(4, JacksonUtil.getObjectMapper().writeValueAsString(data));
dataBase.executeAsyncUpdate(pState);
} catch (SQLException | JsonProcessingException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public class CustomRecipeSmithing extends CustomRecipe<CustomRecipeSmithing> {
public CustomRecipeSmithing(NamespacedKey namespacedKey, JsonNode node) {
super(namespacedKey, node);
this.type = RecipeType.SMITHING;
base = ItemLoader.loadIngredient(node.path(KEY_BASE));
addition = ItemLoader.loadIngredient(node.path(KEY_ADDITION));
setBase(ItemLoader.loadIngredient(node.path(KEY_BASE)));
setAddition(ItemLoader.loadIngredient(node.path(KEY_ADDITION)));
preserveEnchants = node.path("preserve_enchants").asBoolean(true);
preserveDamage = node.path("preserveDamage").asBoolean(true);
onlyChangeMaterial = node.path("onlyChangeMaterial").asBoolean(false);
Expand Down
21 changes: 4 additions & 17 deletions src/main/java/me/wolfyscript/customcrafting/utils/ChatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,27 +216,14 @@ public static void sendAttributeModifierManager(Player player) {
}, true));
}

public static void sendRecipeItemLoadingError(String namespace, String key, String type, Exception ex) {
api.getConsole().severe("-------------------------------------------------");
api.getConsole().severe("Error loading Contents for: " + namespace + ":" + key);
api.getConsole().severe(" Type: " + type);
if (ex.getMessage() != null) {
api.getConsole().severe(" Message: " + ex.getMessage());
}
public static void sendRecipeItemLoadingError(String prefix, String namespace, String key, Exception ex) {
api.getConsole().warn(prefix + "[Error] Invalid Recipe: \"" + namespace + ":" + key + "\": " + (ex.getMessage() != null ? ex.getMessage() : ""));
if (ex.getCause() != null) {
api.getConsole().severe(" Cause: " + ex.getCause().getMessage());
api.getConsole().warn(prefix + "[Error] Caused by: " + ex.getCause().getMessage());
}
api.getConsole().severe("Please check the config of the recipe.");
if (CustomCrafting.inst().getConfigHandler().getConfig().isPrintingStacktrace()) {
api.getConsole().severe("------------------[StackTrace]-------------------");
api.getConsole().warn("------------------[StackTrace]-------------------");
ex.printStackTrace();
if (ex.getCause() != null) {
api.getConsole().severe("Caused StackTrace: ");
ex.getCause().printStackTrace();
}
} else {
api.getConsole().severe("For more info enable stacktraces in the config ('data.print_stacktrace')");
}
api.getConsole().severe("-------------------------------------------------");
}
}

0 comments on commit 8ce7b52

Please sign in to comment.