提供: Minecraft Modding Wiki
移動先: 案内検索
(ソースコード)
(サンプルmodのソースを全面差し替え)
1行目: 1行目:
 
{{Stb}}
 
{{Stb}}
{{前提MOD|reqmod="Minecraft Forge Universal 10.13.0.x~"及び"ComputerCraft 1.64~"}}
+
{{前提MOD|reqmod="Minecraft Forge Universal 10.13.0.x~"及び"ComputerCraft 1.65~"}}
  
 
== 周辺機器の追加 ==
 
== 周辺機器の追加 ==
ComputerやTurtleから利用できる簡単な周辺機器ブロックを追加する。
+
ComputerやTurtleから呼出し可能なメソッドを提供する簡単な周辺機器ブロックを追加する。
  
 
=== ソースコード ===
 
=== ソースコード ===
16行目: 16行目:
 
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")
 
@Mod(modid="SamplePeripheralMod", name="SamplePeripheralMod", version="1.0")
26行目: 27行目:
 
public void init(FMLInitializationEvent event)
 
public void init(FMLInitializationEvent event)
 
{
 
{
blockSamplePeripheral = new SamplePeripheralBlock();
+
blockSamplePeripheral = new BlockSamplePeripheral();
 
GameRegistry.registerBlock(blockSamplePeripheral, "blockSamplePeripheral");
 
GameRegistry.registerBlock(blockSamplePeripheral, "blockSamplePeripheral");
GameRegistry.registerTileEntity(SamplePeripheralTile.class, "tileSamplePeripheral");
+
GameRegistry.registerTileEntity(TileSamplePeripheral.class, "tileSamplePeripheral");
 
ComputerCraftAPI.registerPeripheralProvider(new PeripheralProvider());
 
ComputerCraftAPI.registerPeripheralProvider(new PeripheralProvider());
 
}
 
}
 +
 
}
 
}
 
</source>
 
</source>
  
*SamplePeripheralBlock.java
+
*BlockSamplePeripheral.java
 
<source lang = "java">
 
<source lang = "java">
 
package mods.blocksample;
 
package mods.blocksample;
41行目: 43行目:
 
import net.minecraft.block.material.Material;
 
import net.minecraft.block.material.Material;
 
import net.minecraft.creativetab.CreativeTabs;
 
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;
  
public class SamplePeripheralBlock extends BlockContainer {
+
public class BlockSamplePeripheral extends BlockContainer {
  
protected SamplePeripheralBlock() {
+
protected BlockSamplePeripheral() {
 
super(Material.rock);
 
super(Material.rock);
 
setBlockName("blockSamplePeripheral");
 
setBlockName("blockSamplePeripheral");
54行目: 58行目:
 
@Override
 
@Override
 
public TileEntity createNewTileEntity(World world, int a) {
 
public TileEntity createNewTileEntity(World world, int a) {
return new SamplePeripheralTile();
+
return new TileSamplePeripheral();
 
}
 
}
  
60行目: 64行目:
 
</source>
 
</source>
  
*SamplePeripheralTile.java
+
*TileSamplePeripheral.java
 
<source lang = "java">
 
<source lang = "java">
 
package mods.blocksample;
 
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;
70行目: 91行目:
 
import dan200.computercraft.api.peripheral.IPeripheral;
 
import dan200.computercraft.api.peripheral.IPeripheral;
  
public class SamplePeripheralTile extends TileEntity implements IPeripheral
+
public class SamplePeripheral implements IPeripheral {
{
+
 
 +
private final TileSamplePeripheral m_tilePeripheral;
 +
 
 +
public SamplePeripheral(TileSamplePeripheral tile) {
 +
this.m_tilePeripheral = tile;
 +
}
  
 
@Override
 
@Override
82行目: 108行目:
 
public String[] getMethodNames()
 
public String[] getMethodNames()
 
{
 
{
return new String[] { "fn1", "fn2", "fn3", "fn4" };
+
return new String[] { "fn1", "fn2" };
 
}
 
}
  
92行目: 118行目:
 
switch (method) {
 
switch (method) {
 
case 0:
 
case 0:
if( arguments.length < 1 || !(arguments[0] instanceof Boolean) ) {
+
return new Object[] {"fn1"};
throw new LuaException("Expected boolean");
 
}
 
return new Object[] {"fn1", arguments[0]};
 
  
 
case 1:
 
case 1:
if( arguments.length < 1 || !(arguments[0] instanceof Double) ) {
+
if ( arguments.length < 1 ) {
throw new LuaException("Expected number");
+
throw new LuaException("Expected argument");
}
 
return new Object[] {"fn2", arguments[0]};
 
 
 
case 2:
 
if( arguments.length < 1 || !(arguments[0] instanceof String) ) {
 
throw new LuaException("Expected string");
 
 
}
 
}
return new Object[] {"fn3", arguments[0]};
+
if ( arguments[0] == null ) {
 
+
throw new LuaException("Illegal argument");
case 3:
 
if( arguments.length < 1 || arguments[0] == null) {
 
throw new LuaException("Expected argument");
 
 
}
 
}
return new Object[] {"fn4", arguments[0], arguments[0].getClass().getName()};
+
return new Object[] {"fn2", arguments[0], arguments[0].getClass().getName()};
 
}
 
}
  
134行目: 148行目:
 
public boolean equals(IPeripheral other)
 
public boolean equals(IPeripheral other)
 
{
 
{
if ( (other != null) && (other instanceof SamplePeripheralTile) ) {
+
if ((other != null) && ( other instanceof SamplePeripheral)) {
return other == this;
+
return ((SamplePeripheral)other).m_tilePeripheral == this.m_tilePeripheral;
 
}
 
}
 
return false;
 
return false;
160行目: 174行目:
 
TileEntity tile = world.getTileEntity(x, y, z);
 
TileEntity tile = world.getTileEntity(x, y, z);
  
if (tile != null && tile instanceof SamplePeripheralTile) {
+
if (tile != null && tile instanceof TileSamplePeripheral) {
return new SamplePeripheralTile();
+
return new SamplePeripheral((TileSamplePeripheral)tile);
 
}
 
}
  
173行目: 187行目:
 
書きかけ
 
書きかけ
 
==== SamplePeripheralCore.java ====
 
==== SamplePeripheralCore.java ====
==== SamplePeripheralBlock.java ====
+
==== BlockSamplePeripheral.java ====
==== SamplePeripheralTile.java ====
+
==== TileSamplePeripheral.java ====
 +
==== SamplePeripheral.java ====
 
==== PeripheralProvider.java ====
 
==== PeripheralProvider.java ====

2014年10月12日 (日) 19:13時点における版

この記事は執筆中です。加筆してくださる人を募集しています。

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

周辺機器の追加

ComputerやTurtleから呼出し可能なメソッドを提供する簡単な周辺機器ブロックを追加する。

ソースコード

  • SamplePeripheralCore.java
package mods.blocksample;

import net.minecraft.block.Block;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.registry.GameRegistry;
import dan200.computercraft.api.ComputerCraftAPI;


@Mod(modid="SamplePeripheralMod", name="SamplePeripheralMod", version="1.0")
public class SamplePeripheralCore
{

	public static Block blockSamplePeripheral;

	@EventHandler
	public void init(FMLInitializationEvent event)
	{
		blockSamplePeripheral = new BlockSamplePeripheral();
		GameRegistry.registerBlock(blockSamplePeripheral, "blockSamplePeripheral");
		GameRegistry.registerTileEntity(TileSamplePeripheral.class, "tileSamplePeripheral");
		ComputerCraftAPI.registerPeripheralProvider(new PeripheralProvider());
	}

}
  • BlockSamplePeripheral.java
package mods.blocksample;

import net.minecraft.block.BlockContainer;
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.world.World;

public class BlockSamplePeripheral extends BlockContainer {

	protected BlockSamplePeripheral() {
		super(Material.rock);
		setBlockName("blockSamplePeripheral");
		setCreativeTab(CreativeTabs.tabBlock);
	}

	@Override
	public TileEntity createNewTileEntity(World world, int a) {
		return new TileSamplePeripheral();
	}

}
  • TileSamplePeripheral.java
package mods.blocksample;

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

import net.minecraft.tileentity.TileEntity;
import dan200.computercraft.api.peripheral.IComputerAccess;

public class TileSamplePeripheral extends TileEntity
{



}
  • SamplePeripheral.java
package mods.blocksample;

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

public class SamplePeripheral implements IPeripheral {

	private final TileSamplePeripheral m_tilePeripheral;

	public SamplePeripheral(TileSamplePeripheral tile) {
		this.m_tilePeripheral = tile;
	}

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

	@Override
	public String[] getMethodNames()
	{
		return new String[] { "fn1", "fn2" };
	}

	@Override
	public Object[] callMethod(IComputerAccess computer, ILuaContext context,
			int method, Object[] arguments) throws LuaException,
			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;
	}

	@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 ((SamplePeripheral)other).m_tilePeripheral == this.m_tilePeripheral;
		}
		return false;
	}

}
  • PeripheralProvider.java
package mods.blocksample;

import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.peripheral.IPeripheralProvider;

public class PeripheralProvider implements IPeripheralProvider
{

	@Override
	public IPeripheral getPeripheral(World world, int x, int y, int z, int side)
	{
		TileEntity tile = world.getTileEntity(x, y, z);

		if (tile != null && tile instanceof TileSamplePeripheral) {
			return new SamplePeripheral((TileSamplePeripheral)tile);
		}

		return null;
	}

}

解説

書きかけ

SamplePeripheralCore.java

BlockSamplePeripheral.java

TileSamplePeripheral.java

SamplePeripheral.java

PeripheralProvider.java