Example #1
0
  /** Returns true if the item can be used on the given entity, e.g. shears on sheep. */
  @Override
  public boolean itemInteractionForEntity(
      ItemStack itemStack, EntityPlayer entityPlayer, EntityLivingBase entity) {
    if (itemStack != null) {
      onItemRightClick(itemStack, entity.worldObj, entityPlayer);
      if (NBTHelper.getString(itemStack, "command") != null
          && NBTHelper.getString(itemStack, "command") == "/kill Entity") {
        if (entity != null && !entity.worldObj.isRemote) {
          ChatHelper.send(entityPlayer, "Attempting to damage entity: " + entity.toString());
          entity.attackEntityFrom(
              DamageSource.outOfWorld, Float.MAX_VALUE); // MAKE A NEW DAMAGE SOURCE
          // entity.setHealth(0.0F);
          // entity.setDead();
        }
      }
    }

    return true;
  }
  /*
   * 右クリック使用をやめた時に呼ばれるメソッド。右クリックを継続して押していた時間をもとに、エンティティを発射する処理を行う。
   */
  public void onPlayerStoppedUsing(
      ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer, int par4) {
    boolean ff =
        par3EntityPlayer.capabilities.isCreativeMode
            || EnchantmentHelper.getEnchantmentLevel(Enchantment.infinity.effectId, par1ItemStack)
                > 0;
    boolean flag2 = false;
    if (par1ItemStack.getItem() == this) {
      int c2 = this.discharge(par1ItemStack, 400, false);
      if (ff || c2 == 400) flag2 = true;
    }

    if (flag2) {
      float yaw = par3EntityPlayer.rotationYaw;
      float pitch = par3EntityPlayer.rotationPitch;
      double dx = -(double) (MathHelper.sin(yaw / 180.0F * (float) Math.PI)) * 30.0D;
      double dz = (double) (MathHelper.cos(yaw / 180.0F * (float) Math.PI)) * 30.0D;
      double dy = -(double) (MathHelper.sin(pitch / 180.0F * (float) Math.PI)) * 30.0D;

      double minX = par3EntityPlayer.posX + Math.min(-1, dx);
      double minY = par3EntityPlayer.posY + Math.min(-1, dy);
      double minZ = par3EntityPlayer.posZ + Math.min(-1, dz);
      double maxX = par3EntityPlayer.posX + Math.max(1, dx);
      double maxY = par3EntityPlayer.posY + Math.max(1, dy);
      double maxZ = par3EntityPlayer.posZ + Math.max(1, dz);

      AxisAlignedBB aabb = AxisAlignedBB.getBoundingBox(minX, minY, minZ, maxX, maxY, maxZ);
      List list = par2World.getEntitiesWithinAABB(EntityLivingBase.class, aabb);
      EntityLivingBase target = null;

      AMTLogger.debugInfo("yaw & pitch : " + yaw + "," + pitch);
      AMTLogger.debugInfo(
          "aabb : " + minX + "," + minY + "," + minZ + "," + maxX + "," + maxY + "," + maxZ);

      if (list != null && !list.isEmpty()) {
        for (int k = 0; k < list.size(); k++) {
          EntityLivingBase entity = (EntityLivingBase) list.get(k);
          if (entity.canEntityBeSeen(par3EntityPlayer)
              && !(entity instanceof EntityPlayer)
              && !(entity instanceof EntityTameable)
              && !(entity instanceof EntityHorse)
              && !(entity instanceof EntityVillager)) {
            target = entity;
            AMTLogger.debugInfo("target : " + target.toString());
            break;
          }
        }
      }

      // par4は右クリックの押下時間。
      int j = this.getMaxItemUseDuration(par1ItemStack) - par4;

      // 右クリック押下時間をもとに計算。20で割り(単位を秒に変換)、なにやら二次関数的な計算式に入れている。
      // ここではバニラ弓のまま使っているが、独自の計算式でも良いと思います。
      float f = (float) j / 20.0F;
      f = (f * f + f * 2.0F) / 3.0F;
      // タメ時間が一定以下の場合、何も起こさず処理から抜ける。
      if ((double) f < 0.1D) {
        return;
      }
      // fの上限値。
      if (f > 1.0F) {
        f = 1.0F;
      }

      int power = EnchantmentHelper.getEnchantmentLevel(Enchantment.power.effectId, par1ItemStack);
      int punch = EnchantmentHelper.getEnchantmentLevel(Enchantment.power.effectId, par1ItemStack);
      int fire = EnchantmentHelper.getEnchantmentLevel(Enchantment.flame.effectId, par1ItemStack);

      boolean flag = false;

      if (target != null) {
        EntityAnchorMissile bullet =
            new EntityAnchorMissile(
                par2World,
                par3EntityPlayer,
                target,
                1.0F,
                1.0F,
                par3EntityPlayer.rotationYaw,
                0.0F,
                0.0F,
                0.0F);

        if (power > 0) {
          bullet.setDamage(50.0D + power * 5.0D);
        }

        if (punch > 0) {
          bullet.setKnockbackStrength(1 + punch);
        }

        if (fire > 0) {
          bullet.setFire(100);
        }

        ((IBattery) par1ItemStack.getItem()).discharge(par1ItemStack, 400, true);
        par2World.playSoundAtEntity(
            par3EntityPlayer,
            "random.pop",
            1.0F,
            1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + f * 0.5F);
        if (!par2World.isRemote) {
          flag = par2World.spawnEntityInWorld(bullet);
        }
      }
    }
  }