« 耐圧合板材と不確定性 | トップページ | Minecraft MOD開発 15 »

2014年4月 5日 (土)

Minecraft MOD開発 14

ForgeのフォーラムWikiを参考に、TileEntity.getDescriptionPacketとonDataPacketでの同期を試みてみる。

BlockCounterCore.java

package nishina;
 
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;
import cpw.mods.fml.common.network.NetworkMod;
 
@Mod(modid = "CounterBlockMod", name = "CounterMod", version = "1.0")
public class BlockCounterCore
{
 public static Block block;
 public static int blockID = 3500;
 
 @EventHandler
 public void init(FMLInitializationEvent event)
 {
  // 表示名の登録
  LanguageRegistry.addName(block, "Counter Block");
  LanguageRegistry.instance().addNameForObject(block, "ja_JP", "カウンタ ブロック");
  GameRegistry.registerTileEntity(TileEntityCounter.class, "TileEntityCounter");
 }
 
 @EventHandler
 public void preInit(FMLPreInitializationEvent event)
 {
  // ブロックの登録
  block = new BlockCounter(blockID, Material.rock);
  GameRegistry.registerBlock(block, "blockCounter");
 }
}

BlockCounter.java

package nishina;
 
import net.minecraft.block.Block;
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.world.World;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatMessageComponent;
 
public class BlockCounter extends BlockContainer
{
 
 public BlockCounter(int id, Material material)
 {
  super(id, material);
  this.setCreativeTab(CreativeTabs.tabBlock);
  this.setUnlocalizedName("Counter Block");
  this.setTextureName("nishina:pink_block");
 }
 
 @Override
 public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityPlayer, int side, float hitX, float hitY, float hitZ)
 {
  TileEntityCounter tileentitycounter = (TileEntityCounter) world.getBlockTileEntity(x, y, z);
  int counter = tileentitycounter.getCount();
  tileentitycounter.incrementCount(x, y, z);
  entityPlayer.sendChatToPlayer(new ChatMessageComponent().createFromText(new Integer(counter).toString()));
  return super.onBlockActivated(world, x, y, z, entityPlayer, side, hitX, hitY, hitZ);
 }
 
 @Override
 public TileEntity createNewTileEntity(World world)
 {
  return new TileEntityCounter();
 }
}

TileEntityCounter.java

package nishina;
 
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.tileentity.TileEntity;
 
public class TileEntityCounter extends TileEntity
{
 public int x, y, z;
 public int count;
 
 public TileEntityCounter()
 {
  super();
 }
 
 @Override
 public void readFromNBT(NBTTagCompound par1nbtTagCompound)
 {
  count = par1nbtTagCompound.getInteger("count");
  super.readFromNBT(par1nbtTagCompound);
  System.out.printf( "readFromNBT ( %d, %d, %d) count=%d\n", x, y, z, count);
 }
 
 @Override
 public void writeToNBT(NBTTagCompound par1nbtTagCompound)
 {
  par1nbtTagCompound.setInteger("count", count);
  super.writeToNBT(par1nbtTagCompound);
  System.out.printf( "writeToNbt ( %d, %d, %d) count=%d\n", x, y, z, count);
 }
 
 public int getCount()
 {
  return count;
 }
 
 public void incrementCount( int _x, int _y, int _z)
 {
  x = _x;
  y = _y;
  z = _z;
  count++;
  System.out.printf( "incrementCount ( %d, %d, %d) count=%d\n", x, y, z, count); 
  this.onInventoryChanged();
 }
 
 @Override
 public Packet getDescriptionPacket()
 {
  NBTTagCompound tag = new NBTTagCompound();
  this.writeToNBT(tag);
  return new Packet132TileEntityData( this.xCoord, this.yCoord, this.zCoord, 0, tag);
 }
 
 @Override
 public void onDataPacket(INetworkManager net, Packet132TileEntityData pkt)
 {
  super.onDataPacket(net, pkt);
  readFromNBT(pkt.data);
 }
}

ログから標準出力を抜き出したもの。

2014-04-05 23:29:18 [情報] [STDOUT] Loaded 40 rules from AccessTransformer config file fml_at.cfg
2014-04-05 23:29:19 [情報] [STDOUT] Loaded 110 rules from AccessTransformer config file forge_at.cfg
2014-04-05 23:29:19 [情報] [STDOUT] Loaded 40 rules from AccessTransformer config file fml_at.cfg
2014-04-05 23:29:19 [情報] [STDOUT] Adding AccessTransformer: nei_at.cfg
2014-04-05 23:29:19 [情報] [STDOUT] Adding Accesstransformer map: temp.dat
2014-04-05 23:29:19 [情報] [STDOUT] Loaded 51 rules from AccessTransformer config file temp.dat
2014-04-05 23:29:20 [情報] [STDOUT] Inserted super call into net.minecraft.client.gui.inventory.GuiInventory.updateScreen
2014-04-05 23:29:20 [情報] [STDOUT] awy was overriden from NotEnoughItems 1.6.1.9.jar
2014-04-05 23:29:20 [情報] [STDOUT] Generated BlockMobSpawner helper method.
2014-04-05 23:29:22 [情報] [STDOUT] MinecraftForge v9.11.1.965 Initialized
2014-04-05 23:29:22 [情報] [STDOUT] Replaced 111 ore recipies
2014-04-05 23:29:25 [情報] [STDOUT] Removing TMI Uninstaller
2014-04-05 23:29:25 [情報] [STDOUT] Deleting Dir: C:\forge-1.6.4-9.11.1.965-src\forge\mcp\eclipse\Minecraft\bin\net\minecraft\client\TMIUninstaller
2014-04-05 23:29:25 [情報] [STDOUT] 
2014-04-05 23:29:25 [情報] [STDOUT] Starting up SoundSystem...
2014-04-05 23:29:26 [情報] [STDOUT] Initializing LWJGL OpenAL
2014-04-05 23:29:26 [情報] [STDOUT]     (The LWJGL binding of OpenAL.  For more information, see http://www.lwjgl.org)
2014-04-05 23:29:26 [情報] [STDOUT] OpenAL initialized.
2014-04-05 23:29:26 [情報] [STDOUT] 
2014-04-05 23:29:28 [情報] [STDOUT] readFromNBT ( 0, 0, 0) count=1
2014-04-05 23:29:28 [情報] [STDOUT] readFromNBT ( 0, 0, 0) count=0
2014-04-05 23:29:28 [情報] [STDOUT] Loading NEI
2014-04-05 23:29:29 [情報] [STDOUT] loading single player
2014-04-05 23:29:29 [情報] [STDOUT] Loading Player: Player312
2014-04-05 23:29:29 [情報] [STDOUT] Sending serverside check to: Player312
2014-04-05 23:29:29 [情報] [STDOUT] writeToNbt ( 0, 0, 0) count=1
2014-04-05 23:29:29 [情報] [STDOUT] writeToNbt ( 0, 0, 0) count=0
2014-04-05 23:29:29 [情報] [STDOUT] Setting up custom skins
2014-04-05 23:29:29 [情報] [STDOUT] Loading World: local/New World
2014-04-05 23:29:30 [情報] [STDOUT] readFromNBT ( 0, 0, 0) count=1
2014-04-05 23:29:30 [情報] [STDOUT] readFromNBT ( 0, 0, 0) count=0
2014-04-05 23:29:32 [情報] [STDOUT] incrementCount ( 142, 64, 244) count=2
2014-04-05 23:29:34 [情報] [STDOUT] incrementCount ( 142, 64, 244) count=3
2014-04-05 23:29:35 [情報] [STDOUT] incrementCount ( 142, 64, 244) count=4
2014-04-05 23:29:37 [情報] [STDOUT] writeToNbt ( 0, 0, 0) count=1
2014-04-05 23:29:37 [情報] [STDOUT] writeToNbt ( 0, 0, 0) count=0
2014-04-05 23:29:42 [情報] [STDOUT] Unloading Player: Player312
2014-04-05 23:32:35 [情報] [STDOUT] 
2014-04-05 23:32:35 [情報] [STDOUT] SoundSystem shutting down...
2014-04-05 23:32:36 [情報] [STDOUT]     Author: Paul Lamb, www.paulscode.com
2014-04-05 23:32:36 [情報] [STDOUT] 

« 耐圧合板材と不確定性 | トップページ | Minecraft MOD開発 15 »

コメント

コメントを書く

コメントは記事投稿者が公開するまで表示されません。

(ウェブ上には掲載しません)

トラックバック

この記事のトラックバックURL:
http://app.f.cocolog-nifty.com/t/trackback/1499066/55659746

この記事へのトラックバック一覧です: Minecraft MOD開発 14:

« 耐圧合板材と不確定性 | トップページ | Minecraft MOD開発 15 »