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

Different particles depending on hit type & EntityData behaviour (1.16.5)


DeValdi
 Share

Recommended Posts

Hi there!

 

I am currently developing my first mod and I've run into a bit of a problem regarding an item that I am trying to implement. The item is a flintlock pistol and it shoots out a projectile which inherits from ProjectileItemEntity, the same class that SnowballEntity inherits from. I was able to overwrite several things like the damage, default item, etc. on that projectile, however, I am a little bit stuck when it comes to the particle effect that should be played whenever the projectile hits something.

As far as I know, each entity exists both on the client and the server and it is mostly up to the modder to make sure that both "copies" and their relevant data are synchronized properly. Furthermore, it seems that the vanilla snowball shows it's particles on a hit by broadcasting the event from the server-side onHit method, which will then get handled by the client-side handleEntityEvent method.

Since I want my particles to either be the block particles of the block that was hit OR a blood splatter (currently just a redstone dust particle) if it hit an entity, I thought I could use the EntityData (formerly known as EntityDataManager) to pass my particle from the server to the client just before broadcasting the event and therefore before the client handles the event. However, it seems as if the new value I'm trying to set doesn't even reach the client. It just utilizes the standard value I set in defineSynchedData when adding the particles into the world.

Is it possible that the "bond" between client and server is already disrupted by the time I'm trying to transmit my new particle data, since the projectile already impacted and is therefore considered non-existent? Or is the value just simply not transmitted fast enough for the client to pick it up before handling the event? Or is there something I got completely wrong about the mechanism of projectile hitting?

Here is my code of the projectile entity so far. The default particle data is the block particle of a diamond block, which is weirdly used whenever the projectile hits something:

package com.gabvaland.tutorialmod.common.entities;

import com.gabvaland.tutorialmod.core.init.EntityTypeInit;
import com.gabvaland.tutorialmod.core.init.ItemInit;

import net.minecraft.block.Blocks;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererManager;
import net.minecraft.client.renderer.entity.SpriteRenderer;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.EntityType.IFactory;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.projectile.ProjectileItemEntity;
import net.minecraft.item.Item;
import net.minecraft.network.IPacket;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.particles.BlockParticleData;
import net.minecraft.particles.IParticleData;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.particles.RedstoneParticleData;
import net.minecraft.util.DamageSource;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.EntityRayTraceResult;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.client.registry.IRenderFactory;
import net.minecraftforge.fml.network.NetworkHooks;

public class IronBulletEntity extends ProjectileItemEntity {
	
	public static final DataParameter<IParticleData> PARTICLE = EntityDataManager.defineId(IronBulletEntity.class, DataSerializers.PARTICLE);
	
	public IronBulletEntity(EntityType<? extends IronBulletEntity> p_i50159_1_, World p_i50159_2_) {
		super(p_i50159_1_, p_i50159_2_);
		setNoGravity(true);
	}

	public IronBulletEntity(World p_i1774_1_, LivingEntity p_i1774_2_) {
		super(EntityTypeInit.IRON_BULLET.get(), p_i1774_2_, p_i1774_1_);
		setNoGravity(true);
	}

	public IronBulletEntity(World p_i1775_1_, double p_i1775_2_, double p_i1775_4_, double p_i1775_6_) {
		super(EntityTypeInit.IRON_BULLET.get(), p_i1775_2_, p_i1775_4_, p_i1775_6_, p_i1775_1_);
		setNoGravity(true);
	}

	protected Item getDefaultItem() {
		return ItemInit.IRON_BULLET.get();
	}

	@OnlyIn(Dist.CLIENT)
	public void handleEntityEvent(byte eventType) {
		if (eventType == 3) {
			for(int i = 0; i < 8; i++) {
				this.level.addParticle(entityData.get(PARTICLE), this.getX(), this.getY(), this.getZ(), 0.0D, 0.0D, 0.0D);
			}
		}
	}

	protected void onHitEntity(EntityRayTraceResult result) {
		super.onHitEntity(result);
		result.getEntity().hurt(DamageSource.thrown(this, this.getOwner()), 20);
	}

	protected void onHit(RayTraceResult result) {
		super.onHit(result);
		if (!this.level.isClientSide) {
			this.entityData.set(PARTICLE, result.getType().equals(RayTraceResult.Type.BLOCK)
					? new BlockParticleData(ParticleTypes.BLOCK, this.level.getBlockState(new BlockPos(result.getLocation())))
					: new RedstoneParticleData(1, 0, 0, 1)
			);
			this.level.broadcastEntityEvent(this, (byte)3);
			this.remove();
		}
	}
	
	public static class ItemFactory implements IFactory<IronBulletEntity> {
		@Override
		public IronBulletEntity create(EntityType<IronBulletEntity> entityType, World world) {
			return new IronBulletEntity(entityType, world);
		}
	}
	
	public static class RenderFactory implements IRenderFactory<IronBulletEntity> {
		@Override
		public EntityRenderer<? super IronBulletEntity> createRenderFor(EntityRendererManager manager) {
			return new SpriteRenderer<>(manager, Minecraft.getInstance().getItemRenderer());
		}
	}
	
	@Override
	public IPacket<?> getAddEntityPacket() {
		return NetworkHooks.getEntitySpawningPacket(this);
	}
	
	@Override
	public void defineSynchedData() {
		super.defineSynchedData();
		this.entityData.define(PARTICLE, new BlockParticleData(ParticleTypes.BLOCK, Blocks.DIAMOND_BLOCK.defaultBlockState()));
	}
}

 

Thank you for your time and help in advance!

 

Best regards,

DeValdi

Link to comment
Share on other sites

  • Use @Override when overriding methods.
  • Do not use @OnlyIn.
  • Instead of using EntityDataManager and then broadcastEntityEvent it would probably be simpler to just send a custom packet that contains the necessary data.
  • Regardless of this, this should work fine, I would advice playing around with the debugger to see what exactly is going on (are the packets send correctly, etc.).
Link to comment
Share on other sites

On 6/7/2021 at 6:10 PM, diesieben07 said:
  • Use @Override when overriding methods.
  • Do not use @OnlyIn.
  • Instead of using EntityDataManager and then broadcastEntityEvent it would probably be simpler to just send a custom packet that contains the necessary data.
  • Regardless of this, this should work fine, I would advice playing around with the debugger to see what exactly is going on (are the packets send correctly, etc.).

First of all, thank you very much for the quick response and your help!

I've played around a bit more and tried setting a random particle color each tick on the server side and then rendering that particle client-side by overriding the Projectile's tick method and differentiating via the isClientSide method. As expected, it works flawlessly and my bullet now has a nice rainbow trail 😅.

	@Override
	public void tick() {
		super.tick();
		if (this.level.isClientSide) {
			this.level.addParticle(this.entityData.get(PARTICLE), this.getX(), this.getY(), this.getZ(), 0.0D, 0.0D, 0.0D);
		} else {
			this.entityData.set(PARTICLE, new RedstoneParticleData(random.nextFloat(), random.nextFloat(), random.nextFloat(), 1));
		}
	}

This leads me to believe that Minecraft somehow disrupts or invalidates the connection between the two projectile objects once they hit something. Therefore, I can see that there's probably no way around a custom packet.

Do you know any up-to-date resources regarding custom packets? The one's I found are mostly for older versions of Minecraft and as far I could tell, a lot of method names etc. changed in the recent updates.

Appreciate the support!

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.

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.

 Share



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Something very strange is going on about your gradle cache with files randomly disappearing. Delete it again (as instructed above) and try again. I would also recommend you check that your drive is still working properly.
    • Don't do this: https://github.com/ArianKG/KGForge/blob/main/src/main/java/com/kg/kgforge/init/Init.java#L7. Do not store the event bus in a static field. Do not put a DeferredRegister and its entries in separate classes. You will cause issues. Do not create registry entries (PaintingType and EntityType in your case) in a static initializer. It must be done in a registry event, DeferredRegister is recommended. You must register a renderer for your entities using RenderingRegistry.registerEntityRenderingHandler.
    • I tried to make an entity, but when I summon my entity, Minecraft crashes: https://gist.github.com/ArianKG/27288da1b8d670817711372a9e715d96 Source code: https://github.com/ArianKG/KGForge
    • en un servidor que he intentado crear me da este error es de la 1.12.2       ---- Minecraft Crash Report ---- WARNING: coremods are present:   Do not report to Forge! (If you haven't disabled the FoamFix coremod, try disabling it in the config! Note that this bit of text will still appear.) (foamfix-0.10.7-1.12.2.jar)   llibrary (llibrary-core-1.0.11-1.12.2.jar)   iceandfire (iceandfire-1.8.3.jar)   AppleCore (AppleCore-mc1.12.2-3.2.0.jar)   CreativePatchingLoader (CreativeCore_v1.9.68_mc1.12.2.jar)   MalisisCorePlugin (malisiscore-1.12.2-6.5.1.jar)   McLib core mod (mclib-1.0.3-1.12.2.jar)   LoadingPlugin (Bloodmoon-MC1.12.2-1.5.3.jar) Contact their authors BEFORE contacting forge // Uh... Did I do that? Time: 20/10/21 14:03 Description: Exception in server tick loop net.minecraftforge.fml.common.LoaderExceptionModCrash: Caught exception from Decocraft (props) Caused by: java.lang.OutOfMemoryError: Java heap space     at java.awt.image.DataBufferByte.<init>(Unknown Source)     at java.awt.image.ComponentSampleModel.createDataBuffer(Unknown Source)     at java.awt.image.Raster.createWritableRaster(Unknown Source)     at javax.imageio.ImageTypeSpecifier.createBufferedImage(Unknown Source)     at javax.imageio.ImageReader.getDestination(Unknown Source)     at com.sun.imageio.plugins.png.PNGImageReader.readImage(Unknown Source)     at com.sun.imageio.plugins.png.PNGImageReader.read(Unknown Source)     at com.mia.craftstudio.utils.ImageIOCS.read(ImageIOCS.java:26)     at com.mia.craftstudio.CSModel.<init>(CSModel.java:379)     at com.mia.craftstudio.CSPack.readPackFile(CSPack.java:197)     at com.mia.craftstudio.CSProject.loadPacks(CSProject.java:252)     at com.mia.craftstudio.minecraft.forge.CSLibMod.getCSProjectAndLoad(CSLibMod.java:91)     at com.mia.props.Props.loadModels(Props.java:209)     at com.mia.props.Props.preInit(Props.java:166)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)     at java.lang.reflect.Method.invoke(Unknown Source)     at net.minecraftforge.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:637)     at sun.reflect.GeneratedMethodAccessor10.invoke(Unknown Source)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)     at java.lang.reflect.Method.invoke(Unknown Source)     at com.google.common.eventbus.Subscriber.invokeSubscriberMethod(Subscriber.java:91)     at com.google.common.eventbus.Subscriber$SynchronizedSubscriber.invokeSubscriberMethod(Subscriber.java:150)     at com.google.common.eventbus.Subscriber$1.run(Subscriber.java:76)     at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:399)     at com.google.common.eventbus.Subscriber.dispatchEvent(Subscriber.java:71)     at com.google.common.eventbus.Dispatcher$PerThreadQueuedDispatcher.dispatch(Dispatcher.java:116)     at com.google.common.eventbus.EventBus.post(EventBus.java:217)     at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:219)     at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:197)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- System Details -- Details:     Minecraft Version: 1.12.2     Operating System: Windows 10 (x86) version 10.0     Java Version: 1.8.0_251, Oracle Corporation     Java VM Version: Java HotSpot(TM) Client VM (mixed mode, sharing), Oracle Corporation     Memory: 3977648 bytes (3 MB) / 259522560 bytes (247 MB) up to 259522560 bytes (247 MB)     JVM Flags: 0 total;      IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0     FML: MCP 9.42 Powered by Forge 14.23.5.2855 43 mods loaded, 42 mods active     States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored     | State | ID                  | Version               | Source                                  | Signature                                |     |:----- |:------------------- |:--------------------- |:--------------------------------------- |:---------------------------------------- |     | LCH   | minecraft           | 1.12.2                | minecraft.jar                           | None                                     |     | LCH   | mcp                 | 9.42                  | minecraft.jar                           | None                                     |     | LCH   | FML                 | 8.0.99.99             | forge-1.12.2-14.23.5.2855.jar           | e3c3d50c7c986df74c645c0ac54639741c90a557 |     | LCH   | forge               | 14.23.5.2855          | forge-1.12.2-14.23.5.2855.jar           | e3c3d50c7c986df74c645c0ac54639741c90a557 |     | LCH   | creativecoredummy   | 1.0.0                 | minecraft.jar                           | None                                     |     | LCH   | foamfixcore         | 7.7.4                 | minecraft.jar                           | None                                     |     | LCH   | securitycraft       | v1.8.12.1             | [1.12.2]+SecurityCraft+v1.8.12.1.jar    | None                                     |     | LCH   | damageindicatorsmod | 1.0                   | [1.12.2]DamageIndicatorsMod-3.5.1.jar   | None                                     |     | LCH   | applecore           | 3.2.0                 | AppleCore-mc1.12.2-3.2.0.jar            | None                                     |     | LCH   | jei                 | 4.15.0.268            | jei_1.12.2-4.15.0.268.jar               | None                                     |     | LCH   | appleskin           | 1.0.9                 | AppleSkin-mc1.12-1.0.9.jar              | None                                     |     | LCH   | astralsorcery       | 1.6.1                 | astralsorcery-1.12-1.6.1.jar            | None                                     |     | LCH   | baubles             | 1.5.2                 | Baubles-1.12-1.5.2.jar                  | None                                     |     | LCH   | bloodmoon           | 1.5.3                 | Bloodmoon-MC1.12.2-1.5.3.jar            | d72e0dd57935b3e9476212aea0c0df352dd76291 |     | LCH   | chesttransporter    | 2.8.8                 | Chest-Transporter-Mod-1.12.2.jar        | None                                     |     | LCH   | playerrevive        | 1.0                   | PlayerRevive_v1.2.28_mc1.12.2.jar       | None                                     |     | LCH   | creativecore        | 1.9.9                 | CreativeCore_v1.9.68_mc1.12.2.jar       | None                                     |     | LCE   | props               | 2.6.3                 | Decocraft-2.6.3_1.12.2.jar              | None                                     |     | LC    | dynamictrees        | 1.12.2-0.9.5          | DynamicTrees-1.12.2-0.9.5.jar           | None                                     |     | LC    | harvestcraft        | 1.12.2zb              | Pam's+HarvestCraft+1.12.2zf.jar         | None                                     |     | LC    | dynamictreesphc     | 1.12.2-1.4.2          | DynamicTreesPHC-1.12.2-1.4.2.jar        | None                                     |     | LC    | mclib               | 1.0.3                 | mclib-1.0.3-1.12.2.jar                  | None                                     |     | LC    | emoticons           | 0.3                   | emoticons-0.3-1.12.2-dist.jar           | None                                     |     | LC    | fencejumper         | 1.0.5                 | fencejumper-1.12-1.0.5.jar              | None                                     |     | LC    | ferdinandsflowers   | 1.10.4.1              | Ferdinand's+Flowers-1.12.x-1.10.4.1.jar | None                                     |     | LC    | foamfix             | 0.10.7-1.12.2         | foamfix-0.10.7-1.12.2.jar               | None                                     |     | LC    | waila               | 1.8.26                | Hwyla-1.8.26-B41_1.12.2.jar             | None                                     |     | LC    | llibrary            | 1.7.19                | llibrary-1.7.19-1.12.2.jar              | b9f30a813bee3b9dd5652c460310cfcd54f6b7ec |     | LC    | iceandfire          | 1.8.3                 | iceandfire-1.8.3.jar                    | None                                     |     | LC    | ichunutil           | 7.2.2                 | iChunUtil-1.12.2-7.2.2.jar              | 4db5c2bd1b556f252a5b8b54b256d381b2a0a6b8 |     | LC    | infernalmobs        | 1.7.6                 | InfernalMobs-1.12.2.jar                 | None                                     |     | LC    | journeymap          | 1.12.2-5.5.4          | journeymap-1.12.2-5.5.4.jar             | None                                     |     | LC    | laggoggles          | FAT-1.12.2-4.8        | LagGoggles-FAT-1.12.2-4.8.jar           | None                                     |     | LC    | libraryex           | 1.0.12                | LibraryEx-1.12.2-1.0.12.jar             | None                                     |     | LC    | magma_monsters      | 0.3.0                 | MagmaMonsters-0.3.0.jar                 | None                                     |     | LC    | malisiscore         | 1.12.2-6.5.1-SNAPSHOT | malisiscore-1.12.2-6.5.1.jar            | None                                     |     | LC    | malisisdoors        | 1.12.2-7.3.0          | malisisdoors-1.12.2-7.3.0.jar           | None                                     |     | LC    | netherex            | 2.0.15                | NetherEx-1.12.2-2.0.15.jar              | None                                     |     | LC    | playerskins         | 1.0.4                 | playerskin-1.12.2-1.0.5.jar             | None                                     |     | LC    | radixcore           | 1.12.x-2.2.1          | RadixCore-1.12.x-2.2.1-universal.jar    | None                                     |     | LC    | cactusmod           | 1.2.1                 | The-Cactus-Mod-1.12.2.jar               | None                                     |     | LC    | tombstone           | 3.5.9                 | tombstone-3.5.9-1.12.2.jar              | None                                     |     | UD    | mclib_core          | 1.0.3                 | minecraft.jar                           | None                                     |     Loaded coremods (and transformers):  Do not report to Forge! (If you haven't disabled the FoamFix coremod, try disabling it in the config! Note that this bit of text will still appear.) (foamfix-0.10.7-1.12.2.jar)   pl.asie.foamfix.coremod.FoamFixTransformer llibrary (llibrary-core-1.0.11-1.12.2.jar)   net.ilexiconn.llibrary.server.core.plugin.LLibraryTransformer   net.ilexiconn.llibrary.server.core.patcher.LLibraryRuntimePatcher iceandfire (iceandfire-1.8.3.jar)   com.github.alexthe666.iceandfire.patcher.IceAndFireRuntimePatcher AppleCore (AppleCore-mc1.12.2-3.2.0.jar)   squeek.applecore.asm.TransformerModuleHandler CreativePatchingLoader (CreativeCore_v1.9.68_mc1.12.2.jar)    MalisisCorePlugin (malisiscore-1.12.2-6.5.1.jar)    McLib core mod (mclib-1.0.3-1.12.2.jar)   mchorse.mclib.core.McLibCMClassTransformer LoadingPlugin (Bloodmoon-MC1.12.2-1.5.3.jar)   lumien.bloodmoon.asm.ClassTransformer     Profiler Position: N/A (disabled)     Is Modded: Definitely; Server brand changed to 'fml,forge'     Type: Dedicated Server (map_server.txt)
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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