2013-05-23 05:01:19 +00:00
package net.minecraftforge.fluids ;
import java.util.HashMap ;
import java.util.Map ;
2015-04-08 03:06:55 +00:00
import java.util.Map.Entry ;
2013-05-23 05:01:19 +00:00
import net.minecraft.block.Block ;
2014-01-30 02:34:23 +00:00
import net.minecraft.init.Blocks ;
2013-11-12 09:05:13 +00:00
import net.minecraft.util.StatCollector ;
2013-05-23 05:01:19 +00:00
import net.minecraftforge.common.MinecraftForge ;
import com.google.common.collect.BiMap ;
import com.google.common.collect.HashBiMap ;
import com.google.common.collect.ImmutableMap ;
2014-01-30 02:34:23 +00:00
import com.google.common.collect.Maps ;
2013-05-23 05:01:19 +00:00
2015-04-06 02:37:40 +00:00
import cpw.mods.fml.common.FMLLog ;
2013-12-17 01:18:48 +00:00
import cpw.mods.fml.common.eventhandler.Event ;
2013-05-23 05:01:19 +00:00
/ * *
* Handles Fluid registrations . Fluids MUST be registered in order to function .
2013-07-27 21:36:27 +00:00
*
2013-05-23 05:01:19 +00:00
* @author King Lemming , CovertJaguar ( LiquidDictionary )
2013-07-27 21:36:27 +00:00
*
2013-05-23 05:01:19 +00:00
* /
2013-07-17 04:40:49 +00:00
public abstract class FluidRegistry
{
2013-05-23 05:01:19 +00:00
static int maxID = 0 ;
2015-03-25 05:27:15 +00:00
static BiMap < String , Fluid > fluids = HashBiMap . create ( ) ;
static BiMap < Fluid , Integer > fluidIDs = HashBiMap . create ( ) ;
2015-04-08 03:06:55 +00:00
static BiMap < Integer , String > fluidNames = HashBiMap . create ( ) ; //Caching this just makes some other calls faster
2013-09-05 16:08:16 +00:00
static BiMap < Block , Fluid > fluidBlocks ;
2015-04-05 20:13:17 +00:00
2013-11-12 09:05:13 +00:00
public static final Fluid WATER = new Fluid ( " water " ) {
@Override
public String getLocalizedName ( ) {
return StatCollector . translateToLocal ( " tile.water.name " ) ;
}
2014-02-05 08:05:37 +00:00
} . setBlock ( Blocks . water ) . setUnlocalizedName ( Blocks . water . getUnlocalizedName ( ) ) ;
2015-04-05 20:13:17 +00:00
2013-11-12 09:05:13 +00:00
public static final Fluid LAVA = new Fluid ( " lava " ) {
@Override
public String getLocalizedName ( ) {
return StatCollector . translateToLocal ( " tile.lava.name " ) ;
}
2014-02-05 08:05:37 +00:00
} . setBlock ( Blocks . lava ) . setLuminosity ( 15 ) . setDensity ( 3000 ) . setViscosity ( 6000 ) . setTemperature ( 1300 ) . setUnlocalizedName ( Blocks . lava . getUnlocalizedName ( ) ) ;
2013-05-23 05:01:19 +00:00
public static int renderIdFluid = - 1 ;
2013-07-17 04:40:49 +00:00
static
{
2013-05-23 05:01:19 +00:00
registerFluid ( WATER ) ;
registerFluid ( LAVA ) ;
}
2013-07-17 04:40:49 +00:00
private FluidRegistry ( ) { }
2013-05-23 05:01:19 +00:00
/ * *
* Called by Forge to prepare the ID map for server - > client sync .
2014-01-30 02:34:23 +00:00
* Modders , DO NOT call this .
* /
2015-03-25 05:27:15 +00:00
public static void initFluidIDs ( BiMap < Fluid , Integer > newfluidIDs )
2013-07-17 04:40:49 +00:00
{
2013-05-23 05:01:19 +00:00
maxID = newfluidIDs . size ( ) ;
fluidIDs . clear ( ) ;
fluidIDs . putAll ( newfluidIDs ) ;
2015-04-08 03:06:55 +00:00
fluidNames . clear ( ) ;
for ( Entry < Fluid , Integer > e : fluidIDs . entrySet ( ) )
fluidNames . put ( e . getValue ( ) , e . getKey ( ) . getName ( ) ) ;
2013-05-23 05:01:19 +00:00
}
/ * *
* Register a new Fluid . If a fluid with the same name already exists , registration is denied .
2013-07-27 21:36:27 +00:00
*
2013-05-23 05:01:19 +00:00
* @param fluid
* The fluid to register .
* @return True if the fluid was successfully registered ; false if there is a name clash .
2014-01-30 02:34:23 +00:00
* /
2013-07-17 04:40:49 +00:00
public static boolean registerFluid ( Fluid fluid )
{
2015-04-08 11:26:40 +00:00
if ( fluids . containsKey ( fluid . getName ( ) ) )
2013-07-17 04:40:49 +00:00
{
2015-04-06 02:37:40 +00:00
FMLLog . bigWarning ( " Duplicate registration attempt for fluid %s (type %s) has occurred. This is not a problem itself, but subsequent failed FluidStacks might be a result if not handled properly " , fluid . getName ( ) , fluid . getClass ( ) . getName ( ) ) ;
2013-05-23 05:01:19 +00:00
return false ;
}
fluids . put ( fluid . getName ( ) , fluid ) ;
2015-03-25 05:27:15 +00:00
fluidIDs . put ( fluid , + + maxID ) ;
2015-04-08 03:06:55 +00:00
fluidNames . put ( maxID , fluid . getName ( ) ) ;
2013-05-23 05:01:19 +00:00
MinecraftForge . EVENT_BUS . post ( new FluidRegisterEvent ( fluid . getName ( ) , maxID ) ) ;
return true ;
}
2013-07-17 04:40:49 +00:00
public static boolean isFluidRegistered ( Fluid fluid )
{
2015-03-25 05:27:15 +00:00
return fluids . containsKey ( fluid . getName ( ) ) ;
2013-05-23 05:01:19 +00:00
}
2013-07-17 04:40:49 +00:00
public static boolean isFluidRegistered ( String fluidName )
{
2015-03-25 05:27:15 +00:00
return fluids . containsKey ( fluidName ) ;
2013-05-23 05:01:19 +00:00
}
2013-07-17 04:40:49 +00:00
public static Fluid getFluid ( String fluidName )
{
2013-05-23 05:01:19 +00:00
return fluids . get ( fluidName ) ;
}
2013-07-17 04:40:49 +00:00
public static Fluid getFluid ( int fluidID )
{
2015-03-25 05:27:15 +00:00
return fluidIDs . inverse ( ) . get ( fluidID ) ;
2013-05-23 05:01:19 +00:00
}
2015-03-25 05:27:15 +00:00
public static int getFluidID ( Fluid fluid )
2013-07-17 04:40:49 +00:00
{
2015-03-25 05:27:15 +00:00
return fluidIDs . get ( fluid ) ;
2013-05-23 05:01:19 +00:00
}
2015-03-25 05:27:15 +00:00
public static int getFluidID ( String fluidName )
2013-07-17 04:40:49 +00:00
{
2015-03-25 05:27:15 +00:00
return fluidIDs . get ( getFluid ( fluidName ) ) ;
2013-05-23 05:01:19 +00:00
}
2015-04-08 03:06:55 +00:00
@Deprecated //Remove in 1.8.3
public static String getFluidName ( int fluidID )
{
return fluidNames . get ( fluidID ) ;
}
2015-03-25 05:27:15 +00:00
public static String getFluidName ( Fluid fluid )
{
return fluids . inverse ( ) . get ( fluid ) ;
}
public static String getFluidName ( FluidStack stack )
2013-07-17 04:40:49 +00:00
{
2015-03-25 05:27:15 +00:00
return getFluidName ( stack . fluid ) ;
2013-05-23 05:01:19 +00:00
}
2013-07-17 04:40:49 +00:00
public static FluidStack getFluidStack ( String fluidName , int amount )
{
2015-03-25 05:27:15 +00:00
if ( ! fluids . containsKey ( fluidName ) )
2013-07-17 04:40:49 +00:00
{
2013-05-23 05:01:19 +00:00
return null ;
}
2015-03-25 05:27:15 +00:00
return new FluidStack ( getFluid ( fluidName ) , amount ) ;
2013-05-23 05:01:19 +00:00
}
/ * *
* Returns a read - only map containing Fluid Names and their associated Fluids .
2014-01-30 02:34:23 +00:00
* /
2013-07-17 04:40:49 +00:00
public static Map < String , Fluid > getRegisteredFluids ( )
{
2013-05-23 05:01:19 +00:00
return ImmutableMap . copyOf ( fluids ) ;
}
2015-04-08 03:06:55 +00:00
/ * *
* Returns a read - only map containing Fluid Names and their associated IDs .
* /
@Deprecated //Change return type to <Fluid, Integer> in 1.8.3
public static Map < String , Integer > getRegisteredFluidIDs ( )
{
return ImmutableMap . copyOf ( fluidNames . inverse ( ) ) ;
}
2013-05-23 05:01:19 +00:00
/ * *
2015-03-25 05:27:15 +00:00
* Returns a read - only map containing Fluid IDs and their associated Fluids .
2015-04-08 03:06:55 +00:00
* In 1 . 8 . 3 , this will change to just ' getRegisteredFluidIDs '
2014-01-30 02:34:23 +00:00
* /
2015-04-08 03:06:55 +00:00
public static Map < Fluid , Integer > getRegisteredFluidIDsByFluid ( )
2013-07-17 04:40:49 +00:00
{
2013-05-23 05:01:19 +00:00
return ImmutableMap . copyOf ( fluidIDs ) ;
}
2013-09-05 16:08:16 +00:00
public static Fluid lookupFluidForBlock ( Block block )
{
if ( fluidBlocks = = null )
{
2014-04-03 06:53:05 +00:00
BiMap < Block , Fluid > tmp = HashBiMap . create ( ) ;
2013-09-05 16:08:16 +00:00
for ( Fluid fluid : fluids . values ( ) )
{
2014-01-30 02:34:23 +00:00
if ( fluid . canBePlacedInWorld ( ) & & fluid . getBlock ( ) ! = null )
2013-09-05 16:08:16 +00:00
{
2014-04-03 06:53:05 +00:00
tmp . put ( fluid . getBlock ( ) , fluid ) ;
2013-09-05 16:08:16 +00:00
}
}
2014-04-03 06:53:05 +00:00
fluidBlocks = tmp ;
2013-09-05 16:08:16 +00:00
}
return fluidBlocks . get ( block ) ;
}
2015-04-05 20:13:17 +00:00
2013-07-17 04:40:49 +00:00
public static class FluidRegisterEvent extends Event
{
2013-05-23 05:01:19 +00:00
public final String fluidName ;
public final int fluidID ;
2013-07-17 04:40:49 +00:00
public FluidRegisterEvent ( String fluidName , int fluidID )
{
2013-05-23 05:01:19 +00:00
this . fluidName = fluidName ;
this . fluidID = fluidID ;
}
}
2014-01-30 02:34:23 +00:00
public static int getMaxID ( )
{
return maxID ;
}
2013-05-23 05:01:19 +00:00
}