2019-01-12 17:51:26 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
import dan200.computercraft.ComputerCraft;
|
2019-02-10 09:45:15 +00:00
|
|
|
import dan200.computercraft.client.render.TurtleModelLoader;
|
2019-02-10 09:55:06 +00:00
|
|
|
import dan200.computercraft.shared.media.items.ItemDiskLegacy;
|
2019-03-29 21:21:39 +00:00
|
|
|
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
|
2019-02-10 09:55:06 +00:00
|
|
|
import dan200.computercraft.shared.turtle.items.ItemTurtleBase;
|
2019-01-12 17:51:26 +00:00
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
import net.minecraft.client.renderer.ItemMeshDefinition;
|
|
|
|
import net.minecraft.client.renderer.block.model.IBakedModel;
|
|
|
|
import net.minecraft.client.renderer.block.model.ModelBakery;
|
|
|
|
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
|
|
|
|
import net.minecraft.client.renderer.texture.TextureMap;
|
|
|
|
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.util.ResourceLocation;
|
2019-02-10 09:55:06 +00:00
|
|
|
import net.minecraftforge.client.event.ColorHandlerEvent;
|
2019-01-12 17:51:26 +00:00
|
|
|
import net.minecraftforge.client.event.ModelBakeEvent;
|
|
|
|
import net.minecraftforge.client.event.ModelRegistryEvent;
|
|
|
|
import net.minecraftforge.client.event.TextureStitchEvent;
|
|
|
|
import net.minecraftforge.client.model.IModel;
|
|
|
|
import net.minecraftforge.client.model.ModelLoader;
|
|
|
|
import net.minecraftforge.client.model.ModelLoaderRegistry;
|
|
|
|
import net.minecraftforge.fml.common.Mod;
|
|
|
|
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers textures and models for items.
|
|
|
|
*/
|
|
|
|
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
|
2019-03-29 21:21:39 +00:00
|
|
|
public final class ClientRegistry
|
2019-01-12 17:51:26 +00:00
|
|
|
{
|
2019-03-29 21:21:39 +00:00
|
|
|
private static final String[] EXTRA_MODELS = new String[] {
|
2019-01-12 17:51:26 +00:00
|
|
|
"turtle_modem_off_left",
|
|
|
|
"turtle_modem_on_left",
|
|
|
|
"turtle_modem_off_right",
|
|
|
|
"turtle_modem_on_right",
|
|
|
|
"turtle_crafting_table_left",
|
|
|
|
"turtle_crafting_table_right",
|
|
|
|
"advanced_turtle_modem_off_left",
|
|
|
|
"advanced_turtle_modem_on_left",
|
|
|
|
"advanced_turtle_modem_off_right",
|
|
|
|
"advanced_turtle_modem_on_right",
|
|
|
|
"turtle_speaker_upgrade_left",
|
|
|
|
"turtle_speaker_upgrade_right",
|
2019-02-10 09:45:15 +00:00
|
|
|
|
|
|
|
"turtle_white",
|
|
|
|
"turtle_elf_overlay",
|
2019-01-12 17:51:26 +00:00
|
|
|
};
|
|
|
|
|
2019-03-29 21:21:39 +00:00
|
|
|
private ClientRegistry() {}
|
|
|
|
|
2019-01-12 17:51:26 +00:00
|
|
|
@SubscribeEvent
|
|
|
|
public static void registerModels( ModelRegistryEvent event )
|
|
|
|
{
|
2019-02-10 09:45:15 +00:00
|
|
|
ModelLoaderRegistry.registerLoader( TurtleModelLoader.INSTANCE );
|
|
|
|
|
2019-01-12 17:51:26 +00:00
|
|
|
// Register item models
|
2019-01-12 19:01:32 +00:00
|
|
|
registerUniversalItemModel( ComputerCraft.Items.computer, "computer" );
|
|
|
|
registerItemModel( ComputerCraft.Items.commandComputer, 0, "command_computer" );
|
2019-01-12 17:51:26 +00:00
|
|
|
|
|
|
|
registerItemModel( ComputerCraft.Items.pocketComputer, 0, "pocket_computer" );
|
|
|
|
registerItemModel( ComputerCraft.Items.pocketComputer, 1, "advanced_pocket_computer" );
|
|
|
|
|
2019-01-12 19:01:32 +00:00
|
|
|
registerItemModel( ComputerCraft.Items.peripheral, 0, "peripheral" );
|
|
|
|
registerItemModel( ComputerCraft.Items.peripheral, 1, "wireless_modem" );
|
|
|
|
registerItemModel( ComputerCraft.Items.peripheral, 2, "monitor" );
|
|
|
|
registerItemModel( ComputerCraft.Items.peripheral, 3, "printer" );
|
|
|
|
registerItemModel( ComputerCraft.Items.peripheral, 4, "advanced_monitor" );
|
|
|
|
registerItemModel( ComputerCraft.Items.cable, 0, "cable" );
|
|
|
|
registerItemModel( ComputerCraft.Items.cable, 1, "wired_modem" );
|
|
|
|
registerItemModel( ComputerCraft.Items.advancedModem, 0, "advanced_modem" );
|
|
|
|
registerItemModel( ComputerCraft.Items.peripheral, 5, "speaker" );
|
|
|
|
registerItemModel( ComputerCraft.Items.wiredModemFull, 0, "wired_modem_full" );
|
2019-01-12 17:51:26 +00:00
|
|
|
|
|
|
|
registerUniversalItemModel( ComputerCraft.Items.disk, "disk" );
|
|
|
|
registerItemModel( ComputerCraft.Items.diskExpanded, 0, "disk_expanded" );
|
|
|
|
registerItemModel( ComputerCraft.Items.treasureDisk, 0, "treasure_disk" );
|
|
|
|
|
|
|
|
registerItemModel( ComputerCraft.Items.printout, 0, "printout" );
|
|
|
|
registerItemModel( ComputerCraft.Items.printout, 1, "pages" );
|
|
|
|
registerItemModel( ComputerCraft.Items.printout, 2, "book" );
|
|
|
|
|
2019-02-10 09:45:15 +00:00
|
|
|
registerUniversalItemModel( ComputerCraft.Items.turtle, "turtle" );
|
|
|
|
registerUniversalItemModel( ComputerCraft.Items.turtleExpanded, "turtle" );
|
|
|
|
registerUniversalItemModel( ComputerCraft.Items.turtleAdvanced, "turtle_advanced" );
|
2019-01-12 17:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
|
|
|
public static void onTextureStitchEvent( TextureStitchEvent.Pre event )
|
|
|
|
{
|
2019-02-10 09:45:15 +00:00
|
|
|
// Load all textures for the extra models
|
2019-01-12 17:51:26 +00:00
|
|
|
TextureMap map = event.getMap();
|
2019-02-10 09:45:15 +00:00
|
|
|
for( String upgrade : EXTRA_MODELS )
|
2019-01-12 17:51:26 +00:00
|
|
|
{
|
|
|
|
IModel model = ModelLoaderRegistry.getModelOrMissing( new ResourceLocation( "computercraft", "block/" + upgrade ) );
|
2019-02-10 09:45:15 +00:00
|
|
|
for( ResourceLocation texture : model.getTextures() ) map.registerSprite( texture );
|
2019-01-12 17:51:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
|
|
|
public static void onModelBakeEvent( ModelBakeEvent event )
|
|
|
|
{
|
2019-02-10 09:45:15 +00:00
|
|
|
// Load all extra models
|
|
|
|
for( String model : EXTRA_MODELS ) loadBlockModel( event, model );
|
2019-01-12 17:51:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 09:55:06 +00:00
|
|
|
@SubscribeEvent
|
|
|
|
public static void onItemColours( ColorHandlerEvent.Item event )
|
|
|
|
{
|
|
|
|
event.getItemColors().registerItemColorHandler(
|
2019-03-27 18:59:02 +00:00
|
|
|
( stack, layer ) -> layer == 1 ? ((ItemDiskLegacy) stack.getItem()).getColour( stack ) : 0xFFFFFF,
|
2019-02-10 09:55:06 +00:00
|
|
|
ComputerCraft.Items.disk, ComputerCraft.Items.diskExpanded
|
|
|
|
);
|
|
|
|
|
|
|
|
event.getItemColors().registerItemColorHandler( ( stack, layer ) -> {
|
|
|
|
switch( layer )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
return 0xFFFFFF;
|
2019-03-27 18:59:02 +00:00
|
|
|
case 1: // Frame colour
|
|
|
|
return ComputerCraft.Items.pocketComputer.getColour( stack );
|
|
|
|
case 2: // Light colour
|
2019-03-29 21:21:39 +00:00
|
|
|
return ItemPocketComputer.getLightState( stack );
|
2019-02-10 09:55:06 +00:00
|
|
|
}
|
|
|
|
}, ComputerCraft.Items.pocketComputer );
|
|
|
|
|
|
|
|
// Setup turtle colours
|
2019-03-27 18:59:02 +00:00
|
|
|
event.getItemColors().registerItemColorHandler(
|
|
|
|
( stack, tintIndex ) -> tintIndex == 0 ? ((ItemTurtleBase) stack.getItem()).getColour( stack ) : 0xFFFFFF,
|
|
|
|
ComputerCraft.Blocks.turtle, ComputerCraft.Blocks.turtleExpanded, ComputerCraft.Blocks.turtleAdvanced
|
|
|
|
);
|
2019-02-10 09:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 17:51:26 +00:00
|
|
|
private static void registerItemModel( Item item, int damage, String name )
|
|
|
|
{
|
|
|
|
ResourceLocation location = new ResourceLocation( ComputerCraft.MOD_ID, name );
|
|
|
|
final ModelResourceLocation res = new ModelResourceLocation( location, "inventory" );
|
|
|
|
ModelBakery.registerItemVariants( item, location );
|
|
|
|
ModelLoader.setCustomModelResourceLocation( item, damage, res );
|
|
|
|
}
|
|
|
|
|
2019-02-10 09:45:15 +00:00
|
|
|
private static void registerUniversalItemModel( Item item, String mainModel )
|
2019-01-12 17:51:26 +00:00
|
|
|
{
|
2019-01-12 19:01:32 +00:00
|
|
|
ResourceLocation mainLocation = new ResourceLocation( ComputerCraft.MOD_ID, mainModel );
|
2019-02-10 09:45:15 +00:00
|
|
|
ModelBakery.registerItemVariants( item, mainLocation );
|
2019-01-12 17:51:26 +00:00
|
|
|
|
2019-01-12 19:01:32 +00:00
|
|
|
final ModelResourceLocation mainModelLocation = new ModelResourceLocation( mainLocation, "inventory" );
|
2019-01-12 17:51:26 +00:00
|
|
|
ModelLoader.setCustomMeshDefinition( item, new ItemMeshDefinition()
|
|
|
|
{
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public ModelResourceLocation getModelLocation( @Nonnull ItemStack stack )
|
|
|
|
{
|
2019-01-12 19:01:32 +00:00
|
|
|
return mainModelLocation;
|
2019-01-12 17:51:26 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void loadBlockModel( ModelBakeEvent event, String name )
|
|
|
|
{
|
|
|
|
IModel model = ModelLoaderRegistry.getModelOrMissing( new ResourceLocation( ComputerCraft.MOD_ID, "block/" + name ) );
|
|
|
|
IBakedModel bakedModel = model.bake(
|
|
|
|
model.getDefaultState(), DefaultVertexFormats.ITEM,
|
|
|
|
location -> Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite( location.toString() )
|
|
|
|
);
|
|
|
|
|
|
|
|
event.getModelRegistry().putObject( new ModelResourceLocation( ComputerCraft.MOD_ID + ":" + name, "inventory" ), bakedModel );
|
|
|
|
}
|
|
|
|
}
|