2016-06-22 23:49:47 -04:00
/*
* Minecraft Forge
2019-02-10 17:57:03 -05:00
* Copyright (c) 2016-2019.
2016-06-22 23:49:47 -04:00
*
* This library 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 version 2.1
* of the License.
*
* This library 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 this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
2017-06-19 15:02:18 -07:00
package net.minecraftforge.registries ;
2014-05-24 22:32:24 -04:00
import java.lang.reflect.Field ;
2017-06-19 15:02:18 -07:00
import java.lang.reflect.Modifier ;
2016-10-10 07:34:38 +11:00
import java.util.Collections ;
import java.util.LinkedList ;
import java.util.Queue ;
2019-01-16 12:28:13 -08:00
import java.util.function.Consumer ;
import java.util.function.Predicate ;
2014-09-22 22:01:24 -07:00
2015-11-28 03:01:31 -05:00
import net.minecraft.util.ResourceLocation ;
2019-01-09 20:57:01 -08:00
import net.minecraft.util.ResourceLocationException ;
2014-09-22 22:01:24 -07:00
2017-01-11 15:17:56 -08:00
import javax.annotation.Nullable ;
2018-09-06 04:39:01 -07:00
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
2016-10-09 15:57:18 -07:00
@SuppressWarnings ( "rawtypes" )
2019-01-16 12:28:13 -08:00
public class ObjectHolderRef implements Consumer < Predicate < ResourceLocation >>
2017-06-19 15:02:18 -07:00
{
2018-09-06 04:39:01 -07:00
private static final Logger LOGGER = LogManager . getLogger ();
2014-05-24 22:32:24 -04:00
private Field field ;
2015-11-28 03:01:31 -05:00
private ResourceLocation injectedObject ;
2016-10-10 07:34:38 +11:00
private boolean isValid ;
2017-06-19 15:02:18 -07:00
private ForgeRegistry <?> registry ;
2014-05-24 22:32:24 -04:00
2019-01-09 20:57:01 -08:00
public ObjectHolderRef ( Field field , ResourceLocation injectedObject )
2014-05-24 22:32:24 -04:00
{
2019-01-09 20:57:01 -08:00
this ( field , injectedObject . toString (), false );
}
2016-10-10 07:34:38 +11:00
2019-01-09 20:57:01 -08:00
ObjectHolderRef ( Field field , String injectedObject , boolean extractFromExistingValues )
{
this . registry = getRegistryForType ( field );
2014-05-24 22:32:24 -04:00
this . field = field ;
2016-10-10 07:34:38 +11:00
this . isValid = registry != null ;
2019-01-09 20:57:01 -08:00
2014-05-24 22:32:24 -04:00
if ( extractFromExistingValues )
{
try
{
Object existing = field . get ( null );
2014-05-26 10:58:13 -04:00
// nothing is ever allowed to replace AIR
2019-01-09 20:57:01 -08:00
if ( ! isValid || ( existing == null || existing == registry . getDefault ()))
2014-05-26 10:58:13 -04:00
{
this . injectedObject = null ;
this . field = null ;
2016-10-10 07:34:38 +11:00
this . isValid = false ;
2014-05-26 10:58:13 -04:00
return ;
}
else
{
2018-09-06 04:39:01 -07:00
this . injectedObject = (( IForgeRegistryEntry ) existing ). getRegistryName ();
2014-05-26 10:58:13 -04:00
}
2017-06-19 15:02:18 -07:00
}
2017-07-13 05:50:28 +01:00
catch ( IllegalAccessException e )
2014-05-24 22:32:24 -04:00
{
2017-06-19 15:02:18 -07:00
throw new RuntimeException ( e );
2014-05-24 22:32:24 -04:00
}
}
else
{
2019-01-09 20:57:01 -08:00
try
{
this . injectedObject = new ResourceLocation ( injectedObject );
}
catch ( ResourceLocationException e )
{
throw new IllegalArgumentException ( "Invalid @ObjectHolder annotation on \"" + field . toString () + "\"" , e );
}
2014-05-24 22:32:24 -04:00
}
if ( this . injectedObject == null || ! isValid ())
{
2016-10-10 07:34:38 +11:00
throw new IllegalStateException ( String . format ( "The ObjectHolder annotation cannot apply to a field that does not map to a registry. Ensure the registry was created during the RegistryEvent.NewRegistry event. (found : %s at %s.%s)" , field . getType (). getName (), field . getClass (). getName (), field . getName ()));
2014-05-24 22:32:24 -04:00
}
2018-09-06 04:39:01 -07:00
field . setAccessible ( true );
if ( Modifier . isFinal ( field . getModifiers ()))
2014-05-24 22:32:24 -04:00
{
2018-09-06 04:39:01 -07:00
throw new RuntimeException ( "@ObjectHolder on final field, our transformer did not run? " + field . getDeclaringClass (). getName () + "/" + field . getName ());
2014-05-24 22:32:24 -04:00
}
}
2017-06-19 15:02:18 -07:00
@SuppressWarnings ( "unchecked" )
@Nullable
private ForgeRegistry <?> getRegistryForType ( Field field )
2016-10-10 07:34:38 +11:00
{
Queue < Class <?>> typesToExamine = new LinkedList < Class <?>> ();
typesToExamine . add ( field . getType ());
2017-06-19 15:02:18 -07:00
ForgeRegistry <?> registry = null ;
2016-10-09 15:57:18 -07:00
while ( ! typesToExamine . isEmpty () && registry == null )
{
2016-10-10 07:34:38 +11:00
Class <?> type = typesToExamine . remove ();
Collections . addAll ( typesToExamine , type . getInterfaces ());
2019-01-09 20:57:01 -08:00
if ( IForgeRegistryEntry . class . isAssignableFrom ( type ))
2016-10-10 07:34:38 +11:00
{
2019-01-09 20:57:01 -08:00
registry = ( ForgeRegistry <?> ) RegistryManager . ACTIVE . getRegistry (( Class < IForgeRegistryEntry > ) type );
2016-10-10 07:34:38 +11:00
final Class <?> parentType = type . getSuperclass ();
if ( parentType != null )
{
typesToExamine . add ( parentType );
}
}
}
return registry ;
}
2014-05-24 22:32:24 -04:00
public boolean isValid ()
{
2016-10-10 07:34:38 +11:00
return isValid ;
2014-05-24 22:32:24 -04:00
}
2017-06-19 15:02:18 -07:00
2019-01-09 20:57:01 -08:00
@Override
2019-01-16 12:28:13 -08:00
public void accept ( Predicate < ResourceLocation > filter )
2014-05-24 22:32:24 -04:00
{
2019-01-16 12:28:13 -08:00
if ( registry == null || ! filter . test ( registry . getRegistryName ()))
return ;
2014-05-24 22:32:24 -04:00
Object thing ;
2016-10-09 15:57:18 -07:00
if ( isValid && registry . containsKey ( injectedObject ) && ! registry . isDummied ( injectedObject ))
2014-05-26 10:58:13 -04:00
{
2016-10-10 07:34:38 +11:00
thing = registry . getValue ( injectedObject );
2014-05-26 10:58:13 -04:00
}
else
{
thing = null ;
}
if ( thing == null )
{
2018-09-06 04:39:01 -07:00
LOGGER . debug ( "Unable to lookup {} for {}. This means the object wasn't registered. It's likely just mod options." , injectedObject , field );
2014-05-26 10:58:13 -04:00
return ;
}
2014-05-24 22:32:24 -04:00
try
{
2018-09-06 04:39:01 -07:00
field . set ( null , thing );
2014-05-24 22:32:24 -04:00
}
2017-07-13 05:50:28 +01:00
catch ( IllegalArgumentException | ReflectiveOperationException e )
2014-05-24 22:32:24 -04:00
{
2018-09-06 04:39:01 -07:00
LOGGER . warn ( "Unable to set {} with value {} ({})" , this . field , thing , this . injectedObject , e );
2017-06-19 15:02:18 -07:00
}
}
2019-01-09 20:57:01 -08:00
@Override
public int hashCode ()
{
return field . hashCode ();
}
@Override
public boolean equals ( Object other )
{
if ( ! ( other instanceof ObjectHolderRef ))
return false ;
ObjectHolderRef o = ( ObjectHolderRef ) other ;
return this . field . equals ( o . field );
}
2014-05-24 22:32:24 -04:00
}