mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-07-03 18:42:53 +00:00
Load turtle item models using a model loader
This is far more elegant than our weird method of baking things and manually inserting them into the model map. Also means we no longer need the whole turtle_dynamic thing.
This commit is contained in:
parent
86ad43c3ab
commit
d5edbe700b
@ -7,6 +7,7 @@
|
|||||||
package dan200.computercraft.client;
|
package dan200.computercraft.client;
|
||||||
|
|
||||||
import dan200.computercraft.ComputerCraft;
|
import dan200.computercraft.ComputerCraft;
|
||||||
|
import dan200.computercraft.client.render.TurtleModelLoader;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.renderer.ItemMeshDefinition;
|
import net.minecraft.client.renderer.ItemMeshDefinition;
|
||||||
import net.minecraft.client.renderer.block.model.IBakedModel;
|
import net.minecraft.client.renderer.block.model.IBakedModel;
|
||||||
@ -35,7 +36,7 @@ import javax.annotation.Nonnull;
|
|||||||
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
|
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
|
||||||
public class ClientRegistry
|
public class ClientRegistry
|
||||||
{
|
{
|
||||||
private static final String[] TURTLE_UPGRADES = {
|
private static final String[] EXTRA_MODELS = {
|
||||||
"turtle_modem_off_left",
|
"turtle_modem_off_left",
|
||||||
"turtle_modem_on_left",
|
"turtle_modem_on_left",
|
||||||
"turtle_modem_off_right",
|
"turtle_modem_off_right",
|
||||||
@ -48,11 +49,16 @@ public class ClientRegistry
|
|||||||
"advanced_turtle_modem_on_right",
|
"advanced_turtle_modem_on_right",
|
||||||
"turtle_speaker_upgrade_left",
|
"turtle_speaker_upgrade_left",
|
||||||
"turtle_speaker_upgrade_right",
|
"turtle_speaker_upgrade_right",
|
||||||
|
|
||||||
|
"turtle_white",
|
||||||
|
"turtle_elf_overlay",
|
||||||
};
|
};
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public static void registerModels( ModelRegistryEvent event )
|
public static void registerModels( ModelRegistryEvent event )
|
||||||
{
|
{
|
||||||
|
ModelLoaderRegistry.registerLoader( TurtleModelLoader.INSTANCE );
|
||||||
|
|
||||||
// Register item models
|
// Register item models
|
||||||
registerUniversalItemModel( ComputerCraft.Items.computer, "computer" );
|
registerUniversalItemModel( ComputerCraft.Items.computer, "computer" );
|
||||||
registerItemModel( ComputerCraft.Items.commandComputer, 0, "command_computer" );
|
registerItemModel( ComputerCraft.Items.commandComputer, 0, "command_computer" );
|
||||||
@ -79,35 +85,28 @@ public class ClientRegistry
|
|||||||
registerItemModel( ComputerCraft.Items.printout, 1, "pages" );
|
registerItemModel( ComputerCraft.Items.printout, 1, "pages" );
|
||||||
registerItemModel( ComputerCraft.Items.printout, 2, "book" );
|
registerItemModel( ComputerCraft.Items.printout, 2, "book" );
|
||||||
|
|
||||||
String[] extraTurtleModels = new String[] { "turtle", "turtle_advanced", "turtle_white", "turtle_elf_overlay" };
|
registerUniversalItemModel( ComputerCraft.Items.turtle, "turtle" );
|
||||||
registerUniversalItemModel( ComputerCraft.Items.turtle, "turtle_dynamic", extraTurtleModels );
|
registerUniversalItemModel( ComputerCraft.Items.turtleExpanded, "turtle" );
|
||||||
registerUniversalItemModel( ComputerCraft.Items.turtleExpanded, "turtle_dynamic", extraTurtleModels );
|
registerUniversalItemModel( ComputerCraft.Items.turtleAdvanced, "turtle_advanced" );
|
||||||
registerUniversalItemModel( ComputerCraft.Items.turtleAdvanced, "turtle_dynamic", extraTurtleModels );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public static void onTextureStitchEvent( TextureStitchEvent.Pre event )
|
public static void onTextureStitchEvent( TextureStitchEvent.Pre event )
|
||||||
{
|
{
|
||||||
// Load all textures for upgrades
|
// Load all textures for the extra models
|
||||||
TextureMap map = event.getMap();
|
TextureMap map = event.getMap();
|
||||||
for( String upgrade : TURTLE_UPGRADES )
|
for( String upgrade : EXTRA_MODELS )
|
||||||
{
|
{
|
||||||
IModel model = ModelLoaderRegistry.getModelOrMissing( new ResourceLocation( "computercraft", "block/" + upgrade ) );
|
IModel model = ModelLoaderRegistry.getModelOrMissing( new ResourceLocation( "computercraft", "block/" + upgrade ) );
|
||||||
for( ResourceLocation texture : model.getTextures() )
|
for( ResourceLocation texture : model.getTextures() ) map.registerSprite( texture );
|
||||||
{
|
|
||||||
map.registerSprite( texture );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public static void onModelBakeEvent( ModelBakeEvent event )
|
public static void onModelBakeEvent( ModelBakeEvent event )
|
||||||
{
|
{
|
||||||
// Load all upgrade models
|
// Load all extra models
|
||||||
for( String upgrade : TURTLE_UPGRADES )
|
for( String model : EXTRA_MODELS ) loadBlockModel( event, model );
|
||||||
{
|
|
||||||
loadBlockModel( event, upgrade );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void registerItemModel( Item item, int damage, String name )
|
private static void registerItemModel( Item item, int damage, String name )
|
||||||
@ -118,18 +117,10 @@ public class ClientRegistry
|
|||||||
ModelLoader.setCustomModelResourceLocation( item, damage, res );
|
ModelLoader.setCustomModelResourceLocation( item, damage, res );
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void registerUniversalItemModel( Item item, String mainModel, String... extraModels )
|
private static void registerUniversalItemModel( Item item, String mainModel )
|
||||||
{
|
{
|
||||||
ResourceLocation mainLocation = new ResourceLocation( ComputerCraft.MOD_ID, mainModel );
|
ResourceLocation mainLocation = new ResourceLocation( ComputerCraft.MOD_ID, mainModel );
|
||||||
|
ModelBakery.registerItemVariants( item, mainLocation );
|
||||||
ResourceLocation[] modelLocations = new ResourceLocation[extraModels.length + 1];
|
|
||||||
modelLocations[0] = mainLocation;
|
|
||||||
for( int i = 0; i < extraModels.length; i++ )
|
|
||||||
{
|
|
||||||
modelLocations[i + 1] = new ResourceLocation( ComputerCraft.MOD_ID, extraModels[i] );
|
|
||||||
}
|
|
||||||
|
|
||||||
ModelBakery.registerItemVariants( item, modelLocations );
|
|
||||||
|
|
||||||
final ModelResourceLocation mainModelLocation = new ModelResourceLocation( mainLocation, "inventory" );
|
final ModelResourceLocation mainModelLocation = new ModelResourceLocation( mainLocation, "inventory" );
|
||||||
ModelLoader.setCustomMeshDefinition( item, new ItemMeshDefinition()
|
ModelLoader.setCustomMeshDefinition( item, new ItemMeshDefinition()
|
||||||
|
@ -8,28 +8,14 @@ package dan200.computercraft.client.proxy;
|
|||||||
|
|
||||||
import dan200.computercraft.ComputerCraft;
|
import dan200.computercraft.ComputerCraft;
|
||||||
import dan200.computercraft.client.render.TileEntityTurtleRenderer;
|
import dan200.computercraft.client.render.TileEntityTurtleRenderer;
|
||||||
import dan200.computercraft.client.render.TurtleSmartItemModel;
|
|
||||||
import dan200.computercraft.shared.proxy.CCTurtleProxyCommon;
|
import dan200.computercraft.shared.proxy.CCTurtleProxyCommon;
|
||||||
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
|
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
|
||||||
import dan200.computercraft.shared.turtle.items.ItemTurtleBase;
|
import dan200.computercraft.shared.turtle.items.ItemTurtleBase;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
|
|
||||||
import net.minecraft.client.resources.IReloadableResourceManager;
|
|
||||||
import net.minecraft.client.resources.IResourceManager;
|
|
||||||
import net.minecraftforge.client.event.ModelBakeEvent;
|
|
||||||
import net.minecraftforge.common.MinecraftForge;
|
|
||||||
import net.minecraftforge.fml.client.registry.ClientRegistry;
|
import net.minecraftforge.fml.client.registry.ClientRegistry;
|
||||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
|
||||||
|
|
||||||
public class CCTurtleProxyClient extends CCTurtleProxyCommon
|
public class CCTurtleProxyClient extends CCTurtleProxyCommon
|
||||||
{
|
{
|
||||||
@Override
|
|
||||||
public void preInit()
|
|
||||||
{
|
|
||||||
super.preInit();
|
|
||||||
MinecraftForge.EVENT_BUS.register( new ForgeHandlers() );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init()
|
public void init()
|
||||||
{
|
{
|
||||||
@ -50,25 +36,4 @@ public class CCTurtleProxyClient extends CCTurtleProxyCommon
|
|||||||
// Setup renderers
|
// Setup renderers
|
||||||
ClientRegistry.bindTileEntitySpecialRenderer( TileTurtle.class, new TileEntityTurtleRenderer() );
|
ClientRegistry.bindTileEntitySpecialRenderer( TileTurtle.class, new TileEntityTurtleRenderer() );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ForgeHandlers
|
|
||||||
{
|
|
||||||
private final TurtleSmartItemModel m_turtleSmartItemModel = new TurtleSmartItemModel();
|
|
||||||
|
|
||||||
ForgeHandlers()
|
|
||||||
{
|
|
||||||
IResourceManager resourceManager = Minecraft.getMinecraft().getResourceManager();
|
|
||||||
if( resourceManager instanceof IReloadableResourceManager )
|
|
||||||
{
|
|
||||||
((IReloadableResourceManager) resourceManager).registerReloadListener( m_turtleSmartItemModel );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@SubscribeEvent
|
|
||||||
public void onModelBakeEvent( ModelBakeEvent event )
|
|
||||||
{
|
|
||||||
event.getModelRegistry().putObject( new ModelResourceLocation( "computercraft:turtle_dynamic", "inventory" ), m_turtleSmartItemModel );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -14,23 +14,20 @@ import dan200.computercraft.shared.util.Holiday;
|
|||||||
import dan200.computercraft.shared.util.HolidayUtil;
|
import dan200.computercraft.shared.util.HolidayUtil;
|
||||||
import net.minecraft.block.state.IBlockState;
|
import net.minecraft.block.state.IBlockState;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.gui.FontRenderer;
|
|
||||||
import net.minecraft.client.renderer.BufferBuilder;
|
import net.minecraft.client.renderer.BufferBuilder;
|
||||||
|
import net.minecraft.client.renderer.EntityRenderer;
|
||||||
import net.minecraft.client.renderer.GlStateManager;
|
import net.minecraft.client.renderer.GlStateManager;
|
||||||
import net.minecraft.client.renderer.Tessellator;
|
import net.minecraft.client.renderer.Tessellator;
|
||||||
import net.minecraft.client.renderer.block.model.BakedQuad;
|
import net.minecraft.client.renderer.block.model.BakedQuad;
|
||||||
import net.minecraft.client.renderer.block.model.IBakedModel;
|
import net.minecraft.client.renderer.block.model.IBakedModel;
|
||||||
import net.minecraft.client.renderer.block.model.ModelManager;
|
import net.minecraft.client.renderer.block.model.ModelManager;
|
||||||
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
|
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
|
||||||
import net.minecraft.client.renderer.entity.RenderManager;
|
|
||||||
import net.minecraft.client.renderer.texture.TextureMap;
|
import net.minecraft.client.renderer.texture.TextureMap;
|
||||||
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
|
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
|
||||||
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
||||||
import net.minecraft.client.renderer.vertex.VertexFormat;
|
import net.minecraft.client.renderer.vertex.VertexFormat;
|
||||||
import net.minecraft.util.EnumFacing;
|
import net.minecraft.util.EnumFacing;
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
import net.minecraft.util.math.BlockPos;
|
|
||||||
import net.minecraft.util.math.RayTraceResult;
|
|
||||||
import net.minecraft.util.math.Vec3d;
|
import net.minecraft.util.math.Vec3d;
|
||||||
import net.minecraftforge.client.ForgeHooksClient;
|
import net.minecraftforge.client.ForgeHooksClient;
|
||||||
import net.minecraftforge.client.model.pipeline.LightUtil;
|
import net.minecraftforge.client.model.pipeline.LightUtil;
|
||||||
@ -43,7 +40,7 @@ import java.util.List;
|
|||||||
public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurtle>
|
public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurtle>
|
||||||
{
|
{
|
||||||
private static final ModelResourceLocation NORMAL_TURTLE_MODEL = new ModelResourceLocation( "computercraft:turtle", "inventory" );
|
private static final ModelResourceLocation NORMAL_TURTLE_MODEL = new ModelResourceLocation( "computercraft:turtle", "inventory" );
|
||||||
private static final ModelResourceLocation ADVANCED_TURTLE_MODEL = new ModelResourceLocation( "computercraft:turtle_advanced", "inventory" );
|
private static final ModelResourceLocation ADVANCED_TURTLE_MODEL = new ModelResourceLocation( "computercraft:advanced_turtle", "inventory" );
|
||||||
private static final ModelResourceLocation COLOUR_TURTLE_MODEL = new ModelResourceLocation( "computercraft:turtle_white", "inventory" );
|
private static final ModelResourceLocation COLOUR_TURTLE_MODEL = new ModelResourceLocation( "computercraft:turtle_white", "inventory" );
|
||||||
private static final ModelResourceLocation ELF_OVERLAY_MODEL = new ModelResourceLocation( "computercraft:turtle_elf_overlay", "inventory" );
|
private static final ModelResourceLocation ELF_OVERLAY_MODEL = new ModelResourceLocation( "computercraft:turtle_elf_overlay", "inventory" );
|
||||||
|
|
||||||
@ -65,7 +62,7 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ModelResourceLocation getTurtleOverlayModel( ComputerFamily family, ResourceLocation overlay, boolean christmas )
|
public static ModelResourceLocation getTurtleOverlayModel( ResourceLocation overlay, boolean christmas )
|
||||||
{
|
{
|
||||||
if( overlay != null )
|
if( overlay != null )
|
||||||
{
|
{
|
||||||
@ -83,6 +80,19 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
|
|||||||
|
|
||||||
private void renderTurtleAt( TileTurtle turtle, double posX, double posY, double posZ, float f, int i )
|
private void renderTurtleAt( TileTurtle turtle, double posX, double posY, double posZ, float f, int i )
|
||||||
{
|
{
|
||||||
|
// Render the label
|
||||||
|
String label = turtle.createProxy().getLabel();
|
||||||
|
if( label != null && rendererDispatcher.cameraHitResult != null && turtle.getPos().equals( rendererDispatcher.cameraHitResult.getBlockPos() ) )
|
||||||
|
{
|
||||||
|
setLightmapDisabled( true );
|
||||||
|
EntityRenderer.drawNameplate(
|
||||||
|
getFontRenderer(), label,
|
||||||
|
(float) posX + 0.5F, (float) posY + 1.2F, (float) posZ + 0.5F, 0,
|
||||||
|
rendererDispatcher.entityYaw, rendererDispatcher.entityPitch, false, false
|
||||||
|
);
|
||||||
|
setLightmapDisabled( false );
|
||||||
|
}
|
||||||
|
|
||||||
IBlockState state = turtle.getWorld().getBlockState( turtle.getPos() );
|
IBlockState state = turtle.getWorld().getBlockState( turtle.getPos() );
|
||||||
GlStateManager.pushMatrix();
|
GlStateManager.pushMatrix();
|
||||||
try
|
try
|
||||||
@ -94,13 +104,6 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
|
|||||||
yaw = turtle.getRenderYaw( f );
|
yaw = turtle.getRenderYaw( f );
|
||||||
GlStateManager.translate( posX + offset.x, posY + offset.y, posZ + offset.z );
|
GlStateManager.translate( posX + offset.x, posY + offset.y, posZ + offset.z );
|
||||||
|
|
||||||
// Render the label
|
|
||||||
String label = turtle.createProxy().getLabel();
|
|
||||||
if( label != null )
|
|
||||||
{
|
|
||||||
renderLabel( turtle.getAccess().getPosition(), label );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Render the turtle
|
// Render the turtle
|
||||||
GlStateManager.translate( 0.5f, 0.5f, 0.5f );
|
GlStateManager.translate( 0.5f, 0.5f, 0.5f );
|
||||||
GlStateManager.rotate( 180.0f - yaw, 0.0f, 1.0f, 0.0f );
|
GlStateManager.rotate( 180.0f - yaw, 0.0f, 1.0f, 0.0f );
|
||||||
@ -123,7 +126,6 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
|
|||||||
|
|
||||||
// Render the overlay
|
// Render the overlay
|
||||||
ModelResourceLocation overlayModel = getTurtleOverlayModel(
|
ModelResourceLocation overlayModel = getTurtleOverlayModel(
|
||||||
family,
|
|
||||||
overlay,
|
overlay,
|
||||||
HolidayUtil.getCurrentHoliday() == Holiday.Christmas
|
HolidayUtil.getCurrentHoliday() == Holiday.Christmas
|
||||||
);
|
);
|
||||||
@ -232,72 +234,4 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
|
|||||||
}
|
}
|
||||||
tessellator.draw();
|
tessellator.draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void renderLabel( BlockPos position, String label )
|
|
||||||
{
|
|
||||||
Minecraft mc = Minecraft.getMinecraft();
|
|
||||||
RayTraceResult mop = mc.objectMouseOver;
|
|
||||||
if( mop != null && mop.typeOfHit == RayTraceResult.Type.BLOCK && mop.getBlockPos().equals( position ) )
|
|
||||||
{
|
|
||||||
RenderManager renderManager = mc.getRenderManager();
|
|
||||||
FontRenderer fontrenderer = renderManager.getFontRenderer();
|
|
||||||
float scale = 0.016666668F * 1.6f;
|
|
||||||
|
|
||||||
GlStateManager.pushMatrix();
|
|
||||||
GlStateManager.disableLighting();
|
|
||||||
GlStateManager.enableBlend();
|
|
||||||
GlStateManager.blendFunc( GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA );
|
|
||||||
try
|
|
||||||
{
|
|
||||||
GlStateManager.translate( 0.5f, 1.25f, 0.5f );
|
|
||||||
GlStateManager.rotate( -renderManager.playerViewY, 0.0F, 1.0F, 0.0F );
|
|
||||||
GlStateManager.rotate( renderManager.playerViewX, 1.0F, 0.0F, 0.0F );
|
|
||||||
GlStateManager.scale( -scale, -scale, scale );
|
|
||||||
|
|
||||||
int yOffset = 0;
|
|
||||||
int xOffset = fontrenderer.getStringWidth( label ) / 2;
|
|
||||||
|
|
||||||
// Draw background
|
|
||||||
GlStateManager.depthMask( false );
|
|
||||||
GlStateManager.disableDepth();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Quad
|
|
||||||
GlStateManager.disableTexture2D();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Tessellator tessellator = Tessellator.getInstance();
|
|
||||||
BufferBuilder renderer = tessellator.getBuffer();
|
|
||||||
renderer.begin( GL11.GL_QUADS, DefaultVertexFormats.POSITION_COLOR );
|
|
||||||
renderer.pos( -xOffset - 1, -1 + yOffset, 0.0D ).color( 0.0F, 0.0F, 0.0F, 0.25F ).endVertex();
|
|
||||||
renderer.pos( -xOffset - 1, 8 + yOffset, 0.0D ).color( 0.0F, 0.0F, 0.0F, 0.25F ).endVertex();
|
|
||||||
renderer.pos( xOffset + 1, 8 + yOffset, 0.0D ).color( 0.0F, 0.0F, 0.0F, 0.25F ).endVertex();
|
|
||||||
renderer.pos( xOffset + 1, -1 + yOffset, 0.0D ).color( 0.0F, 0.0F, 0.0F, 0.25F ).endVertex();
|
|
||||||
tessellator.draw();
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
GlStateManager.enableTexture2D();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Text
|
|
||||||
fontrenderer.drawString( label, -fontrenderer.getStringWidth( label ) / 2, yOffset, 0x20ffffff );
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
GlStateManager.enableDepth();
|
|
||||||
GlStateManager.depthMask( true );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw foreground text
|
|
||||||
fontrenderer.drawString( label, -fontrenderer.getStringWidth( label ) / 2, yOffset, -1 );
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
GlStateManager.disableBlend();
|
|
||||||
GlStateManager.enableLighting();
|
|
||||||
GlStateManager.popMatrix();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,121 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||||
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
||||||
|
* Send enquiries to dratcliffe@gmail.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dan200.computercraft.client.render;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import dan200.computercraft.ComputerCraft;
|
||||||
|
import net.minecraft.client.renderer.block.model.IBakedModel;
|
||||||
|
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||||
|
import net.minecraft.client.renderer.vertex.VertexFormat;
|
||||||
|
import net.minecraft.client.resources.IResourceManager;
|
||||||
|
import net.minecraft.util.ResourceLocation;
|
||||||
|
import net.minecraftforge.client.model.ICustomModelLoader;
|
||||||
|
import net.minecraftforge.client.model.IModel;
|
||||||
|
import net.minecraftforge.client.model.ModelLoaderRegistry;
|
||||||
|
import net.minecraftforge.common.model.IModelState;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
public class TurtleModelLoader implements ICustomModelLoader
|
||||||
|
{
|
||||||
|
private static final ResourceLocation NORMAL_TURTLE_MODEL = new ResourceLocation( ComputerCraft.MOD_ID, "block/turtle" );
|
||||||
|
private static final ResourceLocation ADVANCED_TURTLE_MODEL = new ResourceLocation( ComputerCraft.MOD_ID, "block/advanced_turtle" );
|
||||||
|
private static final ResourceLocation COLOUR_TURTLE_MODEL = new ResourceLocation( ComputerCraft.MOD_ID, "block/turtle_white" );
|
||||||
|
|
||||||
|
public static final TurtleModelLoader INSTANCE = new TurtleModelLoader();
|
||||||
|
|
||||||
|
private TurtleModelLoader()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResourceManagerReload( @Nonnull IResourceManager manager )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean accepts( @Nonnull ResourceLocation name )
|
||||||
|
{
|
||||||
|
return name.getNamespace().equals( ComputerCraft.MOD_ID )
|
||||||
|
&& (name.getPath().equals( "turtle" ) || name.getPath().equals( "turtle_advanced" ));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public IModel loadModel( @Nonnull ResourceLocation name ) throws Exception
|
||||||
|
{
|
||||||
|
if( name.getNamespace().equals( ComputerCraft.MOD_ID ) )
|
||||||
|
{
|
||||||
|
IModel colourModel = ModelLoaderRegistry.getModel( COLOUR_TURTLE_MODEL );
|
||||||
|
switch( name.getPath() )
|
||||||
|
{
|
||||||
|
case "turtle":
|
||||||
|
return new TurtleModel( ModelLoaderRegistry.getModel( NORMAL_TURTLE_MODEL ), colourModel );
|
||||||
|
case "turtle_advanced":
|
||||||
|
return new TurtleModel( ModelLoaderRegistry.getModel( ADVANCED_TURTLE_MODEL ), colourModel );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IllegalStateException( "Loader does not accept " + name );
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class TurtleModel implements IModel
|
||||||
|
{
|
||||||
|
private final IModel family;
|
||||||
|
private final IModel colour;
|
||||||
|
|
||||||
|
private TurtleModel( IModel family, IModel colour )
|
||||||
|
{
|
||||||
|
this.family = family;
|
||||||
|
this.colour = colour;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public IBakedModel bake( @Nonnull IModelState state, @Nonnull VertexFormat format, @Nonnull Function<ResourceLocation, TextureAtlasSprite> function )
|
||||||
|
{
|
||||||
|
return new TurtleSmartItemModel(
|
||||||
|
family.bake( state, format, function ),
|
||||||
|
colour.bake( state, format, function )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private TurtleModel copy( IModel family, IModel colour )
|
||||||
|
{
|
||||||
|
return this.family == family && this.colour == colour ? this : new TurtleModel( family, colour );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public IModel smoothLighting( boolean value )
|
||||||
|
{
|
||||||
|
return copy( family.smoothLighting( value ), colour.smoothLighting( value ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public IModel gui3d( boolean value )
|
||||||
|
{
|
||||||
|
return copy( family.gui3d( value ), colour.gui3d( value ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public IModel uvlock( boolean value )
|
||||||
|
{
|
||||||
|
return copy( family.uvlock( value ), colour.uvlock( value ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public IModel retexture( ImmutableMap<String, String> textures )
|
||||||
|
{
|
||||||
|
return copy( family.retexture( textures ), colour.retexture( textures ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -9,7 +9,6 @@ package dan200.computercraft.client.render;
|
|||||||
import com.google.common.base.Objects;
|
import com.google.common.base.Objects;
|
||||||
import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
||||||
import dan200.computercraft.api.turtle.TurtleSide;
|
import dan200.computercraft.api.turtle.TurtleSide;
|
||||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
|
||||||
import dan200.computercraft.shared.turtle.items.ItemTurtleBase;
|
import dan200.computercraft.shared.turtle.items.ItemTurtleBase;
|
||||||
import dan200.computercraft.shared.util.Holiday;
|
import dan200.computercraft.shared.util.Holiday;
|
||||||
import dan200.computercraft.shared.util.HolidayUtil;
|
import dan200.computercraft.shared.util.HolidayUtil;
|
||||||
@ -17,15 +16,11 @@ import net.minecraft.block.state.IBlockState;
|
|||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.renderer.block.model.*;
|
import net.minecraft.client.renderer.block.model.*;
|
||||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||||
import net.minecraft.client.resources.IResourceManager;
|
|
||||||
import net.minecraft.entity.EntityLivingBase;
|
import net.minecraft.entity.EntityLivingBase;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.util.EnumFacing;
|
import net.minecraft.util.EnumFacing;
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import net.minecraftforge.client.resource.IResourceType;
|
|
||||||
import net.minecraftforge.client.resource.ISelectiveResourceReloadListener;
|
|
||||||
import net.minecraftforge.client.resource.VanillaResourceType;
|
|
||||||
import org.apache.commons.lang3.tuple.Pair;
|
import org.apache.commons.lang3.tuple.Pair;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
@ -34,9 +29,8 @@ import javax.vecmath.Matrix4f;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Predicate;
|
|
||||||
|
|
||||||
public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceReloadListener
|
public class TurtleSmartItemModel implements IBakedModel
|
||||||
{
|
{
|
||||||
private static final Matrix4f s_identity, s_flip;
|
private static final Matrix4f s_identity, s_flip;
|
||||||
|
|
||||||
@ -53,17 +47,15 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
|
|||||||
|
|
||||||
private static class TurtleModelCombination
|
private static class TurtleModelCombination
|
||||||
{
|
{
|
||||||
public final ComputerFamily m_family;
|
final boolean m_colour;
|
||||||
public final boolean m_colour;
|
final ITurtleUpgrade m_leftUpgrade;
|
||||||
public final ITurtleUpgrade m_leftUpgrade;
|
final ITurtleUpgrade m_rightUpgrade;
|
||||||
public final ITurtleUpgrade m_rightUpgrade;
|
final ResourceLocation m_overlay;
|
||||||
public final ResourceLocation m_overlay;
|
final boolean m_christmas;
|
||||||
public final boolean m_christmas;
|
final boolean m_flip;
|
||||||
public final boolean m_flip;
|
|
||||||
|
|
||||||
public TurtleModelCombination( ComputerFamily family, boolean colour, ITurtleUpgrade leftUpgrade, ITurtleUpgrade rightUpgrade, ResourceLocation overlay, boolean christmas, boolean flip )
|
TurtleModelCombination( boolean colour, ITurtleUpgrade leftUpgrade, ITurtleUpgrade rightUpgrade, ResourceLocation overlay, boolean christmas, boolean flip )
|
||||||
{
|
{
|
||||||
m_family = family;
|
|
||||||
m_colour = colour;
|
m_colour = colour;
|
||||||
m_leftUpgrade = leftUpgrade;
|
m_leftUpgrade = leftUpgrade;
|
||||||
m_rightUpgrade = rightUpgrade;
|
m_rightUpgrade = rightUpgrade;
|
||||||
@ -79,8 +71,7 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
|
|||||||
if( !(other instanceof TurtleModelCombination) ) return false;
|
if( !(other instanceof TurtleModelCombination) ) return false;
|
||||||
|
|
||||||
TurtleModelCombination otherCombo = (TurtleModelCombination) other;
|
TurtleModelCombination otherCombo = (TurtleModelCombination) other;
|
||||||
return otherCombo.m_family == m_family &&
|
return otherCombo.m_colour == m_colour &&
|
||||||
otherCombo.m_colour == m_colour &&
|
|
||||||
otherCombo.m_leftUpgrade == m_leftUpgrade &&
|
otherCombo.m_leftUpgrade == m_leftUpgrade &&
|
||||||
otherCombo.m_rightUpgrade == m_rightUpgrade &&
|
otherCombo.m_rightUpgrade == m_rightUpgrade &&
|
||||||
Objects.equal( otherCombo.m_overlay, m_overlay ) &&
|
Objects.equal( otherCombo.m_overlay, m_overlay ) &&
|
||||||
@ -92,8 +83,7 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
|
|||||||
public int hashCode()
|
public int hashCode()
|
||||||
{
|
{
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
int result = 1;
|
int result = 0;
|
||||||
result = prime * result + m_family.hashCode();
|
|
||||||
result = prime * result + (m_colour ? 1 : 0);
|
result = prime * result + (m_colour ? 1 : 0);
|
||||||
result = prime * result + (m_leftUpgrade != null ? m_leftUpgrade.hashCode() : 0);
|
result = prime * result + (m_leftUpgrade != null ? m_leftUpgrade.hashCode() : 0);
|
||||||
result = prime * result + (m_rightUpgrade != null ? m_rightUpgrade.hashCode() : 0);
|
result = prime * result + (m_rightUpgrade != null ? m_rightUpgrade.hashCode() : 0);
|
||||||
@ -104,14 +94,18 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final IBakedModel familyModel;
|
||||||
|
private final IBakedModel colourModel;
|
||||||
|
|
||||||
private HashMap<TurtleModelCombination, IBakedModel> m_cachedModels;
|
private HashMap<TurtleModelCombination, IBakedModel> m_cachedModels;
|
||||||
private ItemOverrideList m_overrides;
|
private ItemOverrideList m_overrides;
|
||||||
private final TurtleModelCombination m_defaultCombination;
|
|
||||||
|
|
||||||
public TurtleSmartItemModel()
|
public TurtleSmartItemModel( IBakedModel familyModel, IBakedModel colourModel )
|
||||||
{
|
{
|
||||||
|
this.familyModel = familyModel;
|
||||||
|
this.colourModel = colourModel;
|
||||||
|
|
||||||
m_cachedModels = new HashMap<>();
|
m_cachedModels = new HashMap<>();
|
||||||
m_defaultCombination = new TurtleModelCombination( ComputerFamily.Normal, false, null, null, null, false, false );
|
|
||||||
m_overrides = new ItemOverrideList( new ArrayList<>() )
|
m_overrides = new ItemOverrideList( new ArrayList<>() )
|
||||||
{
|
{
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@ -119,7 +113,6 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
|
|||||||
public IBakedModel handleItemState( @Nonnull IBakedModel originalModel, @Nonnull ItemStack stack, @Nullable World world, @Nullable EntityLivingBase entity )
|
public IBakedModel handleItemState( @Nonnull IBakedModel originalModel, @Nonnull ItemStack stack, @Nullable World world, @Nullable EntityLivingBase entity )
|
||||||
{
|
{
|
||||||
ItemTurtleBase turtle = (ItemTurtleBase) stack.getItem();
|
ItemTurtleBase turtle = (ItemTurtleBase) stack.getItem();
|
||||||
ComputerFamily family = turtle.getFamily( stack );
|
|
||||||
int colour = turtle.getColour( stack );
|
int colour = turtle.getColour( stack );
|
||||||
ITurtleUpgrade leftUpgrade = turtle.getUpgrade( stack, TurtleSide.Left );
|
ITurtleUpgrade leftUpgrade = turtle.getUpgrade( stack, TurtleSide.Left );
|
||||||
ITurtleUpgrade rightUpgrade = turtle.getUpgrade( stack, TurtleSide.Right );
|
ITurtleUpgrade rightUpgrade = turtle.getUpgrade( stack, TurtleSide.Right );
|
||||||
@ -127,17 +120,11 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
|
|||||||
boolean christmas = HolidayUtil.getCurrentHoliday() == Holiday.Christmas;
|
boolean christmas = HolidayUtil.getCurrentHoliday() == Holiday.Christmas;
|
||||||
String label = turtle.getLabel( stack );
|
String label = turtle.getLabel( stack );
|
||||||
boolean flip = label != null && (label.equals( "Dinnerbone" ) || label.equals( "Grumm" ));
|
boolean flip = label != null && (label.equals( "Dinnerbone" ) || label.equals( "Grumm" ));
|
||||||
TurtleModelCombination combo = new TurtleModelCombination( family, colour != -1, leftUpgrade, rightUpgrade, overlay, christmas, flip );
|
TurtleModelCombination combo = new TurtleModelCombination( colour != -1, leftUpgrade, rightUpgrade, overlay, christmas, flip );
|
||||||
if( m_cachedModels.containsKey( combo ) )
|
|
||||||
{
|
IBakedModel model = m_cachedModels.get( combo );
|
||||||
return m_cachedModels.get( combo );
|
if( model == null ) m_cachedModels.put( combo, model = buildModel( combo ) );
|
||||||
}
|
return model;
|
||||||
else
|
|
||||||
{
|
|
||||||
IBakedModel model = buildModel( combo );
|
|
||||||
m_cachedModels.put( combo, model );
|
|
||||||
return model;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -149,19 +136,13 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
|
|||||||
return m_overrides;
|
return m_overrides;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onResourceManagerReload( @Nonnull IResourceManager resourceManager, @Nonnull Predicate<IResourceType> resourcePredicate )
|
|
||||||
{
|
|
||||||
if( resourcePredicate.test( VanillaResourceType.MODELS ) ) m_cachedModels.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
private IBakedModel buildModel( TurtleModelCombination combo )
|
private IBakedModel buildModel( TurtleModelCombination combo )
|
||||||
{
|
{
|
||||||
Minecraft mc = Minecraft.getMinecraft();
|
Minecraft mc = Minecraft.getMinecraft();
|
||||||
ModelManager modelManager = mc.getRenderItem().getItemModelMesher().getModelManager();
|
ModelManager modelManager = mc.getRenderItem().getItemModelMesher().getModelManager();
|
||||||
ModelResourceLocation baseModelLocation = TileEntityTurtleRenderer.getTurtleModel( combo.m_family, combo.m_colour );
|
ModelResourceLocation overlayModelLocation = TileEntityTurtleRenderer.getTurtleOverlayModel( combo.m_overlay, combo.m_christmas );
|
||||||
ModelResourceLocation overlayModelLocation = TileEntityTurtleRenderer.getTurtleOverlayModel( combo.m_family, combo.m_overlay, combo.m_christmas );
|
|
||||||
IBakedModel baseModel = modelManager.getModel( baseModelLocation );
|
IBakedModel baseModel = combo.m_colour ? colourModel : familyModel;
|
||||||
IBakedModel overlayModel = (overlayModelLocation != null) ? modelManager.getModel( overlayModelLocation ) : null;
|
IBakedModel overlayModel = (overlayModelLocation != null) ? modelManager.getModel( overlayModelLocation ) : null;
|
||||||
Matrix4f transform = combo.m_flip ? s_flip : s_identity;
|
Matrix4f transform = combo.m_flip ? s_flip : s_identity;
|
||||||
Pair<IBakedModel, Matrix4f> leftModel = (combo.m_leftUpgrade != null) ? combo.m_leftUpgrade.getModel( null, TurtleSide.Left ) : null;
|
Pair<IBakedModel, Matrix4f> leftModel = (combo.m_leftUpgrade != null) ? combo.m_leftUpgrade.getModel( null, TurtleSide.Left ) : null;
|
||||||
@ -184,38 +165,36 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// These should not be called:
|
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public List<BakedQuad> getQuads( IBlockState state, EnumFacing facing, long rand )
|
public List<BakedQuad> getQuads( IBlockState state, EnumFacing facing, long rand )
|
||||||
{
|
{
|
||||||
return getDefaultModel().getQuads( state, facing, rand );
|
return familyModel.getQuads( state, facing, rand );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAmbientOcclusion()
|
public boolean isAmbientOcclusion()
|
||||||
{
|
{
|
||||||
return getDefaultModel().isAmbientOcclusion();
|
return familyModel.isAmbientOcclusion();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isGui3d()
|
public boolean isGui3d()
|
||||||
{
|
{
|
||||||
return getDefaultModel().isGui3d();
|
return familyModel.isGui3d();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isBuiltInRenderer()
|
public boolean isBuiltInRenderer()
|
||||||
{
|
{
|
||||||
return getDefaultModel().isBuiltInRenderer();
|
return familyModel.isBuiltInRenderer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public TextureAtlasSprite getParticleTexture()
|
public TextureAtlasSprite getParticleTexture()
|
||||||
{
|
{
|
||||||
return getDefaultModel().getParticleTexture();
|
return familyModel.getParticleTexture();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@ -223,18 +202,7 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
|
|||||||
@Deprecated
|
@Deprecated
|
||||||
public ItemCameraTransforms getItemCameraTransforms()
|
public ItemCameraTransforms getItemCameraTransforms()
|
||||||
{
|
{
|
||||||
return getDefaultModel().getItemCameraTransforms();
|
return familyModel.getItemCameraTransforms();
|
||||||
}
|
}
|
||||||
|
|
||||||
private IBakedModel getDefaultModel()
|
|
||||||
{
|
|
||||||
IBakedModel model = m_cachedModels.get( m_defaultCombination );
|
|
||||||
if( model == null )
|
|
||||||
{
|
|
||||||
model = buildModel( m_defaultCombination );
|
|
||||||
m_cachedModels.put( m_defaultCombination, model );
|
|
||||||
}
|
|
||||||
|
|
||||||
return model;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user