Move model loading to the correct event. Fixes #2991 (#2994)

* Use the correct event for loading models.
* Introduce IModelRegistry to wrap the ModelLoader
This commit is contained in:
fscan
2017-07-31 15:51:53 +02:00
committed by yueh
parent b13a338948
commit 5e001d86cb
21 changed files with 155 additions and 67 deletions
@@ -235,7 +235,7 @@ class BlockDefinitionBuilder implements IBlockBuilder
// Register all extra handlers
this.preInitCallbacks.forEach( consumer -> this.factory.addPreInit( side -> consumer.accept( block, item ) ) );
this.initCallbacks.forEach( consumer -> this.factory.addInit( side -> consumer.accept( block, item ) ) );
this.modelRegCallbacks.forEach( consumer -> this.factory.addModelReg( side -> consumer.accept( block, item ) ) );
this.modelRegCallbacks.forEach( consumer -> this.factory.addModelReg( ( side, reg ) -> consumer.accept( block, item ) ) );
this.postInitCallbacks.forEach( consumer -> this.factory.addPostInit( side -> consumer.accept( block, item ) ) );
if( this.tileEntityDefinition != null && block instanceof AEBaseTileBlock )
@@ -33,11 +33,11 @@ import appeng.api.definitions.IItemDefinition;
import appeng.api.util.AEColor;
import appeng.api.util.AEColoredItemDefinition;
import appeng.bootstrap.components.BuiltInModelComponent;
import appeng.bootstrap.components.InitComponent;
import appeng.bootstrap.components.IInitComponent;
import appeng.bootstrap.components.IModelRegistrationComponent;
import appeng.bootstrap.components.IPostInitComponent;
import appeng.bootstrap.components.IPreInitComponent;
import appeng.bootstrap.components.ModelOverrideComponent;
import appeng.bootstrap.components.ModelRegComponent;
import appeng.bootstrap.components.PostInitComponent;
import appeng.bootstrap.components.PreInitComponent;
import appeng.bootstrap.components.TileEntityComponent;
import appeng.core.features.AEFeature;
import appeng.core.features.ActivityState;
@@ -128,22 +128,22 @@ public class FeatureFactory
this.bootstrapComponents.add( component );
}
void addPreInit( PreInitComponent component )
void addPreInit( IPreInitComponent component )
{
this.bootstrapComponents.add( component );
}
void addInit( InitComponent component )
void addInit( IInitComponent component )
{
this.bootstrapComponents.add( component );
}
void addModelReg( ModelRegComponent component )
void addModelReg( IModelRegistrationComponent component )
{
this.bootstrapComponents.add( component );
}
void addPostInit( PostInitComponent component )
void addPostInit( IPostInitComponent component )
{
this.bootstrapComponents.add( component );
}
@@ -32,7 +32,7 @@ public interface IBootstrapComponent
{
}
default void modelReg( Side side )
default void modelRegistration( Side side, IModelRegistry registry )
{
}
@@ -0,0 +1,39 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2017, 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>.
*/
package appeng.bootstrap;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.ItemMeshDefinition;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.block.statemap.IStateMapper;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;
public interface IModelRegistry
{
void registerItemVariants( Item item, ResourceLocation... names );
void setCustomModelResourceLocation( Item item, int metadata, ModelResourceLocation model );
void setCustomMeshDefinition( Item item, ItemMeshDefinition meshDefinition );
void setCustomStateMapper( Block block, IStateMapper mapper );
}
@@ -165,7 +165,7 @@ class ItemDefinitionBuilder implements IItemBuilder
// Register all extra handlers
this.preInitCallbacks.forEach( consumer -> this.factory.addPreInit( side -> consumer.accept( item ) ) );
this.initCallbacks.forEach( consumer -> this.factory.addInit( side -> consumer.accept( item ) ) );
this.modelRegCallbacks.forEach( consumer -> this.factory.addModelReg( side -> consumer.accept( item ) ) );
this.modelRegCallbacks.forEach( consumer -> this.factory.addModelReg( ( side, reg ) -> consumer.accept( item ) ) );
this.postInitCallbacks.forEach( consumer -> this.factory.addPostInit( side -> consumer.accept( item ) ) );
// Register custom dispenser behavior if requested
@@ -25,7 +25,7 @@ import net.minecraft.client.renderer.color.IBlockColor;
import net.minecraftforge.fml.relauncher.Side;
public class BlockColorComponent implements InitComponent
public class BlockColorComponent implements IInitComponent
{
private final Block block;
@@ -33,7 +33,7 @@ import appeng.client.render.model.BuiltInModelLoader;
@SideOnly( Side.CLIENT )
public class BuiltInModelComponent implements PreInitComponent
public class BuiltInModelComponent implements IPreInitComponent
{
private final Map<String, IModel> builtInModels = new HashMap<>();
@@ -25,7 +25,7 @@ import appeng.bootstrap.IBootstrapComponent;
@FunctionalInterface
public interface InitComponent extends IBootstrapComponent
public interface IInitComponent extends IBootstrapComponent
{
@Override
@@ -0,0 +1,39 @@
/*
* 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>.
*/
package appeng.bootstrap.components;
import net.minecraftforge.fml.relauncher.Side;
import appeng.bootstrap.IBootstrapComponent;
import appeng.bootstrap.IModelRegistry;
/**
* @author GuntherDW
*/
@FunctionalInterface
public interface IModelRegistrationComponent extends IBootstrapComponent
{
@Override
void modelRegistration( Side side, IModelRegistry registry );
}
@@ -25,7 +25,7 @@ import appeng.bootstrap.IBootstrapComponent;
@FunctionalInterface
public interface PostInitComponent extends IBootstrapComponent
public interface IPostInitComponent extends IBootstrapComponent
{
@Override
@@ -25,7 +25,7 @@ import appeng.bootstrap.IBootstrapComponent;
@FunctionalInterface
public interface PreInitComponent extends IBootstrapComponent
public interface IPreInitComponent extends IBootstrapComponent
{
@Override
@@ -25,7 +25,7 @@ import net.minecraft.item.Item;
import net.minecraftforge.fml.relauncher.Side;
public class ItemColorComponent implements InitComponent
public class ItemColorComponent implements IInitComponent
{
private final Item item;
@@ -21,17 +21,18 @@ package appeng.bootstrap.components;
import javax.annotation.Nonnull;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemMeshDefinition;
import net.minecraft.item.Item;
import net.minecraftforge.fml.relauncher.Side;
import appeng.bootstrap.IModelRegistry;
/**
* Registers a custom item mesh definition that can be used to dynamically determine the item model based on
* item stack properties.
*/
public class ItemMeshDefinitionComponent implements InitComponent
public class ItemMeshDefinitionComponent implements IModelRegistrationComponent
{
private final Item item;
@@ -45,8 +46,8 @@ public class ItemMeshDefinitionComponent implements InitComponent
}
@Override
public void initialize( Side side )
public void modelRegistration( Side side, IModelRegistry registry )
{
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register( this.item, this.meshDefinition );
registry.setCustomMeshDefinition( this.item, this.meshDefinition );
}
}
@@ -23,18 +23,18 @@ import java.util.Map;
import javax.annotation.Nonnull;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemModelMesher;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraftforge.fml.relauncher.Side;
import appeng.bootstrap.IModelRegistry;
/**
* Registers the models that should by used for an item, including the ability to
* distinguish by meta.
*/
public class ItemModelComponent implements InitComponent
public class ItemModelComponent implements IModelRegistrationComponent
{
private final Item item;
@@ -48,13 +48,11 @@ public class ItemModelComponent implements InitComponent
}
@Override
public void initialize( Side side )
public void modelRegistration( Side side, IModelRegistry registry )
{
ItemModelMesher itemMesher = Minecraft.getMinecraft().getRenderItem().getItemModelMesher();
this.modelsByMeta.forEach( ( meta, model ) ->
{
itemMesher.register( this.item, meta, model );
registry.setCustomModelResourceLocation( this.item, meta, model );
} );
}
@@ -21,15 +21,14 @@ package appeng.bootstrap.components;
import java.util.Collection;
import net.minecraft.client.renderer.block.model.ModelBakery;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import appeng.bootstrap.IBootstrapComponent;
import appeng.bootstrap.IModelRegistry;
public class ItemVariantsComponent implements IBootstrapComponent
public class ItemVariantsComponent implements IModelRegistrationComponent
{
private final Item item;
@@ -43,9 +42,9 @@ public class ItemVariantsComponent implements IBootstrapComponent
}
@Override
public void preInitialize( Side side )
public void modelRegistration( Side side, IModelRegistry registry )
{
ResourceLocation[] resourceArr = this.resources.toArray( new ResourceLocation[0] );
ModelBakery.registerItemVariants( this.item, resourceArr );
registry.registerItemVariants( this.item, resourceArr );
}
}
@@ -39,7 +39,7 @@ import net.minecraftforge.fml.relauncher.Side;
import appeng.core.AppEng;
public class ModelOverrideComponent implements PreInitComponent
public class ModelOverrideComponent implements IPreInitComponent
{
private static final ModelResourceLocation MODEL_MISSING = new ModelResourceLocation( "builtin/missing", "missing" );
@@ -1,21 +0,0 @@
package appeng.bootstrap.components;
import net.minecraftforge.fml.relauncher.Side;
import appeng.bootstrap.IBootstrapComponent;
/**
* @author GuntherDW
*/
@FunctionalInterface
public interface ModelRegComponent extends IBootstrapComponent
{
@Override
void modelReg( Side side );
}
@@ -24,14 +24,15 @@ import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.statemap.IStateMapper;
import net.minecraft.client.resources.IReloadableResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.relauncher.Side;
import appeng.bootstrap.IModelRegistry;
/**
* Registers a custom state mapper for a given block.
*/
public class StateMapperComponent implements ModelRegComponent
public class StateMapperComponent implements IModelRegistrationComponent
{
private final Block block;
@@ -45,9 +46,9 @@ public class StateMapperComponent implements ModelRegComponent
}
@Override
public void modelReg( Side side )
public void modelRegistration( Side side, IModelRegistry registry )
{
ModelLoader.setCustomStateMapper( this.block, this.stateMapper );
registry.setCustomStateMapper( this.block, this.stateMapper );
if( this.stateMapper instanceof IResourceManagerReloadListener )
{
( (IReloadableResourceManager) Minecraft.getMinecraft().getResourceManager() )
@@ -32,7 +32,7 @@ import appeng.tile.AEBaseTile;
* @param <T>
*/
// public class TesrComponent<T extends AEBaseTile> implements ModelRegComponent
public class TesrComponent<T extends AEBaseTile> implements PreInitComponent
public class TesrComponent<T extends AEBaseTile> implements IPreInitComponent
{
private final Class<T> tileEntityClass;
@@ -15,7 +15,7 @@ import appeng.core.AppEng;
/**
* @author GuntherDW
*/
public class TileEntityComponent implements PreInitComponent
public class TileEntityComponent implements IPreInitComponent
{
private List<TileEntityDefinition> tileEntityDefinitions = new ArrayList<>();
+38 -6
View File
@@ -30,12 +30,16 @@ import javax.annotation.Nonnull;
import com.google.common.base.Preconditions;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.ItemMeshDefinition;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.block.statemap.IStateMapper;
import net.minecraft.item.Item;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.DimensionType;
import net.minecraft.world.biome.Biome;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.FMLCommonHandler;
@@ -66,6 +70,7 @@ import appeng.api.networking.spatial.ISpatialCache;
import appeng.api.networking.storage.IStorageGrid;
import appeng.api.networking.ticking.ITickManager;
import appeng.api.parts.IPartHelper;
import appeng.bootstrap.IModelRegistry;
import appeng.capabilities.Capabilities;
import appeng.core.features.AEFeature;
import appeng.core.features.registries.P2PTunnelRegistry;
@@ -374,12 +379,9 @@ public final class Registration
@SubscribeEvent
public void modelRegistryEvent( ModelRegistryEvent event )
{
final Api api = Api.INSTANCE;
final IPartHelper partHelper = api.partHelper();
final IRegistryContainer registries = api.registries();
ApiDefinitions definitions = api.definitions();
definitions.getRegistry().getBootstrapComponents().forEach( b -> b.modelReg( FMLCommonHandler.instance().getEffectiveSide() ) );
final ApiDefinitions definitions = Api.INSTANCE.definitions();
final IModelRegistry registry = new ModelLoaderWrapper();
definitions.getRegistry().getBootstrapComponents().forEach( b -> b.modelRegistration( FMLCommonHandler.instance().getEffectiveSide(), registry ) );
}
@SubscribeEvent
@@ -628,4 +630,34 @@ public final class Registration
*/
OreDictionaryHandler.INSTANCE.bakeRecipes();
}
private static class ModelLoaderWrapper implements IModelRegistry
{
@Override
public void registerItemVariants( Item item, ResourceLocation... names )
{
ModelLoader.registerItemVariants( item, names );
}
@Override
public void setCustomModelResourceLocation( Item item, int metadata, ModelResourceLocation model )
{
ModelLoader.setCustomModelResourceLocation( item, metadata, model );
}
@Override
public void setCustomMeshDefinition( Item item, ItemMeshDefinition meshDefinition )
{
ModelLoader.setCustomMeshDefinition( item, meshDefinition );
}
@Override
public void setCustomStateMapper( Block block, IStateMapper mapper )
{
ModelLoader.setCustomStateMapper( block, mapper );
}
}
}