Jump to content

[Solved] [1.16.5] Custom capability doesn't save after player leaves world


mclich

Recommended Posts

So i made my custom capability and attach it to all players. I also made serialization and synchronization using my own packet (at least i think i do) to allow client (player) always access updated capability data after some changes. In my capability event handler class i've subscribed to PlayerLoggedInEvent and send updated data through my packet. The problem is the data just doesn't save after player leaves the world and sets to default values after player joins the world again. Any ideas how to fix that?

Edited by mclich
Link to comment
Share on other sites

9 minutes ago, diesieben07 said:

Post your code.

Capability class:

public class ManaCapability
{
	public static final ResourceLocation LOCATION=new ResourceLocation(ElderNorseGods.MOD_ID, "mana");
	
	@CapabilityInject(IManaHandler.class)
	public static Capability<IManaHandler> CAP_INSTANCE=null;
	
	public static class ManaStorage implements IStorage<IManaHandler>
	{
		@Override
		public INBT writeNBT(Capability<IManaHandler> cap, IManaHandler manaHandler, Direction side)
		{
			CompoundNBT tag=new CompoundNBT();
			tag.putFloat("Mana", manaHandler.getMana());
			tag.putBoolean("Status", manaHandler.getStatus());
			return tag;
		}

		@Override
		public void readNBT(Capability<IManaHandler> cap, IManaHandler manaHandler, Direction side, INBT nbt)
		{
			manaHandler.setMana(((CompoundNBT)nbt).getFloat("Mana"));
			manaHandler.setStatus(((CompoundNBT)nbt).getBoolean("Status"));
		}
	}
	
	public static class ManaProvider implements ICapabilitySerializable<CompoundNBT>
	{
		private final ManaHandler mana=new ManaHandler();
		private final LazyOptional<IManaHandler> manaOptional=LazyOptional.of(()->this.mana);
		
		public void invalidate()
		{
			this.manaOptional.invalidate();
		}
		
		@Override
		public CompoundNBT serializeNBT()
		{
			if(ManaCapability.CAP_INSTANCE==null) return new CompoundNBT();
			else return (CompoundNBT)ManaCapability.CAP_INSTANCE.writeNBT(this.mana, null);
		}

		@Override
		public void deserializeNBT(CompoundNBT nbt)
		{
			if(ManaCapability.CAP_INSTANCE!=null) ManaCapability.CAP_INSTANCE.readNBT(this.mana, null, nbt);
		}
		
		@Override
		public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side)
		{
			return cap==ManaCapability.CAP_INSTANCE?this.manaOptional.cast():LazyOptional.empty();
		}
	}
}

Capability handler class:

public class ManaHandler implements IManaHandler
{
	private float mana;
	private boolean active;
	
	public ManaHandler()
	{
		this.mana=0F;
		this.active=false;
	}
	
	@Override
	public void setMana(float amount)
	{
		if(amount<0F) this.mana=0F;
		else if(amount>20F) this.mana=20F;
		else this.mana=amount;
	}

	@Override
	public float getMana()
	{
		return this.mana;
	}

	@Override
	public void setStatus(boolean value)
	{
		this.active=value;
	}

	@Override
	public boolean getStatus()
	{
		return this.active;
	}

	@Override
	public void update(ServerPlayerEntity player)
	{
		NetworkHandler.sendToPlayer(player, new ManaDataPacket(this.getMana(), this.getStatus()));
	}
}

Capability event handler class:

@EventBusSubscriber(modid=ElderNorseGods.MOD_ID, bus=EventBusSubscriber.Bus.FORGE)
public abstract class ManaEventHandler
{
	private static void sendUpdates(ServerPlayerEntity player)
	{
		if(!player.getCommandSenderWorld().isClientSide())
		{
			player.getCapability(ManaCapability.CAP_INSTANCE).ifPresent(mana->mana.update(player));
		}
	}
	
	@SubscribeEvent
	public static void onPlayerChangedDimension(PlayerChangedDimensionEvent event)
	{
		ManaEventHandler.sendUpdates((ServerPlayerEntity)event.getPlayer());
	}
	
	@SubscribeEvent
	public static void onPlayerRespawn(PlayerRespawnEvent event)
	{
		ManaEventHandler.sendUpdates((ServerPlayerEntity)event.getPlayer());
	}
	
	@SubscribeEvent
	public static void onPlayerLoggedIn(PlayerLoggedInEvent event)
	{
		ManaEventHandler.sendUpdates((ServerPlayerEntity)event.getPlayer());
	}
	
	@SubscribeEvent
	public static void onPlayerClone(PlayerEvent.Clone event)
	{
		if(!event.isWasDeath()) return;
		IManaHandler oldMana=event.getOriginal().getCapability(ManaCapability.CAP_INSTANCE).orElse(null);
		IManaHandler newMana=event.getPlayer().getCapability(ManaCapability.CAP_INSTANCE).orElse(null);
		if(oldMana!=null&&newMana!=null)
		{
			newMana.setMana(oldMana.getMana());
			newMana.setStatus(oldMana.getStatus());
		}
	}
}

Packet class:

public class ManaDataPacket
{
	private float value;
	private boolean status;
	
	public ManaDataPacket(float value, boolean status)
	{
		this.value=value;
		this.status=status;
	}
	
	public static void encode(ManaDataPacket packet, PacketBuffer buffer)
	{
		buffer.writeFloat(packet.value);
		buffer.writeBoolean(packet.status);
	}
	
	public static ManaDataPacket decode(PacketBuffer buffer)
	{
		return new ManaDataPacket(buffer.readFloat(), buffer.readBoolean());
	}
	
	public static void handle(ManaDataPacket packet, Supplier<NetworkEvent.Context> ctx)
	{
		ctx.get().enqueueWork(()->DistExecutor.unsafeRunWhenOn(Dist.CLIENT, ()->()->PacketHandler.handlePacket(packet, ctx)));
		ctx.get().setPacketHandled(true);
	}
	
	private static class PacketHandler
	{
		private static void handlePacket(ManaDataPacket packet, Supplier<NetworkEvent.Context> ctx)
		{
			Minecraft mc=Minecraft.getInstance();
			mc.player.getCapability(ManaCapability.CAP_INSTANCE).ifPresent
			(
				mana->
				{
					mana.setMana(packet.value);
					mana.setStatus(packet.status);
				}
			);
		}
	}
}
Link to comment
Share on other sites

2 minutes ago, diesieben07 said:

Where are you modifying the values?

At the moment i have only one item that change values after use. There is code:
 

public class ManaTinctureItem extends Item
{
	public static final String ID="mana_tincture";
	
	public ManaTinctureItem()
	{
		super(new Item.Properties().food(new Food.Builder().nutrition(2).saturationMod(0.6F).alwaysEat().build()).stacksTo(1).rarity(Rarity.RARE).tab(ENGTabs.FOOD));
	}
	
	@Override
	public ItemStack finishUsingItem(ItemStack itemStack, World world, LivingEntity entity)
	{
		itemStack=entity.eat(world, itemStack);
		if(!world.isClientSide()&&entity instanceof ServerPlayerEntity)
		{
			ServerPlayerEntity player=(ServerPlayerEntity)entity;
			player.getCapability(ManaCapability.CAP_INSTANCE).ifPresent
			(
				mana->
				{
					if(!mana.getStatus())
					{
						mana.setStatus(true);
						mana.setMana(20F);
						mana.update(player);
					}
				}
			);
			if(player.gameMode.isSurvival())
			{
				if(itemStack.getCount()==0&&!player.inventory.contains(new ItemStack(Items.BOWL)))
				{
					player.inventory.removeItem(itemStack);
					player.inventory.add(player.inventory.selected, new ItemStack(Items.BOWL));
				}
				else player.inventory.add(new ItemStack(Items.BOWL));
			}
		}
		return itemStack;
	}
	
	@Override
	public boolean isFoil(ItemStack itemStack)
	{
		return true;
	}
}

 

Link to comment
Share on other sites

16 minutes ago, diesieben07 said:

Are the NBT serialization methods called? Do they operate correctly? Check using the debugger.

If i understood correctly, something is broken here. When i check for capability instance being null:

@Override
public CompoundNBT serializeNBT()
{
	if(ManaCapability.CAP_INSTANCE==null) return new CompoundNBT();
	else return (CompoundNBT)ManaCapability.CAP_INSTANCE.writeNBT(this.mana, null);
}

it always returns true, so serializeNBT() always returns new CompoundNBT(), i don't know why

Edited by mclich
Link to comment
Share on other sites

6 hours ago, diesieben07 said:

Show where you register your capability.

 

@EventBusSubscriber(modid=ElderNorseGods.MOD_ID, bus=EventBusSubscriber.Bus.FORGE)
public abstract class ENGCapabilities
{
	@SubscribeEvent
	public static void registerCapabilities(final FMLCommonSetupEvent event)
	{
		CapabilityManager.INSTANCE.register(IManaHandler.class, new ManaStorage(), ManaHandler::new);
	}
	
	@SubscribeEvent
	public static void attachCapabilities(final AttachCapabilitiesEvent<Entity> event)
	{
		if(!(event.getObject() instanceof PlayerEntity)) return;
		ManaProvider provider=new ManaProvider();
		event.addCapability(ManaCapability.LOCATION, provider);
		event.addListener(provider::invalidate);
	}
}
Edited by mclich
Link to comment
Share on other sites

18 minutes ago, diesieben07 said:

That is the wrong event bus for FMLCommonSetupEvent.

My god, i'm an idiot... I changed registration class like this:

@EventBusSubscriber(modid=ElderNorseGods.MOD_ID, bus=EventBusSubscriber.Bus.MOD)
public abstract class ENGCapabilities
{
	@SubscribeEvent
	public static void registerCapabilities(final FMLCommonSetupEvent event)
	{
		CapabilityManager.INSTANCE.register(IManaHandler.class, new ManaStorage(), ManaHandler::new);
	}
	
	@EventBusSubscriber(modid=ElderNorseGods.MOD_ID, bus=EventBusSubscriber.Bus.FORGE)
	private static class AttachCapabilities
	{
		@SubscribeEvent
		public static void attachCapabilities(final AttachCapabilitiesEvent<Entity> event)
		{
			if(!(event.getObject() instanceof PlayerEntity)) return;
			ManaProvider provider=new ManaProvider();
			event.addCapability(ManaCapability.LOCATION, provider);
			event.addListener(provider::invalidate);
		}
	}
}

and it works now! Thank you for your help!

Link to comment
Share on other sites

  • mclich changed the title to [Solved] [1.16.5] Custom capability doesn't save after player leaves world

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



×
×
  • Create New...

Important Information

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