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

[1.18]How to add PlayerData


ocome
 Share

Recommended Posts

I have read the documentation and run the example once, but attach is not executed.

When the subscribed Entity is called, attach is called, and then MyCapabilityImplementation is called. fill in the code in the place corresponding to MyCapabilityImplementation.
This is how I understand it, am I wrong

And I'm not sure what the need for the ResourceLocation string is.
It says there are no restrictions, so does that mean it is an ID for identification purposes?

Main

    public void registerCapabilities(RegisterCapabilitiesEvent event)
    {
        MyCapability.register(event);

    }

 

 

The problem with not running has been resolved.
I had misinterpreted it.

MinecraftForge.EVENT_BUS.register(MyCapabilityAttacher.class);

So the only thing I'm concerned about is the correctness of future development procedures.

 

Edited by ocome
Link to comment
Share on other sites

13 hours ago, ocome said:

When the subscribed Entity is called, attach is called, and then MyCapabilityImplementation is called. fill in the code in the place corresponding to MyCapabilityImplementation.

in AttachCapabilitiesEvent you need a ICapabilityProvider which holds your Capability

  • Like 1
Link to comment
Share on other sites

uhh...
Mycapabilityattacher has MyCapabilityProvider and implements ICapabilityProvide, which I thought was sufficient, but I guess not.
I'll check again.
 

public class MyCapabilityAttacher {
	private static class MyCapabilityProvider implements ICapabilityProvider, INBTSerializable<CompoundTag> {

        public static final ResourceLocation IDENTIFIER = new ResourceLocation(ExampleMod.MOD_ID, "tutorial");

        private final MyCapabilityInterface backend = new MyCapabilityImplementation();
        private final LazyOptional<MyCapabilityInterface> optionalData = LazyOptional.of(() -> backend);

        @NotNull
        @Override
        public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
            return MyCapability.INSTANCE.orEmpty(cap, this.optionalData);
        }

        void invalidate() {
            this.optionalData.invalidate();
        }

        @Override
        public CompoundTag serializeNBT() {
            return this.backend.serializeNBT();
        }

        @Override
        public void deserializeNBT(CompoundTag nbt) {
            this.backend.deserializeNBT(nbt);
        }
    }
...
Link to comment
Share on other sites

Quote

Why not?

 

Oh, I think my English is wrong again.

Mx Luis taught me what I needed to know?

I think it is enough. 

sorry. I'll study English.

 

Back to the topic at hand, after writing the code to implement the mana.
 If you change it to only call it if the Entity is a player, I think it will work

Thank you, all of you. I'll try.

Edited by ocome
Link to comment
Share on other sites

I'm trying to add some functionality to the Player.

    private static class MyCapabilityProvider implements ICapabilityProvider, INBTSerializable<CompoundTag> {

        public static final ResourceLocation IDENTIFIER = new ResourceLocation(ExampleMod.MOD_ID, "tutorial");
        Level level = Minecraft.getInstance().level;

        private final MyCapabilityInterface backend = new MyCapabilityImplementation(level,BlockPos.ZERO,0,new GameProfile(null,null));
      
      
        private final LazyOptional<MyCapabilityInterface> optionalData = LazyOptional.of(() -> backend);

        @NotNull
          .........

Is this how the call should be made?

public class MyCapabilityImplementation extends Player implements MyCapabilityInterface {

    private static final String NBT_KEY_DAMAGE_DEALT = "damageDealt";

    private String Value = "";

    public MyCapabilityImplementation(Level p_36114_, BlockPos p_36115_, float p_36116_, GameProfile p_36117_) {
        super(p_36114_, p_36115_, p_36116_, p_36117_);
    }

    @Override
    public String getValue() {
        return this.Value;
    }
  .......

    @Override
    public void setValue(String Value) {
        this.Value = Value;
    }

And fill in the code to be added from here?

I wonder how to get a gameplofile...

Link to comment
Share on other sites

2 hours ago, ocome said:

I'm trying to add some functionality to the Player.

the first way is the correct one, just create a constructor with the required parameters (so in your case with the player),
when you attach the CapabilityProvider to the player, you need to pass in the player as parameter

btw you can get the Level from the Player (also note there are more than 1 Level),
the GameProfile is bind so you can also get it from the Player

Link to comment
Share on other sites

I appreciate again .

16 hours ago, Luis_ST said:

when you attach the CapabilityProvider to the player, you need to pass in the player as parameter

    @SubscribeEvent
    public static void onAttachingCapabilities(final AttachCapabilitiesEvent<Entity> event) {
        final MyCapabilityProvider provider = new MyCapabilityProvider();
       String string =  event.getObject().getType().toShortString();
    if (string == "player") {
        event.addCapability(MyCapabilityProvider.IDENTIFIER, provider);
    }

    }

I filled it out like this from the answer, but it doesn't seem to work

 

I tried to check it after getting the player element from AttachCapabilitiesEvent<Entity>.
Am I wrong?

    public static void onAttachingCapabilities(final AttachCapabilitiesEvent<Player> event) {
        final MyCapabilityProvider provider = new MyCapabilityProvider();

Reading the documentation, you can't do this, right?

 

16 hours ago, Luis_ST said:

btw you can get the Level from the Player (also note there are more than 1 Level),
the GameProfile is bind so you can also get it from the Player

Oh, sure, I checked the Player and it's there. So does that mean I can use AT to get it
Thank you very much.

Edited by ocome
Link to comment
Share on other sites

2 hours ago, ocome said:

I filled it out like this from the answer, but it doesn't seem to work

AttachCapabilitiesEvent#grtObject retunrns in your case a Entity so why did you not use a instanceof check 

18 hours ago, Luis_ST said:

just create a constructor with the required parameters (so in your case with the player)

you haven't done this

2 hours ago, ocome said:

Oh, sure, I checked the Player and it's there. So does that mean I can use AT to get it

There is Player#getCommandSenderWorld, you don't need a AT

Link to comment
Share on other sites

15 hours ago, Luis_ST said:

AttachCapabilitiesEvent#grtObject retunrns in your case a Entity so why did you not use a instanceof check 

    @SubscribeEvent
    public static void onAttachingCapabilities(final AttachCapabilitiesEvent<Entity> event) {
        final MyCapabilityProvider provider = new MyCapabilityProvider();

       boolean iof =  event.getObject()  instanceof Player;
       if (iof ==true) {
           event.addCapability(MyCapabilityProvider.IDENTIFIER, provider);
       }


    }

We have implemented it this way.
I think works fine in  tests.

15 hours ago, Luis_ST said:

you haven't done this

 MyCapabilityProvider

    private static class MyCapabilityProvider implements ICapabilityProvider, INBTSerializable<CompoundTag> {

        public static final ResourceLocation IDENTIFIER = new ResourceLocation(ExampleMod.MOD_ID, "tutorial");


        public Player player ;
        public Level level = player.getCommandSenderWorld();
        public BlockPos blockPos = player.blockPosition();
        public GameProfile gameProfile =player.getGameProfile();


        private final MyCapabilityInterface backend = new MyCapabilityImplementation(level,blockPos,0.0F, gameProfile);
      

MyCapabilityImplementation

public class MyCapabilityImplementation extends Player implements MyCapabilityInterface {
    protected ManaData manaData = new ManaData();
    private static final String NBT_KEY_DAMAGE_DEALT = "damageDealt";

    private String Value = "";

    public MyCapabilityImplementation(Level p_36114_, BlockPos p_36115_, float p_36116_, GameProfile p_36117_) {
        super(p_36114_, p_36115_, p_36116_, p_36117_);
    }

I think I understand what I can get.

But I don't know where to get the Player itself.

 

Link to comment
Share on other sites

public class MyCapabilityAttacher {
  private class MyCapabilityProviderEntity implements ICapabilityProvider, INBTSerializable<CompoundTag> {
          public static final ResourceLocation IDENTIFIER = new ResourceLocation(ExampleMod.MOD_ID, "tutorial");
          private final MyCapabilityInterface backend = new MyCapabilityImplementation();
          private final LazyOptional<MyCapabilityInterface> optionalData = LazyOptional.of(() -> backend);

public class MyCapabilityImplementation  implements MyCapabilityInterface {
    protected ManaData manaData = new ManaData();
    private static final String NBT_KEY_DAMAGE_DEALT = "damageDealt";
    public Level level;
    private String Value = "";

    public static ManaData getManaData() {
        return new ManaData();
    }

    @Override
    public void tick() {
        if (!this.level.isClientSide) {
            this.manaData.tick(this);
        }
    }

ManaOverlay

    public static final ManaOverlay INSTANCE = new ManaOverlay(Minecraft.getInstance());

    public ManaOverlay(Minecraft p_93005_) {
        super(p_93005_);

        this.mc = Minecraft.getInstance();
        this.font = mc.font;
        OverlayRegistry.registerOverlayTop("Mana ", (gui, mStack, partialTicks, screenWidth, screenHeight) -> {
            boolean isMounted = gui.minecraft.player.getVehicle() instanceof LivingEntity;
            if (!isMounted && !gui.minecraft.options.hideGui && gui.shouldDrawSurvivalElements())
            {
                setupOverlayRenderState(true, false);
                renderMana(screenWidth, screenHeight, mStack);
            }
        });
    }

    public static void init()
    {
        MinecraftForge.EVENT_BUS.register(INSTANCE);
    }

    public void renderMana(int width, int height, PoseStack mStack)
    {
        minecraft.getProfiler().push("mana");

        Player player = (Player)this.minecraft.getCameraEntity();
        RenderSystem.enableBlend();
        int left = width / 2 + 91;
        int top = height - right_height ;

        boolean unused = false;

        ManaData stats = MyCapabilityImplementation.getManaData();
        int level = stats.getManaLevel();

        for (int i = 0; i < 10; ++i)
        {
            int idx = i * 2 + 1;
            int x = left - i * 8 - 9;
            int y = top;
            int icon = 16;
            byte background = 0;

            if (minecraft.player.hasEffect(MobEffects.HUNGER))
            {
                icon += 36;
                background = 13;
            }
            if (unused) background = 1; //Probably should be a += 1 but vanilla never uses this
            blit(mStack, x, y, 16 + background * 9, 27, 9, 9);

            if (idx < level)
                blit(mStack, x, y, icon + 9, 27, 9, 9);
            else if (idx == level)
                blit(mStack, x, y, icon + 18, 27, 9, 9);
        }
        RenderSystem.disableBlend();
        minecraft.getProfiler().pop();
    }

I created it this way, but there is no change in the overlay.

I think the tick is not being called, but before that, I can't seem to fix the static mismatch between Capability and overlay.
Is there a way to fix this?

 

 

No, something wrong with the way I make the overlay side ?
I'll look at it some more.

Edited by ocome
Fill in the middle of the process
Link to comment
Share on other sites

        LazyOptional<MyCapabilityInterface> stats = minecraft.player.getCapability(MyCapability.INSTANCE);
        int level = ManaData.getManaLevel();

Is this what it looks like?
To be honest, I don't really understand it.

 

 

I feel like I don't have enough skills yet.
I'm still going to continue creating it, but I'm thinking I should understand it a little better before I start working on it.

Edited by ocome
Link to comment
Share on other sites

3 hours ago, ocome said:
LazyOptional<MyCapabilityInterface> stats = minecraft.player.getCapability(MyCapability.INSTANCE);

yeah that's correct if your Capability is client side only, but at the moment you never use the Capability

Link to comment
Share on other sites

11 minutes ago, Luis_ST said:

yeah that's correct if your Capability is client side only, but at the moment you never use the Capability

Yes, I understand that.
But I don't know how to utilize this.
Do I have to get the values related to Manadata from stats?

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




×
×
  • Create New...

Important Information

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