Jump to content

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


thebadscientist

Recommended Posts

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • thebadscientist changed the title to [SOLVED] [1.14.4] How do you use capabilities to store variables such as thirst in the player entity?
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)

Link to comment
Share on other sites

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

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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