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

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


mclich
 Share

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

Show where you register your capability.

And this "Oh, it's null... guess I'll do nothing then and not report this error" behavior is terrible. This is not PHP. If something so fundamental is wrong, throw an exception! Crashing the game is OK if your code has bugs. Don't try to sweep the errors under the rug. It makes things way more hard to debug.

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

    • Server stops launching a couple seconds in and spits out a bunch of errors. This is a new server so it didn't just suddenly break, it has never successfully started up and I can't make anything out of this log, [27Jan2022 22:25:37.820] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--gameDir, ., --launchTarget, fmlserver, --fml.forgeVersion, 36.2.20, --fml.mcpVersion, 20210115.111550, --fml.mcVersion, 1.16.5, --fml.forgeGroup, net.minecraftforge, nogui] [27Jan2022 22:25:37.823] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 8.0.9+86+master.3cf110c starting: java version 1.8.0_321 by Oracle Corporation [27Jan2022 22:25:38.185] [main/INFO] [net.minecraftforge.fml.loading.FixSSL/CORE]: Added Lets Encrypt root certificates as additional trust [27Jan2022 22:25:38.213] [main/INFO] [mixin/]: SpongePowered MIXIN Subsystem Version=0.8.4 Source=file:/F:/f%20in%20the%20a/create%20above%20and%20beyond/server/libraries/org/spongepowered/mixin/0.8.4/mixin-0.8.4.jar Service=ModLauncher Env=SERVER [27Jan2022 22:25:40.330] [main/ERROR] [mixin/]: Mixin config antiqueatlas.mixins.json does not specify "minVersion" property [27Jan2022 22:25:40.406] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: java.lang.NoSuchMethodError: sun.security.util.ManifestEntryVerifier.<init>(Ljava/util/jar/Manifest;)V [27Jan2022 22:25:40.407] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.SecureJarHandler.createCodeSource(SecureJarHandler.java:66) [27Jan2022 22:25:40.408] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.TransformingClassLoader$DelegatedClassLoader.findClass(TransformingClassLoader.java:275) [27Jan2022 22:25:40.408] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.TransformingClassLoader.loadClass(TransformingClassLoader.java:136) [27Jan2022 22:25:40.409] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.TransformingClassLoader.loadClass(TransformingClassLoader.java:98) [27Jan2022 22:25:40.409] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at java.lang.ClassLoader.loadClass(Unknown Source) [27Jan2022 22:25:40.409] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at java.lang.Class.forName0(Native Method) [27Jan2022 22:25:40.409] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at java.lang.Class.forName(Unknown Source) [27Jan2022 22:25:40.409] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at org.spongepowered.asm.service.modlauncher.ModLauncherClassProvider.findClass(ModLauncherClassProvider.java:67) [27Jan2022 22:25:40.410] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at org.spongepowered.asm.launch.platform.MixinConnectorManager.loadConnectors(MixinConnectorManager.java:70) [27Jan2022 22:25:40.410] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at org.spongepowered.asm.launch.platform.MixinConnectorManager.inject(MixinConnectorManager.java:59) [27Jan2022 22:25:40.410] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at org.spongepowered.asm.launch.platform.MixinPlatformManager.inject(MixinPlatformManager.java:196) [27Jan2022 22:25:40.411] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at org.spongepowered.asm.launch.MixinBootstrap.inject(MixinBootstrap.java:202) [27Jan2022 22:25:40.411] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at org.spongepowered.asm.launch.MixinLaunchPluginLegacy.initializeLaunch(MixinLaunchPluginLegacy.java:201) [27Jan2022 22:25:40.411] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at org.spongepowered.asm.launch.MixinLaunchPluginLegacy.initializeLaunch(MixinLaunchPluginLegacy.java:195) [27Jan2022 22:25:40.412] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.LaunchPluginHandler.lambda$announceLaunch$9(LaunchPluginHandler.java:97) [27Jan2022 22:25:40.412] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at java.util.HashMap.forEach(Unknown Source) [27Jan2022 22:25:40.412] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.LaunchPluginHandler.announceLaunch(LaunchPluginHandler.java:97) [27Jan2022 22:25:40.413] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:52) [27Jan2022 22:25:40.414] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [27Jan2022 22:25:40.414] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.Launcher.run(Launcher.java:82) [27Jan2022 22:25:40.414] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) [27Jan2022 22:25:40.415] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at net.minecraftforge.server.ServerMain$Runner.runLauncher(ServerMain.java:63) [27Jan2022 22:25:40.417] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at net.minecraftforge.server.ServerMain$Runner.access$100(ServerMain.java:60) [27Jan2022 22:25:40.417] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: at net.minecraftforge.server.ServerMain.main(ServerMain.java:57)  
    • Sorry, to be more specific the "screen" stands for the window. It is the dimension of the texture you are rendering on the screen, for example you want to render a 1920 x 1080 texture on a 16 x 8 button, then the width and height is 16 x 8. In your case you want to fill the whole window, then the width and height is the window width/height got from iirc Minecraft.getInstance().getWindow().getScaledWidth() or getScaledHeight(.
    • Great minds think alike!  Sounds like what I was thinking of doing.
    • But only Temurin 8 and 17 have .jre versions Could you please tell me the one i have to dl ?
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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