nosrick Posted April 29, 2015 Posted April 29, 2015 Sorry I'm asking so much today, I'm brand new to modding and all of this is a bit overwhelming. Just two questions for now; How do I make a block destroy itself? I tried use worldObj.setBlockToAir but that leaves behind the bounding box for some reason (and when the tile is right clicked, the block respawns). And how do I spawn a mob out of a block? I've tried worldObj.spawnEntityInWorld, but nothing comes out of it. Any help is greatly appreciated. Quote
HappyKiller1O1 Posted April 29, 2015 Posted April 29, 2015 1) That means it only destroyed the block on the client. You need to make sure the server is destroying it as well. 2) You'd need to use the onBlockActivated method and then, spawn the entity by setting it (EntityCreeper creeper = new EntityCreeper(It's params)) and then, spawning it (world.spawnEntityInWorld(creeper)). Hope that helps. Quote I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.
nosrick Posted April 29, 2015 Author Posted April 29, 2015 I made a mistake in my opening post; it's a tile entity, not a block, so onBlockActivated isn't available to me. 1) How would I go about doing that? 2) As above, it's a tile entity. I did try the EntityCreeper creeper = new EntityCreeper then spawning it through spawnEntityInWorld(creeper), but again, nothing came of it. I'm currently trying to do it in updateEntity, as that's where the egg's (the tile entity in question) hatching logic is. Quote
nosrick Posted April 29, 2015 Author Posted April 29, 2015 A TileEntity is always linked to a Block. You cannot have a TileEntity without a Block. So how would I go about accessing that block for the onBlockActivated method? Would I go through this.blockType, or would it be an Override? (which I don't think I can do) On top of that, how am I meant to know what parameters onBlockActivated takes? It's just a bunch of unnamed, undocumented ints, floats, a World and an EntityPlayer. Quote
larsgerrits Posted April 29, 2015 Posted April 29, 2015 Uhm, please tell me you actually have a block class... Quote Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support. 1.12 -> 1.13 primer by williewillus. 1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support. http://www.howoldisminecraft1710.today/
nosrick Posted April 29, 2015 Author Posted April 29, 2015 I do, it creates the TileEntity when placed. Here's the outline of what I'm trying to do; Place down an egg (Not vanilla Minecraft one) When it receives enough heat, it hatches, deleting itself and spawning a creature. So far, I've got the egg placing down fine, it receives heat just fine and it hatches just fine. The only problems I'm having are with it deleting itself (only happens clientside for some reason) and spawning a creature (spawnEntityInWorld does nothing). Quote
nosrick Posted April 29, 2015 Author Posted April 29, 2015 EDIT: I apologise in advance for anything that might be wrong with my code. No comments due to me just trying to bang this out, and my Java is a little rusty, so I might not be doing best practises. The block: package com.nosrick.minersgotchi.blocks; import com.nosrick.minersgotchi.MinersGotchi; import com.nosrick.minersgotchi.tileentities.TileEntityGotchiEgg; import cpw.mods.fml.common.registry.GameRegistry; import net.minecraft.block.Block; import net.minecraft.block.BlockContainer; import net.minecraft.block.ITileEntityProvider; import net.minecraft.block.material.Material; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; public class GotchiEggBlock extends BlockContainer { public final static String name = "GotchiEggBlock"; protected GotchiEggBlock(Material materialRef) { super(materialRef); this.setCreativeTab(CreativeTabs.tabBlock); this.setBlockName(MinersGotchi.MODID + "_" + name); this.setBlockTextureName(MinersGotchi.MODID + ":" + name); this.setHardness(15.0F); this.setBlockBounds(0.1F, 0.0F, 0.1F, 0.9F, 0.7F, 0.9F); } @Override public TileEntity createNewTileEntity(World worldRef, int metadata) { return new TileEntityGotchiEgg(); } @Override public int getRenderType() { return -1; } @Override public boolean isOpaqueCube() { return false; } @Override public boolean renderAsNormalBlock() { return false; } @Override public boolean hasTileEntity(int metadata) { return true; } } The tile entity: package com.nosrick.minersgotchi.tileentities; import java.util.ArrayList; import com.nosrick.minersgotchi.needs.GotchiNeed; import com.nosrick.minersgotchi.needs.GotchiNeedsManager; import net.minecraft.entity.passive.EntitySheep; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.server.MinecraftServer; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; public class TileEntityGotchiEgg extends TileEntity { public static final String publicName = "TileEntityGotchiEgg"; private String name = "TileEntityGotchiEgg"; private GotchiNeedsManager needsManager; private int hatchTimer; public TileEntityGotchiEgg() { ArrayList<GotchiNeed> eggNeeds = new ArrayList<GotchiNeed>(); //20 ticks per second, need decreases every 5 seconds eggNeeds.add(new GotchiNeed("Heat", 0, 20)); needsManager = new GotchiNeedsManager(eggNeeds); hatchTimer = 100; } public String GetName() { return name; } @Override public void updateEntity() { needsManager.Update(); if(hatchTimer > 0) { hatchTimer -= 1; } float totalLight = 0; float nonZeroSources = 0; float light = worldObj.getBlockLightValue(xCoord - 1, yCoord, zCoord); if(light > 0.0F) { totalLight += light; nonZeroSources += 1; } light = worldObj.getBlockLightValue(xCoord + 1, yCoord, zCoord); if(light > 0.0F) { totalLight += light; nonZeroSources += 1; } light = worldObj.getBlockLightValue(xCoord, yCoord, zCoord - 1); if(light > 0.0F) { totalLight += light; nonZeroSources += 1; } light = worldObj.getBlockLightValue(xCoord, yCoord, zCoord + 1); if(light > 0.0F) { totalLight += light; nonZeroSources += 1; } if((totalLight / nonZeroSources) >= 14.0F) { needsManager.SatisfyNeed("Heat", 1); System.out.println("Receiving heat, " + (totalLight / nonZeroSources)); } if(hatchTimer == 0 && needsManager.GetAverageSatisfaction("Heat") > 100) { //Hatch! System.out.println("Hatching!"); //Move onto spawning a creature //Doesn't work EntitySheep sheep = new EntitySheep(worldObj); worldObj.spawnEntityInWorld(sheep); //Only happens clientside? worldObj.setBlockToAir(xCoord, yCoord, zCoord); } } } Quote
larsgerrits Posted April 29, 2015 Posted April 29, 2015 1) You have to pass the coordinates of the entity in the constructor too, or else the entity will be at 0,0,0. 2) Only set the block to air on the server (!world.isRemote), and see if that helps. Quote Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support. 1.12 -> 1.13 primer by williewillus. 1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support. http://www.howoldisminecraft1710.today/
nosrick Posted April 29, 2015 Author Posted April 29, 2015 The creature now spawns, but I can't interact with it for some reason. I get the feeling I'm missing quite a lot and it isn't as simple as just using spawnEntityInWorld. I've now put the setBlockToAir inside an if(!world.IsRemote) but now the block won't delete itself at all. I'm going to bed for now, so I'll check back in a few hours. Quote
Draco18s Posted April 29, 2015 Posted April 29, 2015 Did you spawn it client side or server side? Quote Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
HappyKiller1O1 Posted April 30, 2015 Posted April 30, 2015 If you can't interact with it, it means the texture is there but not the server data. Make sure you spawn it on server side ONLY Quote I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.
nosrick Posted April 30, 2015 Author Posted April 30, 2015 Did you spawn it client side or server side? I have no idea! Totally new to this, so I stupidly assumed Minecraft would handle this sort of thing on its own. Make sure you spawn it on server side ONLY How do I do that? Quote
nosrick Posted April 30, 2015 Author Posted April 30, 2015 Posting from my phone, so forgive any formatting errors. I've already put a world.isRemote around the spawning and block deletion, but when I do, it causes nothing to happen. Quote
nosrick Posted April 30, 2015 Author Posted April 30, 2015 Show the code with the isRemote check. Also, you are not using the default block item but a custom one, right? If so, post that as well. The tile entity trying to spawn an entity, and destroy itself (where the isRemote check is) package com.nosrick.minersgotchi.tileentities; import java.util.ArrayList; import com.nosrick.minersgotchi.needs.GotchiNeed; import com.nosrick.minersgotchi.needs.GotchiNeedsManager; import net.minecraft.entity.passive.EntitySheep; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.server.MinecraftServer; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; public class TileEntityGotchiEgg extends TileEntity { public static final String publicName = "TileEntityGotchiEgg"; private String name = "TileEntityGotchiEgg"; private GotchiNeedsManager needsManager; private int hatchTimer; public TileEntityGotchiEgg() { ArrayList<GotchiNeed> eggNeeds = new ArrayList<GotchiNeed>(); //20 ticks per second, need decreases every 5 seconds eggNeeds.add(new GotchiNeed("Heat", 0, 20)); needsManager = new GotchiNeedsManager(eggNeeds); hatchTimer = 100; } public String GetName() { return name; } @Override public void updateEntity() { needsManager.Update(); if(hatchTimer > 0) { hatchTimer -= 1; } float totalLight = 0; float nonZeroSources = 0; float light = worldObj.getBlockLightValue(xCoord - 1, yCoord, zCoord); if(light > 0.0F) { totalLight += light; nonZeroSources += 1; } light = worldObj.getBlockLightValue(xCoord + 1, yCoord, zCoord); if(light > 0.0F) { totalLight += light; nonZeroSources += 1; } light = worldObj.getBlockLightValue(xCoord, yCoord, zCoord - 1); if(light > 0.0F) { totalLight += light; nonZeroSources += 1; } light = worldObj.getBlockLightValue(xCoord, yCoord, zCoord + 1); if(light > 0.0F) { totalLight += light; nonZeroSources += 1; } if((totalLight / nonZeroSources) >= 14.0F) { needsManager.SatisfyNeed("Heat", 1); System.out.println("Receiving heat, " + (totalLight / nonZeroSources)); } if(hatchTimer == 0 && needsManager.GetAverageSatisfaction("Heat") > 100) { //Hatch! System.out.println("Hatching!"); //Move onto spawning a creature if(!worldObj.isRemote) { //Doesn't work EntitySheep sheep = new EntitySheep(worldObj); sheep.setPosition(xCoord, yCoord, zCoord); worldObj.spawnEntityInWorld(sheep); //Only happens clientside? worldObj.setBlockToAir(xCoord, yCoord, zCoord); } } } } The custom block: package com.nosrick.minersgotchi.blocks; import com.nosrick.minersgotchi.MinersGotchi; import com.nosrick.minersgotchi.tileentities.TileEntityGotchiEgg; import cpw.mods.fml.common.registry.GameRegistry; import net.minecraft.block.Block; import net.minecraft.block.BlockContainer; import net.minecraft.block.ITileEntityProvider; import net.minecraft.block.material.Material; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; public class GotchiEggBlock extends BlockContainer { public final static String name = "GotchiEggBlock"; protected GotchiEggBlock(Material materialRef) { super(materialRef); this.setCreativeTab(CreativeTabs.tabBlock); this.setBlockName(MinersGotchi.MODID + "_" + name); this.setBlockTextureName(MinersGotchi.MODID + ":" + name); this.setHardness(15.0F); this.setBlockBounds(0.1F, 0.0F, 0.1F, 0.9F, 0.7F, 0.9F); } @Override public TileEntity createNewTileEntity(World worldRef, int metadata) { return new TileEntityGotchiEgg(); } @Override public int getRenderType() { return -1; } @Override public boolean isOpaqueCube() { return false; } @Override public boolean renderAsNormalBlock() { return false; } @Override public boolean hasTileEntity(int metadata) { return true; } } Quote
nosrick Posted April 30, 2015 Author Posted April 30, 2015 Okay, I've moved all of the logic to the server. My problem now is the fact that isRemote always returns true, meaning that nothing happens at all. Quote
nosrick Posted April 30, 2015 Author Posted April 30, 2015 So would that be a problem in createNewTileEntity(), or elsewhere? Quote
nosrick Posted April 30, 2015 Author Posted April 30, 2015 I hope this isn't a really dumb answer, but... By right-clicking. It should be a pretty standard procedure. Quote
nosrick Posted April 30, 2015 Author Posted April 30, 2015 Starting a new world fixed it. That's really strange. Thanks for all the help! I'll be sure to keep the client/server stuff in mind when coding from here on out, I've learnt a lot thanks to this thread. Expect more questions soon! Quote
nosrick Posted April 30, 2015 Author Posted April 30, 2015 I'll keep that in mind the next time something goes wrong. Again, thanks for all the help. I'll mark this as solved. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.