Jump to content

(1.18.2) My custom capability doesnt attach to player/entities


ElTotisPro50

Recommended Posts

I followed a tutorial on this(https://www.youtube.com/watch?v=My70x9LzeUM&t=297s) because i never had to use capabilities until now, the tutorial is for 1.19 but the process is exactly the same as 1.18.2.

For testing, my capability is the same as the tutorial (thirst for players) but it's not working, when i press a key it removes 1 "thirst" and sends a message but nothing happens, no errors or crashes, i also tried to do the same but with a package sending it to the server just in case, but APPARENTLY AND FOR NOW, it has nothing to do with client and server.

So maybe im attaching the capability to the player wrong? (BTW: the key pressing works correctly, so thats not the problem)

 

public class PlayerThirstProvider implements ICapabilityProvider, INBTSerializable<CompoundTag> {

    public static Capability<PlayerThirst> CAPABILITY_PLAYER_DATA = CapabilityManager.get(new CapabilityToken<>() {});
    private LazyOptional<PlayerThirst> optional = LazyOptional.of(this::create);
    PlayerThirst thirst = null;

    private PlayerThirst create() {
        if(this.thirst == null) {
            this.thirst = new PlayerThirst();
        }
        return this.thirst;
    }



    @NotNull
    @Override
    public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
        if(cap == CAPABILITY_PLAYER_DATA) {
            return optional.cast();
        }
        return LazyOptional.empty();
    }
    @Override
    public CompoundTag serializeNBT() {
        CompoundTag tag = new CompoundTag();
        create().save(tag);
        return tag;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
        create().load(nbt);
    }
}
public class PlayerThirst {

    private int thirst;

    public int getThirst() {
        return this.thirst;
    }

    public void addThirst(int thirst) {
        this.thirst = Math.min(this.thirst + thirst, 10);
    }
    public void subThirst(int thirst) {
        this.thirst = Math.max(this.thirst - thirst, 0);
    }
    public void copyFrom(PlayerThirst source) {
        this.thirst = source.thirst;
    }
    public void save(CompoundTag tag) {
        tag.putInt("thirst", this.thirst);
    }
    public void load(CompoundTag tag) {
        this.thirst = tag.getInt("thirst");
    }
}

 

@Mod.EventBusSubscriber(modid = Constants.MOD_ID)
public abstract class ClientEvents {

  	@SubscribeEvent
        public static void keyPress2(InputEvent.KeyInputEvent event) {
            if(ModKeybinds.MY_KEY.consumeClick()) {
                //ModMessages.sendToServer(new PacketThirst());
                mc.player.getCapability(PlayerThirstProvider.CAPABILITY_PLAYER_DATA).ifPresent(thirst -> {
                    thirst.subThirst(1);
                    mc.player.sendMessage(new TextComponent(""+thirst.getThirst()),mc.player.getUUID());
                });
            }
        }
    @SubscribeEvent
    public static void register(RegisterCapabilitiesEvent event) {
        event.register(PlayerThirst.class);
    }
    @SubscribeEvent
    public void attachCapabilities(AttachCapabilitiesEvent<Entity> event) {
        if (event.getObject() instanceof Player) {
            if(!event.getObject().getCapability(PlayerThirstProvider.CAPABILITY_PLAYER_DATA).isPresent()) {
                event.addCapability(new ResourceLocation(Constants.MOD_ID, "playerdata"), new PlayerThirstProvider());
            }
        }
    }
    @SubscribeEvent
    public static void onPlayerCloned(PlayerEvent.Clone event) {
        if (event.isWasDeath()) {
            event.getOriginal().getCapability(PlayerThirstProvider.CAPABILITY_PLAYER_DATA).ifPresent(oldStore -> {
                event.getPlayer().getCapability(PlayerThirstProvider.CAPABILITY_PLAYER_DATA).ifPresent(newStore -> {
                    newStore.copyFrom(oldStore);
                });
            });
        }
    }
}
public class PacketThirst {

    public PacketThirst() {
    }

    // Read
    public PacketThirst(FriendlyByteBuf buf) {

    }

    public void write(FriendlyByteBuf buf) {
    }

    public boolean handle(Supplier<NetworkEvent.Context> ctx) {
        NetworkEvent.Context context = ctx.get();
        context.enqueueWork(() -> {
            ServerPlayer serverPlayer = context.getSender();
            serverPlayer.getCapability(PlayerThirstProvider.CAPABILITY_PLAYER_DATA).ifPresent(thirst -> {
                thirst.subThirst(1);
                serverPlayer.sendMessage(new TextComponent(""+thirst.getThirst()),serverPlayer.getUUID());
            });
        });
        return true;
    }
}
Link to comment
Share on other sites

RegisterCapabilities is fired on the mod event bus, not the forge event bus.

https://forge.gemwire.uk/wiki/Events#Event_Bus

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

37 minutes ago, warjort said:

RegisterCapabilities is fired on the mod event bus, not the forge event bus.

https://forge.gemwire.uk/wiki/Events#Event_Bus

it didnt work:

 

@Mod.EventBusSubscriber(modid = Constants.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
public abstract class ModBusClientEvents {

    @SubscribeEvent
    public static void register(RegisterCapabilitiesEvent event) {
        event.register(PlayerThirst.class);
    }
}

 

Link to comment
Share on other sites

Quote

@SubscribeEvent

public void attachCapabilities(AttachCapabilitiesEvent<Entity> event) {

not a static method

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.