I'm currently trying to make a DCPU-16 emulator inside Minecraft (thread here), and the only thing stopping me from releasing a very-extremely-buggy first version is that I don't know how to make a tile entity.
No crazy IInventory, Container-y stuff, I already have a block, I just need a simple tile entity that contains a char[0x10000]. It doesn't even need to save its state when the game exits!
If someone with some experience in this area could point me in the right direction, or write a short code sample, I would be extremely grateful.
I've tried making my block implement ITileEntityProvider, and then override createNewTileEntity to return a new instance of ComputerTileEntity, but it doesn't work! getBlockTileEntity just returns null!
Firstly, thanks for replying.
I understand how tile entities work. I've looked at the command block code, but I don't know what I'm doing wrong:
(Relevant parts of) my code:
MCPU.java
@Mod(modid = "MCPU", name = "MCPU", version = "0.1b1")
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class MCPU {
public final static Block computerBlock = new ComputerBlock(500)
.setHardness(1.5F).setStepSound(Block.soundStoneFootstep)
.setUnlocalizedName("computerBlock")
.setCreativeTab(CreativeTabs.tabRedstone);
@Instance("MCPU")
public static MCPU instance;
@SidedProxy(clientSide = "org.runas.mcpu.ClientProxy", serverSide = "org.runas.mcpu.CommonProxy")
public static CommonProxy proxy;
@Init
public void load(FMLInitializationEvent event) {
GameRegistry.registerBlock(computerBlock, "computerBlock");
GameRegistry.registerTileEntity(ComputerTileEntity.class,
"computerBlock");
LanguageRegistry.addName(computerBlock, "Computer");
MinecraftForge.setBlockHarvestLevel(computerBlock, "pickaxe", 0);
NetworkRegistry.instance().registerGuiHandler(this,
new ComputerGUIHandler());
proxy.registerRenderers();
}
}
ComputerBlock.java
public class ComputerBlock extends BlockContainer {
@Override
public boolean onBlockActivated(World world, int xPos, int zPos, int yPos,
EntityPlayer player, int par6, float par7, float par8, float par9) {
if (!world.isRemote) {
player.addChatMessage("onBlockActivated: "
+ world.getBlockTileEntity(xPos, yPos, zPos));
}
return true;
}
@Override
public TileEntity createNewTileEntity(World world) {
return new ComputerTileEntity();
}
//Is this one even necessary???
@Override
public boolean hasTileEntity(int metadata) {
return true;
}
}
ComputerTileEntity.java
public class ComputerTileEntity extends TileEntity {
@Override
public String toString() {
return "ComputerTileEntity instance";
}
}
When I place a computer block and right click it, it shows
onBlockActivated: null
instead of
onBlockActivated: ComputerTileEntity instance
EDIT: I'll be darned if this is the problem, does the argument to registerTileEntity have to match the block name? I'll try it now. EDIT2: I tried that, still not working.I've updated my code to reflect the changes.
No problem! The order that x,y, and z are put in is annoying... for most MC code just assume xyz... however for world-saving its a whole other story...
No crazy IInventory, Container-y stuff, I already have a block, I just need a simple tile entity that contains a char[0x10000]. It doesn't even need to save its state when the game exits!
If someone with some experience in this area could point me in the right direction, or write a short code sample, I would be extremely grateful.
getBlockTileEntity just returns null!
Help! Anyone!!!
Firstly, thanks for replying.
I understand how tile entities work. I've looked at the command block code, but I don't know what I'm doing wrong:
(Relevant parts of) my code:
ComputerBlock.java
ComputerTileEntity.java
instead of
EDIT: I'll be darned if this is the problem, does the argument to registerTileEntity have to match the block name? I'll try it now.
EDIT2: I tried that, still not working. I've updated my code to reflect the changes.
In the method in your code, the three ints defining the location are arranged x,y,z. You have them x,z,y.
Replace
with
and you should be good to go.
And overriding "hasTileEntity" is not needed, but you might want to do it anyway.
Hmm... Odd. The tile entity seems to have been created.
Makes the console spam 'Hello!' like crazy, but it getBlockTileEntity still returns null.
Maybe my parameters are in the wrong order or something?
EDIT:
Thought so!
Thanks! I'll try it now!
EDIT2:
Awesome, it works! I think that x,z,y thing was a leftover from copying an outdated tutorial.
Thanks for your help!