Esempio n. 1
0
  /** Damages the item in the ItemStack */
  public void damageItem(int par1, EntityLivingBase par2EntityLivingBase) {
    if (!(par2EntityLivingBase instanceof EntityPlayer)
        || !((EntityPlayer) par2EntityLivingBase).capabilities.isCreativeMode) {
      if (this.isItemStackDamageable()) {
        if (this.attemptDamageItem(par1, par2EntityLivingBase.getRNG())) {
          par2EntityLivingBase.renderBrokenItemStack(this);
          --this.stackSize;

          if (par2EntityLivingBase instanceof EntityPlayer) {
            EntityPlayer entityplayer = (EntityPlayer) par2EntityLivingBase;
            entityplayer.addStat(
                StatList.objectBreakStats[Item.getIdFromItem(this.field_151002_e)], 1);

            if (this.stackSize == 0 && this.getItem() instanceof ItemBow) {
              entityplayer.destroyCurrentEquippedItem();
            }
          }

          if (this.stackSize < 0) {
            this.stackSize = 0;
          }

          this.itemDamage = 0;
        }
      }
    }
  }
 /*
  * First write of a function to place a block. Plan to rewrite soon now that I have more
  * experience with the Minecraft code and Java.
  *
  * Attaches a block from my mod by the name of "block_name" to the block at pos in the direction of side.
  */
 public static void placeBlock(
     ItemStack stack,
     EntityPlayer playerIn,
     World worldIn,
     BlockPos pos,
     EnumFacing side,
     String block_name) {
   String placeMe = ("bh_housing:" + block_name);
   int direction = side.getIndex(); // D-U-N-S-W-E
   switch (direction) {
     case 0:
       worldIn.setBlockState(pos.down(1), Block.getBlockFromName(placeMe).getDefaultState());
       break;
     case 1:
       worldIn.setBlockState(pos.up(1), Block.getBlockFromName(placeMe).getDefaultState());
       break;
     case 2:
       worldIn.setBlockState(pos.north(1), Block.getBlockFromName(placeMe).getDefaultState());
       break;
     case 3:
       worldIn.setBlockState(pos.south(1), Block.getBlockFromName(placeMe).getDefaultState());
       break;
     case 4:
       worldIn.setBlockState(pos.west(1), Block.getBlockFromName(placeMe).getDefaultState());
       break;
     case 5:
       worldIn.setBlockState(pos.east(1), Block.getBlockFromName(placeMe).getDefaultState());
       break;
   }
   --stack.stackSize;
   if (stack.stackSize == 0) {
     playerIn.destroyCurrentEquippedItem();
   }
 }
 /*
  * This messy function is a work in progress.  It is a temporary setup for
  * placing a custom block from my mod over the door of the given npch.
  *
  * Returns the location of where the sign should be placed.
  *
  * Needs to be rewritten badly.
  */
 public static BlockPos placeBlockOverDoor(
     ItemStack stack,
     EntityPlayer playerIn,
     World worldIn,
     NPCHouse npch,
     String block_name,
     float hitX,
     float hitY,
     float hitZ) {
   String placeMe = ("bh_housing:" + block_name);
   BlockPos door = BlockHelper.findDoor(worldIn, npch);
   if (!door.equals(npch.getCorner(0))) {
     int north = npch.getCorner(0).getZ();
     int south = npch.getCorner(2).getZ();
     int west = npch.getCorner(0).getX();
     int east = npch.getCorner(1).getX();
     BlockPos placeHere = door;
     boolean shouldUse = false;
     EnumFacing direction = EnumFacing.NORTH;
     if (worldIn.getBlockState(placeHere).getBlock() instanceof NPCHouse_Block) {
       return placeHere;
     } else if (door.getX() == west) {
       placeHere = door.up().west();
       direction = EnumFacing.WEST;
     } else if (door.getX() == east) {
       placeHere = door.up().east();
       direction = EnumFacing.EAST;
     } else if (door.getZ() == north) {
       placeHere = door.up().north();
       direction = EnumFacing.NORTH;
     } else if (door.getZ() == south) {
       placeHere = door.up().south();
       direction = EnumFacing.SOUTH;
     }
     shouldUse =
         worldIn.setBlockState(
             placeHere,
             NPCHouse_Block.getBlockFromName(placeMe)
                 .getDefaultState()
                 .withProperty(NPCHouse_Block.FACING, direction),
             3);
     if (shouldUse) {
       --stack.stackSize;
       if (stack.stackSize == 0) {
         playerIn.destroyCurrentEquippedItem();
       }
       if (block_name == "npch_block") {
         NPCHouse_Block npchblock = (NPCHouse_Block) worldIn.getBlockState(placeHere).getBlock();
         npchblock.setNPCHouse(npch);
       }
       IBlockState temp = worldIn.getBlockState(placeHere);
       if (temp.getBlock().getRegistryName() == "npch_block") {
         ItemBlock.setTileEntityNBT(
             worldIn, playerIn, placeHere, new ItemStack(Block.getBlockFromName("npch_block"), 1));
       }
     }
     return placeHere;
   }
   return null;
 }
 private void emulateBlockHarvest(
     ItemStack stack,
     World world,
     int x,
     int y,
     int z,
     Block block,
     int meta,
     EntityPlayer player) {
   world.playAuxSFXAtEntity(player, 2001, x, y, z, Block.getIdFromBlock(block) | meta << 12);
   stack.func_150999_a(world, block, x, y, z, player);
   if (stack.stackSize == 0) player.destroyCurrentEquippedItem();
   if (block.removedByPlayer(world, player, x, y, z, false)) {
     block.dropBlockAsItem(world, x, y, z, meta, EnchantmentHelper.getFortuneModifier(player));
   }
 }
Esempio n. 5
0
  /* Right-click adds sticks */
  @Override
  public boolean onBlockActivated(
      World world,
      int x,
      int y,
      int z,
      EntityPlayer player,
      int par6,
      float hitX,
      float hitY,
      float hitZ) {
    /*
     * if (world.isRemote) return false;
     */

    int meta = world.getBlockMetadata(x, y, z);
    if (meta < 4) {
      ItemStack stack = player.getCurrentEquippedItem();
      if (stack != null && stack.getItem() == Item.getItemFromBlock(TRepo.punji)) {
        world.setBlockMetadataWithNotify(x, y, z, meta + 1, 3);
        world.playSoundEffect(
            (double) ((float) x + 0.5F),
            (double) ((float) y + 0.5F),
            (double) ((float) z + 0.5F),
            this.stepSound.soundName,
            (this.stepSound.getVolume() + 1.0F) / 2.0F,
            this.stepSound.getPitch() * 0.8F);
        player.swingItem();
        if (!player.capabilities.isCreativeMode) {
          stack.stackSize--;
          if (stack.stackSize <= 0) player.destroyCurrentEquippedItem();
        }
      }
    }
    return true;
  }
 /**
  * Called when the shield blocks an attack when held in the normal fashion (i.e. non-BG2) used by
  * Deku Shield to damage / destroy the stack and by Mirror Shield to reflect projectiles
  *
  * @return Return the amount of damage remaining, if any; 0 cancels the hurt event
  */
 public float onBlock(EntityPlayer player, ItemStack shield, DamageSource source, float damage) {
   ZSSPlayerInfo.get(player).onAttackBlocked(shield, damage);
   WorldUtils.playSoundAtEntity(player, Sounds.HAMMER, 0.4F, 0.5F);
   float damageBlocked = damage;
   if (toolMaterial == ToolMaterial.WOOD) {
     if (source.isProjectile()
         && !source.isExplosion()
         && source.getSourceOfDamage() instanceof IProjectile) {
       if (ZSSMain.isBG2Enabled
           && player.getHeldItem() == shield
           && shield.getItem() instanceof IArrowCatcher) {
         if (((IArrowCatcher) shield.getItem())
             .catchArrow(shield, player, (IProjectile) source.getSourceOfDamage())) {
           ((InventoryPlayerBattle) player.inventory).hasChanged = true;
         }
       }
     } else if (source instanceof IDamageAoE && ((IDamageAoE) source).isAoEDamage()) {
       damageBlocked *= magicReduction;
     }
     int dmg = Math.round(source.isFireDamage() ? damage + 10.0F : damage - 2.0F);
     if (dmg > 0) {
       shield.damageItem(dmg, player);
       if (shield.stackSize <= 0) {
         ForgeEventFactory.onPlayerDestroyItem(player, shield);
         if (ZSSMain.isBG2Enabled && BattlegearUtils.isPlayerInBattlemode(player)) {
           BattlegearUtils.setPlayerOffhandItem(player, null);
         } else {
           player.destroyCurrentEquippedItem();
         }
       }
     }
   } else if (toolMaterial == ToolMaterial.EMERALD) {
     if (source.isProjectile() && !source.isExplosion() && source.getSourceOfDamage() != null) {
       float chance = (source.isMagicDamage() ? (1F / 3F) : 1.0F);
       if (source.getSourceOfDamage() instanceof IReflectable) {
         ((IReflectable) source.getSourceOfDamage())
             .getReflectChance(shield, player, source.getEntity());
       }
       if (player.worldObj.rand.nextFloat() < chance) {
         Entity projectile = null;
         try {
           projectile =
               source
                   .getSourceOfDamage()
                   .getClass()
                   .getConstructor(World.class)
                   .newInstance(player.worldObj);
         } catch (Exception e) {;
         }
         if (projectile != null) {
           NBTTagCompound data = new NBTTagCompound();
           source.getSourceOfDamage().writeToNBT(data);
           projectile.readFromNBT(data);
           projectile.getEntityData().setBoolean("isReflected", true);
           projectile.posX -= projectile.motionX;
           projectile.posY -= projectile.motionY;
           projectile.posZ -= projectile.motionZ;
           double motionX =
               (double)
                   (-MathHelper.sin(player.rotationYaw / 180.0F * (float) Math.PI)
                       * MathHelper.cos(player.rotationPitch / 180.0F * (float) Math.PI));
           double motionZ =
               (double)
                   (MathHelper.cos(player.rotationYaw / 180.0F * (float) Math.PI)
                       * MathHelper.cos(player.rotationPitch / 180.0F * (float) Math.PI));
           double motionY =
               (double) (-MathHelper.sin(player.rotationPitch / 180.0F * (float) Math.PI));
           TargetUtils.setEntityHeading(
               projectile,
               motionX,
               motionY,
               motionZ,
               1.0F,
               2.0F + (20.0F * player.worldObj.rand.nextFloat()),
               false);
           if (projectile instanceof IReflectable) {
             ((IReflectable) projectile)
                 .onReflected(shield, player, source.getEntity(), source.getSourceOfDamage());
           }
           player.worldObj.spawnEntityInWorld(projectile);
         }
       } else if (source.isUnblockable()
           || (source instanceof IDamageAoE
               && ((IDamageAoE) source).isAoEDamage())) { // failed to reflect projectile
         damageBlocked *= magicReduction;
       }
     }
   } else if (source.isUnblockable()
       || (source instanceof IDamageAoE && ((IDamageAoE) source).isAoEDamage())) {
     damageBlocked *=
         magicReduction; // default shield behavior blocks half damage from AoE magic attacks
   }
   return (damage - damageBlocked);
 }
  private void tryToCraft(ItemStack tool, EntityPlayer user) {
    recipe = FoodPrepRecipe.getRecipeFor(inv[0], tool);
    boolean cook = false;
    if (recipe != null) {
      if (tool.getItem() instanceof ItemKnifeMF) {
        if (((ItemKnifeMF) tool.getItem()).getMaterial() == ToolMaterialMedieval.DRAGONFORGE) {
          cook = true;
          worldObj.spawnParticle(
              "flame",
              xCoord + rand.nextFloat(),
              yCoord + 0.2D,
              zCoord + rand.nextFloat(),
              0,
              0,
              0);
        }
      }
      if (requiredUtensil != UtensilManager.getTypeOfTool(tool)) {
        time = 0;
        requiredUtensil = UtensilManager.getTypeOfTool(tool);
      }

      float e = 1.0F;
      if (tool.getItem() != null && tool.getItem() instanceof IUtensil) {
        e = ((IUtensil) tool.getItem()).getEfficiency(tool);
      }

      if (!worldObj.isRemote) {
        time += e;
        tool.damageItem(1, user);
        if (tool.getItemDamage() >= tool.getMaxDamage() && tool.stackSize <= 1) {
          user.renderBrokenItemStack(tool);
          user.destroyCurrentEquippedItem();
        }
        worldObj.playSoundEffect(xCoord + 0.5D, yCoord, zCoord + 0.5D, recipe.prepSound, 1, 1);
      }

      if (inv != null && inv[0] != null) {
        worldObj.spawnParticle(
            "iconcrack_" + inv[0].itemID,
            xCoord + rand.nextFloat(),
            yCoord + 0.2D,
            zCoord + rand.nextFloat(),
            0F,
            0F,
            0F);
      }

      if (!worldObj.isRemote && time >= recipe.time) {
        ItemStack result = recipe.output;
        if (cook
            && result != null
            && FurnaceRecipes.smelting().getSmeltingResult(recipe.output) != null) {
          result = FurnaceRecipes.smelting().getSmeltingResult(recipe.output);
        }
        int ss = result.stackSize;
        if (inv[0] != null) {
          ss *= inv[0].stackSize;
        }
        if (ss <= result.getMaxStackSize()) {
          inv[0] = result.copy();
          inv[0].stackSize = ss;
          time = 0;
        }
      }
    }
  }
  @SubscribeEvent
  public void onAttackEntity(AttackEntityEvent event) {
    if (event.entityLiving.worldObj.isRemote) return;

    EntityLivingBase attacker = event.entityLiving;
    EntityPlayer player = event.entityPlayer;
    Entity target = event.target;
    ItemStack stack = attacker.getEquipmentInSlot(0);
    if (stack != null && stack.getItem().onLeftClickEntity(stack, player, target)) return;

    if (target.canAttackWithItem()) {
      if (!target.hitByEntity(target)) {
        float damageAmount = TFC_MobData.STEVE_DAMAGE;
        if (stack != null) {
          damageAmount =
              (float)
                  player
                      .getEntityAttribute(SharedMonsterAttributes.attackDamage)
                      .getAttributeValue();
          // player.addChatMessage("Damage: " + i);
          if (damageAmount == 1.0f) {
            damageAmount = TFC_MobData.STEVE_DAMAGE;
            // i = player.inventory.getCurrentItem().getItem().getDamageVsEntity(target,
            // player.inventory.getCurrentItem());
          }
        }

        if (player.isPotionActive(Potion.damageBoost))
          damageAmount += 3 << player.getActivePotionEffect(Potion.damageBoost).getAmplifier();

        if (player.isPotionActive(Potion.weakness))
          damageAmount -= 2 << player.getActivePotionEffect(Potion.weakness).getAmplifier();

        int knockback = 0;
        float enchantmentDamage = 0;

        if (target instanceof EntityLiving) {
          enchantmentDamage =
              EnchantmentHelper.getEnchantmentModifierLiving(player, (EntityLiving) target);
          knockback += EnchantmentHelper.getKnockbackModifier(player, (EntityLiving) target);
        }

        if (player.isSprinting()) ++knockback;

        if (damageAmount > 0 || enchantmentDamage > 0) {
          boolean criticalHit =
              player.fallDistance > 0.0F
                  && !player.onGround
                  && !player.isOnLadder()
                  && !player.isInWater()
                  && !player.isPotionActive(Potion.blindness)
                  && player.ridingEntity == null
                  && target instanceof EntityLiving;

          if (criticalHit && damageAmount > 0)
            damageAmount += event.entity.worldObj.rand.nextInt((int) (damageAmount / 2 + 2));

          damageAmount += enchantmentDamage;
          boolean onFire = false;
          int fireAspect = EnchantmentHelper.getFireAspectModifier(player);

          if (target instanceof EntityLiving && fireAspect > 0 && !target.isBurning()) {
            onFire = true;
            target.setFire(1);
          }

          boolean entityAttacked =
              target.attackEntityFrom(DamageSource.causePlayerDamage(player), damageAmount);

          if (entityAttacked) {
            if (knockback > 0) {
              target.addVelocity(
                  -MathHelper.sin(player.rotationYaw * (float) Math.PI / 180.0F) * knockback * 0.5F,
                  0.1D,
                  MathHelper.cos(player.rotationYaw * (float) Math.PI / 180.0F) * knockback * 0.5F);
              player.motionX *= 0.6D;
              player.motionZ *= 0.6D;
              player.setSprinting(false);
            }

            if (criticalHit) player.onCriticalHit(target);

            if (enchantmentDamage > 0) player.onEnchantmentCritical(target);

            if (damageAmount >= 18) player.triggerAchievement(AchievementList.overkill);

            player.setLastAttacker(target);

            if (target instanceof EntityLiving)
              target.attackEntityFrom(DamageSource.causeThornsDamage(attacker), damageAmount);
          }

          ItemStack itemstack = player.getCurrentEquippedItem();
          Object object = target;

          if (target instanceof EntityDragonPart) {
            IEntityMultiPart ientitymultipart = ((EntityDragonPart) target).entityDragonObj;
            if (ientitymultipart instanceof EntityLiving) object = ientitymultipart;
          }

          if (itemstack != null && object instanceof EntityLiving) {
            itemstack.hitEntity((EntityLiving) object, player);
            if (itemstack.stackSize <= 0) player.destroyCurrentEquippedItem();
          }

          if (target instanceof EntityLivingBase) {
            player.addStat(StatList.damageDealtStat, Math.round(damageAmount * 10.0f));
            if (fireAspect > 0 && entityAttacked) target.setFire(fireAspect * 4);
            else if (onFire) target.extinguish();
          }

          player.addExhaustion(0.3F);
        }
      }
    }
    event.setCanceled(true);
  }
Esempio n. 9
0
  @Override
  public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity) {
    if (entity.canAttackWithItem()) {
      if (!entity.hitByEntity(player)) {
        float damage = (float) getAttackDamage(stack);
        int i = 0;
        float extraDamage = 0.0F;
        if (entity instanceof EntityLivingBase) {
          extraDamage =
              EnchantmentHelper.getEnchantmentModifierLiving(player, (EntityLivingBase) entity);
          i += EnchantmentHelper.getKnockbackModifier(player, (EntityLivingBase) entity);
        }
        if (player.isSprinting()) {
          i++;
        }
        if (damage > 0.0F || extraDamage > 0.0F) {
          boolean criricalHit =
              player.fallDistance > 0.0F
                  && !player.onGround
                  && !player.isOnLadder()
                  && !player.isInWater()
                  && !player.isPotionActive(Potion.blindness)
                  && player.ridingEntity == null
                  && entity instanceof EntityLivingBase;
          if (criricalHit && damage > 0.0F) {
            damage *= 1.5F;
          }
          damage += extraDamage;
          boolean fire = false;
          int j = EnchantmentHelper.getFireAspectModifier(player);
          if (entity instanceof EntityLivingBase && j > 0 && !entity.isBurning()) {
            fire = true;
            entity.setFire(1);
          }
          boolean attack = entity.attackEntityFrom(DamageSource.causePlayerDamage(player), damage);
          if (attack) {
            if (i > 0) {
              entity.addVelocity(
                  (double)
                      (-MathHelper.sin(player.rotationYaw * (float) Math.PI / 180.0F)
                          * (float) i
                          * 0.5F),
                  0.1D,
                  (double)
                      (MathHelper.cos(player.rotationYaw * (float) Math.PI / 180.0F)
                          * (float) i
                          * 0.5F));
              player.motionX *= 0.6D;
              player.motionZ *= 0.6D;
              player.setSprinting(false);
            }
            if (criricalHit) {
              player.onCriticalHit(entity);
            }
            if (extraDamage > 0.0F) {
              player.onEnchantmentCritical(entity);
            }
            if (damage >= 18.0F) {
              player.triggerAchievement(AchievementList.overkill);
            }
            player.setLastAttacker(entity);
            if (entity instanceof EntityLivingBase) {
              EnchantmentHelper.func_151384_a((EntityLivingBase) entity, player);
            }
          }

          ItemStack itemstack = player.getCurrentEquippedItem();
          Object object = entity;

          if (entity instanceof EntityDragonPart) {
            IEntityMultiPart entityMultipart = ((EntityDragonPart) entity).entityDragonObj;
            if (entityMultipart != null && entityMultipart instanceof EntityLivingBase) {
              object = (EntityLivingBase) entityMultipart;
            }
          }
          if (itemstack != null && object instanceof EntityLivingBase) {
            itemstack.hitEntity((EntityLivingBase) object, player);
            if (itemstack.stackSize <= 0) {
              player.destroyCurrentEquippedItem();
            }
          }
          if (entity instanceof EntityLivingBase) {
            player.addStat(StatList.damageDealtStat, Math.round(damage * 10.0F));
            if (j > 0 && attack) {
              entity.setFire(j * 4);
            } else if (fire) {
              entity.extinguish();
            }
          }
          player.addExhaustion(0.3F);
        }
      }
    }
    return true;
  }