Jump to content

Trouble Communicating Between Sides


Tablock_

Recommended Posts

I am trying to make any LivingEntity that collides with a custom projectile ride the player. However after some testing, Projectile$onHitEntity(EntityHitResult entityHitResult) is client-side only, and both the client and the server need to be notified that an entity is riding another. Is there a way to comminate to the server?

public class Carrier extends Snowball
{
	private LivingEntity player;
	
	public Carrier(Level level, LivingEntity player)
	{
		super(level, player);
		
		this.player = player;
	}
	
	@Override
	protected void onHitEntity(EntityHitResult entityHitResult)
	{
      		//client-side only
		entityHitResult.getEntity().startRiding(player);
	}
}
Link to comment
Share on other sites

4 hours ago, Tablock_ said:

However after some testing, Projectile$onHitEntity(EntityHitResult entityHitResult) is client-side only

I find this highly unlikely.

4 hours ago, Tablock_ said:
private LivingEntity player;

This causes a memory leak. Projectile (which Snowball extends) already has a system for tracking the owner. Use setOwner and getOwner. Note that getOwner will return null if the player logs out, you must handle that case.

Link to comment
Share on other sites

Ok, I didn't think about how that variable caused a memory leak. I used getOwner() instead but get the same result.

public class Carrier extends Snowball
{
	public Carrier(Level level, LivingEntity player)
	{
		super(level, player);
	}
	
	@Override
	protected void onHitEntity(EntityHitResult entityHitResult)
	{
		getOwner().playSound(SoundEvents.BREWING_STAND_BREW, 0.5F, 0.4F / (level.getRandom().nextFloat() * 0.4F + 0.8F));
		
		entityHitResult.getEntity().startRiding(getOwner());
	}
}

EDIT: I meant to say server side only in my previous message

Edited by Tablock_
Link to comment
Share on other sites

11 hours ago, diesieben07 said:

getOwner can return null. You must handle this.

Got it

11 hours ago, diesieben07 said:

Yes, that is correct. You want it server side only.

Ok, I want an entity that comes in contact with the projectile to ride the player. I do this by calling entityHitResult.getEntity().startRiding(getOwner()) (assume getOwner() is handled). But when I run the game, only the server believes that the entity is riding the player. I know this because the entity still appears in the same location when hit, but its drops spawn above the player's head when killed. I must somehow tell the client that the entity is riding the player too. 

Edited by Tablock_
Link to comment
Share on other sites

I do not have a git repo, unfortunately. That is a good idea though, I will be creating one in the future. But since I'm running low on time at the moment I will give you it here directly. Note that ID$newItemProperties() just returns new Item.Properties().tab(Main.MOD_TAB).

public class EntityCarrier extends Item
{
	public EntityCarrier()
	{
		super(ID.newItemProperties());
	}
	
	@Override
	public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand interactionHand)
	{
		ItemStack itemStack = player.getItemInHand(interactionHand);
		
		player.playSound(SoundEvents.BONE_BLOCK_PLACE, 0.5F, 0.4F / (level.getRandom().nextFloat() * 0.4F + 0.8F));
		
		if(!level.isClientSide)
		{
			Carrier carrier = new Carrier(level, player);
	        
			carrier.setItem(itemStack);
			carrier.shootFromRotation(player, player.getXRot(), player.getYRot(), 0.0F, 1.5F, 1.0F);
			
			level.addFreshEntity(carrier);
		}
		
		return InteractionResultHolder.consume(itemStack);
	}
}

public class Carrier extends Snowball
{
	public Carrier(Level level, LivingEntity player)
	{
		super(level, player);
	}
	
	@Override
	protected void onHitEntity(EntityHitResult entityHitResult)
	{
		entityHitResult.getEntity().startRiding(getOwner());
	}
}

And if you need it, this is how I am registering EntityCarrier:

public static final RegistryObject<Item> ENTITY_CARRIER = Main.ITEMS.register("entity_carrier", () -> new EntityCarrier());

Do you think this isn't working because I need to create my own entity type? I'm stumped. If you would like anything else please let me know.

Link to comment
Share on other sites

You must register a custom EntityType for your entity and use it. Currently your entity uses EntityType.SNOWBALL, so it will be saved to disk as a vanilla snowball and the client will also spawn a vanilla snowball. You register it using DeferredRegister, just like you do with items. You must also override getAddEntityPacket in any custom entity class and use NetworkHooks.getEntitySpawningPacket. The vanilla spawning packets only work for vanilla entities.

6 hours ago, Tablock_ said:

And if you need it, this is how I am registering EntityCarrier:

public static final RegistryObject<Item> ENTITY_CARRIER = Main.ITEMS.register("entity_carrier", () -> new EntityCarrier());

I would recommend keeping a DeferredRegister and its entries in the same class. The DeferredRegister field should just be private. This ensures the proper initialization of things, which you must otherwise achieve in other way uglier ways.

Link to comment
Share on other sites

I have tried to follow what you have asked of me, but I get this error:

Cannot invoke "net.minecraft.client.renderer.entity.EntityRenderer.shouldRender(net.minecraft.world.entity.Entity, net.minecraft.client.renderer.culling.Frustum, double, double, double)" because "entityrenderer" is null

 

I have registered a new entity like so:

public static final RegistryObject<EntityType<Carrier>> CARRIER = ENTITIES.register("entity_carrier", () ->
EntityType.Builder.<Carrier>of(Carrier::new, MobCategory.MISC).sized(0.25F, 0.25F).clientTrackingRange(4).updateInterval(10).build("entity_carrier"));

Please disregard how messy this is. I would like to get it working first. This is my custom entity class:

public class Carrier extends ThrowableItemProjectile
{
	public Carrier(EntityType<Carrier> entityType, Level level)
	{
		super(entityType, level);
	}
	
	public Carrier(Level level)
	{
		super(Main.CARRIER.get(), level);
    	}
	
	@Override
	public Packet<?> getAddEntityPacket()
	{
		return NetworkHooks.getEntitySpawningPacket(this);
	}
	
	@Override
	protected void onHitEntity(EntityHitResult entityHitResult)
	{
		entityHitResult.getEntity().startRiding(getOwner());
	}

	@Override
	protected Item getDefaultItem()
	{
		return ID.ENTITY_CARRIER.getItem();
	}
}

And if you need it, my item class:

public class EntityCarrier extends Item
{
	public EntityCarrier()
	{
		super(ID.newItemProperties());
	}
	
	@Override
	public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand interactionHand)
	{
		ItemStack itemStack = player.getItemInHand(interactionHand);
		
		player.playSound(SoundEvents.BONE_BLOCK_PLACE, 0.5F, 0.4F / (level.getRandom().nextFloat() * 0.4F + 0.8F));
		
		if(!level.isClientSide)
		{
			Carrier carrier = new Carrier(level);
			
			carrier.setItem(itemStack);
			carrier.shootFromRotation(player, player.getXRot(), player.getYRot(), 0.0F, 1.5F, 1.0F);
			
			level.addFreshEntity(carrier);
		}
		
		return InteractionResultHolder.consume(itemStack);
	}
}

 

18 hours ago, diesieben07 said:

I would recommend keeping a DeferredRegister and its entries in the same class. The DeferredRegister field should just be private. This ensures the proper initialization of things, which you must otherwise achieve in other way uglier ways.

My entries will be in the same class and be private. I am trying to get it working first then clean up. No I didn't handle getOwner() yet. I also didn't make a git repo yet, I understand how much easier that will make this. I will try to set it up before you hear back from me.

Edited by Tablock_
Link to comment
Share on other sites

That makes sense. I did that and the Carrier entity renders correctly with no error now. However, entities still do not ride the player correctly after being hit with the Carrier projectile. Again, the server knows that the entity is riding the player, but the client is clueless. I created a github repository, here it is: https://github.com/Tablocked/MyFirstMod. Hopefully you can see it with no issues. I set up my entity renderer in a class named Client. I already showed you every other class that you should need.

Edited by Tablock_
Link to comment
Share on other sites

  • The root of your Git repo should be at your project root (where your build.gradle is).
  • Your Eclipse Workspace should not be in your Git repo.
  • What the heck is this?
  • It seems Minecraft is not correctly set up to handle entities riding players. However it can be easily fixed:
    1. onHitEntity needs to check that you're on the server side.
    2. onHitEntity needs to check if the owner is null. If it is, it needs to handle this gracefully.
    3. If the owner is a ServerPlayer, onHitEntity needs to send ClientboundSetPassengersPacket to the player, which vanilla Minecraft doesn't do for players automatically
    In conclusion:
    if (!level.isClientSide && getOwner() != null) {
    	entityHitResult.getEntity().startRiding(getOwner());
    	if (getOwner() instanceof ServerPlayer ownerPlayer) {
    		ownerPlayer.connection.send(new ClientboundSetPassengersPacket(ownerPlayer));
    	}
    }
Link to comment
Share on other sites

16 hours ago, diesieben07 said:

The root of your Git repo should be at your project root (where your build.gradle is).

Got it, its changed now.

16 hours ago, diesieben07 said:

What the heck is this?

This was to load the ID class so that all of those static fields were initialized before I called DeferredRegister$register(IEventBus bus). I removed that now and replaced it with a new method I created: ID$init().

17 hours ago, diesieben07 said:
if (!level.isClientSide && getOwner() != null) {
	entityHitResult.getEntity().startRiding(getOwner());
	if (getOwner() instanceof ServerPlayer ownerPlayer) {
		ownerPlayer.connection.send(new ClientboundSetPassengersPacket(ownerPlayer));
	}
}

This did it! But I have a couple questions on why this works.

  • Why do I need to check if its server side? Isn't it already server side only?
  • What exactly is ServerPlayer? I see that it extends Player, but what's the difference?
  • Is ClientboundSetPassengersPacket telling the client that the entity is riding the player?

Thanks as always.

Link to comment
Share on other sites

1 hour ago, Tablock_ said:

This was to load the ID class so that all of those static fields were initialized before I called DeferredRegister$register(IEventBus bus). I removed that now and replaced it with a new method I created: ID$init().

If you didn't use that ass-backwards way of registering things, you would not need hacks like this.

1 hour ago, Tablock_ said:

Why do I need to check if its server side? Isn't it already server side only?

In my testing it wasn't. Doesn't hurt to be sure.

1 hour ago, Tablock_ said:

What exactly is ServerPlayer? I see that it extends Player, but what's the difference?

ServerPlayer is the player entity used by the server thread. The client uses LocalPlayer for the player you are controlling and RemotePlayer for other players that are loaded. Player is the abstract base class for those.

1 hour ago, Tablock_ said:

Is ClientboundSetPassengersPacket telling the client that the entity is riding the player?

The packet tells the client about the passengers of whatever entity you give it. Since you're giving it the player itself, you are telling the client about the player's passengers. Minecraft sends this packet automatically for tracked entities (i.e. entities that are within the client's range) so that the client knows about passengers. However it is not sent for the player itself by default, because this situation never occurs in vanilla Minecraft and thus is not an issue.

Link to comment
Share on other sites

22 hours ago, diesieben07 said:

If you didn't use that ass-backwards way of registering things, you would not need hacks like this.

This is not true, Class$forName has nothing to do with the way I registered things. It has to do with the fact that I did not call DeferredRegister$register in the ID class. I could have also made the DeferredRegister fields private in the ID class. Anyway, thank you for answering all of my questions and giving me all of your help.

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now


×
×
  • Create New...

Important Information

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