Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[SOLVED] [1.18] Custom entity only spawns in client when reading additional save data, not on item use.

Featured Replies

Posted

Hello everyone! 

There's this entity I've created that extends HangingEntity, along with its item that extends HangingEntityItem. Works pretty much like a painting would.

I'm getting this unusual behavior in which, when right-clicking to place it, the entity seems to get created on the server but not on the client. However, upon saving and reloading the world, the entity is actually right there, perfectly as intended.

At some point I came across getAddEntityPacket so I did override it with a call to NetworkHooks.getEntitySpawningPacket, but it appears to never be called and I'm not sure why. I'd even included IEntityAdditionalSpawnData to sync stuff such as the painting's facing direction to no avail.

Since the Entity's working fine when loading the world (and even when summoning directly with /summon), I'm inclined to think there's something up with the Item, but there's not that much going on there.

I've attached it below along with the entity.

Spoiler

Item:

public class SpecialPaintingItem extends HangingEntityItem {
	public SpecialPaintingItem(EntityType<? extends HangingEntity> type, Properties properties) {
		super(type, properties);
	}

	@Override
	public InteractionResult useOn(UseOnContext ctx) {
		BlockPos blockpos = ctx.getClickedPos();
		Direction direction = ctx.getClickedFace();
		BlockPos blockpos1 = blockpos.relative(direction);
		Player player = ctx.getPlayer();
		ItemStack itemstack = ctx.getItemInHand();
		if (player != null && !this.mayPlace(player, direction, itemstack, blockpos1)) {
			return InteractionResult.FAIL;
		} else {
			Level level = ctx.getLevel();
			SpecialPaintingEntity hangingentity = new SpecialPaintingEntity(level, blockpos1, direction);

			CompoundTag compoundtag = itemstack.getTag();
			if (compoundtag != null) {
				EntityType.updateCustomEntityTag(level, player, hangingentity, compoundtag);
			}

			if (hangingentity.survives()) {
				if (!level.isClientSide) {
					hangingentity.playPlacementSound();
					level.gameEvent(player, GameEvent.ENTITY_PLACE, blockpos);
					level.addFreshEntity(hangingentity);
				}

				itemstack.shrink(1);
				return InteractionResult.sidedSuccess(level.isClientSide);
			} else {
				return InteractionResult.CONSUME;
			}
		}
	}
}

Entity:

public class SpecialPaintingEntity extends HangingEntity implements IEntityAdditionalSpawnData {
    private final String DEFAULT_MOTIVE_ID = "test";
	private String motiveId = DEFAULT_MOTIVE_ID;
	public Motive motive;

	public SpecialPaintingEntity(EntityType<? extends SpecialPaintingEntity> entityType, Level level) {
		super(entityType, level);
	}

	public SpecialPaintingEntity(Level level, BlockPos blockpos, Direction direction) {
		this(ModEntity.SPECIAL_PAINTING_ENTITY.get(), level);
		this.pos = blockpos;
		this.setMotiveId(DEFAULT_MOTIVE_ID);
	}

	public SpecialPaintingEntity(Level level, BlockPos blockpos, Direction direction, String motiveId) {
		this(level, blockpos, direction);
		this.setMotiveId(motiveId);
	}

	public ResourceLocation getMotiveResourceLocation() {
		return new ResourceLocation(TestMod.MOD_ID, "painting/" + motiveId);
	}

	@Override
	public void dropItem(@Nullable Entity p_31925_) {
		if (this.level.getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
			this.playSound(SoundEvents.PAINTING_BREAK, 1.0F, 1.0F);
			if (p_31925_ instanceof Player) {
				Player player = (Player) p_31925_;
				if (player.getAbilities().instabuild) {
					return;
				}
			}

			ItemStack stack = new ItemStack(
					ForgeRegistries.ITEMS.getValue(new ResourceLocation(TestMod.MOD_ID, "special_painting")));
			stack.getOrCreateTag().put(TestMod.MOD_ID, new CompoundTag());
			CompoundTag nbt = stack.getOrCreateTag().getCompound(TestMod.MOD_ID);
			nbt.putString("Motive", motiveId);

			this.spawnAtLocation(stack, 0F);
		}
	}

	@Override
	public Packet<?> getAddEntityPacket() {
		return NetworkHooks.getEntitySpawningPacket(this);
	}
	
    @Override
    public void readSpawnData(FriendlyByteBuf buffer) {
        setMotiveId(buffer.readUtf());
        this.direction = Direction.from2DDataValue(buffer.readByte());
        this.setDirection(this.direction);
    }

    @Override
    public void writeSpawnData(FriendlyByteBuf buffer) {
        buffer.writeUtf(this.motiveId);
        buffer.writeByte((byte) this.direction.get2DDataValue());
    }

	public void addAdditionalSaveData(CompoundTag p_31935_) {
		p_31935_.putString("Motive", motiveId);
		p_31935_.putByte("Facing", (byte) this.direction.get2DDataValue());
		p_31935_.putBoolean("Invisible", this.isInvisible());
		super.addAdditionalSaveData(p_31935_);
	}

	public void readAdditionalSaveData(CompoundTag p_31927_) {
		setMotiveId(p_31927_.getString("Motive"));
		this.direction = Direction.from2DDataValue(p_31927_.getByte("Facing"));
		this.setInvisible(p_31927_.getBoolean("Invisible"));
		super.readAdditionalSaveData(p_31927_);
		this.setDirection(this.direction);
	}

	@Override
	public int getWidth() {
		return this.motive.getWidth();
	}

	@Override
	public int getHeight() {
		return this.motive.getHeight();
	}
	
	public void setMotiveId(String motiveId) {
		this.motiveId = motiveId;
		this.motive = ModPaintings.SPECIAL_PAINTING_REGISTRY.get()
				.getValue(new ResourceLocation(TestMod.MOD_ID, motiveId)).get();
	}

	@Override
	public void playPlacementSound() {
		this.playSound(SoundEvents.PAINTING_PLACE, 1.0F, 1.0F);
	}

	public void moveTo(double p_31929_, double p_31930_, double p_31931_, float p_31932_, float p_31933_) {
		this.setPos(p_31929_, p_31930_, p_31931_);
	}

	public void lerpTo(double p_31917_, double p_31918_, double p_31919_, float p_31920_, float p_31921_, int p_31922_,
			boolean p_31923_) {
		BlockPos blockpos = this.pos.offset(p_31917_ - this.getX(), p_31918_ - this.getY(), p_31919_ - this.getZ());
		this.setPos((double) blockpos.getX(), (double) blockpos.getY(), (double) blockpos.getZ());
	}
}

 

--------------

Moreover, I was able to verify that addFreshEntity was indeed called on the server, and the entity's constructor is being called on both sides. Just to make sure I also checked the entity's remove method and it was not being called. (which makes sense since the entity gets saved with the world)

I'll appreciate any and all help including pointers on where to poke next for clues. 

Thanks a bunch!

Edited by Virtu
Issue solved.

  • Author

Thanks for the swift response! 

To make matters easier I made a repo to reproduce the issue minimally, linked here.

The original goal was to create some paintings that couldn't be obtained from the regular pool. In short the approach I attempted was:

  • Creating a registry for the custom painting motives (at core/ModPaintings.java)
  • Stitching the registered motive's textures in the painting atlas (at client/event/ClientModEvents.java)
  • Referencing the included sprites in a renderer based off PaintingRenderer (at client/renderer/SpecialPaintingRenderer.java)

There are still some quirks such as the entity initially displaying slightly offset and then popping into the correct position after a few seconds, the back texture not being loaded properly and stuff, but I'll hopefully get to those once this issue is down. 

  • Author
Quote

You are not calling setDirection when creating your entity

Absolutely hit the nail on the head. Can't believe I overlooked that. lol

Everything is working flawlessly now. Also thanks for the extra tips, I've applied them both with success and the unnecessary code is now gone.

  • Virtu changed the title to [SOLVED] [1.18] Custom entity only spawns in client when reading additional save data, not on item use.

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.