Jump to content

[SOLVED] [1.15.2] Trouble with capabilities...


Ghost20000

Recommended Posts

I'm trying to add a custom capability to an item that will store a string representing an EntityType, but it seems like the NBT functions keep overriding it! Am I missing something obvious here?

// Class that stores actual data for capability.
public class Pet {
  private EntityType<?> entityType;

  public Pet() {
    this.entityType = null;
  }

  public String getEntityTypeString() {
    if (this.entityType == null) {
      return "";
    }

    return EntityType.getKey(this.entityType).toString();
  }

  public EntityType<?> getEntityType() {
    return this.entityType;
  }

  public void setEntityType(EntityType<?> entityType) {
    this.entityType = entityType;
    Datamine.LOGGER.debug("setEntityType type: " + this.entityType);
  }

  public void setEntityType(String entityType) {
    this.entityType = EntityType.byKey(entityType).orElse(null);
    Datamine.LOGGER.debug("setEntityType string: " + this.entityType);
  }

  public void releaseEntity() {
    this.entityType = null;
  }

  public static class PetStorage implements IStorage<Pet> {
    @Override
    public void readNBT(Capability<Pet> capability, Pet instance, Direction side, INBT nbt) {
      if (nbt instanceof CompoundNBT) {
        instance.setEntityType(((CompoundNBT) nbt).getString("type"));
      }
    }

    @Override
    public INBT writeNBT(Capability<Pet> capability, Pet instance, Direction side) {
      CompoundNBT nbt = new CompoundNBT();
      nbt.putString("type", instance.getEntityTypeString());
      return nbt;
    }
  }

}
public class PetItem extends Item {
  public PetItem(Item.Properties properties) {
    super(properties);
  }

  @Override
  public boolean itemInteractionForEntity(ItemStack stack, PlayerEntity playerIn, LivingEntity target, Hand hand) {
    if (target.world.isRemote) {
      return false;
    }

    Pet pet = stack.getCapability(PetCapability.CAPABILITY_PET).orElse(null);

    if (pet == null) {
      return false;
    }

    if (target instanceof AnimalEntity) {
      Datamine.LOGGER.debug("AnimalEntity: " + target.getType());
      pet.setEntityType(target.getType());
      return true;
    }

    return false;
  }

  @Override
  public ActionResultType onItemUse(ItemUseContext context) {
    Datamine.LOGGER.debug("onItemUse: " + context.getItem().getCapability(PetCapability.CAPABILITY_PET).orElse(null).getEntityType());
    return super.onItemUse(context);
  }

  @Override
  public ICapabilityProvider initCapabilities(ItemStack stack, CompoundNBT nbt) {
    return new PetCapabilityProvider();
  }
}
public class PetCapabilityProvider implements ICapabilitySerializable<INBT> {
  private final Pet pet = new Pet();
  private final LazyOptional<Pet> petOptional = LazyOptional.of(() -> pet);

  @Override
  public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
    return petOptional.cast();
  }

  @Override
  public INBT serializeNBT() {
    return PetCapability.CAPABILITY_PET.writeNBT(this.pet, null);
  }

  @Override
  public void deserializeNBT(INBT nbt) {
    PetCapability.CAPABILITY_PET.readNBT(this.pet, null, nbt);
  }
}
public class PetCapability {
  @CapabilityInject(Pet.class)
  public static Capability<Pet> CAPABILITY_PET = null;

  public static void register() {
    CapabilityManager.INSTANCE.register(Pet.class, new Pet.PetStorage(), () -> new Pet());
  }
}

Thanks in advance!

Edited by Ghost20000
Link to comment
Share on other sites

56 minutes ago, diesieben07 said:

If this is your item, why are you using a capability for it?

Oh, is there a simpler way to store data in itemstacks? I'm not very proficient with forge (as you can probably tell lol).

56 minutes ago, diesieben07 said:

What do you mean by this?

It sets entityType, but when read/writeNBT are called it's back to null.

Link to comment
Share on other sites

29 minutes ago, diesieben07 said:

It works fine in survival mode. In creative mode, item stack changes during most item actions are completely ignored by the game (you are just acting on a copied ItemStack which will be discarded afterwards). If you want these changes to stick you need to check for creative mode and if it is active, call setHeldItem manually during your interaction handler.

Thank you so much!!!

Would've never thought that was the case, I'd assume that stuff like buckets have special creative mode checks, but I guess not!

Will mark as solved!

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



×
×
  • Create New...

Important Information

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