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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hi, I've been having trouble trying to use forge as it shows a black screen when I open the game, but I can still interact with it and hear the music.  I've done all of the step by steps and most common fixes like updating drivers, keeping up to date with Java, deleting and reinstalling minecraft, restarting my computer MANY times, even smaller things like splash.properties (I didn't have that file so I added it and set it to false thinking it would do something, definitely not) and making sure to prioritize my rtx 3070 in the settings but with no luck. Minecraft works as intended when I uninstall forge and I also don't have any mods currently, it just gives me this issue when I install forge. I also increased the ram usage, made sure my hardware isn't full or anything, and even changed the resolution in hopes it would fix things. I checked my antivirus and firewall but that isn't the issue either. Trust me, I've done everything I can think of. For some reason the black screen does flicker a little into the main menu, but obviously unplayable. I couldn't even make my way to the settings with how little it flickered. I'm not sure if it flickered randomly or if it was because I was messing around moving and clicking a bunch, I didn't really test it that much.  
    • I've had a really weird issue recently,  I wanted to add the Depper and Darker mod on my dedicated server (MC 1.21 with Fabric 0.16.9, hosted on nitroserv.com) but whenever I do add the mod the sever stops doing anything after listing the mods, and I get no crash or error or anything, just a stuck server. Here's a normal log of the server booting up: https://pastebin.com/JipFF2Eh and here's the log of the server doing the weird thing: https://pastebin.com/W4JBh3eX I just don't understand it. I've tried removing other mods (somewhat randomly) but deeper and darker still breaks my server whenever I add it. NitroServ support staff is about as confused as I am and I've had no response from the Deeper and Darker support staff... Now I know this is the Forge support not the Fabric support but I'm just trying to know if anyone has any kind of idea to fix this (aside from not using the mod obviously) Also I still have a bunch of errors and warnings whenever the server does start properly, are there any of them I should be worried about?
    • Delete the config of RandomTweaker (config folder) If there is no change, remove this mod
    • Hello! So i have been trying to make a mod that adds plant fiber to minecraft 1.16.5 (i believe there are mods that add plant fiber but not for 1.16.5) but the problem is that i want to modify the loot table of grass to always drop plant fiber but also keep the vanilla drops. Most common answer i have seen is GlobalLootModifiers. But my tiny brain cant understand any tutorials. So any help is appreciated.
    • Minecraft forge 1.12.2 does not load. Here is both logs. I assume its because of a mod that i have, idk. (L521)
  • Topics

×
×
  • Create New...

Important Information

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