Jump to content

Linking item and capability


Blooode

Recommended Posts

I can't link using item with thirst capability. I want to use item and add thirst points after that. I try lots of times, that is one of examples. Version 1.19.2

@SubscribeEvent
	    public void OnDrinking(LivingEntityUseItemEvent.Finish event)
	    {
			LivingEntity player = event.getEntity();
	        if (event.getItem().equals(Items.APPLE))
	        {
	        	player.getCapability(PlayerThirstProvider.PLAYER_THIRST).ifPresent(thirst -> {
	        		thirst.addThirst(10);
	            });
	        }
	    }

 

Link to comment
Share on other sites

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

I have attached capability. 

@SubscribeEvent
	    public static void OnAttachCapabilitiesPlayer(AttachCapabilitiesEvent<Entity> event) {
	        if(event.getObject() instanceof Player) {
	            if(!event.getObject().getCapability(PlayerThirstProvider.PLAYER_THIRST).isPresent()) {
	                event.addCapability(new ResourceLocation(HardLife.MODID, "properties"), new PlayerThirstProvider());
	            }
	        }
	    }

This capability working, but I can't understand how to add thirst points via using items such food or bottle of water.

Link to comment
Share on other sites

Also I have method for add thirst by pushing the keybord button.

public boolean handle(Supplier<NetworkEvent.Context> supplier) {
        NetworkEvent.Context context = supplier.get();
        context.enqueueWork(() -> {
            ServerPlayer player = context.getSender();
            ServerLevel level = player.getLevel();
            	
            if(hasWaterAroundThem(player, level, 2)) {
                player.sendSystemMessage(Component.translatable(MESSAGE_DRINK_WATER).withStyle(ChatFormatting.DARK_AQUA));
                level.playSound(null, player.getOnPos(), SoundEvents.GENERIC_DRINK, SoundSource.PLAYERS,
                        0.5F, level.random.nextFloat() * 0.1F + 0.9F);
                player.getCapability(PlayerThirstProvider.PLAYER_THIRST).ifPresent(thirst -> {
                    thirst.addThirst(1);
                    player.sendSystemMessage(Component.literal("Current Thirst " + thirst.getThirst())
                            .withStyle(ChatFormatting.AQUA));
                    ModMessages.sendToPlayer(new ThirstDataSyncS2CPacket(thirst.getThirst()), player);
                });


            } else {
                player.sendSystemMessage(Component.translatable(MESSAGE_NO_WATER).withStyle(ChatFormatting.RED));
                player.getCapability(PlayerThirstProvider.PLAYER_THIRST).ifPresent(thirst -> {
                    player.sendSystemMessage(Component.literal("Current Thirst " + thirst.getThirst())
                            .withStyle(ChatFormatting.AQUA));
                    ModMessages.sendToPlayer(new ThirstDataSyncS2CPacket(thirst.getThirst()), player);
                });
            }
        });
        return true;
    }

Maybe that can be made in this method?

Link to comment
Share on other sites

You don't have an answerable question. All you say is something about your code doesn't work.

The code you do show references other code you don't show and lacks context, so we can't even reverse engineer (guess) what your problem is.

For a question about capabilities, you don't even show yours.

 

One way to get people not to ignore your question is to put your code on github where they can run it for themselves (and see all the relevant code).

But if you don't clearly define what the problem is, nobody can help you.

"Please find the bug somewhere in this random code I wrote", is question that won't attract many answers.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

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



×
×
  • Create New...

Important Information

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