Jump to content

Itemstack based Cooldown Tracker


Babelincoln1809

Recommended Posts

I'm trying to make an item that has a cool down that is itemstack based, not player based, so that it can be dual-wielded without creating a duplicate item that just creates clutter.  I created a capability, and it works, but the issue is trying to apply the cool down, or using any method from the CooldownTracker class. Basically crashes the game. Any idea on what the issue could be or just something that achieves something similar that I can reference would be much appreciated

 

Code (Both Pastebin and Hastebin aren't working rn for some reason)

Spoiler

public class BoltcasterItem extends ShootableItem implements IVanishable, ICoolDown {

    private final double damage;
    private final int velocity;
    public int ticks = 30;
    public CooldownTracker tracker;
    public boolean cooldown = false;

    public BoltcasterItem(double damageIn,int velocityIn, Properties builder) {
        super(builder);
        this.damage = damageIn;
        this.velocity = velocityIn;

        ItemModelsProperties.registerProperty(this, new ResourceLocation("loaded"), (stack, world, player) -> {
            if(player != null)
                return player.isHandActive() && player.getActiveItemStack() == stack ? 1.0F : player.findAmmo(stack).getCount();
            else
                return 0.0F;
        });
    }
    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        ItemStack stack = playerIn.getHeldItem(handIn);
        boolean flag = playerIn.abilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY, stack) > 0;
        ItemStack arrowstack = playerIn.findAmmo(stack);

        if (!arrowstack.isEmpty() || flag) {
            if (arrowstack.isEmpty()) {
                arrowstack = new ItemStack(Items.ARROW);
            }
        }

        float f = getArrowVelocity(10 + this.velocity);

        boolean flag1 = playerIn.abilities.isCreativeMode || (arrowstack.getItem() instanceof ArrowItem && ((ArrowItem) arrowstack.getItem()).isInfinite(arrowstack, stack, playerIn));

        boolean flag2 = !playerIn.findAmmo(stack).isEmpty();

        if (!worldIn.isRemote) {
            if (!playerIn.abilities.isCreativeMode && !flag2) {
                worldIn.playSound((PlayerEntity) null, playerIn.getPosX(), playerIn.getPosY(), playerIn.getPosZ(), SoundEvents.BLOCK_DISPENSER_FAIL, SoundCategory.NEUTRAL, 1.0F, 1.5F);
            } else {

                ArrowItem arrowitem = (ArrowItem) (arrowstack.getItem() instanceof ArrowItem ? arrowstack.getItem() : Items.ARROW);
                AbstractArrowEntity abstractarrowentity = arrowitem.createArrow(worldIn, arrowstack, playerIn);
                abstractarrowentity = customArrow(abstractarrowentity);
                abstractarrowentity.func_234612_a_(playerIn, playerIn.rotationPitch, playerIn.rotationYaw, 0.0F, f * 3.0F, 1.0F);
                abstractarrowentity.setDamage(abstractarrowentity.getDamage() + this.damage);
                if (f == 1.0F) {
                    abstractarrowentity.setIsCritical(true);
                }

                int j = EnchantmentHelper.getEnchantmentLevel(Enchantments.POWER, stack);
                if (j > 0) {
                    abstractarrowentity.setDamage(abstractarrowentity.getDamage() + (double) j * 0.5D + 0.5D);
                }

                int k = EnchantmentHelper.getEnchantmentLevel(Enchantments.PUNCH, stack);
                if (k > 0) {
                    abstractarrowentity.setKnockbackStrength(k);
                }

                if (EnchantmentHelper.getEnchantmentLevel(Enchantments.FLAME, stack) > 0) {
                    abstractarrowentity.setFire(100);
                }

                stack.damageItem(1, playerIn, (p_220009_1_) -> {
                    p_220009_1_.sendBreakAnimation(playerIn.getActiveHand());
                });
                if (flag1 || playerIn.abilities.isCreativeMode && (arrowstack.getItem() == Items.SPECTRAL_ARROW || arrowstack.getItem() == Items.TIPPED_ARROW)) {
                    abstractarrowentity.pickupStatus = AbstractArrowEntity.PickupStatus.CREATIVE_ONLY;
                }

                worldIn.playSound((PlayerEntity) null, playerIn.getPosX(), playerIn.getPosY(), playerIn.getPosZ(), SoundEvents.ITEM_CROSSBOW_SHOOT, SoundCategory.NEUTRAL, 1.0F, 1.5F);
                worldIn.addEntity(abstractarrowentity);
            }

            if (!flag1 && !playerIn.abilities.isCreativeMode) {
                arrowstack.shrink(1);
                if (arrowstack.isEmpty()) {
                    playerIn.inventory.deleteStack(arrowstack);
                }
            }
        }

        ActionResult<ItemStack> ret = net.minecraftforge.event.ForgeEventFactory.onArrowNock(stack, worldIn, playerIn, handIn, flag);
        if (ret != null) return ret;

        if (!playerIn.abilities.isCreativeMode && !flag2) {

            stack.getCapability(CoolDownCapability.COOLDOWN_CAPABILITY).map(h -> {
                boolean canCoolDown = h.getCanCoolDown();

                //want to put a cool down tracker that isn't player side here, so that the cool down is separate among itemstacks, but crashes no matter what I put here

                ProjectDawn.LOGGER.info("empty side");

                return canCoolDown;

            }).orElse(null);


            return ActionResult.resultFail(stack);
        } else {
            playerIn.setActiveHand(handIn);


            stack.getCapability(CoolDownCapability.COOLDOWN_CAPABILITY).map(h -> {
                boolean canCoolDown = h.getCanCoolDown();

                //want to put a cool down tracker that isn't player side here, so that the cool down is separate among itemstacks, but crashes no matter what I put here

                ProjectDawn.LOGGER.info("loaded side");

                return canCoolDown;

            }).orElse(null);


            return ActionResult.resultConsume(stack);
        }
    }

    public static float getArrowVelocity(int charge) {
        float f = (float)charge / 20.0F;
        f = (f * f + f * 2.0F) / 3.0F;
        if (f > 1.0F) {
            f = 1.0F;
        }

        return f;
    }
    public AbstractArrowEntity customArrow(AbstractArrowEntity arrow) {
        return arrow;
    }

    @Override
    public Predicate<ItemStack> getInventoryAmmoPredicate() {
        return ARROWS;
    }

    @Override
    public int func_230305_d_() { return 8; }

    @Override
    public void canCoolDown(boolean canCoolDownIn) { this.cooldown = canCoolDownIn; }

    @Override
    public boolean getCanCoolDown() { return this.cooldown; }

    @Override
    public void setTracker(CooldownTracker tracker) { this.tracker = tracker; }

    @Override
    public CooldownTracker getTracker() { return tracker; }
}

 

 

Link to comment
Share on other sites

4 minutes ago, diesieben07 said:

Please show it. The item class you posted does not use capabilities.

Here's the capability: 

Spoiler

public interface ICoolDown
{
    void canCoolDown(boolean canCoolDownIn);

    boolean getCanCoolDown();

    void setTracker(CooldownTracker tracker);

    CooldownTracker getTracker();
}

 

 

Link to comment
Share on other sites

1 hour ago, diesieben07 said:

That is an interface which your item implements. That is not what a capability is.

Refer to the docs: https://mcforge.readthedocs.io/en/latest/datastorage/capabilities/

Oh shoot sorry! Didn't realize I sent that one by accident, was in a bit of a rush earlier. Here's the capability class

Spoiler

public class CoolDownCapability
{
    @CapabilityInject(ICoolDown.class)
    public static Capability<ICoolDown> COOLDOWN_CAPABILITY = null;

    public static void register()
    {
        CapabilityManager.INSTANCE.register(ICoolDown.class, new Storage(), DefaultCoolDownCapability::new);
    }

    public static class Storage implements Capability.IStorage<ICoolDown>
    {
        @Nullable
        @Override
        public INBT writeNBT(Capability<ICoolDown> capability, ICoolDown instance, Direction side) {
            CompoundNBT tag = new CompoundNBT();
            tag.putBoolean("canCoolDown", instance.getCanCoolDown());
            return tag;
        }

        @Override
        public void readNBT(Capability<ICoolDown> capability, ICoolDown instance, Direction side, INBT nbt) {
            boolean canCoolDown = ((CompoundNBT)nbt).getBoolean("coolDown");
            instance.canCoolDown(canCoolDown);
        }
    }
}

 

 

Link to comment
Share on other sites

46 minutes ago, diesieben07 said:

And where do you add it to your item? And where do you use it? Why does your Item implement ICoolDown?

I add it here, and originally used it here too. Thinking about it, yeah it really doesn't make sense for me to implement it into my item

Spoiler


@Mod.EventBusSubscriber(modid = ProjectDawn.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class CoolDownHandler
{
    public CoolDownHandler() {  }

    @SubscribeEvent
    public static void onAttachCapabilities(AttachCapabilitiesEvent<ItemStack> event) {
        if (event.getObject().getItem() instanceof BoltcasterItem)
        {
                CoolDownProvider provider = new CoolDownProvider();
                event.addCapability(new ResourceLocation(ProjectDawn.MOD_ID, "cooldown"), provider);
                event.addListener(provider::invalidate);
    }

    }

}

 

I'm gonna make it be used there, like I originally had it, instead of in the class. I guess cause I was looking around other classes and thought something in the line of IVanishable it needed to be implemented in my item class

Edited by Babelincoln1809
Link to comment
Share on other sites

4 hours ago, diesieben07 said:

There is no reason to use the event for your own items. Simply override initCapabilities.

Okay, so I override that and just attach the capability through there in the item class?

 

[Edit]

I got it to work by doing that, so how do I fix the cooldown issue exactly?

Edited by Babelincoln1809
Link to comment
Share on other sites

11 minutes ago, diesieben07 said:

Post your updated code.

Here's the boltcaster class: https://pastebin.com/yKTnnjif

And Here's the default capability: https://pastebin.com/w3yRJDGP

I referenced how the PlayerEntity creates it's cool down tracker, and that seems to have fixed the crashing issue I had yesterday, but other than that I really can't put a finger on why it just simply doesn't work. I looked around more in the PlayerEntity class and what uses that method to see if it has something that I don't, but I haven't found anything that could resolve it

 

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.



×
×
  • Create New...

Important Information

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