提供: Minecraft Modding Wiki
移動先: 案内検索
(独自テクスチャの利用に対応)
8行目: 8行目:
 
*TurtleUpgradeの登録
 
*TurtleUpgradeの登録
 
*IPeripheralの実装
 
*IPeripheralの実装
*Turtle Upgradeの外観の実装
+
*Turtle Upgradeの外観(独自テクスチャ・モデル)の実装
  
 
=== ソースコード ===
 
=== ソースコード ===
62行目: 62行目:
 
}
 
}
  
public Set<ResourceLocation> getModelLocations() {
+
public void setTextureLocation(String domain, String path) {
 +
 
 +
}
 +
 
 +
public Set<ResourceLocation> getTextureLocations() {
 
return null;
 
return null;
 
}
 
}
  
 
public ModelResourceLocation loadModelLocation(String domain, String path) {
 
public ModelResourceLocation loadModelLocation(String domain, String path) {
 +
return null;
 +
}
 +
 +
public Set<ResourceLocation> getModelLocations() {
 
return null;
 
return null;
 
}
 
}
90行目: 98行目:
 
public class ClientProxy extends CommonProxy {
 
public class ClientProxy extends CommonProxy {
  
 +
private Set<ResourceLocation> textureLocations = new HashSet<ResourceLocation>();
 
private Set<ResourceLocation> modelLocations = new HashSet<ResourceLocation>();
 
private Set<ResourceLocation> modelLocations = new HashSet<ResourceLocation>();
  
 
@Override
 
@Override
 
public void registerEventHandlers() {
 
public void registerEventHandlers() {
MinecraftForge.EVENT_BUS.register(new ModelBakeEventHandler());
+
MinecraftForge.EVENT_BUS.register(new ClientEventHandler());
 
}
 
}
  
 
@Override
 
@Override
public Set<ResourceLocation> getModelLocations() {
+
public void setTextureLocation(String domain, String path) {
return modelLocations;
+
ResourceLocation location = new ResourceLocation(domain, path);
 +
textureLocations.add(location);
 +
}
 +
 
 +
@Override
 +
public Set<ResourceLocation> getTextureLocations() {
 +
return textureLocations;
 
}
 
}
  
107行目: 122行目:
 
modelLocations.add(location);
 
modelLocations.add(location);
 
return new ModelResourceLocation(location, "inventory");
 
return new ModelResourceLocation(location, "inventory");
 +
}
 +
 +
@Override
 +
public Set<ResourceLocation> getModelLocations() {
 +
return modelLocations;
 
}
 
}
  
112行目: 132行目:
 
</source>
 
</source>
  
==== ModelBakeEventHandler.java ====
+
==== ClientEventHandler.java ====
 
<source lang = "java">
 
<source lang = "java">
 
package mods.sample.upgrade.client;
 
package mods.sample.upgrade.client;
126行目: 146行目:
 
import net.minecraft.util.ResourceLocation;
 
import net.minecraft.util.ResourceLocation;
 
import net.minecraftforge.client.event.ModelBakeEvent;
 
import net.minecraftforge.client.event.ModelBakeEvent;
 +
import net.minecraftforge.client.event.TextureStitchEvent;
 
import net.minecraftforge.client.model.IModel;
 
import net.minecraftforge.client.model.IModel;
 
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
 
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
131行目: 152行目:
 
import com.google.common.base.Function;
 
import com.google.common.base.Function;
  
public class ModelBakeEventHandler {
+
public class ClientEventHandler {
 +
 
 +
@SubscribeEvent
 +
public void onTextureStitchEvent(TextureStitchEvent.Pre event) {
 +
registerTextures(event);
 +
}
  
 
@SubscribeEvent
 
@SubscribeEvent
 
public void onModelBakeEvent(ModelBakeEvent event) {
 
public void onModelBakeEvent(ModelBakeEvent event) {
 
bakeModels(event);
 
bakeModels(event);
 +
}
 +
 +
private void registerTextures(TextureStitchEvent.Pre event) {
 +
Set<ResourceLocation> textureLocations = SampleUpgradeCore.proxy.getTextureLocations();
 +
 +
if (textureLocations == null) {
 +
return;
 +
}
 +
 +
for (ResourceLocation location : textureLocations) {
 +
event.map.registerSprite(location);
 +
}
 +
 
}
 
}
  
207行目: 246行目:
 
public TurtleSample() {
 
public TurtleSample() {
 
String modid = SampleUpgradeCore.MOD_ID;
 
String modid = SampleUpgradeCore.MOD_ID;
 +
SampleUpgradeCore.proxy.setTextureLocation(modid, "blocks/sample_upgrade");
 
modelLeft = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_left");
 
modelLeft = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_left");
 
modelRight = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_right");
 
modelRight = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_right");
266行目: 306行目:
  
 
}
 
}
 +
 
</source>
 
</source>
  
332行目: 373行目:
 
</source>
 
</source>
  
==== turtle_sample_left.json (models/block) ====
+
==== turtle_sample_left.json ====
 
assets\sampleupgrademod\models\block ディレクトリに設置します。
 
assets\sampleupgrademod\models\block ディレクトリに設置します。
 
<source lang = "javascript">
 
<source lang = "javascript">
338行目: 379行目:
 
"parent": "computercraft:block/turtle_upgrade_base_left",
 
"parent": "computercraft:block/turtle_upgrade_base_left",
 
"textures": {
 
"textures": {
"texture": "blocks/stone"
+
"texture": "sampleupgrademod:blocks/sample_upgrade"
 
}
 
}
 
}
 
}
 
</source>
 
</source>
  
==== turtle_sample_right.json (models/block) ====
+
==== turtle_sample_right.json ====
 
assets\sampleupgrademod\models\block ディレクトリに設置します。
 
assets\sampleupgrademod\models\block ディレクトリに設置します。
 
<source lang = "javascript">
 
<source lang = "javascript">
349行目: 390行目:
 
"parent": "computercraft:block/turtle_upgrade_base_right",
 
"parent": "computercraft:block/turtle_upgrade_base_right",
 
"textures": {
 
"textures": {
"texture": "blocks/stone"
+
"texture": "sampleupgrademod:blocks/sample_upgrade"
 
}
 
}
 
}
 
}
 
</source>
 
</source>
 +
 +
==== sample_upgrade.png ====
 +
assets\sampleupgrademod\textures\blocks ディレクトリに設置します。
 +
 +
適当なテクスチャファイル(16x16の画像など)を各自で用意してください。
  
  
366行目: 412行目:
 
Turtle Upgradeのモデルに関する処理はクライアント側でのみ行うため、今回の例では[[プロキシシステムについて|プロキシシステム]]を利用しています。
 
Turtle Upgradeのモデルに関する処理はクライアント側でのみ行うため、今回の例では[[プロキシシステムについて|プロキシシステム]]を利用しています。
  
*ModelBakeEvent受信クラスの登録
+
*Event受信クラスの登録
 
<source lang = "java">
 
<source lang = "java">
 
proxy.registerEventHandlers();
 
proxy.registerEventHandlers();
381行目: 427行目:
 
クライアント側でのみ呼び出されるプロキシクラス
 
クライアント側でのみ呼び出されるプロキシクラス
  
*ModelBakeEvent受信クラスの登録
+
*クライアントevent受信クラスの登録
 
<source lang = "java">
 
<source lang = "java">
 
@Override
 
@Override
388行目: 434行目:
 
}
 
}
 
</source>
 
</source>
ModelBakeEvent受信クラス(ModelBakeEventHandler)のインスタンスをForgeに登録します。
+
クライアントevent受信クラス(ClientEventHandler)のインスタンスをForgeに登録します。
 +
 
 +
*テクスチャ関連のフィールドやメソッド
 +
<source lang = "java">
 +
private Set<ResourceLocation> textureLocations = new HashSet<ResourceLocation>();
 +
 
 +
@Override
 +
public void setTextureLocation(String domain, String path) {
 +
ResourceLocation location = new ResourceLocation(domain, path);
 +
textureLocations.add(location);
 +
}
 +
 
 +
@Override
 +
public Set<ResourceLocation> getTextureLocations() {
 +
return textureLocations;
 +
}
 +
</source>
 +
今回の例ではTurtle Upgradeの独自テクスチャをTextureStitchEvent.Preイベントでまとめて登録するために画像ファイルの位置を前もって登録するためのリストとメソッドを用意しています。<br>
 +
画像ファイルの位置は各ITurtleUpgrade実装クラスのコンストラクタでsetTextureLocationメソッドを呼び出して登録します。
  
 
*モデル関連のフィールドやメソッド
 
*モデル関連のフィールドやメソッド
406行目: 470行目:
 
}
 
}
 
</source>
 
</source>
今回の例ではTurtle Upgradeの各モデルをModelBakeEventでまとめてbakeするために、モデルのJSONファイルの位置を登録するためのリストとメソッドを用意しています。<br>
+
今回の例ではTurtle Upgradeの各モデルをModelBakeEventでまとめてbakeするためにモデルのJSONファイルの位置を前もって登録するためのリストとメソッドを用意しています。<br>
モデルの位置は各ITurtleUpgrade実装クラスのコンストラクタでloadModelLocationメソッドを呼び出して登録します。<br>
+
モデルの位置は各ITurtleUpgrade実装クラスのコンストラクタでloadModelLocationメソッドを呼び出して登録します。
 +
 
 +
==== ClientEventHandler.java ====
 +
各種クライアントeventを受信するためのクラス
 +
 
 +
*TextureStitchEvent.Preイベント
 +
<source lang = "java">
 +
@SubscribeEvent
 +
public void onTextureStitchEvent(TextureStitchEvent.Pre event) {
 +
registerTextures(event);
 +
}
 +
</source>
 +
このメソッドはTextureStitchEvent.Preイベント発生時に呼び出されます。<br>
 +
ここでは自前のテクスチャ画像をテクスチャマップに登録するために、registerTextures()を呼び出しています。
 +
 
 +
*独自テクスチャの登録
 +
<source lang = "java">
 +
private void registerTextures(TextureStitchEvent.Pre event) {
 +
Set<ResourceLocation> textureLocations = SampleUpgradeCore.proxy.getTextureLocations();
 +
 
 +
if (textureLocations == null) {
 +
return;
 +
}
  
==== ModelBakeEventHandler.java ====
+
for (ResourceLocation location : textureLocations) {
ModelBakeEventを受信するためのクラス
+
event.map.registerSprite(location);
 +
}
 +
}
 +
</source>
 +
ClientProxy.textureLocationsに登録されたテクスチャファイルをすべてテクスチャマップに登録します。<br>
 +
これにより、自前の画像ファイルがテクスチャとして使用できるようになります。
  
 
*ModelBakeEvent
 
*ModelBakeEvent
419行目: 510行目:
 
}
 
}
 
</source>
 
</source>
ModelBakeEvent発生時にはこのメソッドが呼び出されます。<br>
+
このメソッドはModelBakeEvent発生時に呼び出されます。<br>
今回はbakeModels()を呼び出しています。
+
ここではモデルをbakeしてmodelRegistryへ登録するため、bakeModels()を呼び出しています。
  
 
*モデルのbakeと登録
 
*モデルのbakeと登録
459行目: 550行目:
 
Turtle Upgradeの機能を定義するクラス
 
Turtle Upgradeの機能を定義するクラス
  
*TurtleUpgradeのモデル位置の登録
+
*コンストラクタ
 
<source lang = "java">
 
<source lang = "java">
private ModelResourceLocation modelLeft;
 
private ModelResourceLocation modelRight;
 
 
 
public TurtleSample() {
 
public TurtleSample() {
 
String modid = SampleUpgradeCore.MOD_ID;
 
String modid = SampleUpgradeCore.MOD_ID;
 +
SampleUpgradeCore.proxy.setTextureLocation(modid, "blocks/sample_upgrade");
 
modelLeft = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_left");
 
modelLeft = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_left");
 
modelRight = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_right");
 
modelRight = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_right");
 
}
 
}
 +
</source>
 +
*TurtleUpgradeで使用するテクスチャファイル位置の登録
 +
<source lang = "java">
 +
String modid = SampleUpgradeCore.MOD_ID;
 +
SampleUpgradeCore.proxy.setTextureLocation(modid, "blocks/sample_upgrade");
 +
</source>
 +
今回は自前の画像ファイルをテクスチャとして使用するので、プロキシクラスで用意したsetTextureLocationメソッドをコンストラクタで呼び出して(クライアント側でのみ)Turtle Upgradeのテクスチャファイルの位置を登録します。<br>
 +
ちなみにこの工程は、バニラなどで使われている(つまり他所でテクスチャマップに登録済みの)テクスチャを流用するときには必要ありません。
 +
*TurtleUpgradeで使用するモデルャファイル位置の登録
 +
<source lang = "java">
 +
String modid = SampleUpgradeCore.MOD_ID;
 +
 +
modelLeft = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_left");
 +
modelRight = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_right");
 
</source>
 
</source>
 
プロキシクラスで用意したloadModelLocationメソッドをコンストラクタで呼び出して(クライアント側でのみ)Turtle UpgradeのモデルのJSONファイルの位置を登録します。<br>
 
プロキシクラスで用意したloadModelLocationメソッドをコンストラクタで呼び出して(クライアント側でのみ)Turtle UpgradeのモデルのJSONファイルの位置を登録します。<br>
495行目: 598行目:
 
Turtle Upgradeを装着するためのアイテムをItemStackで指定します。
 
Turtle Upgradeを装着するためのアイテムをItemStackで指定します。
  
今回はバニラブロックの石(焼石)を指定していますが、modで追加した独自アイテムも同様に指定できます。
+
今回はバニラブロックの石(焼石)を指定していますが、modで追加した独自のブロックやアイテムも同様に指定できます。
  
 
*createPeripheral()
 
*createPeripheral()
570行目: 673行目:
 
周辺機器でTurtle Upgradeの情報を利用したい場合、コンストラクタで受け取って周辺機器側で保存しておきます。<br>
 
周辺機器でTurtle Upgradeの情報を利用したい場合、コンストラクタで受け取って周辺機器側で保存しておきます。<br>
  
==== turtle_sample_left.json (models/block) ====
+
==== turtle_sample_left.json ====
 
Turtleの左側に装着された時のTurtle Upgradeのモデルを指定するJSONファイルです。<br>
 
Turtleの左側に装着された時のTurtle Upgradeのモデルを指定するJSONファイルです。<br>
  
今回の例ではJSONファイルの位置を "block/turtle_sample_left" と指定したため(TurtleSampleのコンストラクタ)、assets\<MOD_ID>\models\block ディレクトリに設置します。
+
今回の例では、TurtleSampleのコンストラクタでファイル位置を "block/turtle_sample_left" と指定したため、assets\<MOD_ID>\models\block ディレクトリに設置します。
  
 
<source lang = "javascript">
 
<source lang = "javascript">
 
"parent": "computercraft:block/turtle_upgrade_base_left",
 
"parent": "computercraft:block/turtle_upgrade_base_left",
 
"textures": {
 
"textures": {
"texture": "blocks/stone"
+
"texture": "sampleupgrademod:blocks/sample_upgrade"
 
}
 
}
 
</source>
 
</source>
モデル自体はComputerCraft本体で定義されている物を流用しており、テクスチャのみ独自に設定(今回はバニラの石ブロック)しています。
+
モデル自体はComputerCraft本体で定義されている物を流用しており、テクスチャのみ独自に設定しています。
  
==== turtle_sample_right.json (models/block) ====
+
==== turtle_sample_right.json ====
 
Turtleの右側に装着された時のTurtle Upgradeのモデルを指定するJSONファイルです。<br>
 
Turtleの右側に装着された時のTurtle Upgradeのモデルを指定するJSONファイルです。<br>
 
解説はturtle_sample_left.jsonの項目を参照してください。
 
解説はturtle_sample_left.jsonの項目を参照してください。
 +
 +
==== sample_upgrade.png ====
 +
Turtle Upgradeのテクスチャ画像ファイルです。
 +
 +
今回の例では、TurtleSampleのコンストラクタとモデルのJSONファイルでファイル位置を "sampleupgrademod:blocks/sample_upgrade" と指定したため、assets\sampleupgrademod\textures\blocks ディレクトリに設置します。

2016年1月11日 (月) 13:37時点における版

ComputerCraft API >

この記事は"Minecraft Forge Universal 10.14.4.x~"及び"ComputerCraft 1.76~"を前提MODとしています。

周辺機器タイプTurtleの追加

周辺機器タイプのTurtle Upgradeを追加します。

  • ITurtleUpgrade(周辺機器タイプ)の実装
  • TurtleUpgradeの登録
  • IPeripheralの実装
  • Turtle Upgradeの外観(独自テクスチャ・モデル)の実装

ソースコード

MC1.8 ツールタイプTurtleの追加」や「MC1.8 周辺機器の追加」の解説を元にして、変更部分のみを解説します。
Packageは適宜設定してください。

SampleUpgradeCore.java

package mods.sample.upgrade;

import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import dan200.computercraft.api.ComputerCraftAPI;

@Mod(modid=SampleUpgradeCore.MOD_ID,
	name=SampleUpgradeCore.MOD_NAME,
	version=SampleUpgradeCore.MOD_VERSION,
	dependencies = SampleUpgradeCore.MOD_DEPENDENCIES)
public class SampleUpgradeCore {

	public static final String MOD_ID = "sampleupgrademod";
	public static final String MOD_NAME = "Sample Upgrade Mod";
	public static final String MOD_VERSION = "1.0";
	public static final String MOD_DEPENDENCIES = "after:ComputerCraft";

	@SidedProxy(clientSide = "mods.sample.upgrade.client.ClientProxy", serverSide = "mods.sample.upgrade.CommonProxy")
	public static CommonProxy proxy;

	@EventHandler
	public void init(FMLInitializationEvent event) {
		proxy.registerEventHandlers();
		ComputerCraftAPI.registerTurtleUpgrade(new TurtleSample());
	}

}

CommonProxy.java

package mods.sample.upgrade;

import java.util.Set;

import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.util.ResourceLocation;

public class CommonProxy {

	public void registerEventHandlers() {

	}

	public void setTextureLocation(String domain, String path) {

	}

	public Set<ResourceLocation> getTextureLocations() {
		return null;
	}

	public ModelResourceLocation loadModelLocation(String domain, String path) {
		return null;
	}

	public Set<ResourceLocation> getModelLocations() {
		return null;
	}

}

ClientProxy.java

package mods.sample.upgrade.client;

import java.util.HashSet;
import java.util.Set;

import mods.sample.upgrade.CommonProxy;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class ClientProxy extends CommonProxy {

	private Set<ResourceLocation> textureLocations = new HashSet<ResourceLocation>();
	private Set<ResourceLocation> modelLocations = new HashSet<ResourceLocation>();

	@Override
	public void registerEventHandlers() {
		MinecraftForge.EVENT_BUS.register(new ClientEventHandler());
	}

	@Override
	public void setTextureLocation(String domain, String path) {
		ResourceLocation location = new ResourceLocation(domain, path);
		textureLocations.add(location);
	}

	@Override
	public Set<ResourceLocation> getTextureLocations() {
		return textureLocations;
	}

	@Override
	public ModelResourceLocation loadModelLocation(String domain, String path) {
		ResourceLocation location = new ResourceLocation(domain, path);
		modelLocations.add(location);
		return new ModelResourceLocation(location, "inventory");
	}

	@Override
	public Set<ResourceLocation> getModelLocations() {
		return modelLocations;
	}

}

ClientEventHandler.java

package mods.sample.upgrade.client;

import java.util.Set;

import mods.sample.upgrade.SampleUpgradeCore;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.client.resources.model.IBakedModel;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.ModelBakeEvent;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.client.model.IModel;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import com.google.common.base.Function;

public class ClientEventHandler {

	@SubscribeEvent
	public void onTextureStitchEvent(TextureStitchEvent.Pre event) {
		registerTextures(event);
	}

	@SubscribeEvent
	public void onModelBakeEvent(ModelBakeEvent event) {
		bakeModels(event);
	}

	private void registerTextures(TextureStitchEvent.Pre event) {
		Set<ResourceLocation> textureLocations = SampleUpgradeCore.proxy.getTextureLocations();

		if (textureLocations == null) {
			return;
		}

		for (ResourceLocation location : textureLocations) {
			event.map.registerSprite(location);
		}

	}

	private void bakeModels(ModelBakeEvent event) {
		Set<ResourceLocation> modelLocations = SampleUpgradeCore.proxy.getModelLocations();

		if (modelLocations == null) {
			return;
		}

		for (ResourceLocation location : modelLocations) {
			try {
				IModel model = event.modelLoader.getModel(location);
				IBakedModel bakedModel = model.bake(model.getDefaultState(),
						DefaultVertexFormats.ITEM,
						new Function<ResourceLocation, TextureAtlasSprite>() {

							@Override
							public TextureAtlasSprite apply(ResourceLocation location) {
								Minecraft mc = Minecraft.getMinecraft();
								return mc.getTextureMapBlocks().getAtlasSprite(location.toString());
							}

						});
				ModelResourceLocation modelLocation = new ModelResourceLocation(location, "inventory");
				event.modelRegistry.putObject(modelLocation, bakedModel);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}

TurtleSample.java

package mods.sample.upgrade;

import javax.vecmath.Matrix4f;

import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.IBakedModel;
import net.minecraft.client.resources.model.ModelManager;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import org.apache.commons.lang3.tuple.Pair;

import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.api.turtle.TurtleUpgradeType;
import dan200.computercraft.api.turtle.TurtleVerb;

public class TurtleSample implements ITurtleUpgrade {

	private ResourceLocation upgradeID = new ResourceLocation(SampleUpgradeCore.MOD_ID, "sample");
	private ItemStack upgradeItem = new ItemStack(Blocks.stone);

	private ModelResourceLocation modelLeft;
	private ModelResourceLocation modelRight;

	public TurtleSample() {
		String modid = SampleUpgradeCore.MOD_ID;
		SampleUpgradeCore.proxy.setTextureLocation(modid, "blocks/sample_upgrade");
		modelLeft = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_left");
		modelRight = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_right");
	}

	@Override
	public ResourceLocation getUpgradeID() {
		return upgradeID;
	}

	@Override
	public int getLegacyUpgradeID() {
		return -1;
	}

	@Override
	public String getUnlocalisedAdjective() {
		return "Sample";
	}

	@Override
	public TurtleUpgradeType getType() {
		return TurtleUpgradeType.Peripheral;
	}

	@Override
	public ItemStack getCraftingItem() {
		return upgradeItem;
	}

	@Override
	public IPeripheral createPeripheral(ITurtleAccess turtle, TurtleSide side) {
		return new SamplePeripheral(turtle, side);
	}

	@Override
	public TurtleCommandResult useTool(ITurtleAccess turtle, TurtleSide side,
			TurtleVerb verb, EnumFacing direction) {
		return null;
	}

	@Override
	@SideOnly(Side.CLIENT)
	public Pair<IBakedModel, Matrix4f> getModel(ITurtleAccess turtle, TurtleSide side) {
		Minecraft mc = Minecraft.getMinecraft();
		ModelManager modelManager = mc.getRenderItem().getItemModelMesher().getModelManager();

		if (side == TurtleSide.Left) {
			return Pair.of(modelManager.getModel(modelLeft), null);
		} else {
			return Pair.of(modelManager.getModel(modelRight), null);
		}
	}

	@Override
	public void update(ITurtleAccess turtle, TurtleSide side) {

	}

}

SamplePeripheral.java

package mods.sample.upgrade;

import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.TurtleSide;

public class SamplePeripheral implements IPeripheral {

	private final ITurtleAccess turtleAccess;
	private final TurtleSide turtleSide;

	public SamplePeripheral(ITurtleAccess turtle, TurtleSide side) {
		turtleAccess = turtle;
		turtleSide = side;
	}

	@Override
	public String getType() {
		return "sample";
	}

	@Override
	public String[] getMethodNames() {
		return new String[] {};
	}

	@Override
	public Object[] callMethod(IComputerAccess computer, ILuaContext context,
			int method, Object[] arguments) throws LuaException,
			InterruptedException {
		switch(method) {

		}

		return null;
	}

	@Override
	public void attach(IComputerAccess computer) {

	}

	@Override
	public void detach(IComputerAccess computer) {

	}

	@Override
	public boolean equals(IPeripheral other) {
		if ((other != null) && (other instanceof SamplePeripheral)) {
			return other == this;
		}

		return false;
	}

}

turtle_sample_left.json

assets\sampleupgrademod\models\block ディレクトリに設置します。

{
	"parent": "computercraft:block/turtle_upgrade_base_left",
	"textures": {
		"texture": "sampleupgrademod:blocks/sample_upgrade"
	}
}

turtle_sample_right.json

assets\sampleupgrademod\models\block ディレクトリに設置します。

{
	"parent": "computercraft:block/turtle_upgrade_base_right",
	"textures": {
		"texture": "sampleupgrademod:blocks/sample_upgrade"
	}
}

sample_upgrade.png

assets\sampleupgrademod\textures\blocks ディレクトリに設置します。

適当なテクスチャファイル(16x16の画像など)を各自で用意してください。


解説

SampleUpgradeCore.java

Modのコアとなるクラス

  • プロキシクラスの指定
@SidedProxy(clientSide = "mods.sample.upgrade.client.ClientProxy", serverSide = "mods.sample.upgrade.CommonProxy")
public static CommonProxy proxy;

Turtle Upgradeのモデルに関する処理はクライアント側でのみ行うため、今回の例ではプロキシシステムを利用しています。

  • Event受信クラスの登録
proxy.registerEventHandlers();

プロキシを介して登録用メソッドを呼び出し、クライアント側でのみ登録されるようにしています。

CommonProxy.java

サーバー側でのみ呼び出されるプロキシクラス

クライアント側のプロキシクラスでオーバーライドするためのメソッドを実装します。
クライアント側での処理がメインとなるため、こちらでは何もしません。

ClientProxy.java

クライアント側でのみ呼び出されるプロキシクラス

  • クライアントevent受信クラスの登録
@Override
public void registerEventHandlers() {
	MinecraftForge.EVENT_BUS.register(new ModelBakeEventHandler());
}

クライアントevent受信クラス(ClientEventHandler)のインスタンスをForgeに登録します。

  • テクスチャ関連のフィールドやメソッド
private Set<ResourceLocation> textureLocations = new HashSet<ResourceLocation>();

@Override
public void setTextureLocation(String domain, String path) {
	ResourceLocation location = new ResourceLocation(domain, path);
	textureLocations.add(location);
}

@Override
public Set<ResourceLocation> getTextureLocations() {
	return textureLocations;
}

今回の例ではTurtle Upgradeの独自テクスチャをTextureStitchEvent.Preイベントでまとめて登録するために画像ファイルの位置を前もって登録するためのリストとメソッドを用意しています。
画像ファイルの位置は各ITurtleUpgrade実装クラスのコンストラクタでsetTextureLocationメソッドを呼び出して登録します。

  • モデル関連のフィールドやメソッド
private Set<ResourceLocation> modelLocations = new HashSet<ResourceLocation>();

@Override
public Set<ResourceLocation> getModelLocations() {
	return modelLocations;
}

@Override
public ModelResourceLocation loadModelLocation(String domain, String path) {
	ResourceLocation location = new ResourceLocation(domain, path);
	modelLocations.add(location);
	return new ModelResourceLocation(location, "inventory");
}

今回の例ではTurtle Upgradeの各モデルをModelBakeEventでまとめてbakeするためにモデルのJSONファイルの位置を前もって登録するためのリストとメソッドを用意しています。
モデルの位置は各ITurtleUpgrade実装クラスのコンストラクタでloadModelLocationメソッドを呼び出して登録します。

ClientEventHandler.java

各種クライアントeventを受信するためのクラス

  • TextureStitchEvent.Preイベント
@SubscribeEvent
public void onTextureStitchEvent(TextureStitchEvent.Pre event) {
	registerTextures(event);
}

このメソッドはTextureStitchEvent.Preイベント発生時に呼び出されます。
ここでは自前のテクスチャ画像をテクスチャマップに登録するために、registerTextures()を呼び出しています。

  • 独自テクスチャの登録
private void registerTextures(TextureStitchEvent.Pre event) {
	Set<ResourceLocation> textureLocations = SampleUpgradeCore.proxy.getTextureLocations();

	if (textureLocations == null) {
		return;
	}

	for (ResourceLocation location : textureLocations) {
		event.map.registerSprite(location);
	}
}

ClientProxy.textureLocationsに登録されたテクスチャファイルをすべてテクスチャマップに登録します。
これにより、自前の画像ファイルがテクスチャとして使用できるようになります。

  • ModelBakeEvent
@SubscribeEvent
public void onModelBakeEvent(ModelBakeEvent event) {
	bakeModels(event);
}

このメソッドはModelBakeEvent発生時に呼び出されます。
ここではモデルをbakeしてmodelRegistryへ登録するため、bakeModels()を呼び出しています。

  • モデルのbakeと登録
private void bakeModels(ModelBakeEvent event) {
	Set<ResourceLocation> modelLocations = SampleUpgradeCore.proxy.getModelLocations();

	if (modelLocations == null) {
		return;
	}

	for (ResourceLocation location : modelLocations) {
		try {
			IModel model = event.modelLoader.getModel(location);
			IBakedModel bakedModel = model.bake(model.getDefaultState(),
					DefaultVertexFormats.ITEM,
					new Function<ResourceLocation, TextureAtlasSprite>() {

						@Override
						public TextureAtlasSprite apply(ResourceLocation location) {
							Minecraft mc = Minecraft.getMinecraft();
							return mc.getTextureMapBlocks().getAtlasSprite(location.toString());
						}

					});
			ModelResourceLocation modelLocation = new ModelResourceLocation(location, "inventory");
			event.modelRegistry.putObject(modelLocation, bakedModel);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

ClientProxy.modelLocationsに登録されたモデルをすべてbakeし、modelRegistryに登録します。
これにより、ITurtleUpgrade実装クラスのgetModel()で指定したモデルが描画されるようになります。

TurtleSample.java

Turtle Upgradeの機能を定義するクラス

  • コンストラクタ
public TurtleSample() {
	String modid = SampleUpgradeCore.MOD_ID;
	SampleUpgradeCore.proxy.setTextureLocation(modid, "blocks/sample_upgrade");
	modelLeft = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_left");
	modelRight = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_right");
}
  • TurtleUpgradeで使用するテクスチャファイル位置の登録
String modid = SampleUpgradeCore.MOD_ID;
SampleUpgradeCore.proxy.setTextureLocation(modid, "blocks/sample_upgrade");

今回は自前の画像ファイルをテクスチャとして使用するので、プロキシクラスで用意したsetTextureLocationメソッドをコンストラクタで呼び出して(クライアント側でのみ)Turtle Upgradeのテクスチャファイルの位置を登録します。
ちなみにこの工程は、バニラなどで使われている(つまり他所でテクスチャマップに登録済みの)テクスチャを流用するときには必要ありません。

  • TurtleUpgradeで使用するモデルャファイル位置の登録
String modid = SampleUpgradeCore.MOD_ID;

modelLeft = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_left");
modelRight = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_right");

プロキシクラスで用意したloadModelLocationメソッドをコンストラクタで呼び出して(クライアント側でのみ)Turtle UpgradeのモデルのJSONファイルの位置を登録します。
戻り値はgetModel()で使うためメンバフィールドに保存します。

  • getType()
@Override
public TurtleUpgradeType getType() {
	return TurtleUpgradeType.Peripheral;
}

Turtle Upgradeの種類をTurtleUpgradeTypeの値で指定します。

今回は周辺機器タイプなので TurtleUpgradeType.Peripheral を返しています。

  • getCraftingItem()
private ItemStack upgradeItem = new ItemStack(Blocks.stone);

@Override
public ItemStack getCraftingItem() {
	return upgradeItem;
}

Turtle Upgradeを装着するためのアイテムをItemStackで指定します。

今回はバニラブロックの石(焼石)を指定していますが、modで追加した独自のブロックやアイテムも同様に指定できます。

  • createPeripheral()
@Override
public IPeripheral createPeripheral(ITurtleAccess turtle, TurtleSide side) {
	return new SamplePeripheral(turtle, side);
}

周辺機器タイプのTurtle Upgradeの場合、周辺機器の動作を定義したIPeripheral実装クラスのインスタンスを返します。

IPeripheral実装クラスにはコンストラクタの引数でTurtle Upgradeの情報を渡しています。

  • useTool()
@Override
public TurtleCommandResult useTool(ITurtleAccess turtle, TurtleSide side,
		TurtleVerb verb, EnumFacing direction) {
	return null;
}

周辺機器タイプのTurtle Upgradeでは呼び出されることは無いため、単にnullを返しています。

  • getModel()
@Override
@SideOnly(Side.CLIENT)
public Pair<IBakedModel, Matrix4f> getModel(ITurtleAccess turtle, TurtleSide side) {
	Minecraft mc = Minecraft.getMinecraft();
	ModelManager modelManager = mc.getRenderItem().getItemModelMesher().getModelManager();

	if (side == TurtleSide.Left) {
		return Pair.of(modelManager.getModel(modelLeft), null);
	} else {
		return Pair.of(modelManager.getModel(modelRight), null);
	}
}

Turtleに装着されたTurtle Upgradeの外観を指定します。
戻り値はPair<IBakedModel, Matrix4f>で、IBakedModelがモデル、Matrix4fがモデルを変形する座標変換行列です。

今回はCC1.76の実装を参考にしています。
コンストラクタで登録してModelBakeEventでbakeされたモデルをModelManager.getModel()で取得して返しています。
Turtle Upgradeを装着した位置がTurtleの左側ならば左用のモデルを、右側ならば右用のモデルを描画するようにしています。

  • update()
@Override
public void update(ITurtleAccess turtle, TurtleSide side) {
 
}

Turtle UpgradeがTurtleに装着されている間、毎tick呼び出されます。
ロードされているワールド上で装着されている数だけ呼び出されますが、各パラメータでどれに対する呼び出しなのかが判別が可能です。
また、サーバ側とクライアント側でそれぞれ別に呼び出されますが、どちら側の呼び出しなのかはturtle.getWorld().isRemoteの値で判別可能です。
ITurtleAccess.getPeripheral()で装着されている周辺機器Upgradeの周辺機器クラスのインスタンスを取得できます。
turtle.getUpgradeNBTData()で読み書き可能なNBTTagCompoundを取得できます。このNBTTagCompoundはTurtleに記録され、ゲームを終了しても消去されません(Turtle Upgradeを取り外したりTurtleが破壊されると消えます)。

今回は何もしていません。

SamplePeripheral.java

周辺機器を定義するクラス

  • 装着されたTurtleの情報取得・保存
private final ITurtleAccess turtleAccess;
private final TurtleSide turtleSide;

public SamplePeripheral(ITurtleAccess turtle, TurtleSide side) {
	turtleAccess = turtle;
	turtleSide = side;
}

周辺機器でTurtle Upgradeの情報を利用したい場合、コンストラクタで受け取って周辺機器側で保存しておきます。

turtle_sample_left.json

Turtleの左側に装着された時のTurtle Upgradeのモデルを指定するJSONファイルです。

今回の例では、TurtleSampleのコンストラクタでファイル位置を "block/turtle_sample_left" と指定したため、assets\<MOD_ID>\models\block ディレクトリに設置します。

"parent": "computercraft:block/turtle_upgrade_base_left",
"textures": {
	"texture": "sampleupgrademod:blocks/sample_upgrade"
}

モデル自体はComputerCraft本体で定義されている物を流用しており、テクスチャのみ独自に設定しています。

turtle_sample_right.json

Turtleの右側に装着された時のTurtle Upgradeのモデルを指定するJSONファイルです。
解説はturtle_sample_left.jsonの項目を参照してください。

sample_upgrade.png

Turtle Upgradeのテクスチャ画像ファイルです。

今回の例では、TurtleSampleのコンストラクタとモデルのJSONファイルでファイル位置を "sampleupgrademod:blocks/sample_upgrade" と指定したため、assets\sampleupgrademod\textures\blocks ディレクトリに設置します。