Jump to content

[1.10.2] [SOLVED] Spawning particles in onBlockActivated


Daeruin

Recommended Posts

I want to spawn some fire particles in my block's onBlockActivated method, when a variable in my tile entity is the right value (basically, you have to "activate" the block 3 times within a small window of time before the particles will spawn, and I'm saving the number of times and time elapsed in my tile entity). I have read about a million posts on this, and everything says that you should be able to spawn vanilla particles from the server. I swear I have seen examples of other mods doing this, too, but I can't get it to work. I know the code is running, because the println statement fires.

 

I also tried sending a packet, but it didn't work. I didn't include that code since I don't really think I should have to do that anyway. Please correct me if I'm wrong.

 

I also tried spawning the particles on the client, but even after grabbing a copy of my tile entity from the BlockPos parameter, I can't seem to access the variables (they don't change like they should).

 

@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ)
{
	EnumCampfireState campfireState = state.getValue(CAMPFIRE_STATE);
	TileEntity tileEntity = world.getTileEntity(pos);
	int stokeTime = ((PrimalTileEntityCampfire) tileEntity).tinderStokeTime;
	int timesBlown = ((PrimalTileEntityCampfire) tileEntity).tinderTimesBlown;

	if (world.isRemote) // client
	{
	// do stuff
	}
	else // server
	{			
		if (heldItem == null && campfireState == EnumCampfireState.TINDER_STOKED)
		{
			if (stokeTime < 40)
			{
				((PrimalTileEntityCampfire) tileEntity).tinderStokeTime = 60;
				((PrimalTileEntityCampfire) tileEntity).tinderTimesBlown += 1;
				PrimalPacketHandler.INSTANCE.sendTo(new PrimalCampfirePacket(player, 0, pos), (EntityPlayerMP) player);
			}
			if (timesBlown > 3)
			{
				System.out.println("Spawning particle");
				world.spawnParticle(EnumParticleTypes.FLAME, (double) pos.getX(), (double) pos.getY(), (double) pos.getZ(), 0.0D, 0.0D, 0.0D, new int[0]);
			}			
		}			
	}
	return true;
}

 

Edited by Daeruin
  • Like 1
Link to comment
Share on other sites

What version are you using? If it's the latest, you are not overriding onBlockActivated, as there is an extra ItemStack parameter. You should be overriding

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { return false; }

which you can find in the Block class.

Edited by TheMasterGabriel
Link to comment
Share on other sites

I'm using 1.10.2, as stated in the post title. Pretty sure my override is fine, as my version of Block shows this:

    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ)

 

Link to comment
Share on other sites

I believe the problem is you are spawning the particles with zero speed and inside the block so they are not visible.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

2 hours ago, Daeruin said:

I'm using 1.10.2, as stated in the post title

 

Whoops, I don't know how I missed that. Apologies

 

From past experience, I'm fairly sure that particles only exist on the client, which is why you are not seeing them. Block#randomDisplayTick fires only on the client side, so it makes sense that you could see them there. Try spawning them on the client side. As for your variable problem, that is also due to the client/server side. In order for tile entity to recognize the variable changes on the client-side, you need to update the number of right clicks on the server side and then send the update to the client-side via a packet. You can read about networking and the side stuff on the Forge docs here.

Link to comment
Share on other sites

World#spawnParticle(EnumParticleTypes, double, double, double, double, double, double, int...) does nothing on the server, it only spawns particles when called on the client. You need to use one of the spawnParticle overloads from WorldServer instead.

  • Like 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

I have read tutorials and other posts on this forum stating that you can initiate a particle on the server, using world#spawnParticle, and the server will automatically send out the required packets. For example:

 

http://jabelarminecraft.blogspot.com/p/minecraft-forge-1721710-modding-tips.html

 

Quote

Tip: Initiate the spawn of vanilla particles on the server side,. Even though EntityFX is only a client side class, the server method doesn't instantiate it but rather sends a packet to all the clients (which then instantiate the class). Since you usually want all players to see the particles, the method to initiate the spawn should be invoked on the server side (world.obj.isRemote should be false). 

 

I have done a few packets before, but it bugs me to go to that effort when a single line method call should work.

 

I already tried sending a packet to spawn the particle directly on the client, and it didn't work. It will play the sound correctly, and the println statements appear, but it won't spawn the particle. Here's the packet:

 

Spoiler

public class PrimalCampfirePacket implements IMessage
{

	private int playerId;
	private int messageId;
	private double posX;
	private double posY;
	private double posZ;

	public PrimalCampfirePacket()
	{
	}

	public PrimalCampfirePacket(EntityPlayer player, int messageId, BlockPos pos)
	{
		this.playerId = player.getEntityId();
		this.messageId = messageId;
		this.posX = pos.getX() + 0.5D;
		this.posY = pos.getY();
		this.posZ = pos.getZ() + 0.5D;
	}

	@Override
	public void fromBytes(ByteBuf buffer)
	{
		this.playerId = buffer.readInt();
		this.messageId = buffer.readInt();
		this.posX = buffer.readInt();
		this.posY = buffer.readInt();
		this.posZ = buffer.readInt();
	}

	@Override
	public void toBytes(ByteBuf buffer)
	{
		buffer.writeInt(playerId);
		buffer.writeInt(messageId);
		buffer.writeDouble(posX);
		buffer.writeDouble(posY);
		buffer.writeDouble(posZ);
	}

	public static class PrimalCampfireHandler implements IMessageHandler<PrimalCampfirePacket, IMessage>
	{

		@Override
		public IMessage onMessage(final PrimalCampfirePacket message, final MessageContext ctx)
		{
			IThreadListener mainThread = Minecraft.getMinecraft();
			mainThread.addScheduledTask(new Runnable()
			{
				@Override
				public void run()
				{
					Entity entity = Minecraft.getMinecraft().world.getEntityByID(message.playerId);
					System.out.println("Packet entity: " + entity);
					System.out.println("Packet messageId: " + message.messageId);
					if (entity instanceof EntityPlayer)
					{
						if (message.messageId == 0)
						{
							System.out.println("Packet playing sound");
							((EntityPlayer) entity).playSound(PrimalSoundRegistry.blowing, 1.0F, 1.0F);
						}
						if (message.messageId == 1)
						{
							System.out.println("Packet spawning flame");
							Minecraft.getMinecraft().world.spawnParticle(EnumParticleTypes.FLAME, message.posX, message.posY, message.posZ, 0.0D, 0.0D, 0.0D, new int[0]);
						}
					}
				}
			});
			return null;
		}

	}

}

 

 

Link to comment
Share on other sites

12 minutes ago, Choonster said:

World#spawnParticle(EnumParticleTypes, double, double, double, double, double, double, int...) does nothing on the server, it only spawns particles when called on the client. You need to use one of the spawnParticle overloads from WorldServer instead.

 

Ninja'd. How do I do that? Do I cast my world parameter to WorldServer?

(On the server side, naturally - !world.isRemote)

Link to comment
Share on other sites

In your code, you already check if you are on the server side. Because of that, you can cast your world object to a WorldServer object and then use the following method to spawn your particles. It's much easier than the manual packet thing (which I wouldn't have told you about if I'm not oblivious and had read the WorldServer class). Well, knowledge of packets is always good anyways :P

 

/** Spawns the desired particle and sends the necessary packets to the relevant connected players. */
public void spawnParticle(EnumParticleTypes particleType, double xCoord, double yCoord, double zCoord, int numberOfParticles, double xOffset, double yOffset, double zOffset, double particleSpeed, int... particleArguments)

 

Edited by TheMasterGabriel
  • Like 2
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

    • Slot Gacor >> Mudah Maxwin Bersama Djarum4D   Slot gacor adalah salah satu jenis permainan judi online yang sangat populer di Indonesia. Bermain slot gacor berarti bermain permainan slot dengan kemungkinan keluaran yang lebih tinggi daripada slot tradisional. Dalam artikel ini, kami akan membahas secara lengkap tentang slot gacor, mulai dari pengertian dasar, cara bermain, strategi pemain, serta aspek keamanan dan etika dalam bermain.
    • DAFTAR & LOGIN TAYO4D   Slot gacor online adalah permainan yang menarik dan menghasilkan keuntungan untuk banyak pemain di seluruh dunia. Dalam artikel ini, kita akan membahas tentang cara memilih dan memainkan slot gacor online terbaik.
    • Tayo4D : Bandar Online Togel Dan Slot Terbesar Di Indonesia     Pemain taruhan Tayo4D yang berkualitas memerlukan platform yang aman, terpercaya, dan mudah digunakan. Dalam era teknologi ini, banyak situs online yang menawarkan layanan taruhan togel 4D, tetapi memilih yang tepat menjadi tuntas. Berikut adalah cara untuk membuat artikel yang membahas tentang situs online terpercaya untuk permainan taruhan togel 4D.  
    • OLXTOTO: Platform Maxwin dan Gacor Terbesar Sepanjang Masa OLXTOTO telah menetapkan standar baru dalam dunia perjudian dengan menjadi platform terbesar untuk pengalaman gaming yang penuh kemenangan dan kegacoran, sepanjang masa. Dengan fokus yang kuat pada menyediakan permainan yang menghadirkan kesenangan tanpa batas dan peluang kemenangan besar, OLXTOTO telah menjadi pilihan utama bagi para pencinta judi berani di Indonesia. Maxwin: Mengejar Kemenangan Terbesar Maxwin bukan sekadar kata-kata kosong di OLXTOTO. Ini adalah konsep yang ditanamkan dalam setiap aspek permainan yang mereka tawarkan. Dari permainan slot yang menghadirkan jackpot besar hingga berbagai opsi permainan togel dengan hadiah fantastis, para pemain dapat memperoleh peluang nyata untuk mencapai kemenangan terbesar dalam setiap taruhan yang mereka lakukan. OLXTOTO tidak hanya menawarkan kesempatan untuk menang, tetapi juga menjadi wadah bagi para pemain untuk meraih impian mereka dalam perjudian yang berani. Gacor: Keberuntungan yang Tak Tertandingi Keberuntungan seringkali menjadi faktor penting dalam perjudian, dan OLXTOTO memahami betul akan hal ini. Dengan berbagai strategi dan analisis yang disediakan, pemain dapat menemukan peluang gacor yang tidak tertandingi dalam setiap taruhan. Dari hasil togel yang tepat hingga putaran slot yang menguntungkan, OLXTOTO memastikan bahwa setiap taruhan memiliki potensi untuk menjadi momen yang mengubah hidup. Inovasi dan Kualitas Tanpa Batas Tidak puas dengan prestasi masa lalu, OLXTOTO terus berinovasi untuk memberikan pengalaman gaming terbaik kepada para pengguna. Dengan menggabungkan teknologi terbaru dengan desain yang ramah pengguna, platform ini menyajikan antarmuka yang mudah digunakan tanpa mengorbankan kualitas. Setiap pembaruan dan peningkatan dilakukan dengan tujuan tunggal: memberikan pengalaman gaming yang tanpa kompromi kepada setiap pengguna. Komitmen Terhadap Kepuasan Pelanggan Di balik kesuksesan OLXTOTO adalah komitmen mereka terhadap kepuasan pelanggan. Tim dukungan pelanggan yang profesional siap membantu para pemain dalam setiap langkah perjalanan gaming mereka. Dari pertanyaan teknis hingga bantuan dengan transaksi keuangan, OLXTOTO selalu siap memberikan pelayanan terbaik kepada para pengguna mereka. Penutup: Mengukir Sejarah dalam Dunia Perjudian Daring OLXTOTO bukan sekadar platform perjudian berani biasa. Ini adalah ikon dalam dunia perjudian daring Indonesia, sebuah destinasi yang menyatukan kemenangan dan keberuntungan dalam satu tempat yang mengasyikkan. Dengan komitmen mereka terhadap kualitas, inovasi, dan kepuasan pelanggan, OLXTOTO terus mengukir sejarah dalam perjudian dunia berani, menjadi nama yang tak terpisahkan dari pengalaman gaming terbaik. Bersiaplah untuk mengalami sensasi kemenangan terbesar dan keberuntungan tak terduga di OLXTOTO - platform maxwin dan gacor terbesar sepanjang masa.
    • OLXTOTO - Bandar Togel Online Dan Slot Terbesar Di Indonesia OLXTOTO telah lama dikenal sebagai salah satu bandar online terkemuka di Indonesia, terutama dalam pasar togel dan slot. Dengan reputasi yang solid dan pengalaman bertahun-tahun, OLXTOTO menawarkan platform yang aman dan andal bagi para penggemar perjudian daring. DAFTAR OLXTOTO DISINI DAFTAR OLXTOTO DISINI DAFTAR OLXTOTO DISINI Beragam Permainan Togel Sebagai bandar online terbesar di Indonesia, OLXTOTO menawarkan berbagai macam permainan togel. Mulai dari togel Singapura, togel Hongkong, hingga togel Sidney, pemain memiliki banyak pilihan untuk mencoba keberuntungan mereka. Dengan sistem yang transparan dan hasil yang adil, OLXTOTO memastikan bahwa setiap taruhan diproses dengan cepat dan tanpa keadaan. Slot Online Berkualitas Selain togel, OLXTOTO juga menawarkan berbagai permainan slot online yang menarik. Dari slot klasik hingga slot video modern, pemain dapat menemukan berbagai opsi permainan yang sesuai dengan preferensi mereka. Dengan grafis yang memukau dan fitur bonus yang menggiurkan, pengalaman bermain slot di OLXTOTO tidak akan pernah membosankan. Keamanan dan Kepuasan Pelanggan Terjamin Keamanan dan kepuasan pelanggan merupakan prioritas utama di OLXTOTO. Mereka menggunakan teknologi enkripsi terbaru untuk melindungi data pribadi dan keuangan para pemain. Tim dukungan pelanggan yang ramah dan responsif siap membantu pemain dengan setiap pertanyaan atau masalah yang mereka hadapi. Promosi dan Bonus Menarik OLXTOTO sering menawarkan promosi dan bonus menarik kepada para pemainnya. Mulai dari bonus selamat datang hingga bonus deposit, pemain memiliki kesempatan untuk meningkatkan kemenangan mereka dengan memanfaatkan berbagai penawaran yang tersedia. Penutup Dengan reputasi yang solid, beragam permainan berkualitas, dan komitmen terhadap keamanan dan kepuasan pelanggan, OLXTOTO tetap menjadi salah satu pilihan utama bagi para pecinta judi online di Indonesia. Jika Anda mencari pengalaman berjudi yang menyenangkan dan terpercaya, OLXTOTO layak dipertimbangkan.
  • Topics

×
×
  • Create New...

Important Information

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