Jump to content

[SOLVED] [1.14.4] How do you use capabilities to store variables such as thirst in the player entity?


Recommended Posts

Posted
5 hours ago, diesieben07 said:

What about the documentation is lacking? What have you tried?

that's for 1.13, which is outdated.

for instance, 1.14 does not have the hasCapability method from ICapabilityProvider and makes no mention of LazyOptional.

 

what I did is tried looking at source code from 1.12 and 1.13 mods and see if I could adapt the code to 1.14 but it didn't end too well.

Posted

Post your code.

1.14 hasn’t changed much from 1.13.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Posted
On 1/3/2020 at 6:26 AM, DavidM said:

Post your code.

1.14 hasn’t changed much from 1.13.

It's an attempt to adapt from 1.12 not 1.13

 

CustomClass:

Spoiler

public class CustomClass implements ICustomClass{

    private int counter;
    private byte version;

    public CustomClass(){
        this.counter = 20;
        this.version = (byte) 1;
    }

    @Override
    public void setVersion(byte version) {
        this.version = version;
    }

    @Override
    public byte getVersion() {
        return version;
    }

    @Override
    public void setCounter(int value) {
        this.counter = value;
    }

    @Override
    public int getCounter() {
        return counter;
    }

    @Override
    public void copyForRespawn(ICustomClass deadPlayer) {
        this.setCounter(deadPlayer.getCounter());
    }

    public static ICustomClass getFromPlayer(PlayerEntity player){
        return player.getCapability(PlayerDispatcher.PLAYER_COUNTER, null).orElseThrow(() -> new IllegalArgumentException(("LazyOptional must not be empty!")));
    }
}

 

PlayerDispatcher:

Spoiler

public class PlayerDispatcher implements ICapabilitySerializable<CompoundNBT> {

    @CapabilityInject(ICustomClass.class)
    public static final Capability<ICustomClass> PLAYER_COUNTER = null;

    private LazyOptional<ICustomClass> instance;

    @Override
    public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
        if (cap != PLAYER_COUNTER)
            return LazyOptional.empty();
        if (this.instance == null){
            this.instance = LazyOptional.of(PLAYER_COUNTER::getDefaultInstance);
        }
        return this.instance.cast();
    }

    @Override
    public CompoundNBT serializeNBT() {
        return (CompoundNBT) PLAYER_COUNTER.getStorage().writeNBT(PLAYER_COUNTER, this.getCapability(PLAYER_COUNTER, null).orElse(null), null);
    }

    @Override
    public void deserializeNBT(CompoundNBT nbt) {
        PLAYER_COUNTER.getStorage().readNBT(PLAYER_COUNTER, this.getCapability(PLAYER_COUNTER, null).orElse(null), null, nbt);
    }
}

 

CounterStorage:

Spoiler

public class CounterStorage implements Capability.IStorage<ICustomClass> {

    @Override
    public INBT writeNBT(Capability<ICustomClass> capability, ICustomClass instance, Direction side) {
        CompoundNBT tag = new CompoundNBT();
        tag.putInt("counter", instance.getCounter());
        tag.putByte("version", instance.getVersion());
        return tag;
    }

    @Override
    public void readNBT(Capability<ICustomClass> capability, ICustomClass instance, Direction side, INBT nbt) {
        CompoundNBT tag = (CompoundNBT) nbt;
        instance.setCounter(tag.getInt("counter"));
        instance.setVersion(tag.getByte("version"));
    }
}

 

PlayerPropertiesEvent:

Spoiler

public class PlayerPropertiesEvent {

    @SubscribeEvent
    public void onPlayerLogIn(net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent event){

        PlayerEntity player = event.getPlayer();
        World world = player.world;

        if (!world.isRemote){
            ICustomClass instance = CustomClass.getFromPlayer(player);
            Capability<ICustomClass> cap = PlayerDispatcher.PLAYER_COUNTER;
            cap.getStorage().readNBT(cap, instance, null, player.getEntityData().getCompound(cap.getName()));
        }
	}
                  @SubscribeEvent
    public void onEntityConstruction(AttachCapabilitiesEvent<Entity> event) {
        if (event.getObject() instanceof PlayerEntity){
            event.addCapability(new ResourceLocation(PracticeMod.MODID, "counter"), new PlayerDispatcher());
        }
    }

    @SubscribeEvent
    public void onPlayerCloned(PlayerEvent.Clone event){
        PlayerEntity deadPlayer = event.getOriginal();
        PlayerEntity newPlayer = event.getEntityPlayer();
        ICustomClass oldCounter = CustomClass.getFromPlayer(deadPlayer);
        ICustomClass newCounter = CustomClass.getFromPlayer(newPlayer);
        newCounter.copyForRespawn(oldCounter);
    }

    @SubscribeEvent
    public static void onPlayerTick(TickEvent.PlayerTickEvent event){
        PlayerEntity player = event.player;
        World world = player.world;

        if(!world.isRemote){
            if(event.phase == TickEvent.Phase.START){
                ICustomClass instance = CustomClass.getFromPlayer(player);
                Capability<ICustomClass> cap = PlayerDispatcher.PLAYER_COUNTER;
                player.getEntityData().put(cap.getName(), cap.getStorage().writeNBT(cap, instance, null));
            }
        }
    }
}

 

EventHandler: (this increases the value I want to store)

Spoiler

@Mod.EventBusSubscriber
public class EventHandler {

    @SubscribeEvent
    public static void onTest(PlayerInteractEvent.RightClickItem event) {

        PlayerEntity player = event.getEntityPlayer();
        ICustomClass cap = CustomClass.getFromPlayer(player);

        if (event.getItemStack().getItem() == Items.GUNPOWDER) {
            cap.setCounter(cap.getCounter() + 10);
            PracticeMod.LOGGER.info("new value is: " + cap.getCounter());
        }
    }

    @SubscribeEvent
    public static void onAirRightClick(PlayerInteractEvent.RightClickEmpty event) {

        PlayerEntity player = event.getEntityPlayer();
        ICustomClass cap = CustomClass.getFromPlayer(player);
        ItemStack item = player.getHeldItemMainhand();

        if (item.isEmpty()) {
            PracticeMod.LOGGER.info("Current value is: " + cap.getCounter());
        }
    }
}

 

 

Posted (edited)
6 hours ago, diesieben07 said:

This double lazy initialization is not necessary. The point of LazyOptional is that its lazy already (hence the name). No need to create the LazyOptional lazily.

I copied from some mod, oh well. So what should the getCapability look like then? I still don't quite understand LazyOptional tbh

6 hours ago, diesieben07 said:

Are you for real...?

My mistake, copied some old code, what I actually put was this, is this ok or is it bad as well?

this.getCapability(PLAYER_COUNTER, null).orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!"))

 

6 hours ago, diesieben07 said:

This makes zero sense. getEntityData has nothing to do with capabilities, this does nothing useful.

Ye probably shouldn't adapt code from two different mods (it's from ToughAsNails, which I thought could be useful)

Edited by thebadscientist
Posted
On 1/5/2020 at 8:54 PM, diesieben07 said:

It should just return a field from your class, which holds the LazyOptional. Create the LazyOptional in your constructor and put it in a final field.

The point of LazyOptional is to provide an optional value (it can be present or not) in a lazy way (i.e. only create the value once it's needed). That's why you give a Supplier to the LazyOptional instead of the actual value.

 

You can do that, but you could just as well just access the capability instance directly, stored in a field.

 

Don't take this mod's code for anything. I haven't seen anything good in it.

Alright, I think I got everything sorted except that the value I want to store gets reset to 0 everytime I log into the world:

[23:41:41.634] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: reading NBT!
[23:41:41.634] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: NBT Data: 120
[23:41:41.634] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: Instance Data: 120
[23:41:41.636] [Server thread/INFO] [minecraft/PlayerList]: Dev[local:E:2d267046] logged in with entity id 372 at (193.58322637936269, 72.0, -162.9556295622775)
[23:41:41.642] [Server thread/INFO] [minecraft/MinecraftServer]: Dev joined the game
[23:41:41.719] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: PLAYER LOGIN EVENT
[23:41:41.719] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: reading NBT!
[23:41:41.719] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: NBT Data: 0
[23:41:41.719] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: Instance Data: 0

Do you know what would cause this?

  • thebadscientist changed the title to [SOLVED] [1.14.4] How do you use capabilities to store variables such as thirst in the player entity?
Posted
2 hours ago, thebadscientist said:

nvm got it working.

How did you get it working, for other people with the same problem in the future?

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted
On 1/7/2020 at 2:31 AM, Cadiboo said:

How did you get it working, for other people with the same problem in the future?

You mean regarding the value resetting to 0 or regarding the whole capability implementation?

 

Either way, check my GitHub repository to see how I implemented it. 

https://github.com/deneth-weerasinghe/PracticeMod

Note the capability's stored value is increased by 10 when right-clicking water with a custom item I made.

  • Thanks 1

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



×
×
  • Create New...

Important Information

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