提供: 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から呼出し可能なメソッドを提供する簡単な周辺機器ブロックを追加する。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=== ソースコード === | === ソースコード === | ||
*SamplePeripheralCore.java | *SamplePeripheralCore.java | ||
<source lang = "java"> | <source lang = "java"> | ||
− | package mods. | + | package mods.blocksample; |
import net.minecraft.block.Block; | import net.minecraft.block.Block; | ||
− | |||
import cpw.mods.fml.common.Mod; | import cpw.mods.fml.common.Mod; | ||
import cpw.mods.fml.common.Mod.EventHandler; | import cpw.mods.fml.common.Mod.EventHandler; | ||
import cpw.mods.fml.common.event.FMLInitializationEvent; | import cpw.mods.fml.common.event.FMLInitializationEvent; | ||
− | |||
import cpw.mods.fml.common.registry.GameRegistry; | import cpw.mods.fml.common.registry.GameRegistry; | ||
import dan200.computercraft.api.ComputerCraftAPI; | import dan200.computercraft.api.ComputerCraftAPI; | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | @Mod(modid="SamplePeripheralMod", name="SamplePeripheralMod", version="1.0") | |
− | + | public class SamplePeripheralCore | |
− | + | { | |
− | |||
public static Block blockSamplePeripheral; | public static Block blockSamplePeripheral; | ||
@EventHandler | @EventHandler | ||
− | public void | + | public void init(FMLInitializationEvent event) |
− | blockSamplePeripheral = new BlockSamplePeripheral() | + | { |
− | GameRegistry.registerBlock(blockSamplePeripheral, " | + | blockSamplePeripheral = new BlockSamplePeripheral(); |
− | + | GameRegistry.registerBlock(blockSamplePeripheral, "blockSamplePeripheral"); | |
− | + | GameRegistry.registerTileEntity(TileSamplePeripheral.class, "tileSamplePeripheral"); | |
− | |||
− | |||
− | GameRegistry.registerTileEntity(TileSamplePeripheral.class, " | ||
ComputerCraftAPI.registerPeripheralProvider(new PeripheralProvider()); | ComputerCraftAPI.registerPeripheralProvider(new PeripheralProvider()); | ||
} | } | ||
55行目: | 38行目: | ||
*BlockSamplePeripheral.java | *BlockSamplePeripheral.java | ||
<source lang = "java"> | <source lang = "java"> | ||
− | package mods. | + | package mods.blocksample; |
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.item.ItemStack; | ||
import net.minecraft.tileentity.TileEntity; | import net.minecraft.tileentity.TileEntity; | ||
import net.minecraft.world.World; | import net.minecraft.world.World; | ||
64行目: | 50行目: | ||
public class BlockSamplePeripheral extends BlockContainer { | public class BlockSamplePeripheral extends BlockContainer { | ||
− | + | protected BlockSamplePeripheral() { | |
− | super(Material. | + | super(Material.rock); |
− | + | setBlockName("blockSamplePeripheral"); | |
− | setBlockName(" | ||
setCreativeTab(CreativeTabs.tabBlock); | setCreativeTab(CreativeTabs.tabBlock); | ||
} | } | ||
77行目: | 62行目: | ||
} | } | ||
− | |||
</source> | </source> | ||
*TileSamplePeripheral.java | *TileSamplePeripheral.java | ||
<source lang = "java"> | <source lang = "java"> | ||
− | package mods. | + | package mods.blocksample; |
+ | |||
+ | import java.util.HashSet; | ||
+ | import java.util.Set; | ||
import net.minecraft.tileentity.TileEntity; | import net.minecraft.tileentity.TileEntity; | ||
+ | import dan200.computercraft.api.peripheral.IComputerAccess; | ||
+ | |||
+ | public class TileSamplePeripheral extends TileEntity | ||
+ | { | ||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | </source> | ||
+ | |||
+ | *SamplePeripheral.java | ||
+ | <source lang = "java"> | ||
+ | package mods.blocksample; | ||
+ | |||
import dan200.computercraft.api.lua.ILuaContext; | import dan200.computercraft.api.lua.ILuaContext; | ||
import dan200.computercraft.api.lua.LuaException; | import dan200.computercraft.api.lua.LuaException; | ||
90行目: | 91行目: | ||
import dan200.computercraft.api.peripheral.IPeripheral; | import dan200.computercraft.api.peripheral.IPeripheral; | ||
− | public class | + | public class SamplePeripheral implements IPeripheral { |
+ | |||
+ | private final TileSamplePeripheral m_tilePeripheral; | ||
+ | |||
+ | public SamplePeripheral(TileSamplePeripheral tile) { | ||
+ | this.m_tilePeripheral = tile; | ||
+ | } | ||
@Override | @Override | ||
− | public String getType() { | + | public String getType() |
+ | { | ||
return "sample"; | return "sample"; | ||
} | } | ||
@Override | @Override | ||
− | public String[] getMethodNames() { | + | public String[] getMethodNames() |
− | return new String[] {}; | + | { |
+ | return new String[] { "fn1", "fn2" }; | ||
} | } | ||
105行目: | 114行目: | ||
public Object[] callMethod(IComputerAccess computer, ILuaContext context, | public Object[] callMethod(IComputerAccess computer, ILuaContext context, | ||
int method, Object[] arguments) throws LuaException, | int method, Object[] arguments) throws LuaException, | ||
− | InterruptedException { | + | InterruptedException |
+ | { | ||
+ | switch (method) { | ||
+ | case 0: | ||
+ | return new Object[] {"fn1"}; | ||
+ | |||
+ | case 1: | ||
+ | if ( arguments.length < 1 ) { | ||
+ | throw new LuaException("Expected argument"); | ||
+ | } | ||
+ | if ( arguments[0] == null ) { | ||
+ | throw new LuaException("Illegal argument"); | ||
+ | } | ||
+ | return new Object[] {"fn2", arguments[0], arguments[0].getClass().getName()}; | ||
+ | } | ||
+ | |||
return null; | return null; | ||
} | } | ||
@Override | @Override | ||
− | public void attach(IComputerAccess computer) { | + | public void attach(IComputerAccess computer) |
+ | { | ||
} | } | ||
@Override | @Override | ||
− | public void detach(IComputerAccess computer) { | + | public void detach(IComputerAccess computer) |
+ | { | ||
} | } | ||
@Override | @Override | ||
− | public boolean equals(IPeripheral other) { | + | public boolean equals(IPeripheral other) |
− | if ((other != null) && (other instanceof | + | { |
− | return other == this; | + | if ((other != null) && ( other instanceof SamplePeripheral)) { |
+ | return ((SamplePeripheral)other).m_tilePeripheral == this.m_tilePeripheral; | ||
} | } | ||
− | |||
return false; | return false; | ||
} | } | ||
133行目: | 159行目: | ||
*PeripheralProvider.java | *PeripheralProvider.java | ||
<source lang = "java"> | <source lang = "java"> | ||
− | package mods. | + | package mods.blocksample; |
import net.minecraft.tileentity.TileEntity; | import net.minecraft.tileentity.TileEntity; | ||
140行目: | 166行目: | ||
import dan200.computercraft.api.peripheral.IPeripheralProvider; | import dan200.computercraft.api.peripheral.IPeripheralProvider; | ||
− | public class PeripheralProvider implements IPeripheralProvider { | + | public class PeripheralProvider implements IPeripheralProvider |
+ | { | ||
@Override | @Override | ||
− | public IPeripheral getPeripheral(World world, int x, int y, int z, int side) { | + | public IPeripheral getPeripheral(World world, int x, int y, int z, int side) |
+ | { | ||
TileEntity tile = world.getTileEntity(x, y, z); | TileEntity tile = world.getTileEntity(x, y, z); | ||
+ | |||
if (tile != null && tile instanceof TileSamplePeripheral) { | if (tile != null && tile instanceof TileSamplePeripheral) { | ||
− | return ( | + | return new SamplePeripheral((TileSamplePeripheral)tile); |
} | } | ||
156行目: | 185行目: | ||
=== 解説 === | === 解説 === | ||
+ | 書きかけ | ||
==== SamplePeripheralCore.java ==== | ==== SamplePeripheralCore.java ==== | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
==== BlockSamplePeripheral.java ==== | ==== BlockSamplePeripheral.java ==== | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
==== TileSamplePeripheral.java ==== | ==== TileSamplePeripheral.java ==== | ||
− | + | ==== SamplePeripheral.java ==== | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
==== PeripheralProvider.java ==== | ==== PeripheralProvider.java ==== | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |