Weapon still shoots in Cooldown

Started by JustSoupTime on

Topic category: Help with modding (Java Edition)

Last seen on 03:07, 10. Aug 2021
Joined Aug 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Weapon still shoots in Cooldown

Hey so i struggled a while with making the draw speed of my range-weapon slower , now i got it with a command :

/give @p diamond_sword 1 0 {AttributeModifiers:[{AttributeName:"generic.attackSpeed",Name:"generic.attackSpeed",Amount:1000,Operation:0,UUIDLeast:1,UUIDMost:1}]}

its like 3 Seconds of drawing the Weapon , 

and in these 3 seconds i still can shoot perfectly , i want to make it that it wont shoot while drawing , or at least deal No Damage 

[Im using MCreator 2020.2]

Code of my weapon if needed : 

 

package net.mcreator.phimod.item;

import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.common.registry.EntityEntryBuilder;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.client.registry.RenderingRegistry;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.client.event.ModelRegistryEvent;

import net.minecraft.world.World;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.ActionResult;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Item;
import net.minecraft.item.EnumAction;
import net.minecraft.init.Enchantments;
import net.minecraft.entity.projectile.EntityTippedArrow;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.Entity;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.client.renderer.entity.RenderSnowball;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.Minecraft;

import net.mcreator.phimod.ElementsPhimod;

@ElementsPhimod.ModElement.Tag
public class ItemMp5A4 extends ElementsPhimod.ModElement {
    @GameRegistry.ObjectHolder("phimod:mp5a4")
    public static final Item block = null;
    public static final int ENTITYID = 38;
    public ItemMp5A4(ElementsPhimod instance) {
        super(instance, 28);
    }

    @Override
    public void initElements() {
        elements.items.add(() -> new RangedItem());
        elements.entities.add(() -> EntityEntryBuilder.create().entity(EntityArrowCustom.class)
                .id(new ResourceLocation("phimod", "entitybulletmp5a4"), ENTITYID).name("entitybulletmp5a4").tracker(64, 1, true).build());
    }

    @Override
    @SideOnly(Side.CLIENT)
    public void registerModels(ModelRegistryEvent event) {
        ModelLoader.setCustomModelResourceLocation(block, 0, new ModelResourceLocation("phimod:mp5a4", "inventory"));
    }

    @SideOnly(Side.CLIENT)
    @Override
    public void preInit(FMLPreInitializationEvent event) {
        RenderingRegistry.registerEntityRenderingHandler(EntityArrowCustom.class, renderManager -> {
            return new RenderSnowball(renderManager, new ItemStack(ItemPistolBullet.block, (int) (1)).getItem(),
                    Minecraft.getMinecraft().getRenderItem());
        });
    }
    public static class RangedItem extends Item {
        public RangedItem() {
            super();
            setMaxDamage(30);
            setFull3D();
            setUnlocalizedName("mp5a4");
            setRegistryName("mp5a4");
            maxStackSize = 1;
            setCreativeTab(CreativeTabs.COMBAT);
        }

        @Override
        public void onUsingTick(ItemStack itemstack, EntityLivingBase entityLivingBase, int count) {
            World world = entityLivingBase.world;
            if (!world.isRemote && entityLivingBase instanceof EntityPlayerMP) {
                EntityPlayerMP entity = (EntityPlayerMP) entityLivingBase;
                int slotID = -1;
                for (int i = 0; i < entity.inventory.mainInventory.size(); i++) {
                    ItemStack stack = entity.inventory.mainInventory.get(i);
                    if (stack != null && stack.getItem() == new ItemStack(ItemPistolBullet.block, (int) (1)).getItem()
                            && stack.getMetadata() == new ItemStack(ItemPistolBullet.block, (int) (1)).getMetadata()) {
                        slotID = i;
                        break;
                    }
                }
                if (entity.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY, itemstack) > 0
                        || slotID != -1) {
                    float power = 1.7f;
                    EntityArrowCustom entityarrow = new EntityArrowCustom(world, entity);
                    entityarrow.shoot(entity.getLookVec().x, entity.getLookVec().y, entity.getLookVec().z, power * 2, 0);
                    entityarrow.setSilent(true);
                    entityarrow.setIsCritical(true);
                    entityarrow.setDamage(5);
                    entityarrow.setKnockbackStrength(5);
                    itemstack.damageItem(1, entity);
                    int x = (int) entity.posX;
                    int y = (int) entity.posY;
                    int z = (int) entity.posZ;
                    world.playSound((EntityPlayer) null, (double) x, (double) y, (double) z,
                            (net.minecraft.util.SoundEvent) net.minecraft.util.SoundEvent.REGISTRY.getObject(new ResourceLocation((""))),
                            SoundCategory.NEUTRAL, 1, 1f / (itemRand.nextFloat() * 0.5f + 1f) + (power / 2));
                    if (entity.capabilities.isCreativeMode) {
                        entityarrow.pickupStatus = EntityArrow.PickupStatus.CREATIVE_ONLY;
                    } else {
                        if (new ItemStack(ItemPistolBullet.block, (int) (1)).isItemStackDamageable()) {
                            ItemStack stack = entity.inventory.getStackInSlot(slotID);
                            if (stack.attemptDamageItem(1, itemRand, entity)) {
                                stack.shrink(1);
                                stack.setItemDamage(0);
                            }
                        } else {
                            entity.inventory.clearMatchingItems(new ItemStack(ItemPistolBullet.block, (int) (1)).getItem(), -1, 1, null);
                        }
                    }
                    if (!world.isRemote)
                        world.spawnEntity(entityarrow);
                }
                entity.stopActiveHand();
            }
        }

        @Override
        public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer entity, EnumHand hand) {
            entity.setActiveHand(hand);
            return new ActionResult(EnumActionResult.SUCCESS, entity.getHeldItem(hand));
        }

        @Override
        public EnumAction getItemUseAction(ItemStack itemstack) {
            return EnumAction.BOW;
        }

        @Override
        public int getMaxItemUseDuration(ItemStack itemstack) {
            return 72000;
        }

        @Override
        @SideOnly(Side.CLIENT)
        public boolean hasEffect(ItemStack itemstack) {
            return true;
        }
    }

    public static class EntityArrowCustom extends EntityTippedArrow {
        public EntityArrowCustom(World a) {
            super(a);
        }

        public EntityArrowCustom(World worldIn, double x, double y, double z) {
            super(worldIn, x, y, z);
        }

        public EntityArrowCustom(World worldIn, EntityLivingBase shooter) {
            super(worldIn, shooter);
        }

        @Override
        protected void arrowHit(EntityLivingBase entity) {
            super.arrowHit(entity);
            entity.setArrowCountInEntity(entity.getArrowCountInEntity() - 1);
        }

        @Override
        public void onUpdate() {
            super.onUpdate();
            int x = (int) this.posX;
            int y = (int) this.posY;
            int z = (int) this.posZ;
            World world = this.world;
            Entity entity = (Entity) shootingEntity;
            if (this.inGround) {
                this.world.removeEntity(this);
            }
        }
    }
}
 

 

 

 

 

Last seen on 09:42, 5. Sep 2023
Joined May 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
No need unlock the code. Use…
Mon, 08/09/2021 - 07:58

No need unlock the code. Use cooldown procedure. Just type "cool" in the search when making a procedure. Add the procedure to the weapon "when ranged item used". 

 

Tick Calculator (mapmaking.fr)

Go here to find out how long you want the tick. I use 200 on some of my revolvers I made.

GL

Chew

Last seen on 09:42, 5. Sep 2023
Joined May 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I just looked at your code…
Mon, 08/09/2021 - 08:00

I just looked at your code and noticed your making a MP5. 

Go to this line in the code

    entity.setActiveHand(hand);
            return new ActionResult(EnumActionResult.SUCCESS, entity.getHeldItem(hand));

CHANGE TO

    entity.setActiveHand(hand);
            return new ActionResult(EnumActionResult.FAIL, entity.getHeldItem(hand));

This will loose the bow animation and make it feel like a gun.

Last seen on 03:07, 10. Aug 2021
Joined Aug 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
@chewdog Hey , thanks for…
Mon, 08/09/2021 - 11:15

@chewdog

Hey , thanks for trying to help me , but lemme explain my idea properly , I wanted to have an ammo system with the Mp5 , i couldnt get it done , so i made it that after 30 shots , the Mp5 broke , and you had to switch to another one , now id like the time between switching to an Mp5 changed to about 3 seconds , so it would be like reloading . Because if you can switch instantly to another Mp5 you can shoot it nonstop .

The Cooldown procedure is only for the shots , if i set it to 40 ticks , there will be a cooldown between the shots and not a cooldown between pulling a weapon.

 

I hope you can still help me   ^^

Last seen on 03:07, 10. Aug 2021
Joined Aug 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Theres also a way i already…
Mon, 08/09/2021 - 11:20

Theres also a way i already thoight of how i could fix my issue , id need to apply mining fatigue while holding the Mp5 but when i put the

"when item () is in main hand  apply effect () duration()"

Procedure to , for an example when range item is used , it just gives me Mining fatigue if the weapon is shot

 

Last seen on 09:42, 5. Sep 2023
Joined May 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Check this out Mcreator…
Mon, 08/09/2021 - 18:16

Check this out

Mcreator tutorial: 1 [Gun with storage System] - YouTube

 

I was checking this out about a month ago. It may be what you are looking for. I made 3D revolvers that I wanted to make a reload system for. IMO to do this properly you would need to make a VAR for each bullet. Everytime you fire the var goes up and when it hits 30 then you do reload somehow and bring var back to 0. 

So when the var hits 0 it would put a item/clip in your right hand and wait a few ticks and POOF remove item/clip and then add MP5 to right hand.

This is all guess work but in theory it should work.

Chew

Last seen on 03:07, 10. Aug 2021
Joined Aug 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
This doesnt work for me , i…
Tue, 08/10/2021 - 00:16

This doesnt work for me , i already tried several times , but i cant get it , there is no Procedure "CanUseRangedItem"

[See Video 1:49] 

I think this is because im using 2020.2 and not 2021.1 or whatever he is using .

 

Any Other ideas ? Im struggling for days! 

 

Last seen on 03:07, 10. Aug 2021
Joined Aug 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Isnt there just a way to…
Tue, 08/10/2021 - 00:19

Isnt there just a way to block the gun from shooting while equipping it ?

[ I mean equipping , like pulling out , like a sword takes 1 sec to pull out , and an axe takes 2 sec ]

Like maybe with putting something in the Code ? I never did coding , so if you come up with an idea for a code , please tell me the exact location to put it .

 

still thanks for trying to help me out , but idk , nothing seems to work !