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 ;
2020-05-04 22:24:09 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2018-07-06 15:30:00 +02:00
import java.sql.Connection ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
2019-05-26 20:14:53 +02:00
public class CraftingManager {
2020-05-04 22:24:09 +02:00
private static final Logger LOGGER = LoggerFactory . getLogger ( CraftingManager . class ) ;
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
private final THashMap < Item , CraftingAltar > altars ;
2019-05-26 20:14:53 +02:00
public CraftingManager ( ) {
2018-09-28 21:25:00 +02:00
this . altars = new THashMap < > ( ) ;
2018-07-06 15:30:00 +02:00
this . reload ( ) ;
}
2019-05-26 20:14:53 +02:00
public void reload ( ) {
2018-07-06 15:30:00 +02:00
this . dispose ( ) ;
2019-05-26 20:14:53 +02: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 {
2020-05-04 22:24:09 +02:00
LOGGER . error ( " Unknown ingredient item " + set . getInt ( " crafting_recipes_ingredients.item_id " ) ) ;
2019-05-26 20:14:53 +02:00
}
}
}
}
}
} catch ( SQLException e ) {
2020-05-04 22:24:09 +02:00
LOGGER . error ( " Caught SQL exception " , e ) ;
2019-05-26 20:14:53 +02:00
}
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public int getRecipesWithItemCount ( final Item item ) {
2018-07-06 15:30:00 +02:00
final int [ ] i = { 0 } ;
2019-05-26 20:14:53 +02:00
synchronized ( this . altars ) {
this . altars . forEachValue ( new TObjectProcedure < CraftingAltar > ( ) {
2018-07-06 15:30:00 +02:00
@Override
2019-05-26 20:14:53 +02:00
public boolean execute ( CraftingAltar altar ) {
if ( altar . hasIngredient ( item ) ) {
2018-07-06 15:30:00 +02:00
i [ 0 ] + + ;
}
return true ;
}
} ) ;
}
return i [ 0 ] ;
}
2019-05-26 20:14:53 +02:00
public CraftingRecipe getRecipe ( String recipeName ) {
CraftingRecipe recipe ;
for ( CraftingAltar altar : this . altars . values ( ) ) {
recipe = altar . getRecipe ( recipeName ) ;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
if ( recipe ! = null ) {
return recipe ;
}
}
2018-07-06 15:30:00 +02:00
return null ;
}
2019-05-26 20:14:53 +02:00
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
}
2019-05-26 20:14:53 +02:00
public void dispose ( ) {
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 ) {
2020-05-04 22:24:09 +02:00
LOGGER . error ( " Caught SQL exception " , e ) ;
2019-05-26 20:14:53 +02:00
}
2018-07-06 15:30:00 +02:00
this . altars . clear ( ) ;
}
}