Jump to content

[1.16.5] Capability cannot be casted


Tener

Recommended Posts

Iam using two mods and they are crashing
 

java.lang.ClassCastException: nonamecrackers2.witherstormmod.client.capability.PlayerScreenBlinder cannot be cast to com.bottomtextdanny.dannys_expansion.core.capabilities.player.DannyEntityCap


So iam trying to fix both with mixin, but i legit cant find the reason since i never had a Capability crash on my mods , that is how they registry a capabilitie

 

@EventBusSubscriber(
    modid = "dannys_expansion"
)
public class DannyEntityCap implements INBTSerializable<CompoundNBT>, ICapabilityProvider {
    private final AccessoryInventory accessories = new AccessoryInventory(5);
    public boolean accessoriesNeedSync;
    private float recoil;
    private float prevRecoil;
    private float recoilSubstract;
    private float recoilMultiplier;
    private int holdableProgression;
    private int prevHoldableProgression;
    @Nullable
    public ItemStack dannyActiveItemstack;
    private int itemSlot;
    @CapabilityInject(DannyEntityCap.class)
    public static Capability<DannyEntityCap> GUN_STABILITY = null;

    public DannyEntityCap() {
    }

    public float getPrevRecoil() {
        return this.prevRecoil;
    }

    public void setPrevRecoil(float f) {
        this.prevRecoil = f;
    }

    public float getRecoilMult() {
        return this.recoilMultiplier;
    }

    public void setRecoilMult(float f) {
        this.recoilMultiplier = f;
    }

    public float getRecoilSub() {
        return this.recoilSubstract;
    }

    public void setRecoilSub(float f) {
        this.recoilSubstract = f;
    }

    public float getRecoil() {
        return this.recoil;
    }

    public void setRecoil(float f) {
        this.recoil = f;
    }

    public int getHoldableProgression() {
        return this.holdableProgression;
    }

    public void setHoldableProgression(int i) {
        this.holdableProgression = i;
    }

    public int getItemSlot() {
        return this.itemSlot;
    }

    public void setItemSlot(int i) {
        this.itemSlot = i;
    }

    public int getPrevHoldableProgression() {
        return this.prevHoldableProgression;
    }

    public void setPrevHoldableProgression(int i) {
        this.prevHoldableProgression = i;
    }

    public AccessoryInventory getAccessories() {
        return this.accessories;
    }

    public void setAccessory(int index, ItemStack itemStack) {
        this.accessories.setItem(index, itemStack);
    }

    @Nonnull
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
        return cap == GUN_STABILITY ? LazyOptional.of(() -> {
            return this;
        }).cast() : LazyOptional.empty();
    }

    public CompoundNBT serializeNBT() {
        CompoundNBT nbt = new CompoundNBT();
        nbt.putInt("player_item_progression", this.getHoldableProgression());
        nbt.putInt("player_item", this.getItemSlot());

        for(int i = 0; i < this.accessories.getContainerSize(); ++i) {
            CompoundNBT itemNBT = this.accessories.getItem(i).serializeNBT();
            CompoundNBT accessoryNBT = ((Accessory)this.accessories.getAccessoryInstances().get(i)).write();
            nbt.put("accessory_item_" + i, itemNBT);
            nbt.put("accessory_" + i, accessoryNBT);
        }

        return nbt;
    }

    public void deserializeNBT(CompoundNBT nbt) {
        this.setRecoil((float)nbt.getInt("player_item_progression"));
        this.setItemSlot(nbt.getInt("player_item"));

        for(int i = 0; i < this.accessories.getContainerSize(); ++i) {
            ItemStack stack = ItemStack.EMPTY;
            INBT itemNBT = nbt.get("accessory_item_" + i);
            if (itemNBT != null) {
                stack = ItemStack.of((CompoundNBT)itemNBT);
            }

            this.accessories.setItem(i, stack);
            ((Accessory)this.accessories.getAccessoryInstances().get(i)).read(nbt.getCompound("accessory_" + i));
        }

    }
}

 

And other

 

@SubscribeEvent
    public static void attachEntityCapabilities(AttachCapabilitiesEvent<Entity> event) {
        Entity entity = (Entity)event.getObject();
        if (entity instanceof ClientPlayerEntity) {
            ClientPlayerEntity player = (ClientPlayerEntity)entity;
            final LazyOptional<PlayerCameraShaker> cameraShaker = LazyOptional.of(() -> {
                return new PlayerCameraShaker(player);
            });
            event.addCapability(new ResourceLocation("witherstormmod", "camera_shaker"), new ICapabilityProvider() {
                public <T> LazyOptional<T> getCapability(Capability<T> capability, Direction side) {
                    return capability == WitherStormModClientCapabilities.CAMERA_SHAKER ? cameraShaker.cast() : LazyOptional.empty();
                }
            });
            event.addListener(cameraShaker::invalidate);
            final LazyOptional<PlayerScreenBlinder> screenBlinder = LazyOptional.of(PlayerScreenBlinder::new);
            event.addCapability(new ResourceLocation("witherstormmod", "blinder"), new ICapabilityProvider() {
                public <T> LazyOptional<T> getCapability(Capability<T> capability, Direction side) {
                    return capability == WitherStormModClientCapabilities.SCREEN_BLINDER ? screenBlinder.cast() : LazyOptional.empty();
                }
            });
            event.addListener(screenBlinder::invalidate);
        }

    }

 

Erros happens on calls like that

 

DannyEntityCap capability = (DannyEntityCap)entityIn.getCapability(DannyEntityCap.GUN_STABILITY).orElse((Object)null);

 

Both codes are open sourced, just want to fix and run on my modpack.

Edited by Tener
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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