提供: Minecraft Modding Wiki
この編集を取り消せます。
下記の差分を確認して、本当に取り消していいか検証してください。よろしければ変更を保存して取り消しを完了してください。
最新版 | 編集中の文章 | ||
1行目: | 1行目: | ||
− | + | {{Stb}} | |
{{前提MOD|reqmod="Minecraft Forge Universal 10.13.0.x~"及び"ComputerCraft 1.65~"}} | {{前提MOD|reqmod="Minecraft Forge Universal 10.13.0.x~"及び"ComputerCraft 1.65~"}} | ||
− | == | + | == 周辺機器からイベントを発生させる == |
− | + | クリックされたとき、接続されたComputerやTurtleに対してイベントを発生させる周辺機器ブロックを追加する。 | |
− | |||
− | |||
− | |||
=== ソースコード === | === ソースコード === | ||
− | 「[[ComputerCraft_API | + | 「[[ComputerCraft_API/周辺機器の追加|周辺機器の追加]]」のソースコードを元にして、変更部分のみを解説する。 |
*BlockSamplePeripheral.java | *BlockSamplePeripheral.java | ||
17行目: | 14行目: | ||
import net.minecraft.block.BlockContainer; | import net.minecraft.block.BlockContainer; | ||
import net.minecraft.block.material.Material; | import net.minecraft.block.material.Material; | ||
+ | import net.minecraft.creativetab.CreativeTabs; | ||
import net.minecraft.entity.player.EntityPlayer; | import net.minecraft.entity.player.EntityPlayer; | ||
import net.minecraft.tileentity.TileEntity; | import net.minecraft.tileentity.TileEntity; | ||
24行目: | 22行目: | ||
public BlockSamplePeripheral() { | public BlockSamplePeripheral() { | ||
− | super(Material. | + | super(Material.rock); |
+ | setBlockName("blockSamplePeripheral"); | ||
+ | setCreativeTab(CreativeTabs.tabBlock); | ||
} | } | ||
− | |||
public boolean onBlockActivated(World world, int x, int y, int z, | public boolean onBlockActivated(World world, int x, int y, int z, | ||
EntityPlayer entityPlayer, int side, float hitX, float hitY, float hitZ) { | EntityPlayer entityPlayer, int side, float hitX, float hitY, float hitZ) { | ||
49行目: | 48行目: | ||
package mods.sample.peripheral; | package mods.sample.peripheral; | ||
− | |||
import java.util.HashSet; | import java.util.HashSet; | ||
− | |||
import java.util.Set; | import java.util.Set; | ||
87行目: | 84行目: | ||
@Override | @Override | ||
public void attach(IComputerAccess computer) { | public void attach(IComputerAccess computer) { | ||
− | synchronized (this) { | + | synchronized (this.m_computers) { |
− | m_computers.add(computer); | + | this.m_computers.add(computer); |
} | } | ||
} | } | ||
94行目: | 91行目: | ||
@Override | @Override | ||
public void detach(IComputerAccess computer) { | public void detach(IComputerAccess computer) { | ||
− | synchronized (this) { | + | synchronized (this.m_computers) { |
− | m_computers.remove(computer); | + | this.m_computers.remove(computer); |
} | } | ||
} | } | ||
108行目: | 105行目: | ||
public void click() { | public void click() { | ||
− | + | synchronized (this.m_computers) { | |
− | + | for (IComputerAccess computer : this.m_computers) { | |
− | + | computer.queueEvent("sample_click", new Object[] {computer.getAttachmentName()} ); | |
− | |||
− | |||
− | |||
− | for (IComputerAccess computer : m_computers) { | ||
− | computer.queueEvent("sample_click", | ||
− | |||
} | } | ||
} | } | ||
126行目: | 117行目: | ||
=== 解説 === | === 解説 === | ||
==== BlockSamplePeripheral.java ==== | ==== BlockSamplePeripheral.java ==== | ||
− | + | 周辺機器のブロックがクリックされた時の処理を追加する。 | |
<source lang = "java"> | <source lang = "java"> | ||
− | |||
public boolean onBlockActivated(World world, int x, int y, int z, | public boolean onBlockActivated(World world, int x, int y, int z, | ||
EntityPlayer entityPlayer, int side, float hitX, float hitY, float hitZ) { | EntityPlayer entityPlayer, int side, float hitX, float hitY, float hitZ) { | ||
138行目: | 128行目: | ||
} | } | ||
</source> | </source> | ||
− | + | ブロックがクリックされたらTileEntity(周辺機器)を取得してclick()を呼び出す。 | |
==== TileSamplePeripheral.java ==== | ==== TileSamplePeripheral.java ==== | ||
− | + | イベント発生の処理と、イベントを発生させるための接続されたComputerやTurtleのリストの管理を追加する。 | |
− | |||
− | |||
<source lang = "java"> | <source lang = "java"> | ||
private final Set<IComputerAccess> m_computers; | private final Set<IComputerAccess> m_computers; | ||
151行目: | 139行目: | ||
} | } | ||
</source> | </source> | ||
− | + | ↑コンストラクタで接続されたComputerやTurtleへイベントを発生させるのに必要なIComputerAccessインスタンスのリストを初期化する。 | |
− | |||
− | |||
− | |||
<source lang = "java"> | <source lang = "java"> | ||
@Override | @Override | ||
public void attach(IComputerAccess computer) { | public void attach(IComputerAccess computer) { | ||
− | synchronized(this) { | + | synchronized(this.m_computers) { |
− | m_computers.add(computer); | + | this.m_computers.add(computer); |
} | } | ||
} | } | ||
165行目: | 150行目: | ||
@Override | @Override | ||
public void detach(IComputerAccess computer) { | public void detach(IComputerAccess computer) { | ||
− | synchronized(this) { | + | synchronized(this.m_computers) { |
− | m_computers.remove(computer); | + | this.m_computers.remove(computer); |
} | } | ||
} | } | ||
</source> | </source> | ||
− | + | ↑attach()で接続されたComputerやTurtleのIComputerAccessインスタンスをリストへ追加、detach()で取り外されたComputerやTurtleのComputerAccessインスタンスをリストから削除。attach()とdetach()はComputerCraftのLuaスレッドから呼び出されている。このリストはMinecraftスレッドから呼び出されるclick()からもアクセスされるのでsynchronizedで排他制御している。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
<source lang = "java"> | <source lang = "java"> | ||
public void click() { | public void click() { | ||
− | + | synchronized (this.m_computers) { | |
− | + | for (IComputerAccess computer : this.m_computers) { | |
− | + | computer.queueEvent("sample_click", new Object[] {computer.getAttachmentName()} ); | |
− | |||
− | |||
− | |||
− | for (IComputerAccess computer : m_computers) { | ||
− | computer.queueEvent("sample_click", | ||
− | |||
} | } | ||
} | } | ||
} | } | ||
</source> | </source> | ||
− | + | ↑周辺機器ブロックがクリックされた時に、ブロックのクラスから呼び出される。IComputerAccessインスタンスのリストにある全てのComputerやTurtleへIComputerAccess.queueEvent()で"sample_click"イベントを発生させる。前述の通り、リストへは排他制御でアクセスしている。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | *イベント | |
+ | イベントの内容が分かりやすいように、イベント名は"[周辺機器の種類]_[イベントの内容]"の形にする事が推奨されている。<br> | ||
+ | ComputerやTurtleがどの周辺機器から発生したイベントなのか分かるように、IComputerAccess.queueEvent()の第二引数のオブジェクト型配列argumentsの一番目にはIComputerAccess.getAttachmentName()を返すとよい。<br> | ||
+ | os.pullEvent関数に対してargumentsは最大で5つの値を返すことができ、値の型はcallMethod()と同様に適切なLuaの型へと変換される。argumentsに空の配列を渡すとos.pullEvent関数はイベント名以外は何も返さない。 |