2016-01-05 04:47:35 +01:00
/ *
2016-05-16 22:52:27 +02:00
* This file ( " CrusherRecipeRegistry.java " ) is part of the Actually Additions mod for Minecraft .
2016-01-05 04:47:35 +01:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http : //ellpeck.de/actaddlicense
2016-01-05 04:47:35 +01:00
* View the source code at https : //github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015 - 2017 Ellpeck
2016-01-05 04:47:35 +01:00
* /
package de.ellpeck.actuallyadditions.mod.recipe ;
2018-06-23 01:39:30 +02:00
import java.util.ArrayList ;
import java.util.List ;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI ;
import de.ellpeck.actuallyadditions.api.recipe.CrusherRecipe ;
2018-05-10 11:38:58 +02:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions ;
2016-06-05 12:15:02 +02:00
import de.ellpeck.actuallyadditions.mod.config.values.ConfigStringListValues ;
2016-11-16 20:31:16 +01:00
import de.ellpeck.actuallyadditions.mod.util.StackUtil ;
2016-10-19 15:57:30 +02:00
import net.minecraft.item.Item ;
2016-01-05 04:47:35 +01:00
import net.minecraft.item.ItemStack ;
import net.minecraftforge.oredict.OreDictionary ;
2018-06-23 01:39:30 +02:00
import net.minecraftforge.oredict.OreIngredient ;
2017-06-29 18:36:33 +02:00
2016-01-05 04:47:35 +01:00
2016-06-17 23:50:38 +02:00
public final class CrusherRecipeRegistry {
2016-01-05 04:47:35 +01:00
2018-06-23 01:39:30 +02:00
public static final ArrayList < SearchCase > SEARCH_CASES = new ArrayList < > ( ) ;
2016-01-05 04:47:35 +01:00
public static void registerFinally ( ) {
ArrayList < String > oresNoResult = new ArrayList < String > ( ) ;
2016-05-19 20:05:12 +02:00
int recipeStartedAt = ActuallyAdditionsAPI . CRUSHER_RECIPES . size ( ) ;
2016-01-05 04:47:35 +01:00
for ( String ore : OreDictionary . getOreNames ( ) ) {
if ( ! hasException ( ore ) ) {
2016-05-20 17:55:33 +02:00
for ( SearchCase theCase : SEARCH_CASES ) {
2016-01-05 04:47:35 +01:00
if ( ore . length ( ) > theCase . theCase . length ( ) ) {
if ( ore . substring ( 0 , theCase . theCase . length ( ) ) . equals ( theCase . theCase ) ) {
2018-06-23 01:39:30 +02:00
String outputOre = theCase . resultPreString + ore . substring ( theCase . theCase . length ( ) ) ;
List < ItemStack > outputs = OreDictionary . getOres ( outputOre , false ) ;
ItemStack output = outputs . isEmpty ( ) ? ItemStack . EMPTY : outputs . get ( 0 ) . copy ( ) ;
output . setCount ( theCase . resultAmount ) ;
if ( output . isEmpty ( ) ) {
2016-11-16 19:08:56 +01:00
if ( ! oresNoResult . contains ( ore ) ) {
oresNoResult . add ( ore ) ;
}
2018-06-23 01:39:30 +02:00
} else ActuallyAdditionsAPI . addCrusherRecipe ( new OreIngredient ( ore ) , output , StackUtil . getEmpty ( ) , 0 ) ;
2016-01-05 04:47:35 +01:00
}
}
}
}
}
ArrayList < String > addedRecipes = new ArrayList < String > ( ) ;
2016-05-19 20:05:12 +02:00
for ( int i = recipeStartedAt ; i < ActuallyAdditionsAPI . CRUSHER_RECIPES . size ( ) ; i + + ) {
CrusherRecipe recipe = ActuallyAdditionsAPI . CRUSHER_RECIPES . get ( i ) ;
2018-06-23 01:39:30 +02:00
addedRecipes . add ( recipe . getInput ( ) . getMatchingStacks ( ) + " -> " + recipe . getOutputOne ( ) ) ;
2016-01-05 04:47:35 +01:00
}
2018-05-10 11:38:58 +02:00
ActuallyAdditions . LOGGER . debug ( " Added " + addedRecipes . size ( ) + " Crusher Recipes automatically: " + addedRecipes ) ;
ActuallyAdditions . LOGGER . debug ( " Couldn't add " + oresNoResult . size ( ) + " Crusher Recipes automatically, either because the inputs were missing outputs, or because they exist already: " + oresNoResult ) ;
2018-06-23 01:39:30 +02:00
removeDuplicateRecipes ( ) ;
2016-01-05 04:47:35 +01:00
}
2018-06-23 01:39:30 +02:00
public static void removeDuplicateRecipes ( ) {
ArrayList < CrusherRecipe > usable = new ArrayList < > ( ) ;
ArrayList < CrusherRecipe > removed = new ArrayList < > ( ) ;
for ( CrusherRecipe r : ActuallyAdditionsAPI . CRUSHER_RECIPES ) {
boolean canUse = true ;
2018-06-23 04:24:32 +02:00
if ( r . getInput ( ) . getMatchingStacks ( ) . length = = 0 ) canUse = false ;
else for ( CrusherRecipe re : usable ) {
2018-06-23 01:39:30 +02:00
if ( re . getInput ( ) . apply ( r . getInput ( ) . getMatchingStacks ( ) [ 0 ] ) ) canUse = false ;
}
if ( canUse ) usable . add ( r ) ;
else removed . add ( r ) ;
}
ActuallyAdditionsAPI . CRUSHER_RECIPES . clear ( ) ;
ActuallyAdditionsAPI . CRUSHER_RECIPES . addAll ( usable ) ;
ActuallyAdditions . LOGGER . debug ( String . format ( " Removed %s crusher recipes that had dupliate inputs, %s remain. " , removed . size ( ) , usable . size ( ) ) ) ;
}
2016-01-05 04:47:35 +01:00
2016-11-23 17:46:08 +01:00
public static boolean hasBlacklistedOutput ( ItemStack output , String [ ] config ) {
2016-11-16 20:31:16 +01:00
if ( StackUtil . isValid ( output ) ) {
2016-10-19 15:57:30 +02:00
Item item = output . getItem ( ) ;
if ( item ! = null ) {
String reg = item . getRegistryName ( ) . toString ( ) ;
2016-11-23 17:46:08 +01:00
for ( String conf : config ) {
2016-10-19 15:57:30 +02:00
String confReg = conf ;
int meta = 0 ;
if ( conf . contains ( " @ " ) ) {
try {
String [ ] split = conf . split ( " @ " ) ;
confReg = split [ 0 ] ;
meta = Integer . parseInt ( split [ 1 ] ) ;
}
catch ( Exception e ) {
2018-05-10 11:38:58 +02:00
ActuallyAdditions . LOGGER . warn ( " A config option appears to be incorrect: The entry " + conf + " can't be parsed! " ) ;
2016-10-19 15:57:30 +02:00
}
}
if ( reg . equals ( confReg ) & & output . getItemDamage ( ) = = meta ) {
return true ;
}
}
return false ;
}
}
return true ;
}
2016-01-05 04:47:35 +01:00
private static boolean hasException ( String ore ) {
2016-06-05 12:15:02 +02:00
for ( String conf : ConfigStringListValues . CRUSHER_RECIPE_EXCEPTIONS . getValue ( ) ) {
2016-01-05 04:47:35 +01:00
if ( conf . equals ( ore ) ) {
return true ;
}
}
return false ;
}
public static CrusherRecipe getRecipeFromInput ( ItemStack input ) {
2018-06-23 01:39:30 +02:00
for ( CrusherRecipe recipe : ActuallyAdditionsAPI . CRUSHER_RECIPES )
if ( recipe . matches ( input ) ) return recipe ;
2016-01-05 04:47:35 +01:00
return null ;
}
public static class SearchCase {
2016-05-19 20:05:12 +02:00
final String theCase ;
final int resultAmount ;
final String resultPreString ;
2016-01-05 04:47:35 +01:00
public SearchCase ( String theCase , int resultAmount ) {
this ( theCase , resultAmount , " dust " ) ;
}
public SearchCase ( String theCase , int resultAmount , String resultPreString ) {
this . theCase = theCase ;
this . resultAmount = resultAmount ;
this . resultPreString = resultPreString ;
}
}
}