Jump to content

[1.8] Trying to add resistance to player whenever blocking


doodasquash

Recommended Posts

Hi all, I'd just like to preface this by saying that I'm fairly new to the modding scene.

 

That being said, I'm having a bit of trouble here. I'm trying to make a custom sword class that will give the player a resistance effect while blocking. In order to do that, I figured that I would use the isBlocking method, seeing as it is made to check if the player is blocking. However, doing this will completely freeze the game once I block, at least with the way I'm coding this. I think that this might be an infinite loop situation, but I can't for the life of me figure out why this would be the case. Any suggestions on how to fix this? All help is appreciated!

 

 

 

@Override

public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5) {

 

if(entity instanceof EntityPlayer) {

EntityPlayer player = (EntityPlayer) entity;

 

ItemStack equipped = player.getCurrentEquippedItem();

 

if(equipped == stack) {

 

if(swingSpeed == 0) {}

else if (swingSpeed == 1) {

player.addPotionEffect(new PotionEffect(Potion.digSpeed.id, 1, 0));

}

 

else if (swingSpeed == 2) {

player.addPotionEffect(new PotionEffect(Potion.digSpeed.id, 1, 1));

}

 

if (blockEnhance != 0) {

while (player.isBlocking()) {

if (blockEnhance == 1) {

player.addPotionEffect(new PotionEffect(Potion.resistance.id, 1, 0));

}

else if (blockEnhance == 2) {

player.addPotionEffect(new PotionEffect(Potion.resistance.id, 1, 1));

}

        }

        }

}

}

}

 

 

 

By the way, the main thing I'm having trouble with there is everything to do with the blockEnhance variable (basically the while loop and down). Everything above that works fine.

 

Link to comment
Share on other sites

Do you know what while means?

Do you know how code is run on java?

Do you know how minecraft code runs?

If any of these = no - it's bad...

Try to understand these and if you do, you will find a problem. If you don't, come back here and we will explain you things more detailed...

 

Link to comment
Share on other sites

Try:

 

 

@Override
   public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5) {
      
      if(entity instanceof EntityPlayer) {
         EntityPlayer player = (EntityPlayer) entity;
      
         ItemStack equipped = player.getCurrentEquippedItem();
      
         if(equipped == stack) {
         
            if(swingSpeed == 0) {}
            else if (swingSpeed == 1) {
               player.addPotionEffect(new PotionEffect(Potion.digSpeed.id, 1, 0));
            }
            
            else if (swingSpeed == 2) {
               player.addPotionEffect(new PotionEffect(Potion.digSpeed.id, 1, 1));
            }
         
            if (blockEnhance != 0) {
               if(player.isBlocking() == true) {
                  if (blockEnhance == 1) {
                     player.addPotionEffect(new PotionEffect(Potion.resistance.id, 1, 0));
                  }
                  else if (blockEnhance == 2) {
                     player.addPotionEffect(new PotionEffect(Potion.resistance.id, 1, 1));
                  }
                 }
                else
                {
                   //remove potion effect, I forget the exact code here
                }
              }
         }
      }
   }

 

if(pain == 0)

{

   gain = 0;

}

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.