提供: Minecraft Modding Wiki
移動先: 案内検索

特殊モデル付のアイテムブロック追加[編集]

※書き途中です※

ソース[編集]

ExampleItemBlock.java[編集]

package modding.itemblock;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraftforge.client.MinecraftForgeClient;
import cpw.mods.fml.client.registry.RenderingRegistry;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.GameRegistry;

@Mod(
		modid = "ExampleItemBlock",
		name = "Example Item Block",
		version = "1.0"
)
public class ExampleItemBlock
{
	public static Block example_block;
	public static int tessellator_item_block = RenderingRegistry.getNextAvailableRenderId();
	
	public static final String your_domain = "itemBlock";
	public static final String your_example_item_block = "example_item_block";
	public static final String your_example_model = "ModelExampleItemBlock";
	
	@EventHandler
	public void loeaded_of_after(FMLPreInitializationEvent event)
	{
		example_block = new BlockExample(Material.wood);
		example_block.setBlockName("example_item_block");
		example_block.setBlockTextureName(your_domain + ":" + your_example_item_block);
		example_block.setCreativeTab(CreativeTabs.tabBlock);
		GameRegistry.registerBlock(example_block, ItemBlockExample.class, "example_item_block");
	}
	@EventHandler
	public void load_of_before(FMLInitializationEvent event)
	{
		RenderingRegistry.registerBlockHandler(tessellator_item_block, new BlockExampleRenderer());
		MinecraftForgeClient.registerItemRenderer(Item.getItemFromBlock(example_block), new ItemBlockExampleRenderer(new ModelExampleItemBlock()));
	}
}

BlockExample.java[編集]

package modding.itemblock;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;

public class BlockExample extends Block
{
	public BlockExample(Material par1Material)
	{
		super(par1Material);
	}
    public boolean renderAsNormalBlock()
    {
        return false;
    }
    public boolean isOpaqueCube()
    {
    	return false;
    }
    public int getRenderType()
    {
    	return ExampleItemBlock.tessellator_item_block;
    }
}

BlockExampleRenderer.java[編集]

package modding.itemblock;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.init.Blocks;
import net.minecraft.world.IBlockAccess;
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;

public class BlockExampleRenderer implements ISimpleBlockRenderingHandler
{
    public void renderInventoryBlock(Block block, int metadata, int modelId, RenderBlocks renderer)
    {
    	//None Settings!
    }
    public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId, RenderBlocks renderer)
    {
    	if (true)
    	{
    		Block wood = Block.getBlockById(17);
        	renderer.setOverrideBlockTexture(wood.getIcon(0, 0));
    	}
    	
    	block.setBlockBounds(0.05F, 0.9F, 0.05F, 0.95F, 1.0F, 0.95F);
    	renderer.setRenderBoundsFromBlock(block);
    	renderer.renderStandardBlock(block, x, y, z);
		
		block.setBlockBounds(0.05F, 0.0F, 0.05F, 0.95F, 0.1F, 0.95F);
		renderer.setRenderBoundsFromBlock(block);
		renderer.renderStandardBlock(block, x, y, z);
		
		block.setBlockBounds(0.4F, 0.0F, 0.4F, 0.6F, 1.0F, 0.6F);
		renderer.setRenderBoundsFromBlock(block);
		renderer.renderStandardBlock(block, x, y, z);
		
		if (world.getBlock(x, y, z) instanceof BlockExample &&
				world.getBlock(x + 1, y, z) instanceof BlockExample &&
				world.getBlock(x - 1, y, z) instanceof BlockExample &&
				world.getBlock(x, y, z + 1) instanceof BlockExample &&
				world.getBlock(x, y, z - 1) instanceof BlockExample &&
				world.getBlock(x + 1, y, z + 1) instanceof BlockExample &&
				world.getBlock(x + 1, y, z - 1) instanceof BlockExample &&
				world.getBlock(x - 1, y, z + 1) instanceof BlockExample &&
				world.getBlock(x - 1, y, z - 1) instanceof BlockExample)
			{
				block.setBlockBounds(-1.0F, 0.9F, -1.0F, 2.0F, 1.1F, 2.0F);
				renderer.setRenderBoundsFromBlock(block);
				renderer.renderStandardBlock(block, x, y, z);
				
				block.setBlockBounds(-0.95F, 0.9F, -0.95F, 1.95F, 1.0F, 1.95F);
				renderer.setRenderBoundsFromBlock(block);
				renderer.renderStandardBlock(block, x, y, z);
			}
		
		Block air = Blocks.air; 
		block.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
		renderer.renderStandardBlock(block, x, y, z);
		
    	return true;
    }
    public boolean shouldRender3DInInventory(int modelId)
    {
    	return false;
    }
    public int getRenderId()
    {
    	return ExampleItemBlock.tessellator_item_block;
    }
}

ItemBlockExample.java[編集]

package modding.itemblock;

import java.util.List;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class ItemBlockExample extends ItemBlock
{
	public ItemBlockExample(Block par1Block)
	{
		super(par1Block);
	}
	@SideOnly(Side.CLIENT)
	public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4Boolean)
	{
		super.addInformation(par1ItemStack, par2EntityPlayer, par3List, par4Boolean);
		
		String red = EnumChatFormatting.RED + "-";
		String yellow = EnumChatFormatting.YELLOW + "-";
		String green = EnumChatFormatting.GREEN + "-";
		String aqua = EnumChatFormatting.AQUA + "-";
		String blue = EnumChatFormatting.BLUE + "-";
		String purple = EnumChatFormatting.LIGHT_PURPLE + "-";
		
		String message_2 = "This Model Table! !D";
		String light_colors = String.format("%s%s%s%s%s%s", red, yellow, green, aqua, blue, purple);
		String loop = light_colors + light_colors + light_colors;
		
		par3List.add((String) loop);
		par3List.add((String) message_2);
		par3List.add((String) loop);
		//EnumChatFormatting
	}
}

ItemBlockExampleRenderer.java[編集]

package modding.itemblock;

import net.minecraft.client.Minecraft;
import net.minecraft.client.model.ModelBase;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.IItemRenderer;

import org.lwjgl.opengl.GL11;

public class ItemBlockExampleRenderer implements IItemRenderer
{
	private ModelBase model;
	public ItemBlockExampleRenderer(ModelBase model)
	{
		this.model = model;
	}
	@Override
	public boolean handleRenderType(ItemStack item, ItemRenderType type)
	{
		switch (type)
		{
		case ENTITY:
			return true;
		case EQUIPPED:
			return true;
		case EQUIPPED_FIRST_PERSON:
			return true;
		case INVENTORY:
			return false;
		default:
			return false;
		}
	}
	@Override
	public boolean shouldUseRenderHelper(ItemRenderType type, ItemStack item, ItemRendererHelper helper)
	{
		switch (helper)
		{
		case ENTITY_BOBBING:
			return true;
		case EQUIPPED_BLOCK:
			return true;
		case BLOCK_3D:
			return true;
		case INVENTORY_BLOCK:
			return true;
		default:
			return false;
		}
	}
	@Override
	public void renderItem(ItemRenderType type, ItemStack item, Object... data)
	{
		switch (type)
		{
		case ENTITY:
			render(true, model, ExampleItemBlock.your_domain, ExampleItemBlock.your_example_model, data);
			break;
		case EQUIPPED:
			render(true, model, ExampleItemBlock.your_domain, ExampleItemBlock.your_example_model, data);
			break;
		case EQUIPPED_FIRST_PERSON:
			render(true, model, ExampleItemBlock.your_domain, ExampleItemBlock.your_example_model, data);
			break;
		case INVENTORY:
			render(false, model, ExampleItemBlock.your_domain, ExampleItemBlock.your_example_model, data);
			break;
		default:
			break;
		}
	}
	private void render(boolean rendering, ModelBase modeling, String domain, String textures, Object... data)
	{
		GL11.glTranslatef(0.5F, 0.5F, 0.5F);
		
		GL11.glPushMatrix();
		
        float scala = 2.5F;
        
        GL11.glScalef(scala, scala, scala);
        
        GL11.glRotatef(-4F, -10.0F, 0.0F, 0.0F);
        GL11.glRotatef(0F, 0.0F, -10.0F, 0.0F);
        GL11.glRotatef(-20F, 0.0F, 0.0F, -10.0F);
        
        GL11.glTranslatef(0F, 0F, 0F);
        
        Minecraft.getMinecraft().renderEngine.bindTexture(new ResourceLocation(domain, "textures/models/" + textures + ".png"));
        
        if (rendering) modeling.render((Entity) data[1], 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0625F);
        
        GL11.glPopMatrix();
        
		GL11.glTranslatef(-0.5F, -0.5F, -0.5F);
	}
}

ModelExampleItemBlock.java[編集]

package modding.itemblock;

import net.minecraft.client.model.ModelBase;
import net.minecraft.client.model.ModelRenderer;
import net.minecraft.entity.Entity;

public class ModelExampleItemBlock extends ModelBase
{
	public ModelRenderer ItemBlockTop;
	public ModelRenderer ItemBlockCenter;
	public ModelRenderer ItemBlockDown;
	
	public ModelExampleItemBlock()
	{
		textureWidth = 64;
		textureHeight = 32;
		
		ItemBlockTop = new ModelRenderer(this, 0, 0);
		ItemBlockTop.addBox(0F, 0F, 0F, 6, 1, 6);
		ItemBlockTop.setRotationPoint(-3F, -4F, -3F);
		ItemBlockTop.setTextureSize(64, 32);
		ItemBlockTop.mirror = true;
		setRotation(ItemBlockTop, 0F, 0F, 0F);
		ItemBlockCenter = new ModelRenderer(this, 24, 0);
		ItemBlockCenter.addBox(0F, 0F, 0F, 2, 8, 2);
		ItemBlockCenter.setRotationPoint(-1F, -4F, -1F);
		ItemBlockCenter.setTextureSize(64, 32);
		ItemBlockCenter.mirror = true;
		setRotation(ItemBlockCenter, 0F, 0F, 0F);
		ItemBlockDown = new ModelRenderer(this, 0, 0);
		ItemBlockDown.addBox(0F, 0F, 0F, 6, 1, 6);
		ItemBlockDown.setRotationPoint(-3F, 3F, -3F);
		ItemBlockDown.setTextureSize(64, 32);
		ItemBlockDown.mirror = true;
		setRotation(ItemBlockDown, 0F, 0F, 0F);
	}
	public void render(Entity entity, float f, float f1, float f2, float f3, float f4, float f5)
	{
		super.render(entity, f, f1, f2, f3, f4, f5);
		setRotationAngles(f, f1, f2, f3, f4, f5, entity);
		ItemBlockTop.render(f5);
		ItemBlockCenter.render(f5);
		ItemBlockDown.render(f5);
	}
	private void setRotation(ModelRenderer model, float x, float y, float z)
	{
		model.rotateAngleX = x;
		model.rotateAngleY = y;
		model.rotateAngleZ = z;
	}
	public void setRotationAngles(float f, float f1, float f2, float f3, float f4, float f5, Entity entity)
	{
		super.setRotationAngles(f, f1, f2, f3, f4, f5, entity);
	}
}

解説[編集]

注意、Mod開発上級者しかわからないと思いますが、すべて解説していきます。

ExampleItemBlock.java[編集]

BlockExample.java[編集]

BlockExampleRenderer.java[編集]

ItemBlockExample.java[編集]

ItemBlockExampleRenderer.java[編集]

ModelExampleItemBlock.java[編集]