Jump to content

Recommended Posts

Posted

I am trying to implement Mana.
If I can add it to the Player after creating the Manadata, I think I can create it
Is there a way to solve this or a class I can refer to?

Posted (edited)

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

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);
        }
    }
...
Posted (edited)
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
Posted

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

Posted
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

Posted (edited)

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

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

 

Posted (edited)
15 minutes ago, loordgek said:

why do you extend player ?

Extends to mimic the player's Fooddata, but not necessary?

I'll check it out.

Edited by ocome
Posted (edited)
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
Posted (edited)
        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
Posted
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

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

Posted
8 minutes ago, ocome said:

Do I have to get the values related to Manadata from stats?

yeah add the getManaLevel to the interface of your Capability and and implement it into your CapabilityHandler

  • Like 1
Posted (edited)
        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
Posted

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?

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

    • that happens every time I enter a new dimension.
    • This is the last line before the crash: [ebwizardry]: Synchronising spell emitters for PixelTraveler But I have no idea what this means
    • What in particular? I barely used that mod this time around, and it's never been a problem in the past.
    • Im trying to build my mod using shade since i use the luaj library however i keep getting this error Reason: Task ':reobfJar' uses this output of task ':shadowJar' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. So i try adding reobfJar.dependsOn shadowJar  Could not get unknown property 'reobfJar' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. my gradle file plugins { id 'eclipse' id 'idea' id 'maven-publish' id 'net.minecraftforge.gradle' version '[6.0,6.2)' id 'com.github.johnrengelman.shadow' version '7.1.2' id 'org.spongepowered.mixin' version '0.7.+' } apply plugin: 'net.minecraftforge.gradle' apply plugin: 'org.spongepowered.mixin' apply plugin: 'com.github.johnrengelman.shadow' version = mod_version group = mod_group_id base { archivesName = mod_id } // Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. java.toolchain.languageVersion = JavaLanguageVersion.of(17) //jarJar.enable() println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" minecraft { mappings channel: mapping_channel, version: mapping_version copyIdeResources = true runs { configureEach { workingDirectory project.file('run') property 'forge.logging.markers', 'REGISTRIES' property 'forge.logging.console.level', 'debug' arg "-mixin.config=derp.mixin.json" mods { "${mod_id}" { source sourceSets.main } } } client { // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. property 'forge.enabledGameTestNamespaces', mod_id } server { property 'forge.enabledGameTestNamespaces', mod_id args '--nogui' } gameTestServer { property 'forge.enabledGameTestNamespaces', mod_id } data { workingDirectory project.file('run-data') args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') } } } sourceSets.main.resources { srcDir 'src/generated/resources' } repositories { flatDir { dirs './libs' } maven { url = "https://jitpack.io" } } configurations { shade implementation.extendsFrom shade } dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" implementation 'org.luaj:luaj-jse-3.0.2' implementation fg.deobf("com.github.Virtuoel:Pehkui:${pehkui_version}") annotationProcessor 'org.spongepowered:mixin:0.8.5:processor' minecraftLibrary 'luaj:luaj-jse:3.0.2' shade 'luaj:luaj-jse:3.0.2' } // Example for how to get properties into the manifest for reading at runtime. tasks.named('jar', Jar).configure { manifest { attributes([ 'Specification-Title' : mod_id, 'Specification-Vendor' : mod_authors, 'Specification-Version' : '1', // We are version 1 of ourselves 'Implementation-Title' : project.name, 'Implementation-Version' : project.jar.archiveVersion, 'Implementation-Vendor' : mod_authors, 'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"), "TweakClass" : "org.spongepowered.asm.launch.MixinTweaker", "TweakOrder" : 0, "MixinConfigs" : "derp.mixin.json" ]) } rename 'mixin.refmap.json', 'derp.mixin-refmap.json' } shadowJar { archiveClassifier = '' configurations = [project.configurations.shade] finalizedBy 'reobfShadowJar' } assemble.dependsOn shadowJar reobf { re shadowJar {} } publishing { publications { mavenJava(MavenPublication) { artifact jar } } repositories { maven { url "file://${project.projectDir}/mcmodsrepo" } } } my entire project:https://github.com/kevin051606/DERP-Mod/tree/Derp-1.0-1.20
  • Topics

×
×
  • Create New...

Important Information

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