Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/crafting/CraftingManager.java

153 lines
3.8 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.crafting;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.items.Item;
import gnu.trove.map.hash.THashMap;
import gnu.trove.procedure.TObjectProcedure;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class CraftingManager
{
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
private final THashMap<Item, CraftingAltar> altars;
public CraftingManager()
{
2018-09-28 21:25:00 +02:00
this.altars = new THashMap<>();
2018-07-06 15:30:00 +02:00
this.reload();
}
public void reload()
{
this.dispose();
2018-12-22 11:39:00 +01:00
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM crafting_altars_recipes " +
"INNER JOIN crafting_recipes ON crafting_altars_recipes.recipe_id = crafting_recipes.id " +
"INNER JOIN crafting_recipes_ingredients ON crafting_recipes.id = crafting_recipes_ingredients.recipe_id " +
"WHERE crafting_recipes.enabled = ? ORDER BY altar_id ASC"))
{
statement.setString(1, "1");
try (ResultSet set = statement.executeQuery())
{
while (set.next())
{
Item item = Emulator.getGameEnvironment().getItemManager().getItem(set.getInt("altar_id"));
if (item != null)
{
if (!this.altars.containsKey(item))
{
this.altars.put(item, new CraftingAltar(item));
}
CraftingAltar altar = this.altars.get(item);
if (altar != null)
{
CraftingRecipe recipe = altar.getRecipe(set.getInt("crafting_recipes_ingredients.recipe_id"));
if (recipe == null)
{
recipe = new CraftingRecipe(set);
altar.addRecipe(recipe);
}
Item ingredientItem = Emulator.getGameEnvironment().getItemManager().getItem(set.getInt("crafting_recipes_ingredients.item_id"));
if (ingredientItem != null)
{
recipe.addIngredient(ingredientItem, set.getInt("crafting_recipes_ingredients.amount"));
altar.addIngredient(ingredientItem);
}
else
{
Emulator.getLogging().logErrorLine("Unknown ingredient item " + set.getInt("crafting_recipes_ingredients.item_id"));
}
}
}
}
}
}
catch (SQLException e)
{
Emulator.getLogging().logSQLException(e);
}
2018-07-06 15:30:00 +02:00
}
public int getRecipesWithItemCount(final Item item)
{
final int[] i = {0};
synchronized (this.altars)
{
this.altars.forEachValue(new TObjectProcedure<CraftingAltar>()
{
@Override
public boolean execute(CraftingAltar altar)
{
if (altar.hasIngredient(item))
{
i[0]++;
}
return true;
}
});
}
return i[0];
}
public CraftingRecipe getRecipe(String recipeName)
{
2019-03-18 02:22:00 +01:00
CraftingRecipe recipe;
2018-12-22 11:39:00 +01:00
for (CraftingAltar altar : this.altars.values())
{
recipe = altar.getRecipe(recipeName);
2018-07-06 15:30:00 +02:00
2018-12-22 11:39:00 +01:00
if (recipe != null)
{
return recipe;
}
}
2018-07-06 15:30:00 +02:00
return null;
}
public CraftingAltar getAltar(Item item)
{
2018-12-22 11:39:00 +01:00
return this.altars.get(item);
2018-07-06 15:30:00 +02:00
}
public void dispose()
{
2018-12-22 11:39:00 +01:00
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE crafting_recipes SET remaining = ? WHERE id = ? LIMIT 1"))
{
for (CraftingAltar altar : this.altars.values())
{
for (CraftingRecipe recipe : altar.getRecipes())
{
if (recipe.isLimited())
{
statement.setInt(1, recipe.getRemaining());
statement.setInt(2, recipe.getId());
statement.addBatch();
}
}
}
statement.executeBatch();
}
catch (SQLException e)
{
Emulator.getLogging().logSQLException(e);
}
2018-07-06 15:30:00 +02:00
this.altars.clear();
}
}