提供: Minecraft Modding Wiki
2014年10月11日 (土) 05:02時点におけるNMS (トーク | 投稿記録)による版 (ページの新規作成)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索
この記事は執筆中です。加筆してくださる人を募集しています。

この記事は"Minecraft Forge Universal 10.13.0.x~"及び"ComputerCraft 1.64~"を前提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="BlockSampleMod", name="BlockSampleMod", version="1.0")
public class SamplePeripheralCore
{
    public static Block blockSample;

    @EventHandler
    public void init(FMLInitializationEvent event)
    {
    	blockSample = new SampleBlock();

	GameRegistry.registerBlock(blockSample, "blockSample");
	GameRegistry.registerTileEntity(SampleTile.class, "tileSample");

	ComputerCraftAPI.registerPeripheralProvider(new PeripheralProvider());
    }
}
  • SampleBlock.java
package mods.blocksample;

import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class SampleBlock extends BlockContainer {

	protected SampleBlock()
	{
		super(Material.rock);
		setBlockName("blockSample");
		setCreativeTab(CreativeTabs.tabBlock);
	}

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

}
  • SampleTile.java
package mods.blocksample;

import net.minecraft.tileentity.TileEntity;
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 SampleTile extends TileEntity implements IPeripheral
{

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

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

	@Override
	public Object[] callMethod(IComputerAccess computer, ILuaContext context,
			int method, Object[] arguments) throws LuaException,
			InterruptedException
	{
		switch (method) {
		case 0:
			if( arguments.length < 1 || !(arguments[0] instanceof Boolean) ) {
				throw new LuaException("Expected boolean");
			}
			return new Object[] {"fn1", arguments[0]};

		case 1:
			if( arguments.length < 1 || !(arguments[0] instanceof Double) ) {
				throw new LuaException("Expected number");
			}
			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]};

		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 null;
	}

	@Override
	public void attach(IComputerAccess computer)
	{

	}

	@Override
	public void detach(IComputerAccess computer)
	{

	}

	@Override
	public boolean equals(IPeripheral other)
	{
		return (other != null) && (other.getClass() == this.getClass());
	}

}
  • 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 SampleTile) {
			return new SampleTile();
		}

		return null;
	}

}

解説

書きかけ

SamplePeripheralCore.java

SampleBlock.java

SampleTile.java

PeripheralProvider.java