Applied-Energistics-2-tiler.../src/main/java/appeng/recipes/ores/OreDictionaryHandler.java

123 lines
2.9 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.recipes.ores;
2014-09-24 02:26:27 +02:00
import java.util.ArrayList;
import java.util.List;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
2015-06-16 02:44:59 +02:00
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
2014-09-24 02:26:27 +02:00
import net.minecraftforge.oredict.OreDictionary;
2015-12-24 02:07:03 +01:00
2014-09-24 02:26:27 +02:00
import appeng.core.AELog;
import appeng.recipes.game.IRecipeBakeable;
2014-09-24 02:26:27 +02:00
public class OreDictionaryHandler
{
2015-01-01 22:13:10 +01:00
public static final OreDictionaryHandler INSTANCE = new OreDictionaryHandler();
2014-09-24 02:26:27 +02:00
private final List<IOreListener> oreListeners = new ArrayList<IOreListener>();
2014-09-24 02:26:27 +02:00
private boolean enableRebaking = false;
@SubscribeEvent
2015-09-30 14:24:40 +02:00
public void onOreDictionaryRegister( final OreDictionary.OreRegisterEvent event )
{
if( event.Name == null || event.Ore == null )
2015-04-29 02:30:53 +02:00
{
return;
2015-04-29 02:30:53 +02:00
}
if( this.shouldCare( event.Name ) )
{
2015-09-30 14:24:40 +02:00
for( final IOreListener v : this.oreListeners )
2015-04-29 02:30:53 +02:00
{
v.oreRegistered( event.Name, event.Ore );
2015-04-29 02:30:53 +02:00
}
}
if( this.enableRebaking )
2015-04-29 02:30:53 +02:00
{
this.bakeRecipes();
2015-04-29 02:30:53 +02:00
}
}
2014-09-24 02:26:27 +02:00
/**
* Just limit what items are sent to the final listeners, I got sick of strange items showing up...
2015-02-03 12:04:13 +01:00
*
2014-09-27 23:17:47 +02:00
* @param name name about cared item
*
2014-09-27 23:17:47 +02:00
* @return true if it should care
2014-09-24 02:26:27 +02:00
*/
2015-09-30 14:24:40 +02:00
private boolean shouldCare( final String name )
2014-09-24 02:26:27 +02:00
{
return true;
}
public void bakeRecipes()
2014-09-24 02:26:27 +02:00
{
this.enableRebaking = true;
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
for( final Object o : CraftingManager.getInstance().getRecipeList() )
2014-09-24 02:26:27 +02:00
{
if( o instanceof IRecipeBakeable )
{
try
{
( (IRecipeBakeable) o ).bake();
}
2015-09-30 14:24:40 +02:00
catch( final Throwable e )
{
AELog.debug( e );
}
}
2014-09-24 02:26:27 +02:00
}
}
/**
* Adds a new IOreListener and immediately notifies it of any previous ores, any ores added latter will be added at
* that point.
2015-02-03 12:04:13 +01:00
*
2014-09-27 23:17:47 +02:00
* @param n to be added ore listener
2014-09-24 02:26:27 +02:00
*/
2015-09-30 14:24:40 +02:00
public void observe( final IOreListener n )
2014-09-24 02:26:27 +02:00
{
this.oreListeners.add( n );
2014-09-24 02:26:27 +02:00
// notify the listener of any ore already in existence.
2015-09-30 14:24:40 +02:00
for( final String name : OreDictionary.getOreNames() )
2014-09-24 02:26:27 +02:00
{
if( name != null && this.shouldCare( name ) )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
for( final ItemStack item : OreDictionary.getOres( name ) )
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
n.oreRegistered( name, item );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
}
}
}
}