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

警告: ログインしていません。編集を行うと、あなたの IP アドレスが公開されます。ログインまたはアカウントを作成すれば、あなたの編集はその利用者名とともに表示されるほか、その他の利点もあります。

この編集を取り消せます。 下記の差分を確認して、本当に取り消していいか検証してください。よろしければ変更を保存して取り消しを完了してください。
最新版 編集中の文章
8行目: 8行目:
 
*TurtleUpgradeの登録
 
*TurtleUpgradeの登録
 
*IPeripheralの実装
 
*IPeripheralの実装
*Turtle Upgradeの外観(独自テクスチャ・モデル)の実装
+
*Turtle Upgradeの外観の実装
  
 
=== ソースコード ===
 
=== ソースコード ===
Turtle Upgradeの追加については「[[ComputerCraft_API/1.76/ツールタイプTurtleの追加|MC1.8 ツールタイプTurtleの追加]]」を、周辺機器の実装については「[[ComputerCraft_API/1.76/周辺機器の追加|MC1.8 周辺機器の追加]]」を合わせて参照してください。
+
[[ComputerCraft_API/1.76/ツールタイプTurtleの追加|MC1.8 ツールタイプTurtleの追加]]」や「[[ComputerCraft_API/1.76/周辺機器の追加|MC1.8 周辺機器の追加]]」の解説を元にして、変更部分のみを解説します。<br>
 +
Packageは適宜設定してください。
  
 
==== SampleUpgradeCore.java ====
 
==== SampleUpgradeCore.java ====
Modのコアとなるクラス
 
 
<source lang = "java">
 
<source lang = "java">
package mods.sample.upgrade;
+
// package mods.sample.upgrade;
  
import mods.sample.upgrade.client.ClientEventHandler;
+
import java.util.HashSet;
 +
import java.util.Set;
 +
 
 +
import net.minecraft.client.resources.model.ModelResourceLocation;
 +
import net.minecraft.util.ResourceLocation;
 
import net.minecraftforge.common.MinecraftForge;
 
import net.minecraftforge.common.MinecraftForge;
 
import net.minecraftforge.fml.common.Mod;
 
import net.minecraftforge.fml.common.Mod;
 
import net.minecraftforge.fml.common.Mod.EventHandler;
 
import net.minecraftforge.fml.common.Mod.EventHandler;
 
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
 
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
+
import net.minecraftforge.fml.relauncher.Side;
 +
import net.minecraftforge.fml.relauncher.SideOnly;
 
import dan200.computercraft.api.ComputerCraftAPI;
 
import dan200.computercraft.api.ComputerCraftAPI;
  
@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)
 
 
public class SampleUpgradeCore {
 
public class SampleUpgradeCore {
  
36行目: 40行目:
 
public static final String MOD_NAME = "Sample Upgrade Mod";
 
public static final String MOD_NAME = "Sample Upgrade Mod";
 
public static final String MOD_VERSION = "1.0";
 
public static final String MOD_VERSION = "1.0";
public static final String MOD_DEPENDENCIES = "required-after:ComputerCraft"; // 前提MODとしてComputerCraftを要求
+
public static final String MOD_DEPENDENCIES = "after:ComputerCraft";
public static final String MOD_ACCEPTED_MC_VERSIONS = "[1.8,1.8.9]"; // Minecraft 1.8~1.8.9でのみ起動可能
 
  
@EventHandler
+
public static Set<ResourceLocation> modelLocations = new HashSet<ResourceLocation>();
public void preInit(FMLPreInitializationEvent event) {
 
// Turtle Upgradeの登録
 
ComputerCraftAPI.registerTurtleUpgrade(new TurtleSample());
 
}
 
  
 
@EventHandler
 
@EventHandler
 
public void init(FMLInitializationEvent event) {
 
public void init(FMLInitializationEvent event) {
// クライアント限定の処理
+
ComputerCraftAPI.registerTurtleUpgrade(new TurtleSample(event.getSide()));
if (event.getSide().isClient()) {
+
MinecraftForge.EVENT_BUS.register(new ModelBakeEventHandler());
// クライアントイベント受信クラスの登録
 
MinecraftForge.EVENT_BUS.register(new ClientEventHandler());
 
}
 
 
}
 
}
  
}
+
@SideOnly(Side.CLIENT)
</source>
+
public static ModelResourceLocation loadModelLocation(String domain, String path) {
 
+
ResourceLocation location = new ResourceLocation(domain, path);
==== ClientEventHandler.java ====
+
modelLocations.add(location);
各種クライアントeventを受信するためのクラス<br />
+
return new ModelResourceLocation(location, "inventory");
Turtle Upgradeの外見であるテクスチャとモデルを登録します。
 
<source lang = "java">
 
package mods.sample.upgrade.client;
 
 
 
import java.io.IOException;
 
 
 
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 net.minecraftforge.fml.relauncher.Side;
 
import net.minecraftforge.fml.relauncher.SideOnly;
 
 
 
import com.google.common.base.Function;
 
 
 
@SideOnly(Side.CLIENT)
 
public class ClientEventHandler {
 
 
 
@SubscribeEvent
 
public void onTextureStitchEvent(TextureStitchEvent.Pre event) {
 
// 使用するテクスチャの登録
 
event.map.registerSprite(new ResourceLocation(SampleUpgradeCore.MOD_ID, "blocks/sample_upgrade"));
 
}
 
 
 
@SubscribeEvent
 
public void onModelBakeEvent(ModelBakeEvent event) {
 
// 使用するモデルの場所
 
ResourceLocation modelLeft  = new ResourceLocation(SampleUpgradeCore.MOD_ID, "block/turtle_sample_left");
 
ResourceLocation modelRight = new ResourceLocation(SampleUpgradeCore.MOD_ID, "block/turtle_sample_right");
 
 
 
// 使用するモデルの登録
 
loadModel(event, modelLeft);
 
loadModel(event, modelRight);
 
}
 
 
 
// モデルをロードしbakeして登録
 
private void loadModel(ModelBakeEvent event, ResourceLocation location) {
 
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();
 
}
 
 
}
 
}
  
127行目: 61行目:
  
 
==== TurtleSample.java ====
 
==== TurtleSample.java ====
Turtle Upgradeを定義するクラス
 
 
<source lang = "java">
 
<source lang = "java">
package mods.sample.upgrade;
+
// package mods.sample.upgrade;
  
 
import javax.vecmath.Matrix4f;
 
import javax.vecmath.Matrix4f;
141行目: 74行目:
 
import net.minecraft.util.EnumFacing;
 
import net.minecraft.util.EnumFacing;
 
import net.minecraft.util.ResourceLocation;
 
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.FMLCommonHandler;
 
 
import net.minecraftforge.fml.relauncher.Side;
 
import net.minecraftforge.fml.relauncher.Side;
 
import net.minecraftforge.fml.relauncher.SideOnly;
 
import net.minecraftforge.fml.relauncher.SideOnly;
157行目: 89行目:
 
public class TurtleSample implements ITurtleUpgrade {
 
public class TurtleSample implements ITurtleUpgrade {
  
// Turtle UpgradeのID
+
private ResourceLocation upgradeID = new ResourceLocation(SampleUpgradeCore.MOD_ID, "sample");
private final ResourceLocation upgradeID = new ResourceLocation(SampleUpgradeCore.MOD_ID, "sample_peripheral");
+
private ItemStack upgradeItem = new ItemStack(Blocks.stone);
// Turtle Upgradeの装着に必要なアイテム。ここではバニラブロックの石(焼石)
 
private final ItemStack upgradeItem = new ItemStack(Blocks.stone);
 
  
// Turtle UpgradeのモデルのModelResourceLocation.クライアント側でのみ参照される
 
 
@SideOnly(Side.CLIENT)
 
@SideOnly(Side.CLIENT)
 
private ModelResourceLocation modelLeft;
 
private ModelResourceLocation modelLeft;
168行目: 97行目:
 
private ModelResourceLocation modelRight;
 
private ModelResourceLocation modelRight;
  
public TurtleSample() {
+
public TurtleSample(Side side) {
// クライアント限定の処理
+
if (side.isClient()) {
if (FMLCommonHandler.instance().getSide().isClient()) {
 
 
String modid = SampleUpgradeCore.MOD_ID;
 
String modid = SampleUpgradeCore.MOD_ID;
// 登録済みモデルのModelResourceLocationを前もってコンストラクタで定義しておく
+
modelLeft = SampleUpgradeCore.loadModelLocation(modid, "block/turtle_sample_left");
modelLeft = new ModelResourceLocation(new ResourceLocation(modid, "block/turtle_sample_left"), "inventory");
+
modelRight = SampleUpgradeCore.loadModelLocation(modid, "block/turtle_sample_right");
modelRight = new ModelResourceLocation(new ResourceLocation(modid, "block/turtle_sample_right"), "inventory");
 
 
}
 
}
 
}
 
}
  
// Turtle UpgradeのIDをResourceLocationで指定
 
// 他のTurtle Upgradeと重複しないようにする
 
 
@Override
 
@Override
 
public ResourceLocation getUpgradeID() {
 
public ResourceLocation getUpgradeID() {
185行目: 110行目:
 
}
 
}
  
// Minecraft 1.7.10やそれ以前のワールドからTurtle Upgradeを引き継ぐ場合は、そのTurtle Upgradeと同じIDを返す
 
// 引き継ぐ必要がない場合は-1を返す
 
 
@Override
 
@Override
 
public int getLegacyUpgradeID() {
 
public int getLegacyUpgradeID() {
192行目: 115行目:
 
}
 
}
  
// 装着したTurtleのアイテム名に付加される形容詞を返す(『○○ Turtle』の○○の部分)
 
// 戻り値は(設定されていれば)langファイルによる翻訳を経てゲーム内に表示される
 
 
@Override
 
@Override
 
public String getUnlocalisedAdjective() {
 
public String getUnlocalisedAdjective() {
return "turtle.sample_peripheral.adjective";
+
return "Sample";
 
}
 
}
  
// Turtle Upgradeの種類の指定
 
 
@Override
 
@Override
 
public TurtleUpgradeType getType() {
 
public TurtleUpgradeType getType() {
// 周辺機器タイプを指定
 
 
return TurtleUpgradeType.Peripheral;
 
return TurtleUpgradeType.Peripheral;
 
}
 
}
  
// Turtle Upgradeの装着に必要なアイテムを返す
 
 
@Override
 
@Override
 
public ItemStack getCraftingItem() {
 
public ItemStack getCraftingItem() {
212行目: 130行目:
 
}
 
}
  
// 周辺機器クラスのインスタンスを生成して返す
 
 
@Override
 
@Override
 
public IPeripheral createPeripheral(ITurtleAccess turtle, TurtleSide side) {
 
public IPeripheral createPeripheral(ITurtleAccess turtle, TurtleSide side) {
218行目: 135行目:
 
}
 
}
  
// ツールタイプTurtle Upgradeの処理
 
 
@Override
 
@Override
 
public TurtleCommandResult useTool(ITurtleAccess turtle, TurtleSide side,
 
public TurtleCommandResult useTool(ITurtleAccess turtle, TurtleSide side,
 
TurtleVerb verb, EnumFacing direction) {
 
TurtleVerb verb, EnumFacing direction) {
// 周辺機器タイプでは何もしない
 
 
return null;
 
return null;
 
}
 
}
  
// Turtle Upgradeのモデルと変形行列のペアを返す
 
// Turtle Upgradeの外見を描画するためクライアント側で毎描画フレーム呼び出される
 
// そのため、Wireless Turtleのような「Turtle Upgradeや周辺機器の状態による描画モデルの変更」も可能
 
 
@Override
 
@Override
 
@SideOnly(Side.CLIENT)
 
@SideOnly(Side.CLIENT)
235行目: 147行目:
 
ModelManager modelManager = mc.getRenderItem().getItemModelMesher().getModelManager();
 
ModelManager modelManager = mc.getRenderItem().getItemModelMesher().getModelManager();
  
// Turtle Upgradeの装着方向に応じたモデルを取得して返す
 
// このモデルはTurtleのTileEntitySpecialRenderer内で描画される
 
 
if (side == TurtleSide.Left) {
 
if (side == TurtleSide.Left) {
 
return Pair.of(modelManager.getModel(modelLeft), null);
 
return Pair.of(modelManager.getModel(modelLeft), null);
244行目: 154行目:
 
}
 
}
  
// Tick毎の処理
 
 
@Override
 
@Override
 
public void update(ITurtleAccess turtle, TurtleSide side) {
 
public void update(ITurtleAccess turtle, TurtleSide side) {
// 毎tickこのクラスで定義したTurtle Upgradeの装着数×2(Server側とClient側の分)回呼び出される
+
 
// どのTurtle Upgradeに対するupdateなのかはパラメータのturtleとsideで判別可能
 
// またServerとClientのどちら側の呼び出しなのかはturtle.getWorld().isRemoteの値で判別可能
 
 
}
 
}
  
256行目: 163行目:
  
 
==== SamplePeripheral.java ====
 
==== SamplePeripheral.java ====
周辺機器を定義するクラス
 
 
<source lang = "java">
 
<source lang = "java">
package mods.sample.upgrade;
+
// package mods.sample.upgrade;
  
import net.minecraft.util.BlockPos;
 
 
import dan200.computercraft.api.lua.ILuaContext;
 
import dan200.computercraft.api.lua.ILuaContext;
 
import dan200.computercraft.api.lua.LuaException;
 
import dan200.computercraft.api.lua.LuaException;
274行目: 179行目:
  
 
public SamplePeripheral(ITurtleAccess turtle, TurtleSide side) {
 
public SamplePeripheral(ITurtleAccess turtle, TurtleSide side) {
// 装着されたTurtleのTurtleAccessとTurtleSideを保存
 
 
turtleAccess = turtle;
 
turtleAccess = turtle;
 
turtleSide = side;
 
turtleSide = side;
 
}
 
}
 
/* 以下は周辺機器ブロックと同様に実装 */
 
  
 
@Override
 
@Override
302行目: 204行目:
 
}
 
}
  
// 装着されたTurtleが起動したとき、またはコマンドでTurtleに装着されたときに呼び出される
 
 
@Override
 
@Override
 
public void attach(IComputerAccess computer) {
 
public void attach(IComputerAccess computer) {
308行目: 209行目:
 
}
 
}
  
// 装着されたTurtleが終了したとき、またはコマンドでTurtleから取り外されたときに呼び出される
 
 
@Override
 
@Override
 
public void detach(IComputerAccess computer) {
 
public void detach(IComputerAccess computer) {
314行目: 214行目:
 
}
 
}
  
// 周辺機器が同一のものであるかを比較して返す
 
 
@Override
 
@Override
 
public boolean equals(IPeripheral other) {
 
public boolean equals(IPeripheral other) {
// ここでは、このクラスのインスタンスで同一座標のTurtleの同一方向に装着されている場合にのみtrueを返す
 
 
if ((other != null) && (other instanceof SamplePeripheral)) {
 
if ((other != null) && (other instanceof SamplePeripheral)) {
BlockPos otherPos = ((SamplePeripheral)other).turtleAccess.getPosition();
+
return other == this;
TurtleSide otherSide = ((SamplePeripheral)other).turtleSide;
 
return this.turtleAccess.getPosition().equals(otherPos) && (this.turtleSide == otherSide);
 
 
}
 
}
  
330行目: 226行目:
 
</source>
 
</source>
  
==== en_US.lang ====
+
==== ModelBakeEventHandler.java ====
アメリカ英語(デフォルト言語)のlangファイル<br />
+
<source lang = "java">
assets\sampleupgrademod\lang ディレクトリに設置します。
+
// package mods.sample.upgrade;
<source lang="ini">
+
 
+
import net.minecraft.client.Minecraft;
turtle.sample_peripheral.adjective=Sample
+
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
</source>
+
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.model.IModel;
 +
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
 +
 
 +
import com.google.common.base.Function;
 +
 
 +
public class ModelBakeEventHandler {
 +
 
 +
@SubscribeEvent
 +
public void onModelBakeEvent(ModelBakeEvent event) {
 +
for (ResourceLocation location : SampleUpgradeCore.modelLocations) {
 +
loadModel(event, location);
 +
}
 +
}
 +
 
 +
private void loadModel(ModelBakeEvent event, ResourceLocation location) {
 +
try {
 +
IModel model = event.modelLoader.getModel(location);
 +
IBakedModel bakedModel = model.bake(model.getDefaultState(),
 +
DefaultVertexFormats.ITEM, new Function() {
 +
 
 +
@Override
 +
public Object apply(Object input) {
 +
ResourceLocation location = (ResourceLocation) input;
 +
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();
 +
}
 +
}
  
=== モデル・テクスチャ ===
+
}
今回、モデルの形状についてはComputerCraft本体で定義されているものを流用し、テクスチャのみオリジナルなものを使用しています。<br />
+
</source>
モデルを定義するJSONの仕様については「[[1.8のバニラJsonの仕様]]」や「[[1.8のブロック追加]]」を参照してください。
 
  
==== turtle_sample_left.json ====
+
==== turtle_sample_left.json (models/block) ====
Turtle UpgradeをTurtleの左側に装着したときのモデル<br />
 
 
assets\sampleupgrademod\models\block ディレクトリに設置します。
 
assets\sampleupgrademod\models\block ディレクトリに設置します。
 
<source lang = "javascript">
 
<source lang = "javascript">
350行目: 280行目:
 
"parent": "computercraft:block/turtle_upgrade_base_left",
 
"parent": "computercraft:block/turtle_upgrade_base_left",
 
"textures": {
 
"textures": {
"texture": "sampleupgrademod:blocks/sample_upgrade"
+
"texture": "blocks/stone"
 
}
 
}
 
}
 
}
 
</source>
 
</source>
  
==== turtle_sample_right.json ====
+
==== turtle_sample_right.json (models/block) ====
Turtle UpgradeをTurtleの右側に装着したときのモデル<br />
 
 
assets\sampleupgrademod\models\block ディレクトリに設置します。
 
assets\sampleupgrademod\models\block ディレクトリに設置します。
 
<source lang = "javascript">
 
<source lang = "javascript">
362行目: 291行目:
 
"parent": "computercraft:block/turtle_upgrade_base_right",
 
"parent": "computercraft:block/turtle_upgrade_base_right",
 
"textures": {
 
"textures": {
"texture": "sampleupgrademod:blocks/sample_upgrade"
+
"texture": "blocks/stone"
 +
}
 +
}
 +
</source>
 +
 
 +
 
 +
=== 解説 ===
 +
==== SampleUpgradeCore.java ====
 +
Modのコアとなるクラス
 +
 
 +
*TurtleUpgradeの登録
 +
<source lang = "java">
 +
ComputerCraftAPI.registerTurtleUpgrade(new TurtleSample(event.getSide()));
 +
</source>
 +
このmodがComputerCraftの後に読み込まれるように設定しています。<br>
 +
Turtle Upgradeクラスのコンストラクタでモデルを登録するために引数でSideを渡しています。
 +
 
 +
*ModelBakeEvent受信クラスの登録
 +
<source lang = "java">
 +
MinecraftForge.EVENT_BUS.register(new ModelBakeEventHandler());
 +
</source>
 +
 
 +
*ModelBakeEventでbakeするモデルのリストの実装
 +
<source lang = "java">
 +
public static Set<ResourceLocation> modelLocations = new HashSet<ResourceLocation>();
 +
 
 +
@SideOnly(Side.CLIENT)
 +
public static ModelResourceLocation loadModelLocation(String domain, String path) {
 +
ResourceLocation location = new ResourceLocation(domain, path);
 +
modelLocations.add(location);
 +
return new ModelResourceLocation(location, "inventory");
 +
}
 +
</source>
 +
Turtle Upgradeのモデルを登録するためには、Turtle UpgradeクラスのコンストラクタからloadModelLocationメソッドを呼び出します。<br>
 +
Turtle UpgradeのモデルはSet modelLocationsに登録されたモデル位置を元にModelBakeEventでまとめてbakeされます。
 +
 
 +
==== TurtleSample.java ====
 +
Turtle Upgradeの機能を定義するクラス
 +
 
 +
*TurtleUpgradeのモデル登録
 +
<source lang = "java">
 +
@SideOnly(Side.CLIENT)
 +
private ModelResourceLocation modelLeft;
 +
@SideOnly(Side.CLIENT)
 +
private ModelResourceLocation modelRight;
 +
 
 +
public TurtleSample(Side side) {
 +
if (side.isClient()) {
 +
String modid = SampleUpgradeCore.MOD_ID;
 +
modelLeft = SampleUpgradeCore.loadModelLocation(modid, "block/turtle_sample_left");
 +
modelRight = SampleUpgradeCore.loadModelLocation(modid, "block/turtle_sample_right");
 +
}
 +
}
 +
</source>
 +
SampleUpgradeCoreで実装したloadModelLocationメソッドを使ってコンストラクタでTurtle Upgradeのモデルを登録します。<br>
 +
戻り値はgetModel()で使うためメンバフィールドに保存します。
 +
 
 +
*getType()
 +
Turtle Upgradeの種類をTurtleUpgradeTypeの値で指定します。
 +
 
 +
今回はツールタイプなので TurtleUpgradeType.Peripheral を返しています。
 +
 
 +
*getCraftingItem()
 +
<source lang = "java">
 +
private ItemStack upgradeItem = new ItemStack(Blocks.stone);
 +
 
 +
@Override
 +
public ItemStack getCraftingItem() {
 +
return upgradeItem;
 +
}
 +
</source>
 +
Turtle Upgradeを装着するためのアイテムをItemStackで指定します。
 +
 
 +
今回はバニラブロックの石(焼石)を指定していますが、modで追加した独自アイテムも同様に指定できます。
 +
 
 +
*createPeripheral()
 +
周辺機器タイプのTurtle Upgradeでは周辺機器のインスタンスを返します。
 +
 
 +
周辺機器クラスにはコンストラクタの引数でTurtle Upgradeの情報を晒しています。
 +
 
 +
*useTool()
 +
周辺機器タイプのTurtle Upgradeでは呼び出されることは無いため、単にnullを返しています。
 +
 
 +
*getModel()
 +
<source lang = "java">
 +
@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);
 
}
 
}
 
}
 
}
 
</source>
 
</source>
 +
Turtleに装着されたTurtle Upgradeの外観を指定します。<br>
 +
戻り値はPair<IBakedModel, Matrix4f>で、IBakedModelがモデル、Matrix4fがモデルを変形する座標変換行列です。
 +
 +
今回はCC1.76の実装を参考にしています。<br>
 +
Turtle Upgradeを装着した位置がTurtleの左側ならば左用のモデルを、右側ならば右用のモデルを描画するようにしています。
 +
 +
*update()
 +
Turtle UpgradeがTurtleの装着されている間、毎tick呼び出されます。<br>
 +
ロードされているワールド上で装着されている数だけ呼び出されますが、各パラメータでどれに対する呼び出しなのかが判別が可能です。<br>
 +
また、サーバ側とクライアント側でそれぞれ別に呼び出されますが、どちら側の呼び出しなのかはturtle.getWorld().isRemoteの値で判別可能です。<br>
 +
turtle.getPeripheral()で装着されている周辺機器Upgradeの周辺機器クラスのインスタンスを取得できます。<br>
 +
turtle.getUpgradeNBTData()で読み書き可能なNBTTagCompoundを取得できます。このNBTTagCompoundはTurtleに記録され、ゲームを終了しても消去されません(Turtle Upgradeを取り外したりTurtleが破壊されると消えます)。
  
==== sample_upgrade.png ====
+
今回は何もしていません。
Turtle Upgradeのテクスチャ<br />
+
 
assets\sampleupgrademod\textures\blocks ディレクトリに設置します。
+
==== SamplePeripheral.java ====
 +
*コンストラクタ
 +
<source lang = "java">
 +
private final ITurtleAccess turtleAccess;
 +
private final TurtleSide turtleSide;
 +
 
 +
public SamplePeripheral(ITurtleAccess turtle, TurtleSide side) {
 +
turtleAccess = turtle;
 +
turtleSide = side;
 +
}
 +
</source>
 +
Turtle Upgradeの情報をコンストラクタで受け取り周辺機器側で保存しておきます。
 +
 
 +
==== ModelBakeEventHandler.java ====
 +
*ModelBakeEvent
 +
<source lang = "java">
 +
@SubscribeEvent
 +
public void onModelBakeEvent(ModelBakeEvent event) {
 +
for (ResourceLocation location : SampleUpgradeCore.modelLocations) {
 +
loadModel(event, location);
 +
}
 +
}
 +
 
 +
private void loadModel(ModelBakeEvent event, ResourceLocation location) {
 +
try {
 +
IModel model = event.modelLoader.getModel(location);
 +
IBakedModel bakedModel = model.bake(model.getDefaultState(),
 +
DefaultVertexFormats.ITEM, new Function() {
 +
 
 +
@Override
 +
public Object apply(Object input) {
 +
ResourceLocation location = (ResourceLocation) input;
 +
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();
 +
}
 +
}
 +
</source>
 +
コアクラスのmodelLocationsに登録されたTurtle Upgradeのモデルを取得しすべてbakeします。<br>
 +
BakeすることによってTurtle UpgradeクラスのgetModel()で返したモデルが描画されるようになります。
 +
 
 +
==== turtle_sample_left.json (models/block) ====
 +
Turtleの左側に装着された時のTurtle Upgradeのモデルを指定するJSONファイルです。<br>
 +
ITurtleUpgrade実装クラスで以下のように指定したため、assets\<MOD_ID>\models\block ディレクトリに設置します。
 +
<source lang = "java">
 +
SampleUpgradeCore.loadModelLocation(modid, "block/turtle_sample_left");
 +
</source>
 +
モデル自体はComputerCraft本体で定義されている物を流用しており、テクスチャのみ独自に設定(今回はバニラの石ブロック)しています。
  
※サイズ16x16などの適当なテクスチャ画像を各自で用意してください。
+
==== turtle_sample_right.json (models/block) ====
 +
Turtleの右側に装着された時のTurtle Upgradeのモデルを指定するJSONファイルです。<br>
 +
解説はturtle_sample_left.jsonを参照してください。

Minecraft Modding Wikiへの投稿はすべて、他の投稿者によって編集、変更、除去される場合があります。 自分が書いたものが他の人に容赦なく編集されるのを望まない場合は、ここに投稿しないでください。
また、投稿するのは、自分で書いたものか、パブリック ドメインまたはそれに類するフリーな資料からの複製であることを約束してください(詳細はMinecraft Modding Wiki:著作権を参照)。 著作権保護されている作品は、許諾なしに投稿しないでください!

このページを編集するには、下記の確認用の質問に回答してください (詳細):

取り消し 編集の仕方 (新しいウィンドウで開きます)

このページで使用されているテンプレート: