Jump to content

2 tileentities spawning on one block


Armanse

Recommended Posts

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

 

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 comment
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 comment
Share on other sites

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • i notice a change if i add the min and max ram in the line like this for example:    # Xmx and Xms set the maximum and minimum RAM usage, respectively. # They can take any number, followed by an M or a G. # M means Megabyte, G means Gigabyte. # For example, to set the maximum to 3GB: -Xmx3G # To set the minimum to 2.5GB: -Xms2500M # A good default for a modded server is 4GB. # Uncomment the next line to set it. -Xmx10240M -Xms8192M    i need to make more experiments but for now this apparently works.
    • Selamat datang di OLXTOTO, situs slot gacor terpanas yang sedang booming di industri perjudian online. Jika Anda mencari pengalaman bermain yang luar biasa, maka OLXTOTO adalah tempat yang tepat untuk Anda. Dapatkan sensasi tidak biasa dengan variasi slot online terlengkap dan peluang memenangkan jackpot slot maxwin yang sering. Di sini, Anda akan merasakan keseruan yang luar biasa dalam bermain judi slot. DAFTAR OLXTOTO DISINI LOGIN OLXTOTO DISINI AKUN PRO OLXTOTO DISINI   Slot Gacor untuk Sensasi Bermain Maksimal Olahraga cepat dan seru dengan slot gacor di OLXTOTO. Rasakan sensasi bermain maksimal dengan mesin slot yang memberikan kemenangan beruntun. Temukan keberuntungan Anda di antara berbagai pilihan slot gacor yang tersedia dan rasakan kegembiraan bermain judi slot yang tak terlupakan. Situs Slot Terpercaya dengan Pilihan Terlengkap OLXTOTO adalah situs slot terpercaya yang menawarkan pilihan terlengkap dalam perjudian online. Nikmati berbagai genre dan tema slot online yang menarik, dari slot klasik hingga slot video yang inovatif. Dipercaya oleh jutaan pemain, OLXTOTO memberikan pengalaman bermain yang aman dan terjamin.   Jackpot Slot Maxwin Sering Untuk Peluang Besar Di OLXTOTO, kami tidak hanya memberikan hadiah slot biasa, tapi juga memberikan kesempatan kepada pemain untuk memenangkan jackpot slot maxwin yang sering. Dengan demikian, Anda dapat meraih keberuntungan besar dan memenangkan ribuan rupiah sebagai hadiah jackpot slot maxwin kami. Jackpot slot maxwin merupakan peluang besar bagi para pemain judi slot untuk meraih keuntungan yang lebih besar. Dalam permainan kami, Anda tidak harus terpaku pada kemenangan biasa saja. Kami hadir dengan jackpot slot maxwin yang sering, sehingga Anda memiliki peluang yang lebih besar untuk meraih kemenangan besar dengan hadiah yang menggiurkan. Dalam permainan judi slot, pengalaman bermain bukan hanya tentang keseruan dan hiburan semata. Kami memahami bahwa para pemain juga menginginkan kesempatan untuk meraih keberuntungan besar. Oleh karena itu, OLXTOTO hadir dengan jackpot slot maxwin yang sering untuk memberikan peluang besar kepada para pemain kami. Peluang Besar Menang Jackpot Slot Maxwin Peluang menang jackpot slot maxwin di OLXTOTO sangatlah besar. Anda tidak perlu khawatir tentang batasan atau pembatasan dalam meraih jackpot tersebut. Kami ingin memberikan kesempatan kepada semua pemain kami untuk merasakan sensasi menang dalam jumlah yang luar biasa. Jackpot slot maxwin kami dibuka untuk semua pemain judi slot di OLXTOTO. Anda memiliki peluang yang sama dengan pemain lainnya untuk memenangkan hadiah jackpot yang besar. Kami percaya bahwa semua orang memiliki kesempatan untuk meraih keberuntungan besar, dan itulah mengapa kami menyediakan jackpot slot maxwin yang sering untuk memenuhi harapan dan keinginan Anda.  
    • LOGIN DAN DAFTAR DISINI SEKARANG !!!! Blacktogel adalah situs judi slot online yang menjadi pilihan banyak penggemar judi slot gacor di Indonesia. Dengan platform yang sangat user-friendly dan berbagai macam permainan slot yang tersedia, Blacktogel menjadi tempat yang tepat untuk penggemar judi slot online. Dalam artikel ini, kami akan membahas tentang Blacktogel dan keunggulan situs slot gacor online yang disediakan.  
    • Situs bandar slot online Gacor dengan bonus terbesar saat ini sedang menjadi sorotan para pemain judi online. Dengan persaingan yang semakin ketat dalam industri perjudian online, pemain mencari situs yang tidak hanya menawarkan permainan slot yang gacor (sering memberikan kemenangan), tetapi juga bonus terbesar yang bisa meningkatkan peluang menang. Daftar disini : https://gesit.io/googlegopek
    • Situs bandar slot online Gacor dengan bonus terbesar saat ini sedang menjadi sorotan para pemain judi online. Dengan persaingan yang semakin ketat dalam industri perjudian online, pemain mencari situs yang tidak hanya menawarkan permainan slot yang gacor (sering memberikan kemenangan), tetapi juga bonus terbesar yang bisa meningkatkan peluang menang. Daftar disini : https://gesit.io/googlegopek
  • Topics

×
×
  • Create New...

Important Information

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