提供: Minecraft Modding Wiki
移動先: 案内検索
(対応バージョンをMC1.8.9まで引き上げ)
(サンプルコードで、サーバー側にもModelResourceLocationが使われていたのを修正)
12行目: 12行目:
 
=== ソースコード ===
 
=== ソースコード ===
 
「[[ComputerCraft_API/1.76/ツールタイプTurtleの追加|MC1.8 ツールタイプTurtleの追加]]」や「[[ComputerCraft_API/1.76/周辺機器の追加|MC1.8 周辺機器の追加]]」の解説を元にして、変更部分のみを解説します。<br>
 
「[[ComputerCraft_API/1.76/ツールタイプTurtleの追加|MC1.8 ツールタイプTurtleの追加]]」や「[[ComputerCraft_API/1.76/周辺機器の追加|MC1.8 周辺機器の追加]]」の解説を元にして、変更部分のみを解説します。<br>
Packageは適宜設定してください。
 
  
 
==== SampleUpgradeCore.java ====
 
==== SampleUpgradeCore.java ====
25行目: 24行目:
  
 
@Mod( modid = SampleUpgradeCore.MOD_ID,
 
@Mod( modid = SampleUpgradeCore.MOD_ID,
name = SampleUpgradeCore.MOD_NAME,
+
name = SampleUpgradeCore.MOD_NAME,
version = SampleUpgradeCore.MOD_VERSION,
+
version = SampleUpgradeCore.MOD_VERSION,
dependencies = SampleUpgradeCore.MOD_DEPENDENCIES,
+
dependencies = SampleUpgradeCore.MOD_DEPENDENCIES,
acceptedMinecraftVersions = SampleUpgradeCore.MOD_ACCEPTED_MC_VERSIONS)
+
acceptedMinecraftVersions = SampleUpgradeCore.MOD_ACCEPTED_MC_VERSIONS)
 
public class SampleUpgradeCore {
 
public class SampleUpgradeCore {
  
55行目: 54行目:
 
import java.util.Set;
 
import java.util.Set;
  
import net.minecraft.client.resources.model.ModelResourceLocation;
 
 
import net.minecraft.util.ResourceLocation;
 
import net.minecraft.util.ResourceLocation;
 +
import net.minecraftforge.fml.relauncher.Side;
  
 
public class CommonProxy {
 
public class CommonProxy {
 +
 +
public Side getSide() {
 +
return Side.SERVER;
 +
}
  
 
public void registerEventHandlers() {
 
public void registerEventHandlers() {
64行目: 67行目:
 
}
 
}
  
public void setTextureLocation(String domain, String path) {
+
public void registerTextureLocation(String domain, String path) {
  
 
}
 
}
72行目: 75行目:
 
}
 
}
  
public ModelResourceLocation loadModelLocation(String domain, String path) {
+
public String loadModelLocation(String domain, String path) {
 
return null;
 
return null;
 
}
 
}
95行目: 98行目:
 
import net.minecraftforge.common.MinecraftForge;
 
import net.minecraftforge.common.MinecraftForge;
 
import net.minecraftforge.fml.relauncher.Side;
 
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
 
  
@SideOnly(Side.CLIENT)
 
 
public class ClientProxy extends CommonProxy {
 
public class ClientProxy extends CommonProxy {
  
 
private Set<ResourceLocation> textureLocations = new HashSet<ResourceLocation>();
 
private Set<ResourceLocation> textureLocations = new HashSet<ResourceLocation>();
 
private Set<ResourceLocation> modelLocations = new HashSet<ResourceLocation>();
 
private Set<ResourceLocation> modelLocations = new HashSet<ResourceLocation>();
 +
 +
@Override
 +
public Side getSide() {
 +
return Side.CLIENT;
 +
}
  
 
@Override
 
@Override
109行目: 115行目:
  
 
@Override
 
@Override
public void setTextureLocation(String domain, String path) {
+
public void registerTextureLocation(String domain, String path) {
 
ResourceLocation location = new ResourceLocation(domain, path);
 
ResourceLocation location = new ResourceLocation(domain, path);
 
textureLocations.add(location);
 
textureLocations.add(location);
120行目: 126行目:
  
 
@Override
 
@Override
public ModelResourceLocation loadModelLocation(String domain, String path) {
+
public String loadModelLocation(String domain, String path) {
 
ResourceLocation location = new ResourceLocation(domain, path);
 
ResourceLocation location = new ResourceLocation(domain, path);
 
modelLocations.add(location);
 
modelLocations.add(location);
return new ModelResourceLocation(location, "inventory");
+
return (new ModelResourceLocation(location, "inventory")).toString();
 
}
 
}
  
138行目: 144行目:
 
package mods.sample.upgrade.client;
 
package mods.sample.upgrade.client;
  
 +
import java.io.IOException;
 
import java.util.Set;
 
import java.util.Set;
  
176行目: 183行目:
 
event.map.registerSprite(location);
 
event.map.registerSprite(location);
 
}
 
}
 
 
}
 
}
  
202行目: 208行目:
 
ModelResourceLocation modelLocation = new ModelResourceLocation(location, "inventory");
 
ModelResourceLocation modelLocation = new ModelResourceLocation(location, "inventory");
 
event.modelRegistry.putObject(modelLocation, bakedModel);
 
event.modelRegistry.putObject(modelLocation, bakedModel);
} catch (Exception e) {
+
} catch (IOException e) {
 
e.printStackTrace();
 
e.printStackTrace();
 
}
 
}
243行目: 249行目:
 
private ItemStack upgradeItem = new ItemStack(Blocks.stone);
 
private ItemStack upgradeItem = new ItemStack(Blocks.stone);
  
 +
@SideOnly(Side.CLIENT)
 
private ModelResourceLocation modelLeft;
 
private ModelResourceLocation modelLeft;
 +
@SideOnly(Side.CLIENT)
 
private ModelResourceLocation modelRight;
 
private ModelResourceLocation modelRight;
  
 
public TurtleSample() {
 
public TurtleSample() {
String modid = SampleUpgradeCore.MOD_ID;
+
CommonProxy proxy = SampleUpgradeCore.proxy;
SampleUpgradeCore.proxy.setTextureLocation(modid, "blocks/sample_upgrade");
+
 
modelLeft = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_left");
+
if (proxy.getSide().isClient()) {
modelRight = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_right");
+
String modid = SampleUpgradeCore.MOD_ID;
 +
proxy.registerTextureLocation(modid, "blocks/sample_upgrade");
 +
modelLeft = new ModelResourceLocation(proxy.loadModelLocation(modid, "block/turtle_sample_left"));
 +
modelRight = new ModelResourceLocation(proxy.loadModelLocation(modid, "block/turtle_sample_right"));
 +
}
 
}
 
}
  
308行目: 320行目:
  
 
}
 
}
 
 
</source>
 
</source>
  
422行目: 433行目:
 
==== CommonProxy.java ====
 
==== CommonProxy.java ====
 
サーバー側でのみ呼び出されるプロキシクラス
 
サーバー側でのみ呼び出されるプロキシクラス
 +
 +
*getSide()
 +
<source lang = "java">
 +
public Side getSide() {
 +
return Side.SERVER;
 +
}
 +
</source>
 +
現在の処理がサーバー側かクライアント側かを判別するためのメソッドを用意します。<br>
 +
こちらはサーバー側のときのみ呼び出されます。
 +
 +
*テクスチャ・モデル関連のメソッド
  
 
クライアント側のプロキシクラスでオーバーライドするためのメソッドを実装します。<br>
 
クライアント側のプロキシクラスでオーバーライドするためのメソッドを実装します。<br>
428行目: 450行目:
 
==== ClientProxy.java ====
 
==== ClientProxy.java ====
 
クライアント側でのみ呼び出されるプロキシクラス
 
クライアント側でのみ呼び出されるプロキシクラス
 +
 +
*getSide()
 +
<source lang = "java">
 +
@Override
 +
public Side getSide() {
 +
return Side.CLIENT;
 +
}
 +
</source>
 +
現在の処理がサーバー側かクライアント側かを判別するためのメソッドをオーバーライドで用意します。<br>
 +
こちらはクライアント側のときのみ呼び出されます。
  
 
*クライアントevent受信クラスの登録
 
*クライアントevent受信クラスの登録
443行目: 475行目:
  
 
@Override
 
@Override
public void setTextureLocation(String domain, String path) {
+
public void registerTextureLocation(String domain, String path) {
 
ResourceLocation location = new ResourceLocation(domain, path);
 
ResourceLocation location = new ResourceLocation(domain, path);
 
textureLocations.add(location);
 
textureLocations.add(location);
454行目: 486行目:
 
</source>
 
</source>
 
今回の例ではTurtle Upgradeの独自テクスチャをTextureStitchEvent.Preイベントでまとめて登録するために画像ファイルの位置を前もって登録するためのリストとメソッドを用意しています。<br>
 
今回の例ではTurtle Upgradeの独自テクスチャをTextureStitchEvent.Preイベントでまとめて登録するために画像ファイルの位置を前もって登録するためのリストとメソッドを用意しています。<br>
画像ファイルの位置は各ITurtleUpgrade実装クラスのコンストラクタでsetTextureLocationメソッドを呼び出して登録します。
+
画像ファイルの位置は各ITurtleUpgrade実装クラスのコンストラクタでregisterTextureLocationメソッドを呼び出して登録します。
  
 
*モデル関連のフィールドやメソッド
 
*モデル関連のフィールドやメソッド
461行目: 493行目:
  
 
@Override
 
@Override
public Set<ResourceLocation> getModelLocations() {
+
public String loadModelLocation(String domain, String path) {
return modelLocations;
+
ResourceLocation location = new ResourceLocation(domain, path);
 +
modelLocations.add(location);
 +
return (new ModelResourceLocation(location, "inventory")).toString();
 
}
 
}
  
 
@Override
 
@Override
public ModelResourceLocation loadModelLocation(String domain, String path) {
+
public Set<ResourceLocation> getModelLocations() {
ResourceLocation location = new ResourceLocation(domain, path);
+
return modelLocations;
modelLocations.add(location);
 
return new ModelResourceLocation(location, "inventory");
 
 
}
 
}
 
</source>
 
</source>
547行目: 579行目:
 
</source>
 
</source>
 
ClientProxy.modelLocationsに登録されたモデルをすべてbakeし、modelRegistryに登録します。<br>
 
ClientProxy.modelLocationsに登録されたモデルをすべてbakeし、modelRegistryに登録します。<br>
これにより、ITurtleUpgrade実装クラスのgetModel()で指定したモデルが描画されるようになります。
+
ここで登録したbake済みモデルは、MinecraftのModelManagerからgetModel(ModelResourceLocation)メソッドで取得できるようになります。(後述のTurtleSample.getModel()の解説を参照)
  
 
==== TurtleSample.java ====
 
==== TurtleSample.java ====
554行目: 586行目:
 
*コンストラクタ
 
*コンストラクタ
 
<source lang = "java">
 
<source lang = "java">
 +
@SideOnly(Side.CLIENT)
 +
private ModelResourceLocation modelLeft;
 +
@SideOnly(Side.CLIENT)
 +
private ModelResourceLocation modelRight;
 +
 
public TurtleSample() {
 
public TurtleSample() {
String modid = SampleUpgradeCore.MOD_ID;
+
CommonProxy proxy = SampleUpgradeCore.proxy;
SampleUpgradeCore.proxy.setTextureLocation(modid, "blocks/sample_upgrade");
+
 
modelLeft = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_left");
+
if (proxy.getSide().isClient()) {
modelRight = SampleUpgradeCore.proxy.loadModelLocation(modid, "block/turtle_sample_right");
+
String modid = SampleUpgradeCore.MOD_ID;
 +
proxy.registerTextureLocation(modid, "blocks/sample_upgrade");
 +
modelLeft = new ModelResourceLocation(proxy.loadModelLocation(modid, "block/turtle_sample_left"));
 +
modelRight = new ModelResourceLocation(proxy.loadModelLocation(modid, "block/turtle_sample_right"));
 +
}
 
}
 
}
 
</source>
 
</source>
 +
proxy.getSide()の返値で処理を分岐して、TurtleUpgradeで使用するテクスチャやモデル関連の処理がクライアント側でのみ実行されるようにしています。
 +
 
*TurtleUpgradeで使用するテクスチャファイル位置の登録
 
*TurtleUpgradeで使用するテクスチャファイル位置の登録
 
<source lang = "java">
 
<source lang = "java">
String modid = SampleUpgradeCore.MOD_ID;
+
proxy.registerTextureLocation(modid, "blocks/sample_upgrade");
SampleUpgradeCore.proxy.setTextureLocation(modid, "blocks/sample_upgrade");
 
 
</source>
 
</source>
今回は自前の画像ファイルをテクスチャとして使用するので、プロキシクラスで用意したsetTextureLocationメソッドをコンストラクタで呼び出して(クライアント側でのみ)Turtle Upgradeのテクスチャファイルの位置を登録します。<br>
+
今回は自前の画像ファイルをテクスチャとして使用するので、プロキシクラスで用意したregisterTextureLocationメソッドをコンストラクタで呼び出して(クライアント側でのみ)Turtle Upgradeのテクスチャファイルの位置を登録します。<br>
ちなみにこの工程は、バニラなどで使われている(つまり他所でテクスチャマップに登録済みの)テクスチャを流用するときには必要ありません。
+
ちなみにこの工程は他所で登録されているテクスチャを流用するときには不要です。
 +
 
 
*TurtleUpgradeで使用するモデルャファイル位置の登録
 
*TurtleUpgradeで使用するモデルャファイル位置の登録
 
<source lang = "java">
 
<source lang = "java">
String modid = SampleUpgradeCore.MOD_ID;
+
modelLeft = new ModelResourceLocation(proxy.loadModelLocation(modid, "block/turtle_sample_left"));
 
+
modelRight = new ModelResourceLocation(proxy.loadModelLocation(modid, "block/turtle_sample_right"));
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>
戻り値はgetModel()で使うためメンバフィールドに保存します。
+
戻り値はgetModel()で使うためModelResourceLocation形式でメンバフィールドに保存します。
  
 
*getType()
 
*getType()
678行目: 719行目:
 
Turtleの左側に装着された時のTurtle Upgradeのモデルを指定するJSONファイルです。<br>
 
Turtleの左側に装着された時のTurtle Upgradeのモデルを指定するJSONファイルです。<br>
  
今回の例では、TurtleSampleのコンストラクタでファイル位置を "block/turtle_sample_left" と指定したため、assets\<MOD_ID>\models\block ディレクトリに設置します。
+
今回の例では、TurtleSampleのコンストラクタでファイル位置を "sampleupgrademod:block/turtle_sample_left" と指定したため、assets\sampleupgrademod\models\block ディレクトリに設置します。
  
 
<source lang = "javascript">
 
<source lang = "javascript">

2016年1月22日 (金) 07:04時点における版

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 周辺機器の追加」の解説を元にして、変更部分のみを解説します。

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,
		acceptedMinecraftVersions = SampleUpgradeCore.MOD_ACCEPTED_MC_VERSIONS)
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";
	public static final String MOD_ACCEPTED_MC_VERSIONS = "[1.8,1.8.9]";

	@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.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;

public class CommonProxy {

	public Side getSide() {
		return Side.SERVER;
	}

	public void registerEventHandlers() {

	}

	public void registerTextureLocation(String domain, String path) {

	}

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

	public String 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;

public class ClientProxy extends CommonProxy {

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

	@Override
	public Side getSide() {
		return Side.CLIENT;
	}

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

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

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

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

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

}

ClientEventHandler.java

package mods.sample.upgrade.client;

import java.io.IOException;
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 (IOException 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);

	@SideOnly(Side.CLIENT)
	private ModelResourceLocation modelLeft;
	@SideOnly(Side.CLIENT)
	private ModelResourceLocation modelRight;

	public TurtleSample() {
		CommonProxy proxy = SampleUpgradeCore.proxy;

		if (proxy.getSide().isClient()) {
			String modid = SampleUpgradeCore.MOD_ID;
			proxy.registerTextureLocation(modid, "blocks/sample_upgrade");
			modelLeft = new ModelResourceLocation(proxy.loadModelLocation(modid, "block/turtle_sample_left"));
			modelRight = new ModelResourceLocation(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

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

  • getSide()
public Side getSide() {
	return Side.SERVER;
}

現在の処理がサーバー側かクライアント側かを判別するためのメソッドを用意します。
こちらはサーバー側のときのみ呼び出されます。

  • テクスチャ・モデル関連のメソッド

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

ClientProxy.java

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

  • getSide()
@Override
public Side getSide() {
	return Side.CLIENT;
}

現在の処理がサーバー側かクライアント側かを判別するためのメソッドをオーバーライドで用意します。
こちらはクライアント側のときのみ呼び出されます。

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

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

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

@Override
public void registerTextureLocation(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実装クラスのコンストラクタでregisterTextureLocationメソッドを呼び出して登録します。

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

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

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

今回の例では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に登録します。
ここで登録したbake済みモデルは、MinecraftのModelManagerからgetModel(ModelResourceLocation)メソッドで取得できるようになります。(後述のTurtleSample.getModel()の解説を参照)

TurtleSample.java

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

  • コンストラクタ
@SideOnly(Side.CLIENT)
private ModelResourceLocation modelLeft;
@SideOnly(Side.CLIENT)
private ModelResourceLocation modelRight;

public TurtleSample() {
	CommonProxy proxy = SampleUpgradeCore.proxy;

	if (proxy.getSide().isClient()) {
		String modid = SampleUpgradeCore.MOD_ID;
		proxy.registerTextureLocation(modid, "blocks/sample_upgrade");
		modelLeft = new ModelResourceLocation(proxy.loadModelLocation(modid, "block/turtle_sample_left"));
		modelRight = new ModelResourceLocation(proxy.loadModelLocation(modid, "block/turtle_sample_right"));
	}
}

proxy.getSide()の返値で処理を分岐して、TurtleUpgradeで使用するテクスチャやモデル関連の処理がクライアント側でのみ実行されるようにしています。

  • TurtleUpgradeで使用するテクスチャファイル位置の登録
proxy.registerTextureLocation(modid, "blocks/sample_upgrade");

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

  • TurtleUpgradeで使用するモデルャファイル位置の登録
modelLeft = new ModelResourceLocation(proxy.loadModelLocation(modid, "block/turtle_sample_left"));
modelRight = new ModelResourceLocation(proxy.loadModelLocation(modid, "block/turtle_sample_right"));

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

  • 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のコンストラクタでファイル位置を "sampleupgrademod:block/turtle_sample_left" と指定したため、assets\sampleupgrademod\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 ディレクトリに設置します。