Applied-Energistics-2-tiler.../src/main/java/appeng/core/features/registries/GrinderRecipeManager.java

300 lines
8.3 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
2014-09-24 02:26:27 +02:00
package appeng.core.features.registries;
2014-09-24 02:26:27 +02:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
2015-12-24 02:07:03 +01:00
2014-09-24 02:26:27 +02:00
import appeng.api.features.IGrinderEntry;
import appeng.api.features.IGrinderRegistry;
import appeng.core.AEConfig;
import appeng.core.AELog;
import appeng.core.features.registries.entries.AppEngGrinderRecipe;
import appeng.recipes.ores.IOreListener;
import appeng.recipes.ores.OreDictionaryHandler;
import appeng.util.Platform;
public final class GrinderRecipeManager implements IGrinderRegistry, IOreListener
{
private final List<IGrinderEntry> recipes;
private final Map<ItemStack, String> ores;
private final Map<ItemStack, String> ingots;
private final Map<String, ItemStack> dusts;
2014-09-24 02:26:27 +02:00
public GrinderRecipeManager()
2014-09-24 02:26:27 +02:00
{
this.recipes = new ArrayList<IGrinderEntry>();
this.ores = new HashMap<ItemStack, String>();
this.ingots = new HashMap<ItemStack, String>();
this.dusts = new HashMap<String, ItemStack>();
2014-09-24 02:26:27 +02:00
this.addOre( "Coal", new ItemStack( Items.COAL ) );
this.addOre( "Charcoal", new ItemStack( Items.COAL, 1, 1 ) );
2014-09-24 02:26:27 +02:00
this.addOre( "NetherQuartz", new ItemStack( Blocks.QUARTZ_ORE ) );
this.addIngot( "NetherQuartz", new ItemStack( Items.QUARTZ ) );
2014-09-24 02:26:27 +02:00
this.addOre( "Gold", new ItemStack( Blocks.GOLD_ORE ) );
this.addIngot( "Gold", new ItemStack( Items.GOLD_INGOT ) );
2014-09-24 02:26:27 +02:00
this.addOre( "Iron", new ItemStack( Blocks.IRON_ORE ) );
this.addIngot( "Iron", new ItemStack( Items.IRON_INGOT ) );
2014-09-24 02:26:27 +02:00
this.addOre( "Obsidian", new ItemStack( Blocks.OBSIDIAN ) );
2014-09-24 02:26:27 +02:00
this.addIngot( "Ender", new ItemStack( Items.ENDER_PEARL ) );
this.addIngot( "EnderPearl", new ItemStack( Items.ENDER_PEARL ) );
2014-09-24 02:26:27 +02:00
this.addIngot( "Wheat", new ItemStack( Items.WHEAT ) );
2014-09-24 02:26:27 +02:00
2015-01-01 22:13:10 +01:00
OreDictionaryHandler.INSTANCE.observe( this );
2014-09-24 02:26:27 +02:00
}
@Override
public List<IGrinderEntry> getRecipes()
{
2014-12-29 15:13:47 +01:00
this.log( "API - getRecipes" );
return this.recipes;
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public void addRecipe( final ItemStack in, final ItemStack out, final int cost )
2014-09-24 02:26:27 +02:00
{
if( in == null || out == null )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.log( "Invalid Grinder Recipe Specified." );
2014-09-24 02:26:27 +02:00
return;
}
2014-12-29 15:13:47 +01:00
this.log( "Allow Grinding of " + Platform.getItemDisplayName( in ) + " to " + Platform.getItemDisplayName( out ) + " for " + cost );
this.injectRecipe( new AppEngGrinderRecipe( this.copy( in ), this.copy( out ), cost ) );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public void addRecipe( final ItemStack in, final ItemStack out, final ItemStack optional, final float chance, final int cost )
2014-09-24 02:26:27 +02:00
{
if( in == null || ( optional == null && out == null ) )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.log( "Invalid Grinder Recipe Specified." );
2014-09-24 02:26:27 +02:00
return;
}
this.log( "Allow Grinding of " + Platform.getItemDisplayName( in ) + " to " + Platform.getItemDisplayName( out ) + " with optional " + Platform.getItemDisplayName( optional ) + " for " + cost );
2014-12-29 15:13:47 +01:00
this.injectRecipe( new AppEngGrinderRecipe( this.copy( in ), this.copy( out ), this.copy( optional ), chance, cost ) );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public void addRecipe( final ItemStack in, final ItemStack out, final ItemStack optional, final float chance, final ItemStack optional2, final float chance2, final int cost )
2014-09-24 02:26:27 +02:00
{
if( in == null || ( optional == null && out == null && optional2 == null ) )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.log( "Invalid Grinder Recipe Specified." );
2014-09-24 02:26:27 +02:00
return;
}
this.log( "Allow Grinding of " + Platform.getItemDisplayName( in ) + " to " + Platform.getItemDisplayName( out ) + " with optional " + Platform.getItemDisplayName( optional ) + " for " + cost );
this.injectRecipe( new AppEngGrinderRecipe( this.copy( in ), this.copy( out ), this.copy( optional ), this.copy( optional2 ), chance, chance2, cost ) );
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
private void injectRecipe( final AppEngGrinderRecipe appEngGrinderRecipe )
{
2015-09-30 14:24:40 +02:00
for( final IGrinderEntry gr : this.recipes )
2015-04-29 02:30:53 +02:00
{
if( Platform.itemComparisons().isSameItem( gr.getInput(), appEngGrinderRecipe.getInput() ) )
2015-04-29 02:30:53 +02:00
{
return;
2015-04-29 02:30:53 +02:00
}
}
this.recipes.add( appEngGrinderRecipe );
}
2015-09-30 14:24:40 +02:00
private ItemStack copy( final ItemStack is )
{
if( is != null )
2015-04-29 02:30:53 +02:00
{
return is.copy();
2015-04-29 02:30:53 +02:00
}
return null;
}
2014-09-24 02:26:27 +02:00
@Override
2015-09-30 14:24:40 +02:00
public IGrinderEntry getRecipeForInput( final ItemStack input )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.log( "Looking up recipe for " + Platform.getItemDisplayName( input ) );
if( input != null )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
for( final IGrinderEntry r : this.recipes )
2014-09-24 02:26:27 +02:00
{
if( Platform.itemComparisons().isEqualItem( input, r.getInput() ) )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.log( "Recipe for " + input.getUnlocalizedName() + " found " + Platform.getItemDisplayName( r.getOutput() ) );
2014-09-24 02:26:27 +02:00
return r;
}
}
2014-12-29 15:13:47 +01:00
this.log( "Could not find recipe for " + Platform.getItemDisplayName( input ) );
2014-09-24 02:26:27 +02:00
}
return null;
}
private void log( final String o )
2014-09-24 02:26:27 +02:00
{
AELog.grinder( o );
}
2015-09-30 14:24:40 +02:00
private int getDustToOreRatio( final String name )
2014-09-24 02:26:27 +02:00
{
if( name.equals( "Obsidian" ) )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return 1;
2015-04-29 02:30:53 +02:00
}
if( name.equals( "Charcoal" ) )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return 1;
2015-04-29 02:30:53 +02:00
}
if( name.equals( "Coal" ) )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return 1;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
return 2;
}
2015-09-30 14:24:40 +02:00
private void addOre( final String name, final ItemStack item )
2014-09-24 02:26:27 +02:00
{
if( item == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return;
2015-04-29 02:30:53 +02:00
}
2014-12-29 15:13:47 +01:00
this.log( "Adding Ore - " + name + " : " + Platform.getItemDisplayName( item ) );
2014-09-24 02:26:27 +02:00
this.ores.put( item, name );
2014-09-24 02:26:27 +02:00
if( this.dusts.containsKey( name ) )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final ItemStack is = this.dusts.get( name ).copy();
final int ratio = this.getDustToOreRatio( name );
if( ratio > 1 )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final ItemStack extra = is.copy();
2014-09-24 02:26:27 +02:00
extra.stackSize = ratio - 1;
this.addRecipe( item, is, extra, (float) ( AEConfig.instance().getOreDoublePercentage() / 100.0 ), 8 );
2014-09-24 02:26:27 +02:00
}
else
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.addRecipe( item, is, 8 );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
}
2015-09-30 14:24:40 +02:00
private void addIngot( final String name, final ItemStack item )
2014-09-24 02:26:27 +02:00
{
if( item == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return;
2015-04-29 02:30:53 +02:00
}
2014-12-29 15:13:47 +01:00
this.log( "Adding Ingot - " + name + " : " + Platform.getItemDisplayName( item ) );
2014-09-24 02:26:27 +02:00
this.ingots.put( item, name );
2014-09-24 02:26:27 +02:00
if( this.dusts.containsKey( name ) )
2014-09-24 02:26:27 +02:00
{
this.addRecipe( item, this.dusts.get( name ), 4 );
2014-09-24 02:26:27 +02:00
}
}
2015-09-30 14:24:40 +02:00
private void addDust( final String name, final ItemStack item )
2014-09-24 02:26:27 +02:00
{
if( item == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return;
2015-04-29 02:30:53 +02:00
}
if( this.dusts.containsKey( name ) )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.log( "Rejecting Dust - " + name + " : " + Platform.getItemDisplayName( item ) );
2014-09-24 02:26:27 +02:00
return;
}
2014-12-29 15:13:47 +01:00
this.log( "Adding Dust - " + name + " : " + Platform.getItemDisplayName( item ) );
2014-09-24 02:26:27 +02:00
this.dusts.put( name, item );
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
for( final Entry<ItemStack, String> d : this.ores.entrySet() )
2015-04-29 02:30:53 +02:00
{
if( name.equals( d.getValue() ) )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final ItemStack is = item.copy();
2014-09-24 02:26:27 +02:00
is.stackSize = 1;
2015-09-30 14:24:40 +02:00
final int ratio = this.getDustToOreRatio( name );
if( ratio > 1 )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final ItemStack extra = is.copy();
2014-09-24 02:26:27 +02:00
extra.stackSize = ratio - 1;
this.addRecipe( d.getKey(), is, extra, (float) ( AEConfig.instance().getOreDoublePercentage() / 100.0 ), 8 );
2014-09-24 02:26:27 +02:00
}
else
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.addRecipe( d.getKey(), is, 8 );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
for( final Entry<ItemStack, String> d : this.ingots.entrySet() )
2015-04-29 02:30:53 +02:00
{
if( name.equals( d.getValue() ) )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.addRecipe( d.getKey(), item, 4 );
2015-04-29 02:30:53 +02:00
}
}
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public void oreRegistered( final String name, final ItemStack item )
2014-09-24 02:26:27 +02:00
{
if( name.startsWith( "ore" ) || name.startsWith( "crystal" ) || name.startsWith( "gem" ) || name.startsWith( "ingot" ) || name.startsWith( "dust" ) )
2014-09-24 02:26:27 +02:00
{
for( final String ore : AEConfig.instance().getGrinderOres() )
2014-09-24 02:26:27 +02:00
{
if( name.equals( "ore" + ore ) )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.addOre( ore, item );
2014-09-24 02:26:27 +02:00
}
else if( name.equals( "crystal" + ore ) || name.equals( "ingot" + ore ) || name.equals( "gem" + ore ) )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.addIngot( ore, item );
2014-09-24 02:26:27 +02:00
}
else if( name.equals( "dust" + ore ) )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.addDust( ore, item );
2014-09-24 02:26:27 +02:00
}
}
}
}
}