Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

2 tileentities spawning on one block


Recommended Posts

Posted (edited)

Hi, since i started codding i always got that bug no matter what i tried, it will stay continue to exist.

Let me explain, when i want to put a tileentity on the ground that i created it will always create 2 tileentities with the same values. I know that because i tried wrote a line that says: "init" whenever a tileentity spawns and it always says Init 2 times.

Furthermore sometimes when the tileentity destroys or place a block the block that has been placed or destroyed is sometimes a illusion (when i click on it i reverts back to what it was before) and i assume that those 2 problems are linked.

image.thumb.png.01f6c21f66764d0d2e0236cd76f2d6c1.png

Here is my code tell me if i did something wrong. Thank you in advance ! (the code is very long i tried to delete useless parts like particle effects and other things sry)

//Chest Variables
	protected NonNullList<ItemStack> ItemsList = NonNullList.withSize(1, ItemStack.EMPTY);
	int inventory_size = 1;
	int numPlayerUsing =0;
	private IItemHandlerModifiable items = createHandler();
	private LazyOptional<IItemHandlerModifiable> itemHandler = LazyOptional.of(() -> items);
	//Chest Variables
	
	//TileEntity variables
	boolean init = false;
	private int MagicFuel;
	Random random = new Random();
	int tick;
	PlayerEntity player;
	int EventLocker, BlockInfected;
	//TileEntity variables

	public Sterious_ChestTileEntity(TileEntityType<?> tileEntityTypeIn) {
		super(tileEntityTypeIn);
		// TODO Auto-generated constructor stub
	}
	
	public Sterious_ChestTileEntity() {
		this(TileEntityInit.STERIOUS_CHEST.get());
	}

	@Override
	public void tick() {
		if(!init) {
			init();
			player.sendMessage(new StringTextComponent("Init"), null); //HERE IS THE LINE THAT SAYS INIT WHEN THE TILEENTITY APPEARS IDK WHY IT SAYS IT TWICE
		} else {
			execute();
		}
		
	}
	
	private void init() {
		MagicFuel = 0;
		init = true;
		tick = 0;
		if(Minecraft.getInstance().isSingleplayer()) {
			player = Minecraft.getInstance().player;
		}
		EventLocker = 0;
		BlockInfected =0;
	}
	
	private void execute() { 
		if(!this.getStackInSlot(0).isEmpty()) {
			int value = random.nextInt(3);
			float seconds =5;
			ItemStack itemstack = this.getStackInSlot(0);
			//Events
			if(MagicFuel > 0) {
				//EVENTS MAGIC FUEL REQUIERED
				if(itemstack.getItem().equals(Items.DIAMOND)) {
					tick++;
					if(EventLocker == 0) {
						seconds = 5;
						if(tick%5 ==0 && tick != 0) {
							this.world.playSound(this.pos.getX(), this.pos.getY(), this.pos.getZ(), new SoundEvent(new ResourceLocation("block.dispenser.dispense")), SoundCategory.BLOCKS, 100f, 10f, false);
						}
					} else seconds = 0.2f;
					
					if(tick > 20*seconds) {
						if(EventLocker != 0) value = EventLocker;
						switch(value) {
						case 0:
							TntSpawning();
							break;
						case 1:
							netherInvasion();
							break;
						case 2:
							DiamondTower();
							break;
						}
						if(EventLocker == 0) {
							itemstack.setCount(itemstack.getCount()-1);
							MagicFuel--;
							for(float i=0; i<2; i=i+0.1f) {
								ringofParticle(1f, i-0.5f, ParticleTypes.PORTAL);
							}
							this.world.playSound(this.pos.getX(), this.pos.getY(), this.pos.getZ(), new SoundEvent(new ResourceLocation("entity.enderman.teleport")), SoundCategory.BLOCKS, 100f, 10f, false);
							this.world.removeBlock(this.pos, false);
							this.world.removeTileEntity(this.pos);
						}
						
						tick = 0;
					}
				}
				
				//EVENTS MAGIC FUEL REQUIERED
			}
			
			
			//Events
			
			//Recharge Magic point
			if(itemstack.getItem().equals(Items.GOLD_INGOT) && MagicFuel == 0) {
				tick++;
				if(tick%5 ==0 && tick != 0) {
					this.world.playSound(this.pos.getX(), this.pos.getY(), this.pos.getZ(), new SoundEvent(new ResourceLocation("block.dispenser.dispense")), SoundCategory.BLOCKS, 100f, 10f, false);
				}
				if(tick > 40) {
					MagicFuel++;
					this.world.playSound(this.pos.getX(), this.pos.getY(), this.pos.getZ(), new SoundEvent(new ResourceLocation("entity.player.levelup")), SoundCategory.BLOCKS, 100f, 0f, false);
					ringofParticle(1, 0.5f, ParticleTypes.HAPPY_VILLAGER);
					itemstack.setCount(itemstack.getCount()-1);
					tick=0;
				}
			}
			//Recharg Magic point
		} else {
			tick =0;
		}
	}
	
	
	
	//EVENTS
	private void DiamondTower() {
		this.world.playSound(this.pos.getX(), this.pos.getY(), this.pos.getZ(), new SoundEvent(new ResourceLocation("entity.firework_rocket.launch")), SoundCategory.BLOCKS, 100f, 10f, false);
		for(int i =0; i<20; i++) {
			Block.spawnDrops(Blocks.DIAMOND_ORE.getDefaultState(), this.world, this.pos.add(0, i+1, 0));
			ringofParticle(2, i, ParticleTypes.SOUL_FIRE_FLAME);
		}
	}
	
	
	private void TntSpawning() {
		this.world.playSound(this.pos.getX(), this.pos.getY(), this.pos.getZ(), new SoundEvent(new ResourceLocation("entity.ender_dragon.growl")), SoundCategory.BLOCKS, 100f, 0f, false);
		float radius;
		int value = random.nextInt(10);
		TNTEntity tnt = new TNTEntity(this.world, this.pos.getX(), this.pos.getY()+1, this.pos.getZ(), null);
		for(radius = value; radius>0; radius=radius-(value/2)) {
			for(float i=-radius; i<radius;i=i+1f) {
				this.world.addEntity(new TNTEntity(this.world, this.pos.getX()+i+0.5, this.pos.getY()+40, this.pos.getZ()+circleTop(i, radius)+0.5, null));
				this.world.addEntity(new TNTEntity(this.world, this.pos.getX()+i+0.5, this.pos.getY()+40, this.pos.getZ()+circleBot(i, radius)+0.5, null));
				ringofParticle(value, 1, ParticleTypes.FLAME);
			}
		}
		this.world.playSound(this.pos.getX(), this.pos.getY(), this.pos.getZ(), new SoundEvent(new ResourceLocation("block.dispenser.dispense")), SoundCategory.BLOCKS, 100f, 0f, false);
		player.sendStatusMessage(new StringTextComponent("RUN !"), true);
		
	}
	
	private void netherInvasion() {
		int SelectBlock;
		int x,y,z;
		this.EventLocker = 1;
		x= random.nextInt(20)-10;
		y= random.nextInt(10)-5;
		z= random.nextInt(20)-10;
		BlockPos posBlock = new BlockPos(this.pos.getX()+x,this.pos.getY()+y,this.pos.getZ()+z);
		if(this.world.getBlockState(posBlock) == Blocks.GRASS_BLOCK.getDefaultState()) {
			SelectBlock = random.nextInt(3);
			addInfected(posBlock);
			switch(SelectBlock) {
			case 0:
				this.world.setBlockState(posBlock, Blocks.NETHERRACK.getDefaultState());
				break;
			case 1:
				this.world.setBlockState(posBlock, Blocks.WARPED_NYLIUM.getDefaultState());
				break;
			default:
				this.world.setBlockState(posBlock, Blocks.CRIMSON_NYLIUM.getDefaultState());
				break;
			}
		}
		if(this.world.getBlockState(posBlock) == Blocks.STONE.getDefaultState()
				|| this.world.getBlockState(posBlock) == Blocks.COBBLESTONE.getDefaultState() 
				|| this.world.getBlockState(posBlock) == Blocks.DIORITE.getDefaultState() 
				|| this.world.getBlockState(posBlock) == Blocks.GRANITE.getDefaultState() 
				|| this.world.getBlockState(posBlock) == Blocks.ANDESITE.getDefaultState() || world.getBlockState(posBlock) == Blocks.DIRT.getDefaultState()) {
			addInfected(posBlock);
			this.world.setBlockState(posBlock, Blocks.LAVA.getDefaultState());
		}
		if(this.world.getBlockState(posBlock) == Blocks.IRON_ORE.getDefaultState() || this.world.getBlockState(posBlock) == Blocks.GOLD_ORE.getDefaultState()) {
			SelectBlock = random.nextInt(10);
			if(SelectBlock == 0) {
				this.world.setBlockState(posBlock, Blocks.ANCIENT_DEBRIS.getDefaultState());
			} else this.world.setBlockState(posBlock, Blocks.LAVA.getDefaultState());
		}
		if(this.world.getBlockState(posBlock) == Blocks.WATER.getDefaultState()) {
			world.setBlockState(posBlock, Blocks.LAVA.getDefaultState());
			addInfected(posBlock);
		}
		if(this.world.getBlockState(posBlock) == Blocks.GRAVEL.getDefaultState()) {
			this.world.setBlockState(posBlock, Blocks.LAVA.getDefaultState());
			addInfected(posBlock);
		}
		if(this.world.getBlockState(posBlock) == Blocks.OAK_LOG.getDefaultState() || this.world.getBlockState(posBlock) == Blocks.BIRCH_LOG.getDefaultState() 
				|| this.world.getBlockState(posBlock) == Blocks.DARK_OAK_LOG.getDefaultState() 
				|| this.world.getBlockState(posBlock) == Blocks.JUNGLE_LOG.getDefaultState() || this.world.getBlockState(posBlock) == Blocks.SPRUCE_LOG.getDefaultState() || this.world.getBlockState(posBlock) == Blocks.ACACIA_LOG.getDefaultState()) {
			SelectBlock = random.nextInt(2);
			addInfected(posBlock);
			switch(SelectBlock) {
			case 0:
				this.world.setBlockState(posBlock, Blocks.CRIMSON_STEM.getDefaultState());
				break;
			default:
				this.world.setBlockState(posBlock, Blocks.WARPED_STEM.getDefaultState());
				break;
			}
			
		}
		if(this.world.getBlockState(posBlock) == Blocks.POPPY.getDefaultState()) {
			spawnRoses(posBlock);
		}
		if(this.world.getBlockState(posBlock) == Blocks.DANDELION.getDefaultState()) {
			this.world.setBlockState(posBlock, Blocks.WARPED_FUNGUS.getDefaultState());
		}
		if(this.world.getBlockState(posBlock) == Blocks.GRASS.getDefaultState() || this.world.getBlockState(posBlock) == Blocks.DEAD_BUSH.getDefaultState()) {
			SelectBlock = random.nextInt(3);
			if(SelectBlock ==0) this.world.setBlockState(posBlock, Blocks.WARPED_ROOTS.getDefaultState());
			if(SelectBlock ==1) this.world.setBlockState(posBlock, Blocks.FIRE.getDefaultState());
			if(SelectBlock ==2) this.world.setBlockState(posBlock, Blocks.SOUL_FIRE.getDefaultState());
			addInfected(posBlock);
		}
		if(this.world.getBlockState(posBlock) == Blocks.SAND.getDefaultState() || this.world.getBlockState(posBlock) == Blocks.RED_SAND.getDefaultState()) {
			this.world.setBlockState(posBlock, Blocks.SOUL_SAND.getDefaultState());
			addInfected(posBlock);
		}
		if(this.world.getBlockState(posBlock) == Blocks.SANDSTONE.getDefaultState() || this.world.getBlockState(posBlock) == Blocks.RED_SANDSTONE.getDefaultState()) {
			this.world.setBlockState(posBlock, Blocks.SOUL_SOIL.getDefaultState());
			addInfected(posBlock);
		}
		if(this.world.getBlockState(posBlock) == Blocks.CAMPFIRE.getDefaultState()) this.world.setBlockState(posBlock, Blocks.SOUL_CAMPFIRE.getDefaultState());
		
		if(this.BlockInfected>200) {
			this.EventLocker = 0;
			this.BlockInfected =0;
		}
		lightExplosionOfParticle(posBlock, ParticleTypes.LAVA);
		ringofParticle(1f, 0.5f, ParticleTypes.FLAME);
		ringofParticle(1f, 0.5f, ParticleTypes.SOUL_FIRE_FLAME);
	}
	
	private void spawnRoses(BlockPos posBlock) {
		int value = random.nextInt(3);
		if(value ==1) this.world.setBlockState(posBlock, Blocks.CRIMSON_FUNGUS.getDefaultState());
		if(value == 2) {
			this.world.setBlockState(posBlock, Blocks.WITHER_ROSE.getDefaultState());
			ringofParticle(0.5f, 1, ParticleTypes.FLAME);
			lightExplosionOfParticle(posBlock, ParticleTypes.LAVA);
		}
	}
	
	
	//READ AND WRITE FUNC
	
	@Override
	public void read(BlockState state, CompoundNBT nbt) {
		// TODO Auto-generated method stub
		super.read(state, nbt);
	}
	
	@Override
	public CompoundNBT write(CompoundNBT compound) {
		
		
		return super.write(compound);
	}

 

Edited by Armanse
Link to post
Share on other sites
3 hours ago, Armanse said:

if(Minecraft.getInstance().isSingleplayer()) { player = Minecraft.getInstance().player; }

You cannot do this.

Your init shows twice because your TE is initialized once on the server and once on the client.

Link to post
Share on other sites
Posted (edited)

So i need to delete this in order to fix that bug is that right ? but how can i get the player then ?

19 hours ago, diesieben07 said:

You cannot do this.

Your init shows twice because your TE is initialized once on the server and once on the client.

Edited by Armanse
Link to post
Share on other sites
Posted (edited)

the closest to the block if possible

9 minutes ago, diesieben07 said:

The concept of "the player" does not make sense on a server. There are many players. Which one do you want?

Edited by Armanse
Link to post
Share on other sites
Posted (edited)
42 minutes ago, diesieben07 said:

The concept of "the player" does not make sense on a server. There are many players. Which one do you want?

Btw even thought i removed the player variable the bug is still there but it's less "powerful"/ buggy than before. Thank you for help btw

Is there anything else that makes the same problem in my code ?

Edited by Armanse
Link to post
Share on other sites
Posted (edited)

 

6 minutes ago, diesieben07 said:

I have no idea what you mean by "less powerful" / "buggy".

Show updated code and elaborate.

What i mean by that is that the bug is happening less frequently but it is still happening. And if you want the updated code it's the same one but i removed the player variable. 

Edited by Armanse
Link to post
Share on other sites
2 minutes ago, diesieben07 said:

You must place blocks only on the server.

Sorry i'm dum but wdym when you say server i know that there is a client side and a server side but i have no idea what those are. If u send me a link to a documentation it's ok but i would like a simple explanation.

Link to post
Share on other sites
Posted (edited)
12 hours ago, diesieben07 said:

You must place blocks only on the server.

So as you said i did run the code on the server side however now none of the sounds and the particles are here, since they need to be on the client side. How can i fix that ? i want the particles to appear even thought it's in the server side ? If not possible i would like to at least sync the client side TileEntity to the server side TileEntity but i don't know how since they are not sharing the same variables.

Is it possible to send the particles and sounds to the client side while in the server side ?

Edited by Armanse
Link to post
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.

Guest
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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.