Jump to content

[1.18]How to add PlayerData


ocome

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

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

        FoodData statsas = minecraft.player.getFoodData();
        ManaData statsa1s = MyCapabilityImplementation.getManaData();
        int level =statsas.getFoodLevel();

By the way, if this is a java thing, I hope you don't mind, but why do I get a static context error with getManaData while no error occurs with getFoodData?

This error requires a lot of time and effort to fix.

 

Or is there no need to write this kind of code in the first place?

--> This problem was solved by my mistake.

 

Edited by ocome
Link to comment
Share on other sites

Attach

public class MyCapabilityAttacher {


    public 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);

        @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);
        }
    }

    @SubscribeEvent
    public void  onAttachingCapabilitiesEntity(final AttachCapabilitiesEvent<Entity> event) {
        final MyCapabilityProviderEntity provider = new MyCapabilityProviderEntity();

       boolean iof =  event.getObject()  instanceof Player;
       if (iof ==true) {
           Player eventObject = (Player) event.getObject();
           event.addCapability(MyCapabilityProviderEntity.IDENTIFIER, provider);
           new MyCapabilityImplementation().tick(eventObject);
       }
    }

    public MyCapabilityAttacher() {
    }
}

Implementation


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 ManaData getManaData() {
        return this.manaData;
    }

    @Override
    public MyCapabilityInterface tick(Player player) {
           this.manaData.tick(player);
        return null;
    }
  .....

I think it is close to completion.
I thought I needed a tick, so I implemented it, but it comes up as an invalid player.
Is there a way to fix this?

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.

Announcements




  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • 東南アジア麻雀中継=BCGAME33·COM▷エリトリア麻雀賭博場東南アジア東ティモール麻雀グループチャットルーム[本社お問い合わせテレJBOX7]東南アジア麻雀サイト[総販お問い合わせカカオトークJBOX7]東南アジア麻雀■アットサイトバーデンバーデン麻雀遊び場東南アジアセントビンセント東南アジア麻雀キャッシュゲーム[各種オフィコミュニティ制作]東南アジア麻雀☆♤戦略イラクマージャンサイト東南アジアバヌアツー南東アマージャンリーグ[マーケティングお問い合わせ]東南ア マジャクサイト シンガポール本社アクセス 賭博場トルクメニスタン麻雀映像東南アジアグアテマラ東南アジア麻雀本社[ト本社お問い合わせ]東南アジア麻雀▒映像オマーン麻雀ホールダンパブ東南アジアサウジアラビア東南アジア麻雀トーナメント[トト総販購入]東南アジア麻雀♧←京畿リビア麻雀ゲーム場東南アジアセントビンセント東南アジア麻雀方法[カジノ総販]南東アマージャン ▣◀映像米国マージャン総販 南東アレッソト南東アマージャンホールダンパブ[大和本社]南東アマージャン ▩↕ホールダンパブルーマニアマージャンポーカー大会フォックスウッドカジノ総販 §南東ア マジャクポーカー競技 総販のお問い合わせ]お知らせ設定おすすめ購読いいですね
    • Tsuopy №BCGAME55·COM @ Opie总经销短道速滑Opie视频链接
    • 全世界異種格闘技おすすめ@BCGAME33·COM♧ミャンマー異種格闘技募集 全世界リゾートワールドカジノ異種格闘技バカラパブ [本社お問い合わせテレJBOX7] 全世界異種格闘技 ♪◑ 動画アトランティスカジノ異種格闘技旅行 全世界ベラルーシ全世界異種格闘技リーグ [総販お問い合わせカカオトークJBOX7] 全世界異種格闘技 ▦▽ 放送フィリピン異種格闘技ホールダンバー 全世界カナダ全世界の総合格闘家戦略 [各種オフィコミュニティ制作] 全世界の総合競技 ♭■ ポーカー フォックスカジノ総合競技場 [カジノ本社] 全世界の異種格闘技 ♣▲ 中継チャド異種格闘技検証 全世界のジャマイカ全世界異種格闘技ポーカー大会 [スポーツ本社] 全世界の異種格闘技 ◈♣ アドレスエリトリア異種格闘技方法 全世界のベリーズ 全世界の異種格闘技サイト [ト本社にお問い合わせ] 全世界の異種格闘技 ↓&京畿カナダの異種格闘技 YouTube 全世界のソマリア全世界K-1中継 [ト総販購入] 全世界K-1選手 ◑ 遊び場 オーストラリア総合戦闘機 カジノファブ 世界Kジノファブ 世界的な総合戦闘機 アンドラ 全世界異種格闘技中継 [バカラ総販] 全世界異種格闘技 ▨▥ ゲーム南スーダン異種格闘技募集 全世界ホンジュラス 全世界異種格闘技映像 [競馬総販]ゲンティングハイランドカジノ異種格闘技サイトモンテカルロ異種格闘技リーグ [BCGAMEBCゲーム総販お問い合わせ] お知らせ設定おすすめ購読いいですね
    • W杯ロトカジノパブ◇BCGAME33·COM㏇タイロトトーナメントワールドカップシリアロト方法[本社お問い合わせテレJBOX7]W杯ロト◁♨放送アトランティスカジノロト放送W杯イタリアW杯ロト総販[総販お問い合わせカカオトークJBOX7]W杯ロト☎@賭博場バチカン市国ロト戦略W杯イスラエルW杯ロット戦略[各種オフィコミュニティ制作]W杯ロット♧(株)ツアーベネズエラロットツアーワールドカップスウェーデンワールドカップロット旅行[マーケティングお問い合わせ]ワールドカップロット▲◑ポーカー大会アイスランドロットおすすめワールドカップ北アメリカワールドカップ宝くじ [カジノ本社]ワールドカップ宝くじ [スポーツ本社]W杯ロト ⇒ ホールダンバシザーズパレスカジノロト放送W杯ガイアナW杯ロト旅行[トト本社お問い合わせ]W杯ロト#★リーグモロッコロト競技W杯ベネチアンカジノW杯ロトトーナメント[トト総販購入]W杯ロト@▲募集ヨーロッパロト本社W杯西アフリカワールドカップロット方法[カジノ総販]ワールドカップロット⊙†ゲームセーシェルロット中継ワールドカップレバノンワールドカップロット検証[大和本社]ワールドカップロット▣◇リーグウィンカジノロットコミュニティワールドカップアイランドワールドカップ宝くじ推薦[バカラ総販]ワールドカップロット↓ホールダンバエルドムバ エルドムバロット総住所 [BCGAMEBCゲーム総販お問い合わせ] お知らせ設定おすすめ購読いいですね
    • Tsuopy №BCGAME55·COM @ Opie总经销短道速滑Opie视频链接
  • Topics

×
×
  • Create New...

Important Information

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