Jump to content

Recommended Posts

Posted

Hi guys :D I'm making a Wireless TNT Activator to istantly explode tnt's. Well, all goes fine, the tnt explode if i right click the item but the explosion makes a strange "barrier" that disappear if i exit the world. So it generates this "barrier" only when explode, the next time i will enter the world this "barrier" has disappeared.

Because it's hard to describe and my english is not so that well because i'm not english i made a video to show what happens (the problem described above)

 

https://www.youtube.com/watch?v=DHrAWxz99SQ&feature=youtu.be

 

Also, of course, here is the source code. Thanks in advance for all who will help me :)

package mineworld.items;

import mineworld.blocks.BlockWireless;
import mineworld.core.MWBlocks;
import mineworld.core.MWTabs;
import mineworld.entities.EntityHyperTNTPrimed;
import mineworld.entities.EntityNukeTNTPrimed;
import mineworld.entities.EntitySuperTNTPrimed;
import mineworld.entities.EntityTrapTNTPrimed;
import net.minecraft.entity.item.EntityTNTPrimed;
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.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;

public class ItemTntActivator extends Item {

public ItemTntActivator() {
	super();
	maxStackSize = 64;
	this.setCreativeTab(MWTabs.tabRedstone);
}

/**
 * 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 (!isTntActivatorAttached(par3World, par4, par5 + 1, par6)) {
				if (par3World.getBlock(par4, par5, par6) == Blocks.snow_layer) {
					par3World.setBlock(par4, par5, par6,MWBlocks.tnt_activator, 5, 2);
					x = par4;
					y = par5;
					z = par6;
				} else {
					par3World.setBlock(par4, par5 + 1, par6,MWBlocks.tnt_activator, 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:
			if (!isTntActivatorAttached(par3World, par4, par5 - 1, par6)) {
				if (par3World.getBlock(par4, par5, par6) == Blocks.snow_layer) {
					par3World.setBlock(par4, par5, par6,MWBlocks.tnt_activator, 5, 2);
					x = par4;
					y = par5;
					z = par6;
				} else {
					par3World.setBlock(par4, par5 - 1, par6,MWBlocks.tnt_activator, 0, 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 NORTH:
			if (!isTntActivatorAttached(par3World, par4, par5, par6 - 1)) {
				if (par3World.getBlock(par4, par5, par6) == Blocks.snow_layer) {
					par3World.setBlock(par4, par5, par6,MWBlocks.tnt_activator, 5, 2);
					x = par4;
					y = par5;
					z = par6;
				} else {
					par3World.setBlock(par4, par5, par6 - 1,MWBlocks.tnt_activator, 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 (!isTntActivatorAttached(par3World, par4, par5, par6 + 1)) {
				if (par3World.getBlock(par4, par5, par6) == Blocks.snow_layer) {
					par3World.setBlock(par4, par5, par6,MWBlocks.tnt_activator, 5, 2);
					x = par4;
					y = par5;
					z = par6;
				} else {
					par3World.setBlock(par4, par5, par6 + 1,MWBlocks.tnt_activator, 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 (!isTntActivatorAttached(par3World, par4 - 1, par5, par6)) {
				if (par3World.getBlock(par4, par5, par6) == Blocks.snow_layer) {
					par3World.setBlock(par4, par5, par6,MWBlocks.tnt_activator, 5, 2);
					x = par4;
					y = par5;
					z = par6;
				}

				else {
					par3World.setBlock(par4 - 1, par5, par6,MWBlocks.tnt_activator, 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;
				return false;
			}
			break;
		case EAST:
			if (!isTntActivatorAttached(par3World, par4 + 1, par5, par6)) {
				if (par3World.getBlock(par4, par5, par6) == Blocks.snow_layer) {
					par3World.setBlock(par4, par5, par6,MWBlocks.tnt_activator, 5, 2);
					x = par4;
					y = par5;
					z = par6;
				} else {
					par3World.setBlock(par4 + 1, par5, par6,MWBlocks.tnt_activator, 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;
				return false;
			}
			break;
		default: 
			break;
		}
		int l = par3World.getBlockMetadata(x, y, z);
		int i1 = l & 7;
		int j1 = l & 8;

		if (i1 == BlockWireless.invertMetadata(1)) {
			if ((MathHelper.floor_double((double) (par2EntityPlayer.rotationYaw * 4.0F / 360.0F) + 0.5D) & 1) == 0) {
				par3World.setBlockMetadataWithNotify(x, y, z, 5 | j1, 2);
			} else {
				par3World.setBlockMetadataWithNotify(x, y, z, 6 | j1, 2);
			}
		} else if (i1 == BlockWireless.invertMetadata(0)) {
			if ((MathHelper.floor_double((double) (par2EntityPlayer.rotationYaw * 4.0F / 360.0F) + 0.5D) & 1) == 0) {
				par3World.setBlockMetadataWithNotify(x, y, z, 7 | j1, 2);
			} else {
				par3World.setBlockMetadataWithNotify(x, y, z, 0 | j1, 2);
			}
		}
		return true;
	}
	return false;
}

public boolean isTntActivatorAttached(World par1, int par2, int par3,int par4) {
	if (par1.getBlock(par2, par3, par4) == MWBlocks.tnt_activator)
		return true;
	else
		return false;
}

/**
 * Called whenever this item is equipped and the right mouse button is
 * pressed. Args: itemStack, world, entityPlayer
 */
public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World,EntityPlayer par3EntityPlayer) {

	int id;
	for (int i = -15; i < 16; i++)
		for (int j = -15; j < 16; j++)
			for (int k = -15; k < 16; k++) {
				if (par2World.getBlock((int) par3EntityPlayer.posX + i,(int) par3EntityPlayer.posY + j,(int) par3EntityPlayer.posZ + k) == Blocks.tnt) {
					EntityTNTPrimed EntityTNTPrimed = new EntityTNTPrimed(par2World,(double) ((float) par3EntityPlayer.posX + i + 0.5F),(double) ((float) par3EntityPlayer.posY + j + 0.5F),(double) ((float) par3EntityPlayer.posZ + k + 0.5F),null);
					par2World.setBlockToAir((int) par3EntityPlayer.posX + i,(int) par3EntityPlayer.posY + j,(int) par3EntityPlayer.posZ + k);
					par2World.createExplosion(null,par3EntityPlayer.posX + i,par3EntityPlayer.posY + j,par3EntityPlayer.posZ + k, 4.0F, true);
					if (!par3EntityPlayer.capabilities.isCreativeMode) {
						--par1ItemStack.stackSize;
					}
				}
				if (par2World.getBlock((int) par3EntityPlayer.posX + i,(int) par3EntityPlayer.posY + j,(int) par3EntityPlayer.posZ + k) == MWBlocks.tnt_super) {
					par2World.setBlockToAir((int) par3EntityPlayer.posX + i,(int) par3EntityPlayer.posY + j,(int) par3EntityPlayer.posZ + k);
					EntitySuperTNTPrimed EntityTNTPrimed = new EntitySuperTNTPrimed(par2World,(double) ((float) (int) par3EntityPlayer.posX+ i + 0.5F),(double) ((float) (int) par3EntityPlayer.posY+ j + 0.5F),(double) ((float) (int) par3EntityPlayer.posZ+ k + 0.5F), null);
					par2World.createExplosion(EntityTNTPrimed,par3EntityPlayer.posX + i,par3EntityPlayer.posY + j,par3EntityPlayer.posZ + k, 10.0F, true);
					if (!par3EntityPlayer.capabilities.isCreativeMode) {
						--par1ItemStack.stackSize;
					}
				}
				if (par2World.getBlock((int) par3EntityPlayer.posX + i,(int) par3EntityPlayer.posY + j,(int) par3EntityPlayer.posZ + k) == MWBlocks.tnt_hyper) {
					par2World.setBlockToAir((int) par3EntityPlayer.posX + i,(int) par3EntityPlayer.posY + j,(int) par3EntityPlayer.posZ + k);
					EntityHyperTNTPrimed EntityTNTPrimed = new EntityHyperTNTPrimed(par2World,(double) ((float) (int) par3EntityPlayer.posX+ i + 0.5F),(double) ((float) (int) par3EntityPlayer.posY+ j + 0.5F),(double) ((float) (int) par3EntityPlayer.posZ+ k + 0.5F), null);
					par2World.createExplosion(EntityTNTPrimed,par3EntityPlayer.posX + i,par3EntityPlayer.posY + j,par3EntityPlayer.posZ + k, 20.0F, true);
					if (!par3EntityPlayer.capabilities.isCreativeMode) {
						--par1ItemStack.stackSize;
					}
				}
				if (par2World.getBlock((int) par3EntityPlayer.posX + i,(int) par3EntityPlayer.posY + j,(int) par3EntityPlayer.posZ + k) == MWBlocks.tnt_nuke) {
					par2World.setBlockToAir((int) par3EntityPlayer.posX + i,(int) par3EntityPlayer.posY + j,(int) par3EntityPlayer.posZ + k);
					EntityNukeTNTPrimed EntityTNTPrimed = new EntityNukeTNTPrimed(par2World,(double) ((float) (int) par3EntityPlayer.posX+ i + 0.5F),(double) ((float) (int) par3EntityPlayer.posY+ j + 0.5F),(double) ((float) (int) par3EntityPlayer.posZ+ k + 0.5F), null);
					par2World.createExplosion(EntityTNTPrimed,par3EntityPlayer.posX + i,par3EntityPlayer.posY + j,par3EntityPlayer.posZ + k, 36.0F, true);
					if (!par3EntityPlayer.capabilities.isCreativeMode) {
						--par1ItemStack.stackSize;
					}
				}
				if (par2World.getBlock((int) par3EntityPlayer.posX + i,(int) par3EntityPlayer.posY + j,(int) par3EntityPlayer.posZ + k) == MWBlocks.tnt_trap) {
					par2World.setBlockToAir((int) par3EntityPlayer.posX + i,(int) par3EntityPlayer.posY + j,(int) par3EntityPlayer.posZ + k);
					EntityTrapTNTPrimed EntityTNTPrimed = new EntityTrapTNTPrimed(par2World,(double) ((float) (int) par3EntityPlayer.posX+ i + 0.5F),(double) ((float) (int) par3EntityPlayer.posY+ j + 0.5F),(double) ((float) (int) par3EntityPlayer.posZ+ k + 0.5F), null);
					par2World.createExplosion(EntityTNTPrimed,par3EntityPlayer.posX + i,par3EntityPlayer.posY + j,par3EntityPlayer.posZ + k, 2.0F, true);
					if (!par3EntityPlayer.capabilities.isCreativeMode) {
						--par1ItemStack.stackSize;
					}
				}
			}

	return par1ItemStack;
}
}

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

Posted

First thought after watching the video is that the "barrier" is most likely caused by a both the client and the server ran the code for the explosion, causing blocks to be destroyed, but not exactly the same blocks on both sides. The "barrier" is that the block you're trying to enter (which doesn't exist on client side) does exist on the server side. Reloading the world simply syncs the data between the server and client, which is why it fixes the problem.

 

You might want to try adding an if  (!world.isRemote) check in your code to be sure the blocks are only destroyed on server side.

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.