提供: Minecraft Modding Wiki
2014年6月22日 (日) 10:13時点における49.97.93.114 (トーク)による版 (防具の追加)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

この記事は"Minecraft Forge Universal 9.11.1.965"を前提MODとしています。

防具の追加[編集]

鉄と同じ耐久度を持ち、ダイヤと同じ防御力を持つ防具を追加します。
金床での修復にはエメラルドを使います。
ちなみにレイヤーというのはプレイヤーが防具をつけたときにそのプレイヤーが着るものです。
できたMODはこちらになります。[1]

ソースコード[編集]

  • SampleArmorCore.java
package mods.armorsample;

import net.minecraft.item.EnumArmorMaterial;
import net.minecraft.item.Item;
import net.minecraftforge.common.EnumHelper;
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;
import cpw.mods.fml.common.registry.LanguageRegistry;

@Mod(modid="sampleArmor", name="sampleArmor", version="1.0.0")
public class SampleArmorCore 
{
	//modid名を登録。texturePathを指定する際に用います。
	public static final String modid ="samplearmor";
	
	public static Item sampleHelmet;
	public static int sampleHelmetID = 28524;
	public static Item sampleChestplate;
	public static int sampleChestplateID = 28525;
	public static Item sampleLeggings;
	public static int sampleLeggingsID = 28526;
	public static Item sampleBoots;
	public static int sampleBootsID = 28527;
	
	public static EnumArmorMaterial sampleArmorEnum;
	
	@EventHandler
	public void init(FMLInitializationEvent event)
	{
	//表示名の登録
	LanguageRegistry.addName(sampleHelmet, "Sample Helmet");
	LanguageRegistry.instance().addNameForObject(sampleHelmet, "ja_JP", "サンプルヘルメット");
	GameRegistry.addRecipe(new ItemStack(sampleHelmet), 
			new Object[]{ "XXX","X X",
			'X',Item.emerald});
	LanguageRegistry.addName(sampleChestplate, "Sample Chestplate");
	LanguageRegistry.instance().addNameForObject(sampleChestplate, "ja_JP", "サンプルチェストプレート");
	GameRegistry.addRecipe(new ItemStack(sampleChestplate), 
			new Object[]{ "X X","XXX","XXX",
			'X',Item.emerald});
	LanguageRegistry.addName(sampleLeggings, "Sample Leggings");
	LanguageRegistry.instance().addNameForObject(sampleLeggings, "ja_JP", "サンプルレギンス");
	GameRegistry.addRecipe(new ItemStack(sampleLeggings), 
			new Object[]{ "XXX","X X","X X",
			'X',Item.emerald});
	LanguageRegistry.addName(sampleBoots, "Sample Boots");
	LanguageRegistry.instance().addNameForObject(sampleBoots, "ja_JP", "サンプルブーツ");
	GameRegistry.addRecipe(new ItemStack(sampleBoots), 
			new Object[]{ "X X","X X",	
			'X',Item.emerald});
	}
	
	@EventHandler
	public void preInit(FMLPreInitializationEvent event)
	{
		//インスタンスの代入  setUnlocalizedNameでシステム名の登録、setTextureNameでテクスチャの指定をしています。
		sampleHelmet = new ItemSampleArmor(sampleHelmetID, sampleArmorEnum, 0, 0, "sample").setUnlocalizedName("SampleHelmet").setTextureName("samplearmor:sample_helmet");
		GameRegistry.registerItem(sampleHelmet, "samplehelmet");
		sampleChestplate = new ItemSampleArmor(sampleChestplateID, sampleArmorEnum, 0, 1, "sample").setUnlocalizedName("SampleChestplate").setTextureName("samplearmor:sample_chestplate");
		GameRegistry.registerItem(sampleChestplate, "samplechestplate");
		sampleLeggings = new ItemSampleArmor(sampleLeggingsID, sampleArmorEnum, 0, 2, "sample").setUnlocalizedName("SampleLeggings").setTextureName("samplearmor:sample_leggings");
		GameRegistry.registerItem(sampleLeggings, "sampleleggings");
		sampleBoots = new ItemSampleArmor(sampleBootsID, sampleArmorEnum, 0, 3, "sample").setUnlocalizedName("SampleBoots").setTextureName("samplearmor:sample_boots");
		GameRegistry.registerItem(sampleBoots, "sampleboots");
	}
	static
	{
		//基本素材の設定
		sampleArmorEnum = EnumHelper.addArmorMaterial("SAMPLEARMOR",15, new int[]{3, 8, 6, 3}, 9);
		sampleArmorEnum.customCraftingMaterial = Item.emerald;
	}

}
  • ItemSampleArmor.java
package mods.armorsample;

import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.Entity;
import net.minecraft.item.EnumArmorMaterial;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class ItemSampleArmor extends ItemArmor 
{
	//テクスチャパスを指定。この場合はassets/samplearmor/textures/model/armor/を指定しています。
	private String texturePath = SampleArmorCore.modid + ":" + "textures/models/armor/";
	   
	   public ItemSampleArmor(int par1, EnumArmorMaterial par2EnumArmorMaterial,
	         int par3, int par4,String type) {
	      super(par1, par2EnumArmorMaterial, par3, par4);
	      this.setTextureName(type,par4);
	   }
	   /*layer名を登録しています。
	    * この場合はsample_layer_1.png、sample_layer_2.pngを登録しています。
	    * 上で指定したフォルダに入れてください。
	    */
	    private void setTextureName(String type,int armorPart)
	     {
	       if(armorType == 0 || armorType == 1 || armorType == 3)
	        {
	        this.texturePath += type + "_layer_1.png";
	        }else{
	        this.texturePath += type + "_layer_2.png";
	       }
	      }

	   @SideOnly(Side.CLIENT)
	    public void registerIcon(IconRegister register)
	    {
	       this.itemIcon = register.registerIcon(SampleArmorCore.modid + ":" + this.getUnlocalizedName().substring(this.getUnlocalizedName().indexOf(".")+1));
	    }

	    public String getArmorTexture(ItemStack stack, Entity entity, int slot, int layer)
	      {
	       return this.texturePath;
	      }
}

解説[編集]

SampleArmorCore.java[編集]

sampleHelmet = new ItemSampleArmor(sampleHelmetID, sampleArmorEnum, 0, 0, "sample").setUnlocalizedName("SampleHelmet").setTextureName("samplearmor:sample_helmet");
GameRegistry.registerItem(sampleHelmet, "samplehelmet");
sampleChestplate = new ItemSampleArmor(sampleChestplateID, sampleArmorEnum, 0, 1, "sample").setUnlocalizedName("SampleChestplate").setTextureName("samplearmor:sample_chestplate");
GameRegistry.registerItem(sampleChestplate, "samplechestplate");
sampleLeggings = new ItemSampleArmor(sampleLeggingsID, sampleArmorEnum, 0, 2, "sample").setUnlocalizedName("SampleLeggings").setTextureName("samplearmor:sample_leggings");
GameRegistry.registerItem(sampleLeggings, "sampleleggings");
sampleBoots = new ItemSampleArmor(sampleBootsID, sampleArmorEnum, 0, 3, "sample").setUnlocalizedName("SampleBoots").setTextureName("samplearmor:sample_boots");
GameRegistry.registerItem(sampleBoots, "sampleboots");

防具のデータを登録しています。
ここでシステム名の登録とテクスチャ名の指定をしておきます。 ItemSampleArmor内のstring名がレイヤー名の○○_layer_x.pngの○○の部分になります。 防具のテクスチャはassets/samplearmor/textures/items/に置きます。

//基本素材の設定
sampleArmorEnum = EnumHelper.addArmorMaterial("SAMPLEARMOR",15, new int[]{3, 8, 6, 3}, 9);
sampleArmorEnum.customCraftingMaterial = Item.emerald;

防具の素材を設定しています。
1段目では左から引数に、名前、耐久値、防御力{ヘルメット、チェストプレート、レギンス、ブーツ}、良エンチャントのつきやすさ
を指定しています。
2段目の意味はサンプル剣のページにあるので省略します。今回指定したのはエメラルドです。

ItemSampleArmor.java[編集]

private String texturePath = SampleArmorCore.modid + ":" + "textures/models/armor/";

レイヤーのフォルダ先を指定しています。
レイヤーのテクスチャはassets/samplearmor/textures/models/armor/に置きます。

private void setTextureName(String type,int armorPart)
     {
       if(armorType == 0 || armorType == 1 || armorType == 3)
        {
        this.texturePath += type + "_layer_1.png";
        }else{
        this.texturePath += type + "_layer_2.png";
       }
      }

レイヤーの登録名を指定しています。
SampleArmorCoreで指定した"sample"が_layerの前に指定されます。
この場合指定された登録名はsample_layer_1.pngsample_layer_2.pngです。