Jump to content

[1.7.2] Spawning block who follow the player


JimiIT92

Recommended Posts

Hi guys! I'm making an item (a torch) that when helded it spawn a light block, so i simulated a dynamic light. What the onUpdate method do is to spawn this block above the player and destroy all other blocks around this light block, otherwise while moving the player will leave a light trail. This actually work but it cause a lot of lag, i think because the removing cicles. So my question is, how can i make this works without lag?

 

Here the code for my item

package mod.mineworld.items.mod_redstone;

import mod.mineworld.mod_redstone;
import mod.mineworld.mod_tabs;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.MathHelper;
import net.minecraft.util.Vec3;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;


public class ItemFlambeau extends Item {

private int usage = 10000;

    
public ItemFlambeau() {
	super();
	maxStackSize = 64;
	this.setCreativeTab(mod_tabs.tabModRedstone);
}

/**
 * Callback for item usage. If the item does something special on right
 * clicking, he will have one of those. Return True if something happen and
 * false if it don't. This is for ITEMS, not BLOCKS
 */

public boolean isInWater(World par1, int par2, int par3, int par4) {
	if (par1.getBlock(par2 + 1, par3, par4) == Blocks.water)
		return true;
	else if (par1.getBlock(par2 - 1, par3, par4) == Blocks.water)
		return true;
	else if (par1.getBlock(par2, par3 + 1, par4) == Blocks.water)
		return true;
	else if (par1.getBlock(par2, par3 - 1, par4) == Blocks.water)
		return true;
	else if (par1.getBlock(par2, par3, par4 + 1) == Blocks.water)
		return true;
	else if (par1.getBlock(par2, par3, par4 - 1) == Blocks.water)
		return true;
	else
		return false;
}

public boolean onItemUse(ItemStack par1ItemStack,
		EntityPlayer par2EntityPlayer, World par3World, int par4, int par5,int par6, int par7, float par8, float par9, float par10) {
	if (this.isInWater(par3World, par4, par5, par6)) {
		return false;
	}

	if (par3World.isSideSolid(par4, par5, par6,ForgeDirection.getOrientation(par7))
			|| par3World.getBlock(par4, par5, par6) == Blocks.snow_layer) {
		ForgeDirection dir = ForgeDirection.getOrientation(par7);
		int x = par4, y = par5, z = par6;

		switch (dir) {
		case UP:
			if (!isFlambeauAttached(par3World, par4, par5 + 1, par6)) {
				if (par3World.getBlock(par4, par5, par6) == Blocks.snow_layer) {
					par3World.setBlock(par4, par5, par6,mod_redstone.flambeau_block, 5, 2);
					x = par4;
					y = par5;
					z = par6;
				} else {
					par3World.setBlock(par4, par5 + 1, par6,mod_redstone.flambeau_block, 5, 2);
					x = par4;
					y = par5 + 1;
					z = par6;
				}
				par3World.playSoundEffect((double) ((float) par4 + 0.5F),(double) ((float) par5 + 0.5F),(double) ((float) par6 + 0.5F), "dig.wood", 1.0F,0.8F);
				if (!par2EntityPlayer.capabilities.isCreativeMode)
					--par1ItemStack.stackSize;
			}

			else {
				par1ItemStack.stackSize -= 0;
				return false;
			}
			break;
		case DOWN:
			par1ItemStack.stackSize -= 0;
			return false;
		case NORTH:
			if (!isFlambeauAttached(par3World, par4, par5, par6 - 1)) {
				if (par3World.getBlock(par4, par5, par6) == Blocks.snow_layer) {
					par3World.setBlock(par4, par5, par6,mod_redstone.flambeau_block, 5, 2);
					x = par4;
					y = par5;
					z = par6;
				} else {
					par3World.setBlock(par4, par5, par6 - 1,mod_redstone.flambeau_block, 4, 2);
					x = par4;
					y = par5;
					z = par6 - 1;
				}
				par3World.playSoundEffect((double) ((float) par4 + 0.5F),(double) ((float) par5 + 0.5F),(double) ((float) par6 + 0.5F), "dig.wood", 1.0F,0.8F);
				if (!par2EntityPlayer.capabilities.isCreativeMode)
					--par1ItemStack.stackSize;
			}

			else {
				par1ItemStack.stackSize -= 0;
				return false;
			}
			break;
		case SOUTH:
			if (!isFlambeauAttached(par3World, par4, par5, par6 + 1)) {
				if (par3World.getBlock(par4, par5, par6) == Blocks.snow_layer) {
					par3World.setBlock(par4, par5, par6,mod_redstone.flambeau_block, 5, 2);
					x = par4;
					y = par5;
					z = par6;
				} else {
					par3World.setBlock(par4, par5, par6 + 1,mod_redstone.flambeau_block, 3, 2);
					x = par4;
					y = par5;
					z = par6 + 1;
				}
				par3World.playSoundEffect((double) ((float) par4 + 0.5F),(double) ((float) par5 + 0.5F),(double) ((float) par6 + 0.5F), "dig.wood", 1.0F,0.8F);
				if (!par2EntityPlayer.capabilities.isCreativeMode)
					--par1ItemStack.stackSize;
			}

			else {
				par1ItemStack.stackSize -= 0;
				return false;
			}
			break;
		case WEST:
			if (!isFlambeauAttached(par3World, par4 - 1, par5, par6)) {
				if (par3World.getBlock(par4, par5, par6) == Blocks.snow_layer) {
					par3World.setBlock(par4, par5, par6,mod_redstone.flambeau_block, 5, 2);
					x = par4;
					y = par5;
					z = par6;
				}

				else {
					par3World.setBlock(par4 - 1, par5, par6,mod_redstone.flambeau_block, 2, 2);
					x = par4 - 1;
					y = par5;
					z = par6;
				}
				par3World.playSoundEffect((double) ((float) par4 + 0.5F),(double) ((float) par5 + 0.5F),(double) ((float) par6 + 0.5F), "dig.wood", 1.0F,0.8F);
				if (!par2EntityPlayer.capabilities.isCreativeMode)
					--par1ItemStack.stackSize;
			}

			else {
				par1ItemStack.stackSize -= 0;
				par3World.setBlock(par4-1, par5, par6, par3World.getBlock(par4-1, par5, par6), par3World.getBlockMetadata(par4-1, par5, par6), 2);
				return false;
			}
			break;
		case EAST:
			if (!isFlambeauAttached(par3World, par4 + 1, par5, par6)) {
				if (par3World.getBlock(par4, par5, par6) == Blocks.snow_layer) {
					par3World.setBlock(par4, par5, par6,mod_redstone.flambeau_block, 5, 2);
					x = par4;
					y = par5;
					z = par6;
				} else {
					par3World.setBlock(par4 + 1, par5, par6,mod_redstone.flambeau_block, 1, 2);
					x = par4 + 1;
					y = par5;
					z = par6;
				}
				par3World.playSoundEffect((double) ((float) par4 + 0.5F),(double) ((float) par5 + 0.5F),(double) ((float) par6 + 0.5F), "dig.wood", 1.0F,0.8F);
				if (!par2EntityPlayer.capabilities.isCreativeMode)
					--par1ItemStack.stackSize;
			}

			else {
				par1ItemStack.stackSize -= 0;
				par3World.setBlock(par4+1, par5, par6, par3World.getBlock(par4+1, par5, par6), par3World.getBlockMetadata(par4+1, par5, par6), 2);
				return false;
			}
			break;
		default:
			break;
		}

		return true;
	}
	return false;
}

@Override
public void onUpdate(ItemStack par1ItemStack, World par2World, Entity par3Entity, int par4, boolean par5){

	World world = par2World;
	EntityPlayer player = (EntityPlayer)par3Entity;
	ItemStack hand = player.getCurrentEquippedItem();

	if(!player.isInWater()){
    	int x = (int) player.posX;
    	int y = (int) player.posY;
    	int z = (int) player.posZ;
    	
    	for(int i = -1; i < 2; i++)
		for(int j = -1; j < 2; j++)
			for(int k = -1; k < 2; k++)
				if(world.getBlock(x+i, y+j, z+k) == mod_redstone.light)
					world.setBlockToAir(x+i, y+j, z+k);
    	if(player.getCurrentEquippedItem() != null){
    		
    		if(hand.getItem() == mod_redstone.flambeau){  
    			world.setBlock(x, y+1, z, mod_redstone.light);
    			this.usage--;
    			if(this.usage <= 0){
    				player.inventory.consumeInventoryItem(this);
    				this.usage = 10000;
    			}
    		}
    	}}
}
    
public boolean isFlambeauAttached(World par1, int par2, int par3,int par4) {
	if (par1.getBlock(par2, par3, par4) == mod_redstone.flambeau_block)
		return true;
	else
		return false;
}

}

 

And here is the BlockLight code

package mod.mineworld.blocks.mod_redstone;

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.item.Item;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;

public class BlockLight extends Block{

public BlockLight() {
	super(Material.air);
	this.setLightLevel(1.0F);
}


/**
     * Returns a bounding box from the pool of bounding boxes (this means this box can change after the pool has been
     * cleared to be reused)
     */
    public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World, int par2, int par3, int par4)
    {
        return null;
    }

    /**
     * Is this block (a) opaque and (b) a full 1m cube?  This determines whether or not to render the shared face of two
     * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
     */
    public boolean isOpaqueCube()
    {
        return false;
    }

    /**
     * If this block doesn't render as an ordinary block it will return False (examples: signs, buttons, stairs, etc)
     */
    public boolean renderAsNormalBlock()
    {
        return false;
    }
    
    /**
     * Returns whether this block is collideable based on the arguments passed in n@param par1 block metaData n@param
     * par2 whether the player right-clicked while holding a boat
     */
    public boolean canCollideCheck(int p_149678_1_, boolean p_149678_2_)
    {
        return false;
    }
    
    /**
     * Returns the ID of the items to drop on destruction.
     */
    public Item idDropped(int par1, Random par2Random, int par3)
    {
        return null;
    }
    
    /**
     * only called by clickMiddleMouseButton , and passed to inventory.setCurrentItem (along with isCreative)
     */
    public Item idPicked(World par1World, int par2, int par3, int par4)
    {
        return null;
    }
    
}

 

Thanks in advice for all who will help me :D

Don't blame me if i always ask for your help. I just want to learn to be better :)

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.