Jump to content

[1.15.2] Remove Slowness when using an Item


xanderindalzone

Recommended Posts

I finally figured out how to make my Gun semi auto by overriding the getUseDuration() from the Item Class, but for some reason it adds a slowness effect when holding right click(continously using the item).

How could I remove this slowness when using/holding right click with the Gun Item?

Quote

public class Gun extends Item
{
    
    public int gun_mag;
    
    public GunTypes gun_type;
    public Item ammo_used;
    public float gun_damage;
    public float gun_accuracy;
    public float gun_recoil;
    public float gun_bullet_speed;
    public int gun_firing_rate;
    public int gun_reload_cooldown;
    public int gun_reload_cooldown_cock;
    public float zoom_fov;
    public boolean gun_is_full_auto;
    
    //=============
    //BROKEN CODE!! NEEDS FIXING
    //=============
    //Maintenance Attributes
    protected boolean is_aiming=false;
    protected boolean is_reloading=false;
    //=============
    

    public Gun(Properties properties) {
        super(properties);
    }
    
    
    
    //=============
    //BROKEN CODE!! NEEDS FIXING
    //=============
    public boolean isAiming() 
    {
        return this.is_aiming;
    }
    
//==========================================================================================================
//COMPORTAMIENTOS DEL ITEM
    
    //CANCELAR ANIMACION DE SWING DEL ITEM
    @Override
    public boolean onEntitySwing(ItemStack stack, LivingEntity entity) {return true;}
    //CANCELA GOLPES A ENTITIES
    @Override
    public boolean onLeftClickEntity(ItemStack stack, PlayerEntity player, Entity entity){return true;}
    //CANCELA PODER ROMPER BLOQUES CON ESTE ITEM
    @Override
    public boolean canPlayerBreakBlockWhileHolding(BlockState state, World worldIn, BlockPos pos, PlayerEntity player){return false;}
    
    @Override
    public Multimap<String, AttributeModifier> getAttributeModifiers(EquipmentSlotType equipmentSlot) {
        Multimap<String, AttributeModifier> multimap = super.getAttributeModifiers(equipmentSlot);
        multimap.put(SharedMonsterAttributes.ATTACK_SPEED.getName(), new AttributeModifier(ATTACK_SPEED_MODIFIER, "Tool modifier", 20, Operation.ADDITION));
        return multimap;
    }
    
//==========================================================================================================    
    

    @Override
    public void onPlayerStoppedUsing(ItemStack stack, World worldIn, LivingEntity entityLiving, int timeLeft) 
    {
        super.onPlayerStoppedUsing(stack, worldIn, entityLiving, timeLeft);
        //DO STUFF
    }
    
    @Override
    public void onUse(World worldIn, LivingEntity livingEntityIn, ItemStack stack, int count) 
    {
        super.onUse(worldIn, livingEntityIn, stack, count);
        //DO STUFF WHEN USING ITEM
    }
    
    @Override
    public void inventoryTick(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) 
    {
        super.inventoryTick(stack, worldIn, entityIn, itemSlot, isSelected);
        //DO STUFF
    }
    
    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        
        playerIn.setActiveHand(handIn);
        

        
        if(playerIn.getHeldItemMainhand().getDamage()<this.gun_mag||playerIn.isCreative()) {
                    
            shootGun(worldIn, playerIn); //DISPARAR ARMA
            playerIn.getHeldItemMainhand().damageItem(1, playerIn, null);
            if(!(playerIn.getHeldItemMainhand().getDamage()<this.gun_mag||playerIn.isCreative())) 
            {            
                worldIn.playSound(null, playerIn.getPosition(), SoundEvents.BLOCK_ANVIL_PLACE, SoundCategory.PLAYERS, 1.0F, 3.0F);
            }
        }
        else 
        {
            worldIn.playSound(null, playerIn.getPosition(), InitSounds.SOUND_no_ammo_click.get(), SoundCategory.PLAYERS, 1.0F, 1.0F);
        }    
        return new ActionResult<ItemStack>(ActionResultType.FAIL, playerIn.getHeldItemMainhand());
    }
    
    @Override
    public UseAction getUseAction(ItemStack stack) {
        // TODO Auto-generated method stub
        return UseAction.NONE;
    }
    
    @Override
    public int getUseDuration(ItemStack stack) 
    {
        if(this.gun_is_full_auto) {return 0;}
        else {return 72000;}
    }
    

//===========================================================================================
//PRIVATE METHOD
//===========================================================================================
    private void shootGun(World worldIn, PlayerEntity playerIn) 
    {
        double posX = playerIn.getPosX();
        double posY = playerIn.getPosY();
        double posZ = playerIn.getPosZ();
        Vec3d look = playerIn.getLookVec();
        World world = playerIn.getEntityWorld();
        
        
        //ArrowEntity bullet = new ArrowEntity(world, 1.0D, 1.0D, 1.0D);
        PistolBulletEntity bullet = new PistolBulletEntity(InitEntities.PISTOL_BULLET_ENTITY.get(), 1.0D, 1.0D, 1.0D, world);
        
        //=======================================================
        //PROPIEDADES DEL DISPARO
        //=======================================================
        //SPAWNEAR FOGONAZO
        worldIn.addParticle(ParticleTypes.CLOUD, posX+(look.x*1.5D), posY+(look.y*1.5D)+1.5D, posZ+(look.z*1.5D), 0.0D, 0.5D, 0.0D);
        
        //POSICION INICIAL DE LA BALA
        if(playerIn.rotationPitch>60) 
        {
            bullet.setPosition(posX+(look.x*1.3D), posY+(look.y*1.5D)+1.0D, posZ+(look.z*1.3D));    
        }
        else
        {
            bullet.setPosition(posX+(look.x*1.3D), posY+(look.y*1.5D)+1.3D, posZ+(look.z*1.3D));
        }
        
        
        //ESTABLECE EL DAÑO DE LA BALA
        bullet.setBulletDamage(this.gun_damage);
        
        //ESTABLECE EL ALCANCE DE LA BALA
        bullet.setVelocity(look.x*this.gun_bullet_speed, (look.y*this.gun_bullet_speed), look.z*this.gun_bullet_speed);
        
        //ESTABLECE LA CADENCIA DEL ARMA
        playerIn.getCooldownTracker().setCooldown(this, this.gun_firing_rate); //TICKS - 30 TICKS = 1 seg
        
        //ESTABLECE EL RETROCESO MAXIMO DEL DISPARO
        float recoil_pitch = (float) -(((gun_recoil)*Math.random()));    //ES SIEMPRE NEGATIVO, SIEMPRE TENDRA RETROCESO HACIA ARRIBA
        float recoil_yaw = (float) ((float) gun_recoil-((gun_recoil*2)*Math.random()));
        playerIn.rotationPitch=playerIn.rotationPitch+recoil_pitch;
        playerIn.rotationYaw=playerIn.rotationYaw+recoil_yaw;
        
        

        if(!world.isRemote)
        {
            world.addEntity(bullet);
            shootingGunSound(worldIn, playerIn, this);
        }
        
        
    }


    private void shootingGunSound(World worldIn, PlayerEntity playerIn, Gun gun) 
    {
        if(gun instanceof Colt1911) 
        {
            worldIn.playSound(null, playerIn.getPosition(), InitSounds.SOUND_shot_Colt1911.get(), SoundCategory.PLAYERS, 1.0F, 1.0F);
        }
    }
    
    private void reloadingGunSound(World worldIn, PlayerEntity playerIn, Gun gun, boolean cock_reload) 
    {
        if(gun instanceof Colt1911) 
        {
            if(cock_reload)
            {
                worldIn.playSound(null, playerIn.getPosition(), InitSounds.SOUND_reload_Colt1911_cock.get(), SoundCategory.PLAYERS, 1.0F, 1.0F);
                playerIn.getCooldownTracker().setCooldown(InitItems.PISTOL_COLT_1911.get(), this.gun_reload_cooldown_cock); //TICKS - 30 TICKS = 1 seg
            }
            else
            {
                worldIn.playSound(null, playerIn.getPosition(), InitSounds.SOUND_reload_Colt1911.get(), SoundCategory.PLAYERS, 1.0F, 1.0F);
                playerIn.getCooldownTracker().setCooldown(InitItems.PISTOL_COLT_1911.get(), this.gun_reload_cooldown); //TICKS - 30 TICKS = 1 seg
            }
        }
    }
    
    
    
    
    
    
    
    public boolean ReloadGun(World worldIn, PlayerEntity player) {
        int ammo_left = (this.gun_mag-player.getHeldItemMainhand().getDamage());    
        boolean cock_reload = false;
        
                
        //COMPRUEBA SI EL CARGADOR SE PUEDE RECARGAR
        if(ammo_left!=this.gun_mag&&!player.isCreative())    
        {
            if(ammo_left==0) {cock_reload=true;}
                    
                    
            //COMPRUEBA SI EL JUGADOR TIENE MUNICION EN EL INVENTARIO
            if(player.inventory.hasItemStack(new ItemStack(this.ammo_used))) 
            {
                for(int slotIndex=0; slotIndex<player.inventory.mainInventory.size(); slotIndex++) 
                {
                    int ammo_needed = this.gun_mag-ammo_left;
                    ItemStack item = player.inventory.getStackInSlot(slotIndex).copy();
                    if(item.getItem() == this.ammo_used)
                    {
                        if(!worldIn.isRemote) 
                        {
                            ammo_left=removeAmmo(player, ammo_needed, ammo_left, item, slotIndex);
                        }
                        else
                        {
                            ammo_left=removeAmmo(player, ammo_needed, ammo_left, item, slotIndex);
                        }
                        reloadingGunSound(worldIn, player, this, cock_reload);
                        is_reloading=true;
                    }
                }
                return true;
            }
        }
        return false;
    }
        
    //DEVUELVE LA MUNICION QUE FALTA
    private int removeAmmo(PlayerEntity player, int ammo_needed, int ammo_left, ItemStack item, int slotIndex)
    {
        if(ammo_needed<item.getCount())
        {
            item.shrink(ammo_needed);
            player.inventory.setInventorySlotContents(slotIndex, item);
            player.getHeldItemMainhand().damageItem(-ammo_needed, player, null);
            ammo_left=this.gun_mag;
        }
        else
        {
            player.inventory.setInventorySlotContents(slotIndex, new ItemStack(Blocks.AIR));
            player.getHeldItemMainhand().damageItem(-item.getCount(), player, null);
            ammo_left=ammo_left+item.getCount();
        }
        return ammo_left;
    }

    
}
 

 

Link to comment
Share on other sites

LivingEntity#removePotionEffect(Effect)

Edit: Oof, thought you meant a potion effect, I should really take my time and read these in detail.

Edited by Novârch

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

Link to comment
Share on other sites

1 minute ago, xanderindalzone said:

I've already tried that, the automatic slowness overrides it

Are you sure? This code works for me (and makes the player really fast when using items):

@SubscribeEvent
  public static void onItemUse(LivingEntityUseItemEvent event) {
  if (event.getEntityLiving() instanceof PlayerEntity) {
    PlayerEntity player = (PlayerEntity)event.getEntityLiving();
    player.getAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(2);

  }
}

 

Link to comment
Share on other sites

1 minute ago, vemerion said:

Are you sure? This code works for me (and makes the player really fast when using items):


@SubscribeEvent
  public static void onItemUse(LivingEntityUseItemEvent event) {
  if (event.getEntityLiving() instanceof PlayerEntity) {
    PlayerEntity player = (PlayerEntity)event.getEntityLiving();
    player.getAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(2);

  }
}

 

ok lol, i was editing the player's abbilities instead of the attributes XD.

 

thx. i'll try that in a bit.

Link to comment
Share on other sites

1 hour ago, vemerion said:

Are you sure? This code works for me (and makes the player really fast when using items):


@SubscribeEvent
  public static void onItemUse(LivingEntityUseItemEvent event) {
  if (event.getEntityLiving() instanceof PlayerEntity) {
    PlayerEntity player = (PlayerEntity)event.getEntityLiving();
    player.getAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(2);

  }
}

 

https://i.gyazo.com/2ef1deed8715a60c47b2f532028dfb8b.mp4

it still bugs, same result.

Quote

    @SubscribeEvent
    public static void CancelGunUseSlowness(LivingEntityUseItemEvent event)
    {
        if(event.getEntityLiving() instanceof PlayerEntity) 
        {
            PlayerEntity player = (PlayerEntity) event.getEntityLiving();
            if(player.getHeldItemMainhand().getItem() instanceof Gun) 
            {
                player.getAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.2);
                System.out.println(player.getAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).getValue());
            }
        }
    }

 

Link to comment
Share on other sites

1 minute ago, vemerion said:

Bugs how exactly? Is the speed unchanged, or is it the FOV that is changing as in the previous video you shared?

it changes speed and FOV, and while using the item it doenst appear to be affecting the speed, but I cant really tell because the FOV effect alters the perspective

Link to comment
Share on other sites

22 minutes ago, xanderindalzone said:

it changes speed and FOV, and while using the item it doenst appear to be affecting the speed, but I cant really tell because the FOV effect alters the perspective

I know it is kind of a band aid on top of a band aid, but if you try to stop the FOV update with FOVUpdateEvent you could more easily observe any speed changes.

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.